From 4244d8f81491ec4677d7bb90254c129450d61ea4 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Thu, 2 Jan 2025 23:43:26 +0100 Subject: [PATCH] perf: lower 'go list' JSON payload size (#248) --- lua/neotest-golang/lib/cmd.lua | 19 +- tests/go/internal/notest/notest.go | 1 + tests/go/internal/two/one_test.go | 5 + tests/go/internal/two/two_test.go | 5 + tests/go/lookup_spec.lua | 16 +- tests/unit/golist_spec.lua | 331 ++++++++++++----------------- 6 files changed, 175 insertions(+), 202 deletions(-) create mode 100644 tests/go/internal/notest/notest.go create mode 100644 tests/go/internal/two/one_test.go create mode 100644 tests/go/internal/two/two_test.go diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index 99eae95e..a629bc81 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -36,7 +36,24 @@ function M.golist_data(cwd) end function M.golist_command() - local cmd = { "go", "list", "-json" } + -- NOTE: original command can contain a lot of data: + -- local cmd = { "go", "list", "-json" } + + -- NOTE: optimized command only outputs fields needed: + local cmd = { + "go", + "list", + "-f", + [[{ + "Dir": "{{.Dir}}", + "ImportPath": "{{.ImportPath}}", + "Name": "{{.Name}}", + "TestGoFiles": [{{range $i, $f := .TestGoFiles}}{{if ne $i 0}},{{end}}"{{$f}}"{{end}}], + "XTestGoFiles": [{{range $i, $f := .XTestGoFiles}}{{if ne $i 0}},{{end}}"{{$f}}"{{end}}], + "Module": { "GoMod": "{{.Module.GoMod}}" } + }]], + } + local go_list_args = options.get().go_list_args if type(go_list_args) == "function" then go_list_args = go_list_args() diff --git a/tests/go/internal/notest/notest.go b/tests/go/internal/notest/notest.go new file mode 100644 index 00000000..ee5e82c6 --- /dev/null +++ b/tests/go/internal/notest/notest.go @@ -0,0 +1 @@ +package notest diff --git a/tests/go/internal/two/one_test.go b/tests/go/internal/two/one_test.go new file mode 100644 index 00000000..2c231424 --- /dev/null +++ b/tests/go/internal/two/one_test.go @@ -0,0 +1,5 @@ +package two + +import "testing" + +func TestOne(t *testing.T) {} diff --git a/tests/go/internal/two/two_test.go b/tests/go/internal/two/two_test.go new file mode 100644 index 00000000..1302e4ef --- /dev/null +++ b/tests/go/internal/two/two_test.go @@ -0,0 +1,5 @@ +package two + +import "testing" + +func TestTwo(t *testing.T) {} diff --git a/tests/go/lookup_spec.lua b/tests/go/lookup_spec.lua index 9e8c57e2..cc34c65e 100644 --- a/tests/go/lookup_spec.lua +++ b/tests/go/lookup_spec.lua @@ -40,6 +40,18 @@ describe("Lookup", function() ExampleTestSuite2 = "TestExampleTestSuite2", }, }, + [folderpath .. "/internal/testname/testname_test.go"] = { + package = "testname", + replacements = {}, + }, + [folderpath .. "/internal/two/one_test.go"] = { + package = "two", + replacements = {}, + }, + [folderpath .. "/internal/two/two_test.go"] = { + package = "two", + replacements = {}, + }, [folderpath .. "/internal/x/xtest_blackbox_test.go"] = { package = "x_test", replacements = {}, @@ -48,10 +60,6 @@ describe("Lookup", function() package = "x", replacements = {}, }, - [folderpath .. "/internal/testname/testname_test.go"] = { - package = "testname", - replacements = {}, - }, } -- Act diff --git a/tests/unit/golist_spec.lua b/tests/unit/golist_spec.lua index 367c4692..7fe5a1f3 100644 --- a/tests/unit/golist_spec.lua +++ b/tests/unit/golist_spec.lua @@ -8,79 +8,16 @@ describe("go list output from root", function() local output = lib.cmd.golist_data(tests_filepath) local first_entry = output[1] local expected = { - - Deps = { - "cmp", - "errors", - "fmt", - "internal/abi", - "internal/bytealg", - "internal/chacha8rand", - "internal/coverage/rtcov", - "internal/cpu", - "internal/fmtsort", - "internal/goarch", - "internal/godebugs", - "internal/goexperiment", - "internal/goos", - "internal/itoa", - "internal/oserror", - "internal/poll", - "internal/race", - "internal/reflectlite", - "internal/safefilepath", - "internal/syscall/execenv", - "internal/syscall/unix", - "internal/testlog", - "internal/unsafeheader", - "io", - "io/fs", - "math", - "math/bits", - "os", - "path", - "reflect", - "runtime", - "runtime/internal/atomic", - "runtime/internal/math", - "runtime/internal/sys", - "slices", - "sort", - "strconv", - "sync", - "sync/atomic", - "syscall", - "time", - "unicode", - "unicode/utf8", - "unsafe", - }, Dir = tests_filepath .. "/cmd/main", - GoFiles = { "main.go" }, ImportPath = "github.com/fredrikaverpil/neotest-golang/cmd/main", - Imports = { "fmt" }, - Match = { "./..." }, Module = { - Dir = tests_filepath, GoMod = tests_filepath .. "/go.mod", - Main = true, - Path = "github.com/fredrikaverpil/neotest-golang", }, Name = "main", - Root = tests_filepath, - Stale = true, + TestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command } - -- ignored keys, as they might differ between OS/CI/platforms/too often - expected.Module.GoVersion = nil - first_entry.Module.GoVersion = nil - expected.Deps = nil - first_entry.Deps = nil - expected.StaleReason = nil - first_entry.StaleReason = nil - expected.Target = nil - first_entry.Target = nil - assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) assert.are_same(expected, first_entry) end) @@ -91,83 +28,18 @@ 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 = { - "cmp", - "errors", - "fmt", - "internal/abi", - "internal/bytealg", - "internal/chacha8rand", - "internal/coverage/rtcov", - "internal/cpu", - "internal/fmtsort", - "internal/goarch", - "internal/godebugs", - "internal/goexperiment", - "internal/goos", - "internal/itoa", - "internal/oserror", - "internal/poll", - "internal/race", - "internal/reflectlite", - "internal/safefilepath", - "internal/syscall/execenv", - "internal/syscall/unix", - "internal/testlog", - "internal/unsafeheader", - "io", - "io/fs", - "math", - "math/bits", - "os", - "path", - "reflect", - "runtime", - "runtime/internal/atomic", - "runtime/internal/math", - "runtime/internal/sys", - "slices", - "sort", - "strconv", - "sync", - "sync/atomic", - "syscall", - "time", - "unicode", - "unicode/utf8", - "unsafe", - }, Dir = internal_filepath .. "/positions", - GoFiles = { "add.go" }, ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/positions", - Match = { "./..." }, Module = { - Dir = tests_filepath, GoMod = tests_filepath .. "/go.mod", - GoVersion = "1.23.1", - Main = true, - Path = "github.com/fredrikaverpil/neotest-golang", }, Name = "positions", - Root = tests_filepath, - Stale = true, TestGoFiles = { "positions_test.go" }, - TestImports = { "os", "testing" }, + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command } - -- ignored keys, as they might differ between OS/CI/platforms/too often - expected.Module.GoVersion = nil - first_entry.Module.GoVersion = nil - expected.Deps = nil - first_entry.Deps = nil - expected.StaleReason = nil - first_entry.StaleReason = nil - expected.Target = nil - first_entry.Target = nil - assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) assert.are_same(expected, first_entry) end) @@ -180,80 +52,145 @@ describe("go list output from internal/positions", function() local output = lib.cmd.golist_data(positions_filepath) local first_entry = output[1] local expected = { - - Deps = { - "cmp", - "errors", - "fmt", - "internal/abi", - "internal/bytealg", - "internal/chacha8rand", - "internal/coverage/rtcov", - "internal/cpu", - "internal/fmtsort", - "internal/goarch", - "internal/godebugs", - "internal/goexperiment", - "internal/goos", - "internal/itoa", - "internal/oserror", - "internal/poll", - "internal/race", - "internal/reflectlite", - "internal/safefilepath", - "internal/syscall/execenv", - "internal/syscall/unix", - "internal/testlog", - "internal/unsafeheader", - "io", - "io/fs", - "math", - "math/bits", - "os", - "path", - "reflect", - "runtime", - "runtime/internal/atomic", - "runtime/internal/math", - "runtime/internal/sys", - "slices", - "sort", - "strconv", - "sync", - "sync/atomic", - "syscall", - "time", - "unicode", - "unicode/utf8", - "unsafe", - }, Dir = positions_filepath, - GoFiles = { "add.go" }, ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/positions", - Match = { "./..." }, Module = { - Dir = tests_filepath, GoMod = tests_filepath .. "/go.mod", - GoVersion = "1.23.1", - Main = true, - Path = "github.com/fredrikaverpil/neotest-golang", }, Name = "positions", - Root = tests_filepath, - Stale = true, TestGoFiles = { "positions_test.go" }, - TestImports = { "os", "testing" }, + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + } + + assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) + assert.are_same(expected, first_entry) + end) +end) + +describe("go list output from internal/subpackage", function() + it("contains expected keys/values", function() + local tests_filepath = vim.uv.cwd() .. "/tests/go" + local filepath = vim.uv.cwd() .. "/tests/go/internal/subpackage" + local output = lib.cmd.golist_data(filepath) + local first_entry = output + local expected = { + { + Dir = filepath .. "/subpackage2", + ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/subpackage/subpackage2", + Module = { + GoMod = tests_filepath .. "/go.mod", + }, + Name = "subpackage2", + TestGoFiles = { "subpackage2_test.go" }, + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + }, + { + Dir = filepath .. "/subpackage2/subpackage3", + ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/subpackage/subpackage2/subpackage3", + Module = { + GoMod = tests_filepath .. "/go.mod", + }, + Name = "subpackage3", + TestGoFiles = { "subpackage3_test.go" }, + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + }, + } + + assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) + assert.are_same(expected, first_entry) + end) +end) + +describe("go list output from internal/x", function() + it("contains expected keys/values", function() + local tests_filepath = vim.uv.cwd() .. "/tests/go" + local filepath = vim.uv.cwd() .. "/tests/go/internal/x" + local output = lib.cmd.golist_data(filepath) + local first_entry = output + local expected = { + { + Dir = filepath, + ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/x", + Module = { + GoMod = tests_filepath .. "/go.mod", + }, + Name = "x", + TestGoFiles = { "xtest_whitebox_test.go" }, + XTestGoFiles = { "xtest_blackbox_test.go" }, -- NOTE: added here because of custom `go list -f` command + }, + } + + assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) + assert.are_same(expected, first_entry) + end) +end) + +describe("go list output from internal/x", function() + it("contains TestGoFiles and XTestGoFiles", function() + local tests_filepath = vim.uv.cwd() .. "/tests/go" + local filepath = vim.uv.cwd() .. "/tests/go/internal/x" + local output = lib.cmd.golist_data(filepath) + local first_entry = output + local expected = { + { + Dir = filepath, + ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/x", + Module = { + GoMod = tests_filepath .. "/go.mod", + }, + Name = "x", + TestGoFiles = { "xtest_whitebox_test.go" }, + XTestGoFiles = { "xtest_blackbox_test.go" }, -- NOTE: added here because of custom `go list -f` command + }, + } + + assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) + assert.are_same(expected, first_entry) + end) +end) + +describe("go list output from internal/two", function() + it("contains two TestGoFiles", function() + local tests_filepath = vim.uv.cwd() .. "/tests/go" + local filepath = vim.uv.cwd() .. "/tests/go/internal/two" + local output = lib.cmd.golist_data(filepath) + local first_entry = output + local expected = { + { + Dir = filepath, + ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/two", + Module = { + GoMod = tests_filepath .. "/go.mod", + }, + Name = "two", + TestGoFiles = { "one_test.go", "two_test.go" }, + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + }, } - -- ignored keys, as they might differ between OS/CI/platforms/too often - expected.Module.GoVersion = nil - first_entry.Module.GoVersion = nil - expected.Deps = nil - first_entry.Deps = nil - expected.StaleReason = nil - first_entry.StaleReason = nil - expected.Target = nil - first_entry.Target = nil + assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) + assert.are_same(expected, first_entry) + end) +end) + +describe("go list output from internal/notest", function() + it("contains no tests", function() + local tests_filepath = vim.uv.cwd() .. "/tests/go" + local filepath = vim.uv.cwd() .. "/tests/go/internal/notest" + local output = lib.cmd.golist_data(filepath) + local first_entry = output + local expected = { + { + Dir = filepath, + ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/notest", + Module = { + GoMod = tests_filepath .. "/go.mod", + }, + Name = "notest", + TestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + XTestGoFiles = {}, -- NOTE: added here because of custom `go list -f` command + }, + } assert.are_same(vim.inspect(expected), vim.inspect(first_entry)) assert.are_same(expected, first_entry)