Skip to content

Commit e5000dc

Browse files
cmd/go: fix -coverpkg not ignoring special directories
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 golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
1 parent 1653833 commit e5000dc

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/cmd/go/go_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,47 @@ func TestCoverpkgTestOnly(t *testing.T) {
27782778
tg.grepStdout("coverage: 100", "no coverage")
27792779
}
27802780

2781+
// regression test for github.com/golang/go/issues/66038
2782+
func TestCoverpkgIngoresSpecialDirs(t *testing.T) {
2783+
skipIfGccgo(t, "gccgo has no cover tool")
2784+
tooSlow(t, "links and runs a test binary with coverage enabled")
2785+
2786+
tg := testgo(t)
2787+
defer tg.cleanup()
2788+
2789+
wd, err := os.Getwd()
2790+
tg.check(err)
2791+
tg.makeTempdir()
2792+
tg.check(os.Chdir(tg.path(".")))
2793+
defer func() { tg.check(os.Chdir(wd)) }()
2794+
2795+
tg.tempFile("a/a.go", `package a
2796+
import ( _ "b" )
2797+
func F(i int) int {
2798+
return i*i
2799+
}`)
2800+
tg.tempFile("a/a_test.go", `
2801+
package a
2802+
import ( "testing" )
2803+
func TestF(t *testing.T) { F(2) }
2804+
`)
2805+
2806+
for _, skipDir := range []string{".dir", "_dir", "testdata"} {
2807+
t.Run(skipDir, func(t *testing.T) {
2808+
tg.tempFile(fmt.Sprintf("%s/src/b/b.go", skipDir), `package b
2809+
func G(i int) int {
2810+
return i*i
2811+
}`)
2812+
tg.setenv("GOPATH", tg.path(skipDir))
2813+
2814+
tg.run("test", "-coverpkg=./...", "./...")
2815+
2816+
tg.grepStderrNot("no packages being tested depend on matches", "bad match message")
2817+
tg.grepStdout("coverage: 100", "no coverage")
2818+
})
2819+
}
2820+
}
2821+
27812822
// Regression test for golang.org/issue/34499: version command should not crash
27822823
// when executed in a deleted directory on Linux.
27832824
func TestExecInDeletedDir(t *testing.T) {

src/cmd/go/internal/load/flag_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ var ppfTests = []ppfTest{
9292
ppfDirTest("./...", 3, "/my/test/dir", "/my/test/dir/sub", "/my/test/dir/sub/sub", "/my/test/other", "/my/test/other/sub"),
9393
ppfDirTest("../...", 4, "/my/test/dir", "/my/test/other", "/my/test/dir/sub", "/my/test/other/sub", "/my/other/test"),
9494
ppfDirTest("../...sub...", 3, "/my/test/dir/sub", "/my/test/othersub", "/my/test/yellowsubmarine", "/my/other/test"),
95+
ppfDirTest("./...", 1, "/my/test/dir", "/my/test/dir/.sub", "/test/dir/_sub", "/test/dir/testdata/sub"),
9596
}
9697

9798
func ppfDirTest(pattern string, nmatch int, dirs ...string) ppfTest {

src/cmd/go/internal/load/search.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
3838
// Cannot make relative - e.g. different drive letters on Windows.
3939
return false
4040
}
41+
4142
rel = filepath.ToSlash(rel)
4243
if rel == ".." || strings.HasPrefix(rel, "../") {
4344
return false
4445
}
46+
47+
cwdRel, err := filepath.Rel(cwd, p.Dir)
48+
if err == nil && cwdRel != "." && !strings.HasPrefix(cwdRel, "../") &&
49+
// Avoid .foo, _foo, and testdata subdirectory trees.
50+
(strings.HasPrefix(cwdRel, ".") || strings.HasPrefix(cwdRel, "_") || strings.HasPrefix(cwdRel, "testdata"+string(filepath.Separator))) {
51+
return false
52+
}
53+
4554
return matchPath(rel)
4655
}
4756
case pattern == "all":

0 commit comments

Comments
 (0)