From 35b1a146d9febca70db87dc4e1f8bac33de857bb Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Mon, 4 Mar 2024 15:52:56 +0000 Subject: [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 Reviewed-by: Bryan Mills (cherry picked from commit 2b22fc10459dff0fb4b3e5b08bc14ffb349aa4dd) Reviewed-on: https://go-review.googlesource.com/c/go/+/569575 --- src/cmd/go/internal/test/test.go | 2 +- src/cmd/go/testdata/script/cover_coverpkg_partial.txt | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go index 8a40547f2e..13818b72ab 100644 --- a/src/cmd/go/internal/test/test.go +++ b/src/cmd/go/internal/test/test.go @@ -1396,7 +1396,7 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action) if p := a.Package; len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 { reportNoTestFiles := true - if cfg.BuildCover && cfg.Experiment.CoverageRedesign { + if cfg.BuildCover && cfg.Experiment.CoverageRedesign && p.Internal.Cover.GenMeta { if err := sh.Mkdir(a.Objdir); err != nil { return err } diff --git a/src/cmd/go/testdata/script/cover_coverpkg_partial.txt b/src/cmd/go/testdata/script/cover_coverpkg_partial.txt index 524024101a..ef7a4dd2aa 100644 --- a/src/cmd/go/testdata/script/cover_coverpkg_partial.txt +++ b/src/cmd/go/testdata/script/cover_coverpkg_partial.txt @@ -39,6 +39,14 @@ go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f' stdout '^\s*M/f\s+coverage: 0.0% of statements' +# This sub-test inspired by issue 65653: if package P is is matched +# via the package pattern supplied as the argument to "go test -cover" +# but P is not part of "-coverpkg", then we don't want coverage for P +# (including the specific case where P has no test files). +go test -coverpkg=./a ./... +stdout '^ok\s+M/a\s+\S+\s+coverage: 100.0% of statements in ./a' +stdout '^\s*\?\s+M/f\s+\[no test files\]' + -- a/a.go -- package a -- cgit v1.2.3-54-g00ecf