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 authored Feb 4, 2024
1 parent 2251361 commit 1757961
Show file tree
Hide file tree
Showing 3 changed files with 45 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 to 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)

0 comments on commit 1757961

Please sign in to comment.