Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run go test non-recursively by default. Add recursive_run as an option #72

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading