Skip to content

Commit

Permalink
[release-branch.go1.22] cmd/go: respect -coverpkg for unselected no-t…
Browse files Browse the repository at this point in the history
…est 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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
(cherry picked from commit 2b22fc1)
Reviewed-on: https://go-review.googlesource.com/c/go/+/569575
  • Loading branch information
thanm committed Mar 21, 2024
1 parent db6097f commit 35b1a14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/go/internal/test/test.go
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions src/cmd/go/testdata/script/cover_coverpkg_partial.txt
Expand Up @@ -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

Expand Down

0 comments on commit 35b1a14

Please sign in to comment.