From 94ae29d359080cb9deb04be5fef8d09ff92c570e Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sun, 8 Dec 2024 14:49:47 +0100 Subject: [PATCH 1/2] fix: properly handle XTestGoFiles fixes #222 --- lua/neotest-golang/runspec/file.lua | 10 ++++++++++ tests/go/testify/lookup_spec.lua | 8 ++++++++ tests/go/x/xtest.go | 4 ++++ tests/go/x/xtest_blackbox_test.go | 14 ++++++++++++++ tests/go/x/xtest_whitebox_test.go | 13 +++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 tests/go/x/xtest.go create mode 100644 tests/go/x/xtest_blackbox_test.go create mode 100644 tests/go/x/xtest_whitebox_test.go diff --git a/lua/neotest-golang/runspec/file.lua b/lua/neotest-golang/runspec/file.lua index 9f689cf5..3f778ec7 100644 --- a/lua/neotest-golang/runspec/file.lua +++ b/lua/neotest-golang/runspec/file.lua @@ -50,6 +50,16 @@ function M.build(pos, tree, strategy) break end end + if golist_item.XTestGoFiles ~= nil then + -- NOTE: XTestGoFiles are test files that are part of a [packagename]_test package. + if + pos_path_foldername == golist_item.Dir + and vim.tbl_contains(golist_item.XTestGoFiles, pos_path_filename) + then + package_name = golist_item.ImportPath + break + end + end end -- find all top-level tests in pos.path diff --git a/tests/go/testify/lookup_spec.lua b/tests/go/testify/lookup_spec.lua index 70deb770..8ff3c20f 100644 --- a/tests/go/testify/lookup_spec.lua +++ b/tests/go/testify/lookup_spec.lua @@ -36,6 +36,14 @@ describe("Lookup", function() package = "main", replacements = {}, }, + [folderpath .. "/x/xtest_blackbox_test.go"] = { + package = "x_test", + replacements = {}, + }, + [folderpath .. "/x/xtest_whitebox_test.go"] = { + package = "x", + replacements = {}, + }, } -- Act diff --git a/tests/go/x/xtest.go b/tests/go/x/xtest.go new file mode 100644 index 00000000..1c6092c2 --- /dev/null +++ b/tests/go/x/xtest.go @@ -0,0 +1,4 @@ +package x + +func Add(a, b int) int { return a + b } +func internal() string { return "private" } diff --git a/tests/go/x/xtest_blackbox_test.go b/tests/go/x/xtest_blackbox_test.go new file mode 100644 index 00000000..d83934ce --- /dev/null +++ b/tests/go/x/xtest_blackbox_test.go @@ -0,0 +1,14 @@ +package x_test + +import ( + "testing" + + "github.com/fredrikaverpil/neotest-golang/x" +) + +func TestBlackBox(t *testing.T) { + // Can only access Add() through the public interface of x. + if x.Add(1, 2) != 3 { + t.Fail() + } +} diff --git a/tests/go/x/xtest_whitebox_test.go b/tests/go/x/xtest_whitebox_test.go new file mode 100644 index 00000000..792d8b22 --- /dev/null +++ b/tests/go/x/xtest_whitebox_test.go @@ -0,0 +1,13 @@ +package x + +import "testing" + +func TestWhiteBox(t *testing.T) { + // Can access both Add() and internal() + if Add(1, 2) != 3 { + t.Fail() + } + if internal() != "private" { + t.Fail() + } +} From 06c586ff249a2e78cc9da8b170126719630ad910 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sun, 8 Dec 2024 15:13:40 +0100 Subject: [PATCH 2/2] refactor: move lookup_spec.lua --- tests/go/{testify => }/lookup_spec.lua | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/go/{testify => }/lookup_spec.lua (100%) diff --git a/tests/go/testify/lookup_spec.lua b/tests/go/lookup_spec.lua similarity index 100% rename from tests/go/testify/lookup_spec.lua rename to tests/go/lookup_spec.lua