Skip to content

Commit

Permalink
cmd/go: fix -coverpkg not ignoring special directories
Browse files Browse the repository at this point in the history
The pattern passed to `-coverpkg` when running `go test` would not
ignore directories usually ignored by the `go` command, i.e. those
beginning with "." or "_" are ignored by the go tool, as are directories
named "testdata".

Fix this by adding an explicit check for these (by following a similar
check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them.

Two tests are added for this change, one is a regression test attempting
to directly replicate the behaviour described in the issue, the other is
updating another test I saw fail when trialling other solutions to this
issue so I thought it worthwhile to be explicit about the change there.

See linked issue for a reproduction.

Fixes #66038

[1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
  • Loading branch information
matthewhughes934 committed Mar 7, 2024
1 parent 1653833 commit 63eb9f4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,47 @@ func TestCoverpkgTestOnly(t *testing.T) {
tg.grepStdout("coverage: 100", "no coverage")
}

// regression test for github.com/golang/go/issues/66038
func TestCoverpkgIngoresSpecialDirs(t *testing.T) {
skipIfGccgo(t, "gccgo has no cover tool")
tooSlow(t, "links and runs a test binary with coverage enabled")

tg := testgo(t)
defer tg.cleanup()

wd, err := os.Getwd()
tg.check(err)
tg.makeTempdir()
tg.check(os.Chdir(tg.path(".")))
defer func() { tg.check(os.Chdir(wd)) }()

tg.tempFile("a/a.go", `package a
import ( _ "b" )
func F(i int) int {
return i*i
}`)
tg.tempFile("a/a_test.go", `
package a
import ( "testing" )
func TestF(t *testing.T) { F(2) }
`)

for _, skipDir := range []string{".dir", "_dir", "testdata", "dir/testdata"} {
t.Run(skipDir, func(t *testing.T) {
tg.tempFile(fmt.Sprintf("%s/src/b/b.go", skipDir), `package b
func G(i int) int {
return i*i
}`)
tg.setenv("GOPATH", tg.path(skipDir))

tg.run("test", "-coverpkg=./...", "./...")

tg.grepStderrNot("no packages being tested depend on matches", "bad match message")
tg.grepStdout("coverage: 100", "no coverage")
})
}
}

// Regression test for golang.org/issue/34499: version command should not crash
// when executed in a deleted directory on Linux.
func TestExecInDeletedDir(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/internal/load/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var ppfTests = []ppfTest{
ppfDirTest("./...", 3, "/my/test/dir", "/my/test/dir/sub", "/my/test/dir/sub/sub", "/my/test/other", "/my/test/other/sub"),
ppfDirTest("../...", 4, "/my/test/dir", "/my/test/other", "/my/test/dir/sub", "/my/test/other/sub", "/my/other/test"),
ppfDirTest("../...sub...", 3, "/my/test/dir/sub", "/my/test/othersub", "/my/test/yellowsubmarine", "/my/other/test"),
ppfDirTest("./...", 1, "/my/test/dir", "/my/test/dir/.sub", "/test/dir/_sub", "/test/dir/testdata/sub"),
}

func ppfDirTest(pattern string, nmatch int, dirs ...string) ppfTest {
Expand Down
13 changes: 13 additions & 0 deletions src/cmd/go/internal/load/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
if rel == ".." || strings.HasPrefix(rel, "../") {
return false
}

cwdRel, err := filepath.Rel(cwd, p.Dir)
if err == nil {
if cwdRel != "." && !strings.HasPrefix(cwdRel, "../") {
for _, elem := range strings.Split(cwdRel, string(filepath.Separator)) {
// Avoid .foo, _foo, and testdata subdirectory trees.
if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
return false
}
}
}
}

return matchPath(rel)
}
case pattern == "all":
Expand Down

0 comments on commit 63eb9f4

Please sign in to comment.