Skip to content

Commit 5045538

Browse files
thanmgopherbot
authored andcommitted
internal/coverage: fix bug in text-format coverage output with multiple packages
In ProcessCoverTestDir pass the selected set of packages to EmitTextual in addition to EmitPercent, so that when we have runs with multiple packages selected but without -coverpkg, text format output for package P was incorrectly including output for P's covered dependencies. This is in effect an extension of the fix for issue 65570. Includes a cmd/go script test to verify correct behavior; ideally it would be nice to locate this test in .../internal/coverage somewhere but at the moment script tests are only supported for cmd/{go,compile,link}. Updates #65570. Fixes #70244. Change-Id: Ia0bb10155353aa0f2ead46e81a2aaa71bde4ef82 Reviewed-on: https://go-review.googlesource.com/c/go/+/627316 Reviewed-by: David Chase <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Than McIntosh <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 28d389e commit 5045538

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Testcase for #70244. In this bug we're doing a "go test -coverprofile"
2+
# run for a pair of packages, the first one without tests and the second
3+
# one with tests. When writing the profile for the second test, profile
4+
# data from the first package was leaking into the output (we should
5+
# only see lines in the output profile for the package whose test is
6+
# being run).
7+
8+
[short] skip
9+
10+
# Kick off test.
11+
go test -vet=off -count=1 -coverprofile=cov.p ./...
12+
13+
# Generate a function profile.
14+
go tool cover -func=cov.p
15+
16+
# Prior to GOEXPERIMENT=coverageredesign we should see no output at all for
17+
# pkg1 (since it has no tests).
18+
[!GOEXPERIMENT:coverageredesign] ! stdout 'pkg1'
19+
20+
# With GOEXPERIMENT=coverageredesign enabled we should see zero percent
21+
# coverage for pkg1's DoSomething, not 100% (as in the bug).
22+
[GOEXPERIMENT:coverageredesign] stdout 'cov/pkg1/file.go:3:\s+DoSomething\s+0.0%'
23+
24+
-- go.mod --
25+
module cov
26+
27+
-- pkg1/file.go --
28+
package pkg1
29+
30+
func DoSomething() bool {
31+
return true
32+
}
33+
-- pkg2/file.go --
34+
package pkg2
35+
36+
func DoSomething() bool {
37+
return true
38+
}
39+
-- pkg2/file_test.go --
40+
package pkg2
41+
42+
import (
43+
"cov/pkg1"
44+
"testing"
45+
)
46+
47+
func TestSmth(t *testing.T) {
48+
pkg1.DoSomething()
49+
DoSomething()
50+
}

src/internal/coverage/cfile/testsupport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func ProcessCoverTestDir(dir string, cfile string, cm string, cpkg string, w io.
109109

110110
// Emit text output.
111111
if tf != nil {
112-
if err := ts.cf.EmitTextual(nil, tf); err != nil {
112+
if err := ts.cf.EmitTextual(selpkgs, tf); err != nil {
113113
return err
114114
}
115115
tfClosed = true

0 commit comments

Comments
 (0)