Skip to content

Commit 1757961

Browse files
authored
Run go test non-recursively by default. Add recursive_run as an option (#72)
1 parent 2251361 commit 1757961

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ require("neotest").setup({
5757
})
5858
```
5959

60+
By default `go test` runs for currecnt package only. If you want to run it recursively you need to set:
61+
```lua
62+
require("neotest").setup({
63+
adapters = {
64+
require("neotest-go")({
65+
recursive_run = true
66+
})
67+
}
68+
})
69+
```
70+
6071
## Usage
6172

6273
_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)

lua/neotest-go/init.lua

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ local get_args = function()
1717
return {}
1818
end
1919

20+
local recursive_run = function()
21+
return false
22+
end
23+
2024
---@type neotest.Adapter
2125
local adapter = { name = "neotest-go" }
2226

@@ -149,33 +153,25 @@ end
149153
function adapter.build_spec(args)
150154
local results_path = async.fn.tempname()
151155
local position = args.tree:data()
152-
local dir = position.path
153-
-- The path for the position is not a directory, ensure the directory variable refers to one
156+
local dir = "./"
157+
if recursive_run() then
158+
dir = "./..."
159+
end
160+
local location = position.path
154161
if fn.isdirectory(position.path) ~= 1 then
155-
dir = fn.fnamemodify(position.path, ":h")
162+
location = fn.fnamemodify(position.path, ":h")
156163
end
157-
local package = utils.get_go_package_name(position.path)
158-
159-
local cmd_args = ({
160-
dir = { "./..." },
161-
-- file is the same as dir because running a single test file
162-
-- fails if it has external dependencies
163-
file = { "./..." },
164-
namespace = { package },
165-
test = { "-run", utils.get_prefix(args.tree, position.name) .. "\\$", dir },
166-
})[position.type]
167-
168164
local command = vim.tbl_flatten({
169165
"cd",
170-
dir,
166+
location,
171167
"&&",
172168
"go",
173169
"test",
174170
"-v",
175171
"-json",
176172
utils.get_build_tags(),
177173
vim.list_extend(get_args(), args.extra_args or {}),
178-
unpack(cmd_args),
174+
dir,
179175
})
180176
return {
181177
command = table.concat(command, " "),
@@ -283,6 +279,14 @@ setmetatable(adapter, {
283279
return opts.args
284280
end
285281
end
282+
283+
if is_callable(opts.recursive_run) then
284+
recursive_run = opts.recursive_run
285+
elseif opts.recursive_run then
286+
recursive_run = function()
287+
return opts.recursive_run
288+
end
289+
end
286290
return adapter
287291
end,
288292
})

lua/spec/neotest-go/init_spec.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,22 @@ describe("build_spec", function()
350350
local args = { tree = tree }
351351
local expected_command = "cd "
352352
.. vim.loop.cwd()
353-
.. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..."
353+
.. "/neotest_go && go test -v -json -count=1 -timeout=60s ./"
354354
local result = plugin.build_spec(args)
355355
assert.are.same(expected_command, result.command)
356356
assert.are.same(path, result.context.file)
357357
end)
358+
async.it("build specification for many_table_test.go recuresive run", function()
359+
local plugin_with_recursive_run = require("neotest-go")({ recursive_run = true })
360+
local path = vim.loop.cwd() .. "/neotest_go/many_table_test.go"
361+
local tree = plugin.discover_positions(path)
362+
363+
local args = { tree = tree }
364+
local expected_command = "cd "
365+
.. vim.loop.cwd()
366+
.. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..."
367+
local result = plugin_with_recursive_run.build_spec(args)
368+
assert.are.same(expected_command, result.command)
369+
assert.are.same(path, result.context.file)
370+
end)
358371
end)

0 commit comments

Comments
 (0)