diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index b0b4fc89..b083df74 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -52,8 +52,19 @@ function M.test_command_in_package(package_or_path) return cmd, json_filepath end -function M.test_command_in_package_with_regexp(package_or_path, regexp) - local go_test_required_args = { package_or_path, "-run", regexp } +function M.test_command_in_package_with_regexp( + package_or_path, -- this is a filepath when running individual tests, otherwise it's the package's import path + regexp, + is_benchmark +) + local go_test_required_args = { package_or_path } + + if is_benchmark then + vim.list_extend(go_test_required_args, { "-run", "^$", "-bench", regexp }) + else + vim.list_extend(go_test_required_args, { "-run", regexp }) + end + local cmd, json_filepath = M.test_command(go_test_required_args) return cmd, json_filepath end diff --git a/lua/neotest-golang/process.lua b/lua/neotest-golang/process.lua index f4b2d41d..b3fc2615 100644 --- a/lua/neotest-golang/process.lua +++ b/lua/neotest-golang/process.lua @@ -369,6 +369,16 @@ function M.decorate_with_go_package_and_test_name( tweaked_pos_id = tweaked_pos_id:gsub('"', "") tweaked_pos_id = tweaked_pos_id:gsub("::", "/") + -- A single benchmark was executed. + if + test_data.neotest_data.type == "test" + and string.match(test_data.neotest_data.name, "^Benchmark") + then + test_data.gotest_data.pkg = "unknown-because-benchmark" + test_data.gotest_data.name = test_data.neotest_data.name + end + + -- One or many tests were executed. for _, golistline in ipairs(golist_output) do if folderpath == golistline.Dir then for _, gotestline in ipairs(gotest_output) do @@ -411,20 +421,40 @@ end --- @param gotest_output table --- @return table function M.decorate_with_go_test_results(res, gotest_output) + local count = 0 + for _ in pairs(res) do + count = count + 1 + end + for pos_id, test_data in pairs(res) do for _, line in ipairs(gotest_output) do if - line.Package == test_data.gotest_data.pkg + ( + line.Package == test_data.gotest_data.pkg + or test_data.gotest_data.pkg == "unknown-because-benchmark" + ) and ( line.Test == test_data.gotest_data.name or lib.string.starts_with( line.Test, test_data.gotest_data.name .. "/" ) + or string.match(test_data.gotest_data.name, "^Benchmark") ) then -- record test status - if line.Action == "pass" then + if + test_data.gotest_data.pkg == "unknown-because-benchmark" + and count == 1 + and line.Action == "passed" + then + test_data.status = "passed" + elseif + test_data.gotest_data.pkg == "unknown-because-benchmark" + and count ~= 1 + then + test_data.status = "skipped" + elseif line.Action == "pass" then test_data.status = "passed" elseif line.Action == "fail" then test_data.status = "failed" diff --git a/lua/neotest-golang/query.lua b/lua/neotest-golang/query.lua index ff0f3349..587a564b 100644 --- a/lua/neotest-golang/query.lua +++ b/lua/neotest-golang/query.lua @@ -10,7 +10,7 @@ local M = {} M.test_function = [[ ; query for test function ((function_declaration - name: (identifier) @test.name) (#match? @test.name "^(Test|Example)") (#not-match? @test.name "^TestMain$")) + name: (identifier) @test.name) (#match? @test.name "^(Test|Example|Benchmark)") (#not-match? @test.name "^TestMain$")) @test.definition ; query for subtest, like t.Run() diff --git a/lua/neotest-golang/runspec/file.lua b/lua/neotest-golang/runspec/file.lua index 3f778ec7..1d208ad5 100644 --- a/lua/neotest-golang/runspec/file.lua +++ b/lua/neotest-golang/runspec/file.lua @@ -68,7 +68,7 @@ function M.build(pos, tree, strategy) local regexp = M.get_regexp(pos.path) if regexp ~= nil then test_cmd, json_filepath = - lib.cmd.test_command_in_package_with_regexp(package_name, regexp) + lib.cmd.test_command_in_package_with_regexp(package_name, regexp, false) else -- fallback: run all tests in the package test_cmd, json_filepath = lib.cmd.test_command_in_package(package_name) diff --git a/lua/neotest-golang/runspec/namespace.lua b/lua/neotest-golang/runspec/namespace.lua index dcc5779b..fe82a142 100644 --- a/lua/neotest-golang/runspec/namespace.lua +++ b/lua/neotest-golang/runspec/namespace.lua @@ -28,7 +28,8 @@ function M.build(pos) local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp( test_folder_absolute_path, - test_name + test_name, + false ) --- @type RunspecContext diff --git a/lua/neotest-golang/runspec/test.lua b/lua/neotest-golang/runspec/test.lua index 208a83be..f7bf7f97 100644 --- a/lua/neotest-golang/runspec/test.lua +++ b/lua/neotest-golang/runspec/test.lua @@ -28,9 +28,15 @@ function M.build(pos, strategy) local test_name = lib.convert.to_gotest_test_name(pos.id) local test_name_regex = lib.convert.to_gotest_regex_pattern(test_name) + local is_benchmark = false + if string.match(pos.name, "^Benchmark") then + is_benchmark = true + end + local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp( test_folder_absolute_path, - test_name_regex + test_name_regex, + is_benchmark ) local runspec_strategy = nil