Skip to content

Commit

Permalink
fix: properly handle XTestGoFiles (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Dec 8, 2024
1 parent 57cece8 commit 04f3d44
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/go/testify/lookup_spec.lua → tests/go/lookup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/go/x/xtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package x

func Add(a, b int) int { return a + b }
func internal() string { return "private" }
14 changes: 14 additions & 0 deletions tests/go/x/xtest_blackbox_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
13 changes: 13 additions & 0 deletions tests/go/x/xtest_whitebox_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 04f3d44

Please sign in to comment.