Skip to content

Commit d6c3017

Browse files
feat: add benchmark support
1 parent bd8e69e commit d6c3017

File tree

10 files changed

+91
-20
lines changed

10 files changed

+91
-20
lines changed

lua/neotest-golang/lib/cmd.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,19 @@ function M.test_command_in_package(package_or_path)
5252
return cmd, json_filepath
5353
end
5454

55-
function M.test_command_in_package_with_regexp(package_or_path, regexp)
56-
local go_test_required_args = { package_or_path, "-run", regexp }
55+
function M.test_command_in_package_with_regexp(
56+
package_or_path, -- this is a filepath when running individual tests, otherwise it's the package's import path
57+
regexp,
58+
is_benchmark
59+
)
60+
local go_test_required_args = { package_or_path }
61+
62+
if is_benchmark then
63+
vim.list_extend(go_test_required_args, { "-run", "^$", "-bench", regexp })
64+
else
65+
vim.list_extend(go_test_required_args, { "-run", regexp })
66+
end
67+
5768
local cmd, json_filepath = M.test_command(go_test_required_args)
5869
return cmd, json_filepath
5970
end

lua/neotest-golang/process.lua

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ function M.decorate_with_go_package_and_test_name(
369369
tweaked_pos_id = tweaked_pos_id:gsub('"', "")
370370
tweaked_pos_id = tweaked_pos_id:gsub("::", "/")
371371

372+
-- A single benchmark was executed.
373+
if
374+
test_data.neotest_data.type == "test"
375+
and string.match(test_data.neotest_data.name, "^Benchmark")
376+
then
377+
test_data.gotest_data.pkg = "unknown-because-benchmark"
378+
test_data.gotest_data.name = test_data.neotest_data.name
379+
end
380+
381+
-- One or many tests were executed.
372382
for _, golistline in ipairs(golist_output) do
373383
if folderpath == golistline.Dir then
374384
for _, gotestline in ipairs(gotest_output) do
@@ -411,20 +421,40 @@ end
411421
--- @param gotest_output table
412422
--- @return table<string, TestData>
413423
function M.decorate_with_go_test_results(res, gotest_output)
424+
local count = 0
425+
for _ in pairs(res) do
426+
count = count + 1
427+
end
428+
414429
for pos_id, test_data in pairs(res) do
415430
for _, line in ipairs(gotest_output) do
416431
if
417-
line.Package == test_data.gotest_data.pkg
432+
(
433+
line.Package == test_data.gotest_data.pkg
434+
or test_data.gotest_data.pkg == "unknown-because-benchmark"
435+
)
418436
and (
419437
line.Test == test_data.gotest_data.name
420438
or lib.string.starts_with(
421439
line.Test,
422440
test_data.gotest_data.name .. "/"
423441
)
442+
or string.match(test_data.gotest_data.name, "^Benchmark")
424443
)
425444
then
426445
-- record test status
427-
if line.Action == "pass" then
446+
if
447+
test_data.gotest_data.pkg == "unknown-because-benchmark"
448+
and count == 1
449+
and line.Action == "passed"
450+
then
451+
test_data.status = "passed"
452+
elseif
453+
test_data.gotest_data.pkg == "unknown-because-benchmark"
454+
and count ~= 1
455+
then
456+
test_data.status = "skipped"
457+
elseif line.Action == "pass" then
428458
test_data.status = "passed"
429459
elseif line.Action == "fail" then
430460
test_data.status = "failed"

lua/neotest-golang/query.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local M = {}
1010
M.test_function = [[
1111
; query for test function
1212
((function_declaration
13-
name: (identifier) @test.name) (#match? @test.name "^(Test|Example)") (#not-match? @test.name "^TestMain$"))
13+
name: (identifier) @test.name) (#match? @test.name "^(Test|Example|Benchmark)") (#not-match? @test.name "^TestMain$"))
1414
@test.definition
1515
1616
; query for subtest, like t.Run()

lua/neotest-golang/runspec/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function M.build(pos, tree, strategy)
6868
local regexp = M.get_regexp(pos.path)
6969
if regexp ~= nil then
7070
test_cmd, json_filepath =
71-
lib.cmd.test_command_in_package_with_regexp(package_name, regexp)
71+
lib.cmd.test_command_in_package_with_regexp(package_name, regexp, false)
7272
else
7373
-- fallback: run all tests in the package
7474
test_cmd, json_filepath = lib.cmd.test_command_in_package(package_name)

lua/neotest-golang/runspec/namespace.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function M.build(pos)
2828

2929
local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp(
3030
test_folder_absolute_path,
31-
test_name
31+
test_name,
32+
false
3233
)
3334

3435
--- @type RunspecContext

lua/neotest-golang/runspec/test.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ function M.build(pos, strategy)
2828
local test_name = lib.convert.to_gotest_test_name(pos.id)
2929
local test_name_regex = lib.convert.to_gotest_regex_pattern(test_name)
3030

31+
local is_benchmark = false
32+
if string.match(pos.name, "^Benchmark") then
33+
is_benchmark = true
34+
end
35+
3136
local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp(
3237
test_folder_absolute_path,
33-
test_name_regex
38+
test_name_regex,
39+
is_benchmark
3440
)
3541

3642
local runspec_strategy = nil
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package benchmark
2+
3+
import "testing"
4+
5+
func add(a, b int) int {
6+
return a + b
7+
}
8+
9+
func multiply(a, b int) int {
10+
return a * b
11+
}
12+
13+
func BenchmarkOperations(b *testing.B) {
14+
// TODO: make it possible to run b.Run sub-benchmarks?
15+
16+
b.Run("Addition", func(b *testing.B) {
17+
for i := 0; i < b.N; i++ {
18+
add(5, 7)
19+
}
20+
})
21+
22+
b.Run("Multiplication", func(b *testing.B) {
23+
for i := 0; i < b.N; i++ {
24+
multiply(5, 7)
25+
}
26+
})
27+
}

tests/go/internal/operand/operand_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package operand
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
type dummy struct{}
68

@@ -19,16 +21,6 @@ func Test_Run(t *testing.T) {
1921
})
2022
}
2123

22-
func Benchmark_Run(b *testing.B) {
23-
// NOTE: support for benchmarks could potentially be added.
24-
b.Run("benchmark case", func(b *testing.B) {
25-
for i := 0; i < b.N; i++ {
26-
// Add benchmark logic here
27-
dummy{}.Run("test", func(t *testing.T) {})
28-
}
29-
})
30-
}
31-
3224
func FuzzRun(f *testing.F) {
3325
// NOTE: support for fuzz tests could potentially be added.
3426

tests/go/lookup_spec.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ describe("Lookup", function()
1111
local folderpath = vim.uv.cwd() .. "/tests/go"
1212
local filepaths = lib.find.go_test_filepaths(vim.uv.cwd())
1313
local expected_lookup = {
14+
[folderpath .. "/internal/benchmark/benchmark_test.go"] = {
15+
package = "benchmark",
16+
replacements = {},
17+
},
1418
[folderpath .. "/internal/operand/operand_test.go"] = {
1519
package = "operand",
1620
replacements = {},

tests/unit/golist_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe("go list output from internal", function()
9191
local tests_filepath = vim.uv.cwd() .. "/tests/go"
9292
local internal_filepath = vim.uv.cwd() .. "/tests/go/internal"
9393
local output = lib.cmd.golist_data(internal_filepath)
94-
local first_entry = output[2]
94+
local first_entry = output[3]
9595
local expected = {
9696

9797
Deps = {

0 commit comments

Comments
 (0)