mirror of
https://github.com/golang/go
synced 2024-11-19 16:24:45 -07:00
cmd/go: fix -coverpkg=all with dot imports
If you use -coverpkg=all you get coverage for all packages in the build. Go 1.9 used a global counter for all the GoCover variables, so that they were distinct for the entire build. The global counter caused problems with caching, so we switched to a per-package counter. But now the GoCover_0 in one package may be dot-imported into another and conflict with the GoCover_0 in that other package. Reestablish (overwhelmingly likely) global uniqueness of GoCover variables by appending an _xxxxxxxxxxxx suffix, where the x's are the prefix of the SHA256 hash of the import path. The point is only to avoid accidents, not to defeat people determined to break the tools. Fixes #23432. Change-Id: I3088eceebbe35174f2eefe8d558b7c8b59d3eeac Reviewed-on: https://go-review.googlesource.com/89135 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
236abdb46b
commit
8d88c9ae07
@ -2437,6 +2437,16 @@ func TestCoverageRuns(t *testing.T) {
|
||||
checkCoverage(tg, data)
|
||||
}
|
||||
|
||||
func TestCoverageDotImport(t *testing.T) {
|
||||
tg := testgo(t)
|
||||
defer tg.cleanup()
|
||||
tg.parallel()
|
||||
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
|
||||
tg.run("test", "-coverpkg=coverdot1,coverdot2", "coverdot2")
|
||||
data := tg.getStdout() + tg.getStderr()
|
||||
checkCoverage(tg, data)
|
||||
}
|
||||
|
||||
// Check that coverage analysis uses set mode.
|
||||
// Also check that coverage profiles merge correctly.
|
||||
func TestCoverageUsesSetMode(t *testing.T) {
|
||||
|
@ -6,6 +6,7 @@ package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
@ -1091,13 +1092,21 @@ func isTestFile(file string) bool {
|
||||
func declareCoverVars(importPath string, files ...string) map[string]*load.CoverVar {
|
||||
coverVars := make(map[string]*load.CoverVar)
|
||||
coverIndex := 0
|
||||
// We create the cover counters as new top-level variables in the package.
|
||||
// We need to avoid collisions with user variables (GoCover_0 is unlikely but still)
|
||||
// and more importantly with dot imports of other covered packages,
|
||||
// so we append 12 hex digits from the SHA-256 of the import path.
|
||||
// The point is only to avoid accidents, not to defeat users determined to
|
||||
// break things.
|
||||
sum := sha256.Sum256([]byte(importPath))
|
||||
h := fmt.Sprintf("%x", sum[:6])
|
||||
for _, file := range files {
|
||||
if isTestFile(file) {
|
||||
continue
|
||||
}
|
||||
coverVars[file] = &load.CoverVar{
|
||||
File: filepath.Join(importPath, file),
|
||||
Var: fmt.Sprintf("GoCover_%d", coverIndex),
|
||||
Var: fmt.Sprintf("GoCover_%d_%x", coverIndex, h),
|
||||
}
|
||||
coverIndex++
|
||||
}
|
||||
|
3
src/cmd/go/testdata/src/coverdot1/p.go
vendored
Normal file
3
src/cmd/go/testdata/src/coverdot1/p.go
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
package coverdot1
|
||||
|
||||
func F() {}
|
5
src/cmd/go/testdata/src/coverdot2/p.go
vendored
Normal file
5
src/cmd/go/testdata/src/coverdot2/p.go
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
package coverdot2
|
||||
|
||||
import . "coverdot1"
|
||||
|
||||
func G() { F() }
|
7
src/cmd/go/testdata/src/coverdot2/p_test.go
vendored
Normal file
7
src/cmd/go/testdata/src/coverdot2/p_test.go
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
package coverdot2
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestG(t *testing.T) {
|
||||
G()
|
||||
}
|
Loading…
Reference in New Issue
Block a user