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: add benchmark support #234

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions tests/go/internal/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package benchmark

import "testing"

func add(a, b int) int {
return a + b
}

func multiply(a, b int) int {
return a * b
}

func BenchmarkOperations(b *testing.B) {
// TODO: make it possible to run b.Run sub-benchmarks?

b.Run("Addition", func(b *testing.B) {
for i := 0; i < b.N; i++ {
add(5, 7)
}
})

b.Run("Multiplication", func(b *testing.B) {
for i := 0; i < b.N; i++ {
multiply(5, 7)
}
})
}
14 changes: 3 additions & 11 deletions tests/go/internal/operand/operand_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package operand

import "testing"
import (
"testing"
)

type dummy struct{}

Expand All @@ -19,16 +21,6 @@ func Test_Run(t *testing.T) {
})
}

func Benchmark_Run(b *testing.B) {
// NOTE: support for benchmarks could potentially be added.
b.Run("benchmark case", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// Add benchmark logic here
dummy{}.Run("test", func(t *testing.T) {})
}
})
}

func FuzzRun(f *testing.F) {
// NOTE: support for fuzz tests could potentially be added.

Expand Down
4 changes: 4 additions & 0 deletions tests/go/lookup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe("Lookup", function()
local folderpath = vim.uv.cwd() .. "/tests/go"
local filepaths = lib.find.go_test_filepaths(vim.uv.cwd())
local expected_lookup = {
[folderpath .. "/internal/benchmark/benchmark_test.go"] = {
package = "benchmark",
replacements = {},
},
[folderpath .. "/internal/operand/operand_test.go"] = {
package = "operand",
replacements = {},
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/golist_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("go list output from internal", function()
local tests_filepath = vim.uv.cwd() .. "/tests/go"
local internal_filepath = vim.uv.cwd() .. "/tests/go/internal"
local output = lib.cmd.golist_data(internal_filepath)
local first_entry = output[2]
local first_entry = output[3]
local expected = {

Deps = {
Expand Down
Loading