Skip to content

Commit

Permalink
Run go test non-recursively by default. Add recursive_run as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
sergii4 committed Feb 4, 2024
1 parent 2251361 commit 5914364
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 17 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ require("neotest").setup({
})
```

By default `go test` runs for currecnt package only. If you want to run it recursively you need set:
```lua
require("neotest").setup({
adapters = {
require("neotest-go")({
recursive_run = true
})
}
})
```

## Usage

_NOTE_: all usages of `require('neotest').run.run` can be mapped to a command in your config (this is not included and should be done by the user)
Expand Down
36 changes: 20 additions & 16 deletions lua/neotest-go/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ local get_args = function()
return {}
end

local recursive_run = function()
return false
end

---@type neotest.Adapter
local adapter = { name = "neotest-go" }

Expand Down Expand Up @@ -149,33 +153,25 @@ end
function adapter.build_spec(args)
local results_path = async.fn.tempname()
local position = args.tree:data()
local dir = position.path
-- The path for the position is not a directory, ensure the directory variable refers to one
local dir = "./"
if recursive_run() then
dir = "./..."
end
local location = position.path
if fn.isdirectory(position.path) ~= 1 then
dir = fn.fnamemodify(position.path, ":h")
location = fn.fnamemodify(position.path, ":h")
end
local package = utils.get_go_package_name(position.path)

local cmd_args = ({
dir = { "./..." },
-- file is the same as dir because running a single test file
-- fails if it has external dependencies
file = { "./..." },
namespace = { package },
test = { "-run", utils.get_prefix(args.tree, position.name) .. "\\$", dir },
})[position.type]

local command = vim.tbl_flatten({
"cd",
dir,
location,
"&&",
"go",
"test",
"-v",
"-json",
utils.get_build_tags(),
vim.list_extend(get_args(), args.extra_args or {}),
unpack(cmd_args),
dir,
})
return {
command = table.concat(command, " "),
Expand Down Expand Up @@ -283,6 +279,14 @@ setmetatable(adapter, {
return opts.args
end
end

if is_callable(opts.recursive_run) then
recursive_run = opts.recursive_run
elseif opts.recursive_run then
recursive_run = function()
return opts.recursive_run
end
end
return adapter
end,
})
Expand Down
15 changes: 14 additions & 1 deletion lua/spec/neotest-go/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,22 @@ describe("build_spec", function()
local args = { tree = tree }
local expected_command = "cd "
.. vim.loop.cwd()
.. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..."
.. "/neotest_go && go test -v -json -count=1 -timeout=60s ./"
local result = plugin.build_spec(args)
assert.are.same(expected_command, result.command)
assert.are.same(path, result.context.file)
end)
async.it("build specification for many_table_test.go recuresive run", function()
local plugin_with_recursive_run = require("neotest-go")({ recursive_run = true })
local path = vim.loop.cwd() .. "/neotest_go/many_table_test.go"
local tree = plugin.discover_positions(path)

local args = { tree = tree }
local expected_command = "cd "
.. vim.loop.cwd()
.. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..."
local result = plugin_with_recursive_run.build_spec(args)
assert.are.same(expected_command, result.command)
assert.are.same(path, result.context.file)
end)
end)
9 changes: 9 additions & 0 deletions neotest_go/nested/n_cases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func add(a, b int) int {
return a + b
}

func subtract(a, b int) int {
return a - b
}
49 changes: 49 additions & 0 deletions neotest_go/nested/n_cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSubtract(t *testing.T) {
testCases := []struct {
desc string
a int
b int
want int
}{
{
desc: "test one",
a: 1,
b: 2,
want: 3,
},
{
desc: "test two",
a: 1,
b: 2,
want: 7,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
assert.Equal(t, tC.want, subtract(tC.a, tC.b))
})
}
}

func TestAdd(t *testing.T) {
t.Run("test one", func(t *testing.T) {
assert.Equal(t, 3, add(1, 2))
})

t.Run("test two", func(t *testing.T) {
assert.Equal(t, 5, add(1, 2))
})

variable := "string"
t.Run(variable, func(t *testing.T) {
assert.Equal(t, 3, add(1, 2))
})
}

0 comments on commit 5914364

Please sign in to comment.