cmd/go, testing: streamline direct use of test binaries
Before:
$ go test -c -cover fmt
$ ./fmt.test -test.covermode=set
PASS
coverage: 65.1% of statements in strconv
$
After:
$ go test -c -cover fmt
$ ./fmt.test
PASS
coverage: 65.1% of statements in strconv
$
In addition to being cumbersome, the old flag didn't make sense:
the cover mode cannot be changed after the binary has been built.
Another useful effect of this CL is that if you happen to do
$ go test -c -covermode=atomic fmt
and then forget you did that and run benchmarks,
the final line of the output (the coverage summary) reminds you
that you are benchmarking with coverage enabled, which might
not be what you want.
$ ./fmt.test -test.bench .
PASS
BenchmarkSprintfEmpty 10000000 217 ns/op
BenchmarkSprintfString 2000000 755 ns/op
BenchmarkSprintfInt 2000000 774 ns/op
BenchmarkSprintfIntInt 1000000 1363 ns/op
BenchmarkSprintfPrefixedInt 1000000 1501 ns/op
BenchmarkSprintfFloat 1000000 1257 ns/op
BenchmarkManyArgs 500000 5346 ns/op
BenchmarkScanInts 1000 2562402 ns/op
BenchmarkScanRecursiveInt 500 3189457 ns/op
coverage: 91.4% of statements
$
As part of passing the new mode setting in via _testmain.go, merge
the two registration mechanisms into one extensible mechanism
(a struct).
R=r
CC=golang-dev
https://golang.org/cl/11219043
diff --git a/src/pkg/testing/cover.go b/src/pkg/testing/cover.go
index 22a5299..dd29364 100644
--- a/src/pkg/testing/cover.go
+++ b/src/pkg/testing/cover.go
@@ -22,30 +22,23 @@
Stmts uint16
}
-var (
- coverCounters map[string][]uint32
- coverBlocks map[string][]CoverBlock
-)
+var cover Cover
-var (
- testedPackage string // The package being tested.
- coveredPackage string // List of the package[s] being covered, if distinct from the tested package.
-)
-
-// RegisterCover records the coverage data accumulators for the tests.
+// Cover records information about test coverage checking.
// NOTE: This struct is internal to the testing infrastructure and may change.
// It is not covered (yet) by the Go 1 compatibility guidelines.
-func RegisterCover(c map[string][]uint32, b map[string][]CoverBlock) {
- coverCounters = c
- coverBlocks = b
+type Cover struct {
+ Mode string
+ Counters map[string][]uint32
+ Blocks map[string][]CoverBlock
+ CoveredPackages string
}
-// CoveredPackage records the names of the packages being tested and covered.
+// RegisterCover records the coverage data accumulators for the tests.
// NOTE: This function is internal to the testing infrastructure and may change.
// It is not covered (yet) by the Go 1 compatibility guidelines.
-func CoveredPackage(tested, covered string) {
- testedPackage = tested
- coveredPackage = covered
+func RegisterCover(c Cover) {
+ cover = c
}
// mustBeNil checks the error and, if present, reports it and exits.
@@ -63,13 +56,13 @@
if *coverProfile != "" {
f, err = os.Create(toOutputDir(*coverProfile))
mustBeNil(err)
- fmt.Fprintf(f, "mode: %s\n", *coverMode)
+ fmt.Fprintf(f, "mode: %s\n", cover.Mode)
defer func() { mustBeNil(f.Close()) }()
}
var active, total int64
- for name, counts := range coverCounters {
- blocks := coverBlocks[name]
+ for name, counts := range cover.Counters {
+ blocks := cover.Blocks[name]
for i, count := range counts {
stmts := int64(blocks[i].Stmts)
total += stmts
@@ -89,5 +82,5 @@
if total == 0 {
total = 1
}
- fmt.Printf("coverage: %.1f%% of statements%s\n", 100*float64(active)/float64(total), coveredPackage)
+ fmt.Printf("coverage: %.1f%% of statements%s\n", 100*float64(active)/float64(total), cover.CoveredPackages)
}