cmd/go: another attempt at flag handling for coverage
The -cover flag is now just enable/disable and is implied if
either of the other flags is set.
R=rsc
CC=golang-dev
https://golang.org/cl/10420043
diff --git a/src/cmd/go/doc.go b/src/cmd/go/doc.go
index 0e59078..f4269a8 100644
--- a/src/cmd/go/doc.go
+++ b/src/cmd/go/doc.go
@@ -739,24 +739,24 @@
are recorded, equivalent to -test.blockprofilerate=1.
-cover
- Enable basic coverage analysis; shorthand for -covermode=set.
+ Enable coverage analysis.
TODO: This feature is not yet fully implemented.
-covermode set,count,atomic
Set the mode for coverage analysis for the package[s]
- being tested. The default is to do none, but if -cover or
- -coverprofile is specified, coverage is enabled in "set"
- mode unless this flag is also specified.
+ being tested. The default is "set".
The values:
set: bool: does this statement run?
count: int: how many times does this statement run?
atomic: int: count, but correct in multithreaded tests;
significantly more expensive.
+ Implies -cover.
Sets -v. TODO: This will change.
-coverprofile cover.out
Write a coverage profile to the specified file after all tests
have passed.
+ Implies -cover.
-cpu 1,2,4
Specify a list of GOMAXPROCS values for which the tests or
diff --git a/src/cmd/go/test.go b/src/cmd/go/test.go
index 5a9d832..c06fe37 100644
--- a/src/cmd/go/test.go
+++ b/src/cmd/go/test.go
@@ -125,24 +125,24 @@
are recorded, equivalent to -test.blockprofilerate=1.
-cover
- Enable basic coverage analysis; shorthand for -covermode=set.
+ Enable coverage analysis.
TODO: This feature is not yet fully implemented.
-covermode set,count,atomic
Set the mode for coverage analysis for the package[s]
- being tested. The default is to do none, but if -cover or
- -coverprofile is specified, coverage is enabled in "set"
- mode unless this flag is also specified.
+ being tested. The default is "set".
The values:
set: bool: does this statement run?
count: int: how many times does this statement run?
atomic: int: count, but correct in multithreaded tests;
significantly more expensive.
+ Implies -cover.
Sets -v. TODO: This will change.
-coverprofile cover.out
Write a coverage profile to the specified file after all tests
have passed.
+ Implies -cover.
-cpu 1,2,4
Specify a list of GOMAXPROCS values for which the tests or
diff --git a/src/cmd/go/testflag.go b/src/cmd/go/testflag.go
index 70d4077..e8db0dd 100644
--- a/src/cmd/go/testflag.go
+++ b/src/cmd/go/testflag.go
@@ -27,9 +27,9 @@
-bench="": passes -test.bench to test
-benchmem=false: print memory allocation statistics for benchmarks
-benchtime=1s: passes -test.benchtime to test
- -cover=false: basic coverage; equivalent to -covermode=set
- -covermode="": passes -test.covermode to test
- -coverprofile="": passes -test.coverprofile to test
+ -cover=false: enable coverage analysis
+ -covermode="set": passes -test.covermode to test if -cover
+ -coverprofile="": passes -test.coverprofile to test if -cover
-cpu="": passes -test.cpu to test
-cpuprofile="": passes -test.cpuprofile to test
-memprofile="": passes -test.memprofile to test
@@ -85,7 +85,7 @@
{name: "bench", passToTest: true},
{name: "benchmem", boolVar: new(bool), passToTest: true},
{name: "benchtime", passToTest: true},
- {name: "covermode"}, // Passed to test by special arrangement.
+ {name: "covermode"},
{name: "coverprofile", passToTest: true},
{name: "cpu", passToTest: true},
{name: "cpuprofile", passToTest: true},
@@ -179,12 +179,18 @@
case "blockprofile", "cpuprofile", "memprofile":
testProfile = true
case "coverprofile":
- passToTest = setCoverMode("set", passToTest)
+ testCover = true
testProfile = true
+ case "covermode":
+ switch value {
+ case "set", "count", "atomic":
+ testCoverMode = value
+ default:
+ fatalf("invalid flag argument for -cover: %q", value)
+ }
+ testCover = true
case "outputdir":
outputDir = value
- case "covermode":
- passToTest = setCoverMode(value, passToTest)
}
if extraWord {
i++
@@ -193,9 +199,11 @@
passToTest = append(passToTest, "-test."+f.name+"="+value)
}
}
- // -cover is shorthand for -covermode=set.
- if testCover && testCoverMode == "" {
- passToTest = setCoverMode("set", passToTest)
+ if testCover {
+ if testCoverMode == "" {
+ testCoverMode = "set"
+ }
+ passToTest = append(passToTest, "-test.covermode", testCoverMode)
}
// Tell the test what directory we're running in, so it can write the profiles there.
if testProfile && outputDir == "" {
@@ -208,24 +216,6 @@
return
}
-// setCoverMode sets the cover mode if not already specified; it captures the default behavior and
-// canonicalizes the coverage flags to pass to the test binary.
-func setCoverMode(mode string, passToTest []string) []string {
- if testCoverMode != "" {
- return passToTest
- }
- switch mode {
- case "set", "count", "atomic":
- testCoverMode = mode
- default:
- fatalf("invalid flag argument for -cover: %q", mode)
- }
- testCover = true
- // Guarantee we see the coverage statistics. Doesn't turn -v on generally; tricky. TODO?
- testV = true
- return append(passToTest, "-test.covermode", "set")
-}
-
// testFlag sees if argument i is a known flag and returns its definition, value, and whether it consumed an extra word.
func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) {
arg := args[i]