Skip to content

Commit

Permalink
feat: add benchmark support
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Dec 14, 2024
1 parent 3565f31 commit 8559c1c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
15 changes: 13 additions & 2 deletions lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -411,20 +421,40 @@ end
--- @param gotest_output table
--- @return table<string, TestData>
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"
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lua/neotest-golang/runspec/namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8559c1c

Please sign in to comment.