From 04f3d444c05ec06db6939c56e134488a807a88bc Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sun, 8 Dec 2024 15:31:24 +0100 Subject: [PATCH] fix: properly handle XTestGoFiles (#225) --- 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(+) rename tests/go/{testify => }/lookup_spec.lua (86%) 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/lookup_spec.lua similarity index 86% rename from tests/go/testify/lookup_spec.lua rename to tests/go/lookup_spec.lua index 70deb770..8ff3c20f 100644 --- a/tests/go/testify/lookup_spec.lua +++ b/tests/go/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() + } +}