Skip to content

Commit c59b17e

Browse files
committed
cmd/go: make -coverpkg=all skip test-only packages
Otherwise, the added test would fail in an unnecessary way: go build example.com/cov/onlytest: no non-test Go files ... The test script is mimicking other cover_pkgall_*.txt scripts, so it similarly tests both GOPATH and module modes. Fixes #27333. Change-Id: Ie60be569b31d49b173a78556c0669a87ada6799e Reviewed-on: https://go-review.googlesource.com/c/go/+/288292 Trust: Daniel Martí <[email protected]> Trust: Jay Conrod <[email protected]> Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 0525042 commit c59b17e

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/cmd/go/internal/load/test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,12 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
290290
seen[p1] = true
291291
}
292292
for _, p1 := range cover.Pkgs {
293-
if !seen[p1] {
294-
seen[p1] = true
295-
pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
293+
if seen[p1] {
294+
// Don't add duplicate imports.
295+
continue
296296
}
297+
seen[p1] = true
298+
pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
297299
}
298300
}
299301

src/cmd/go/internal/test/test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
716716
}
717717
}
718718

719+
// A package which only has test files can't be imported
720+
// as a dependency, nor can it be instrumented for coverage.
721+
if len(p.GoFiles)+len(p.CgoFiles) == 0 {
722+
continue
723+
}
724+
719725
// Silently ignore attempts to run coverage on
720726
// sync/atomic when using atomic coverage mode.
721727
// Atomic coverage mode uses sync/atomic, so
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This test checks that -coverpkg=all can be used
2+
# when the package pattern includes packages
3+
# which only have tests.
4+
# Verifies golang.org/issue/27333, golang.org/issue/43242.
5+
6+
[short] skip
7+
cd $GOPATH/src/example.com/cov
8+
9+
env GO111MODULE=on
10+
go test -coverpkg=all ./...
11+
12+
env GO111MODULE=off
13+
go test -coverpkg=all ./...
14+
15+
-- $GOPATH/src/example.com/cov/go.mod --
16+
module example.com/cov
17+
18+
-- $GOPATH/src/example.com/cov/notest/notest.go --
19+
package notest
20+
21+
func Foo() {}
22+
23+
-- $GOPATH/src/example.com/cov/onlytest/onlytest_test.go --
24+
package onlytest_test
25+
26+
import (
27+
"testing"
28+
29+
"example.com/cov/notest"
30+
)
31+
32+
func TestFoo(t *testing.T) {
33+
notest.Foo()
34+
}
35+
36+
-- $GOPATH/src/example.com/cov/withtest/withtest.go --
37+
package withtest
38+
39+
func Bar() {}
40+
41+
-- $GOPATH/src/example.com/cov/withtest/withtest_test.go --
42+
package withtest
43+
44+
import "testing"
45+
46+
func TestBar(t *testing.T) {
47+
Bar()
48+
}

0 commit comments

Comments
 (0)