Skip to content

Commit 35b1a14

Browse files
committed
[release-branch.go1.22] cmd/go: respect -coverpkg for unselected no-test packages
This patch fixes a bug in the code that reports coverage percentages and/or profiles for packages without tests. Specifically, the code added as part of the fix for issue 24570 (in CL 495447) didn't properly consider the -coverpkg selection and would look for the build action meta-data file for a package that wasn't actually selected for coverage. Updates #65653. Fixes #66137. Change-Id: I66ffac11783c00a8cbd855fd05b9a90e4e0ed402 Reviewed-on: https://go-review.googlesource.com/c/go/+/568835 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]> (cherry picked from commit 2b22fc1) Reviewed-on: https://go-review.googlesource.com/c/go/+/569575
1 parent db6097f commit 35b1a14

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action)
13961396

13971397
if p := a.Package; len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
13981398
reportNoTestFiles := true
1399-
if cfg.BuildCover && cfg.Experiment.CoverageRedesign {
1399+
if cfg.BuildCover && cfg.Experiment.CoverageRedesign && p.Internal.Cover.GenMeta {
14001400
if err := sh.Mkdir(a.Objdir); err != nil {
14011401
return err
14021402
}

src/cmd/go/testdata/script/cover_coverpkg_partial.txt

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f
3939
stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f'
4040
stdout '^\s*M/f\s+coverage: 0.0% of statements'
4141

42+
# This sub-test inspired by issue 65653: if package P is is matched
43+
# via the package pattern supplied as the argument to "go test -cover"
44+
# but P is not part of "-coverpkg", then we don't want coverage for P
45+
# (including the specific case where P has no test files).
46+
go test -coverpkg=./a ./...
47+
stdout '^ok\s+M/a\s+\S+\s+coverage: 100.0% of statements in ./a'
48+
stdout '^\s*\?\s+M/f\s+\[no test files\]'
49+
4250
-- a/a.go --
4351
package a
4452

0 commit comments

Comments
 (0)