Skip to content

Commit

Permalink
feat: use --run flag when executing single test
Browse files Browse the repository at this point in the history
Run the go test command with --run flag when executing single test.

Support for neotest `type=test` was removed in nvim-neotest#72

This is unfortunate but i believe in wisdom of the maintainers. Anyway i
really need this functionality. Otherwise the plugin is unusable on
projects with e2e/integration tests. I can't execute the whole suite
each time, that is job for the CI.
  • Loading branch information
encero committed Feb 20, 2024
1 parent ba5d536 commit 97c2785
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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) .. "\\$" }
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

0 comments on commit 97c2785

Please sign in to comment.