Skip to content

Commit

Permalink
cmd/go: search test imports when matching -coverpkg
Browse files Browse the repository at this point in the history
Fixes #25093

Change-Id: If283275e2b73621ade56d014e60c2d18199b366c
Reviewed-on: https://go-review.googlesource.com/122555
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
  • Loading branch information
ianlancetaylor committed Jul 17, 2018
1 parent 3a28a71 commit f3cdc94
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6210,3 +6210,23 @@ func TestGoBuildDashODevNull(t *testing.T) {
tg.mustNotExist("hello")
tg.mustNotExist("hello.exe")
}

// Issue 25093.
func TestCoverpkgTestOnly(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.parallel()
tg.tempFile("src/a/a.go", `package a
func F(i int) int {
return i*i
}`)
tg.tempFile("src/atest/a_test.go", `
package a_test
import ( "a"; "testing" )
func TestF(t *testing.T) { a.F(2) }
`)
tg.setenv("GOPATH", tg.path("."))
tg.run("test", "-coverpkg=a", "atest")
tg.grepStderrNot("no packages being tested depend on matches", "bad match message")
tg.grepStdout("coverage: 100", "no coverage")
}
38 changes: 37 additions & 1 deletion src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ func (p *Package) UsesCgo() bool {
return len(p.CgoFiles) > 0
}

// packageList returns the list of packages in the dag rooted at roots
// PackageList returns the list of packages in the dag rooted at roots
// as visited in a depth-first post-order traversal.
func PackageList(roots []*Package) []*Package {
seen := map[*Package]bool{}
Expand All @@ -1618,6 +1618,42 @@ func PackageList(roots []*Package) []*Package {
return all
}

// TestPackageList returns the list of packages in the dag rooted at roots
// as visited in a depth-first post-order traversal, including the test
// imports of the roots. This ignores errors in test packages.
func TestPackageList(roots []*Package) []*Package {
seen := map[*Package]bool{}
all := []*Package{}
var walk func(*Package)
walk = func(p *Package) {
if seen[p] {
return
}
seen[p] = true
for _, p1 := range p.Internal.Imports {
walk(p1)
}
all = append(all, p)
}
walkTest := func(root *Package, path string) {
var stk ImportStack
p1 := LoadImport(path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
if p1.Error == nil {
walk(p1)
}
}
for _, root := range roots {
walk(root)
for _, path := range root.TestImports {
walkTest(root, path)
}
for _, path := range root.XTestImports {
walkTest(root, path)
}
}
return all
}

var cmdCache = map[string]*Package{}

func ClearCmdCache() {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ func runTest(cmd *base.Command, args []string) {
}

// Select for coverage all dependencies matching the testCoverPaths patterns.
for _, p := range load.PackageList(pkgs) {
for _, p := range load.TestPackageList(pkgs) {
haveMatch := false
for i := range testCoverPaths {
if match[i](p) {
Expand Down

0 comments on commit f3cdc94

Please sign in to comment.