diff --git a/lua/neotest-golang/runspec/dir.lua b/lua/neotest-golang/runspec/dir.lua index 1f54725f..237620ef 100644 --- a/lua/neotest-golang/runspec/dir.lua +++ b/lua/neotest-golang/runspec/dir.lua @@ -20,20 +20,29 @@ local function find_go_package_import_path(pos, golist_data) ---@type string|nil local package_import_path = nil - -- 1. Perfect match: the selected directory corresponds to a package. + -- 1. Perfect match: main package. + for _, golist_item in ipairs(golist_data) do + if + ( + golist_item.Module.GoMod + == pos.path .. lib.find.os_path_sep .. "go.mod" + and golist_item.Name == "main" + ) or (pos.path == golist_item.Dir and golist_item.Name == "main") + then + package_import_path = golist_item.ImportPath .. "/..." + return "./..." + end + end + + -- 2. Perfect match: the selected directory corresponds to a package. for _, golist_item in ipairs(golist_data) do if pos.path == golist_item.Dir then - if golist_item.Name == "main" then - -- found the base go package - return "./..." - else - package_import_path = golist_item.ImportPath .. "/..." - return package_import_path - end + package_import_path = golist_item.ImportPath .. "/..." + return package_import_path end end - -- 2. Sub-package match: the selected directory does not correspond + -- 3. Sub-package match: the selected directory does not correspond -- to a package, but might correspond to one or more sub-packages. local subpackage_import_paths = {} for _, golist_item in ipairs(golist_data) do diff --git a/tests/go/main.go b/tests/go/cmd/main/main.go similarity index 100% rename from tests/go/main.go rename to tests/go/cmd/main/main.go diff --git a/tests/go/internal/positions/add.go b/tests/go/internal/positions/add.go new file mode 100644 index 00000000..4215d6ad --- /dev/null +++ b/tests/go/internal/positions/add.go @@ -0,0 +1,6 @@ +package positions + +// Function used for tests. +func Add(a, b int) int { + return a + b +} diff --git a/tests/go/positions_spec.lua b/tests/go/internal/positions/positions_spec.lua similarity index 98% rename from tests/go/positions_spec.lua rename to tests/go/internal/positions/positions_spec.lua index a123822c..9d98a46f 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/internal/positions/positions_spec.lua @@ -22,7 +22,8 @@ end describe("Discovery of test positions", function() it("Discover OK", function() -- Arrange - local test_filepath = vim.loop.cwd() .. "/tests/go/positions_test.go" + local test_filepath = vim.loop.cwd() + .. "/tests/go/internal/positions/positions_test.go" local expected = { { id = test_filepath, diff --git a/tests/go/positions_test.go b/tests/go/internal/positions/positions_test.go similarity index 99% rename from tests/go/positions_test.go rename to tests/go/internal/positions/positions_test.go index d5222fe9..ab256b57 100644 --- a/tests/go/positions_test.go +++ b/tests/go/internal/positions/positions_test.go @@ -1,4 +1,4 @@ -package main +package positions import ( "os" diff --git a/tests/go/subpackage/subpackage2/subpackage2_test.go b/tests/go/internal/subpackage/subpackage2/subpackage2_test.go similarity index 100% rename from tests/go/subpackage/subpackage2/subpackage2_test.go rename to tests/go/internal/subpackage/subpackage2/subpackage2_test.go diff --git a/tests/go/subpackage/subpackage2/subpackage3/subpackage3_test.go b/tests/go/internal/subpackage/subpackage2/subpackage3/subpackage3_test.go similarity index 100% rename from tests/go/subpackage/subpackage2/subpackage3/subpackage3_test.go rename to tests/go/internal/subpackage/subpackage2/subpackage3/subpackage3_test.go diff --git a/tests/go/testify/othersuite_test.go b/tests/go/internal/testify/othersuite_test.go similarity index 100% rename from tests/go/testify/othersuite_test.go rename to tests/go/internal/testify/othersuite_test.go diff --git a/tests/go/testify/positions_spec.lua b/tests/go/internal/testify/positions_spec.lua similarity index 97% rename from tests/go/testify/positions_spec.lua rename to tests/go/internal/testify/positions_spec.lua index d4240009..ac509ab4 100644 --- a/tests/go/testify/positions_spec.lua +++ b/tests/go/internal/testify/positions_spec.lua @@ -27,7 +27,7 @@ describe("With testify_enabled=false", function() it("Discover test functions", function() -- Arrange local test_filepath = vim.loop.cwd() - .. "/tests/go/testify/positions_test.go" + .. "/tests/go/internal/testify/positions_test.go" local expected = { { id = test_filepath, @@ -80,7 +80,7 @@ describe("With testify_enabled=true", function() it("Discover namespaces, test methods and test function", function() -- Arrange local test_filepath = vim.loop.cwd() - .. "/tests/go/testify/positions_test.go" + .. "/tests/go/internal/testify/positions_test.go" options.set({ testify_enabled = true }) -- enable testify local filepaths = lib.find.go_test_filepaths(test_filepath) testify.lookup.initialize_lookup(filepaths) -- generate lookup diff --git a/tests/go/testify/positions_test.go b/tests/go/internal/testify/positions_test.go similarity index 100% rename from tests/go/testify/positions_test.go rename to tests/go/internal/testify/positions_test.go diff --git a/tests/go/internal/testname/add.go b/tests/go/internal/testname/add.go new file mode 100644 index 00000000..4358b219 --- /dev/null +++ b/tests/go/internal/testname/add.go @@ -0,0 +1,6 @@ +package testname + +// Function used for tests. +func Add(a, b int) int { + return a + b +} diff --git a/tests/go/testname_spec.lua b/tests/go/internal/testname/testname_spec.lua similarity index 94% rename from tests/go/testname_spec.lua rename to tests/go/internal/testname/testname_spec.lua index d9a146c3..fa21145c 100644 --- a/tests/go/testname_spec.lua +++ b/tests/go/internal/testname/testname_spec.lua @@ -5,7 +5,8 @@ local _ = require("plenary") describe("Neotest position to Go test name", function() -- Arrange - local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go" + local test_filepath = vim.loop.cwd() + .. "/tests/go/internal/testname/testname_test.go" ---@type neotest.Tree local tree = @@ -86,7 +87,8 @@ end) describe("Full Go test name conversion", function() -- Arrange - local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go" + local test_filepath = vim.loop.cwd() + .. "/tests/go/internal/testname/testname_test.go" ---@type neotest.Tree local tree = diff --git a/tests/go/testname_test.go b/tests/go/internal/testname/testname_test.go similarity index 97% rename from tests/go/testname_test.go rename to tests/go/internal/testname/testname_test.go index 85d19532..1ecc590e 100644 --- a/tests/go/testname_test.go +++ b/tests/go/internal/testname/testname_test.go @@ -1,4 +1,4 @@ -package main +package testname import "testing" diff --git a/tests/go/x/xtest.go b/tests/go/internal/x/xtest.go similarity index 100% rename from tests/go/x/xtest.go rename to tests/go/internal/x/xtest.go diff --git a/tests/go/x/xtest_blackbox_test.go b/tests/go/internal/x/xtest_blackbox_test.go similarity index 75% rename from tests/go/x/xtest_blackbox_test.go rename to tests/go/internal/x/xtest_blackbox_test.go index d83934ce..f9cee275 100644 --- a/tests/go/x/xtest_blackbox_test.go +++ b/tests/go/internal/x/xtest_blackbox_test.go @@ -3,7 +3,7 @@ package x_test import ( "testing" - "github.com/fredrikaverpil/neotest-golang/x" + "github.com/fredrikaverpil/neotest-golang/internal/x" ) func TestBlackBox(t *testing.T) { diff --git a/tests/go/x/xtest_whitebox_test.go b/tests/go/internal/x/xtest_whitebox_test.go similarity index 100% rename from tests/go/x/xtest_whitebox_test.go rename to tests/go/internal/x/xtest_whitebox_test.go diff --git a/tests/go/lookup_spec.lua b/tests/go/lookup_spec.lua index d88ba352..201af151 100644 --- a/tests/go/lookup_spec.lua +++ b/tests/go/lookup_spec.lua @@ -8,46 +8,46 @@ describe("Lookup", function() it("Generates tree replacement instructions", function() -- Arrange options.set({ testify_enabled = true }) -- enable testify - local folderpath = vim.loop.cwd() .. "/tests/go" - local filepaths = lib.find.go_test_filepaths(vim.loop.cwd()) + local folderpath = vim.uv.cwd() .. "/tests/go" + local filepaths = lib.find.go_test_filepaths(vim.uv.cwd()) local expected_lookup = { - [folderpath .. "/positions_test.go"] = { - package = "main", + [folderpath .. "/internal/positions/positions_test.go"] = { + package = "positions", replacements = {}, }, - [folderpath .. "/subpackage/subpackage2/subpackage2_test.go"] = { + [folderpath .. "/internal/subpackage/subpackage2/subpackage2_test.go"] = { package = "subpackage2", replacements = {}, }, - [folderpath .. "/subpackage/subpackage2/subpackage3/subpackage3_test.go"] = { + [folderpath .. "/internal/subpackage/subpackage2/subpackage3/subpackage3_test.go"] = { package = "subpackage3", replacements = {}, }, - [folderpath .. "/testify/othersuite_test.go"] = { + [folderpath .. "/internal/testify/othersuite_test.go"] = { package = "testify", replacements = { OtherTestSuite = "TestOtherTestSuite", }, }, - [folderpath .. "/testify/positions_test.go"] = { + [folderpath .. "/internal/testify/positions_test.go"] = { package = "testify", replacements = { ExampleTestSuite = "TestExampleTestSuite", ExampleTestSuite2 = "TestExampleTestSuite2", }, }, - [folderpath .. "/testname_test.go"] = { - package = "main", - replacements = {}, - }, - [folderpath .. "/x/xtest_blackbox_test.go"] = { + [folderpath .. "/internal/x/xtest_blackbox_test.go"] = { package = "x_test", replacements = {}, }, - [folderpath .. "/x/xtest_whitebox_test.go"] = { + [folderpath .. "/internal/x/xtest_whitebox_test.go"] = { 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 70d5934b..1302f711 100644 --- a/tests/unit/golist_spec.lua +++ b/tests/unit/golist_spec.lua @@ -2,7 +2,7 @@ local _ = require("plenary") local lib = require("neotest-golang.lib") -describe("go list output", function() +describe("go list output from root", function() it("contains expected keys/values", function() local tests_filepath = vim.loop.cwd() .. "/tests/go" local output = lib.cmd.golist_data(tests_filepath) @@ -55,24 +55,193 @@ describe("go list output", function() "unicode/utf8", "unsafe", }, - Dir = tests_filepath, + Dir = tests_filepath .. "/cmd/main", GoFiles = { "main.go" }, - ImportPath = "github.com/fredrikaverpil/neotest-golang", + ImportPath = "github.com/fredrikaverpil/neotest-golang/cmd/main", Imports = { "fmt" }, Match = { "./..." }, Module = { Dir = tests_filepath, GoMod = tests_filepath .. "/go.mod", - GoVersion = "1.23.1", Main = true, Path = "github.com/fredrikaverpil/neotest-golang", }, Name = "main", Root = tests_filepath, Stale = true, - StaleReason = "build ID mismatch", - Target = "/Users/fredrik/go/bin/neotest-golang", - TestGoFiles = { "positions_test.go", "testname_test.go" }, + } + + -- 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", function() + it("contains expected keys/values", function() + local tests_filepath = vim.loop.cwd() .. "/tests/go" + local internal_filepath = vim.loop.cwd() .. "/tests/go/internal" + local output = lib.cmd.golist_data(internal_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 = 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" }, + } + + -- 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/positions", function() + it("contains expected keys/values", function() + local tests_filepath = vim.loop.cwd() .. "/tests/go" + local positions_filepath = vim.loop.cwd() .. "/tests/go/internal/positions" + 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" }, }