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

feat: use --run flag when executing single test ( run single test instead of all of them ) #81

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions lua/neotest-go/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ function adapter.build_spec(args)
if fn.isdirectory(position.path) ~= 1 then
location = fn.fnamemodify(position.path, ":h")
end

local run_flag = {}
if position.type == "test" then
run_flag = { "--run", "\\^" .. utils.get_prefix(args.tree, position.name) .. "\\$" }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other arguments use just one dash. Not sure what the difference would be here (if any) but perhaps keep it consistent?

Also, why the backslashes?

Suggested change
run_flag = { "--run", "\\^" .. utils.get_prefix(args.tree, position.name) .. "\\$" }
run_flag = { "-run", "^" .. utils.get_prefix(args.tree, position.name) .. "$" }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those slashes are from here, without it works also fine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what @sergii4 says about them, but I think we don't need those backslashes. And I'd vote for -run (one dash) to keep the arguments consistently using one dash.

end

local command = vim.tbl_flatten({
"cd",
location,
Expand All @@ -171,8 +177,10 @@ function adapter.build_spec(args)
"-json",
utils.get_build_tags(),
vim.list_extend(get_args(), args.extra_args or {}),
run_flag,
dir,
})

return {
command = table.concat(command, " "),
context = {
Expand Down
14 changes: 14 additions & 0 deletions lua/spec/neotest-go/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ describe("build_spec", function()
assert.are.same(expected_command, result.command)
assert.are.same(path, result.context.file)
end)
async.it("build specification for single test", function()
local path = vim.loop.cwd() .. "/neotest_go/main_test.go"
local tree = plugin.discover_positions(path):children()[1]

local args = { tree = tree }
local expected_command = "cd "
.. vim.loop.cwd()
.. "/neotest_go && go test -v -json -count=1 -timeout=60s --run \\^TestAddOne\\$ ./"
local result = plugin.build_spec(args)
assert.are.same(expected_command, result.command)
assert.are.same(path, result.context.file)
end)

-- This test is overwriting plugin global state, keep it at end of the file or face the consequences ¯\_(ツ)_/¯
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"
Expand Down
Loading