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
10 changes: 9 additions & 1 deletion lua/neotest-go/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function adapter.discover_positions(path)
field: (field_identifier) @test.field.name1
(#eq? @test.field.name @test.field.name1))))))))

;; query for map table tests
;; query for map table tests
(block
(short_var_declaration
left: (expression_list
Expand Down 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_test_id(args.tree) .. "$" }
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
21 changes: 14 additions & 7 deletions lua/neotest-go/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,22 @@ function utils.get_errors_from_test(test, file_name)
end

---@param tree neotest.Tree
---@param name string
---@return string
function utils.get_prefix(tree, name)
local parent_tree = tree:parent()
if not parent_tree or parent_tree:data().type == "file" then
Copy link
Collaborator

Choose a reason for hiding this comment

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

So we don't need that check anymore, do we?

Copy link
Author

Choose a reason for hiding this comment

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

Which check do you mean?

return name
function utils.get_test_id(tree)
local parts = {}

-- build test name for potentially deeply nested tests
while tree and tree:data().type == "test" do
local name = tree:data().name
name = name:gsub("[\"']", "") -- Remove quotes
name = name:gsub("%s", "_") -- Replace spaces with underscores

table.insert(parts, 1, name)

tree = tree:parent()
end
local parent_name = parent_tree:data().name
return parent_name .. "/" .. name

return table.concat(parts, "/")
end

return utils
49 changes: 47 additions & 2 deletions lua/spec/neotest-go/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe("discover_positions", function()
id = vim.loop.cwd() .. "/neotest_go/cases_test.go",
name = "cases_test.go",
path = vim.loop.cwd() .. "/neotest_go/cases_test.go",
range = { 0, 0, 49, 0 },
range = { 0, 0, 55, 0 },
type = "file",
},
{
Expand Down Expand Up @@ -169,7 +169,7 @@ describe("discover_positions", function()
id = vim.loop.cwd() .. "/neotest_go/cases_test.go::TestAdd",
name = "TestAdd",
path = vim.loop.cwd() .. "/neotest_go/cases_test.go",
range = { 35, 0, 48, 1 },
range = { 35, 0, 54, 1 },
type = "test",
},
{
Expand All @@ -190,6 +190,24 @@ describe("discover_positions", function()
type = "test",
},
},
{
{
id = vim.loop.cwd() .. "/neotest_go/cases_test.go::TestAdd::test_three",
name = '"test three"',
path = vim.loop.cwd() .. "/neotest_go/cases_test.go",
range = { 44, 1, 48, 3 },
type = "test",
},
{
{
id = vim.loop.cwd() .. '/neotest_go/cases_test.go::TestAdd::"test three"::test_four',
name = '"test four"',
path = vim.loop.cwd() .. "/neotest_go/cases_test.go",
range = { 45, 2, 47, 4 },
type = "test",
},
},
},
},
}

Expand Down Expand Up @@ -355,6 +373,33 @@ 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)

async.it("build specification for single nested test", function()
local path = vim.loop.cwd() .. "/neotest_go/cases_test.go"
local tree = plugin.discover_positions(path)
local test_tree = tree:children()[2]:children()[3]:children()[1]

local args = { tree = test_tree }
local expected_command = "cd "
.. vim.loop.cwd()
.. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAdd/test_three/test_four$ ./"
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 ¯\_(ツ)_/¯
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does it overwrite plugin global state?

Copy link
Author

Choose a reason for hiding this comment

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

I don't know enough about lua module imports, but... i think you can't set recursive_run to false, after it was set to something

Copy link
Author

Choose a reason for hiding this comment

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

Anyway, if you try to move the test anywhere in the spec file. The test will fail

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
6 changes: 6 additions & 0 deletions neotest_go/cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func TestAdd(t *testing.T) {
assert.Equal(t, 5, add(1, 2))
})

t.Run("test three", func(t *testing.T) {
t.Run("test four", func(t *testing.T) {
assert.Equal(t, 3, add(1, 2))
})
})

variable := "string"
t.Run(variable, func(t *testing.T) {
assert.Equal(t, 3, add(1, 2))
Expand Down