Commit cb2461ba authored by Rob Pike's avatar Rob Pike

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
parent 27cca31e
...@@ -739,24 +739,24 @@ control the execution of any test: ...@@ -739,24 +739,24 @@ control the execution of any test:
are recorded, equivalent to -test.blockprofilerate=1. are recorded, equivalent to -test.blockprofilerate=1.
-cover -cover
Enable basic coverage analysis; shorthand for -covermode=set. Enable coverage analysis.
TODO: This feature is not yet fully implemented. TODO: This feature is not yet fully implemented.
-covermode set,count,atomic -covermode set,count,atomic
Set the mode for coverage analysis for the package[s] Set the mode for coverage analysis for the package[s]
being tested. The default is to do none, but if -cover or being tested. The default is "set".
-coverprofile is specified, coverage is enabled in "set"
mode unless this flag is also specified.
The values: The values:
set: bool: does this statement run? set: bool: does this statement run?
count: int: how many times does this statement run? count: int: how many times does this statement run?
atomic: int: count, but correct in multithreaded tests; atomic: int: count, but correct in multithreaded tests;
significantly more expensive. significantly more expensive.
Implies -cover.
Sets -v. TODO: This will change. Sets -v. TODO: This will change.
-coverprofile cover.out -coverprofile cover.out
Write a coverage profile to the specified file after all tests Write a coverage profile to the specified file after all tests
have passed. have passed.
Implies -cover.
-cpu 1,2,4 -cpu 1,2,4
Specify a list of GOMAXPROCS values for which the tests or Specify a list of GOMAXPROCS values for which the tests or
......
...@@ -125,24 +125,24 @@ control the execution of any test: ...@@ -125,24 +125,24 @@ control the execution of any test:
are recorded, equivalent to -test.blockprofilerate=1. are recorded, equivalent to -test.blockprofilerate=1.
-cover -cover
Enable basic coverage analysis; shorthand for -covermode=set. Enable coverage analysis.
TODO: This feature is not yet fully implemented. TODO: This feature is not yet fully implemented.
-covermode set,count,atomic -covermode set,count,atomic
Set the mode for coverage analysis for the package[s] Set the mode for coverage analysis for the package[s]
being tested. The default is to do none, but if -cover or being tested. The default is "set".
-coverprofile is specified, coverage is enabled in "set"
mode unless this flag is also specified.
The values: The values:
set: bool: does this statement run? set: bool: does this statement run?
count: int: how many times does this statement run? count: int: how many times does this statement run?
atomic: int: count, but correct in multithreaded tests; atomic: int: count, but correct in multithreaded tests;
significantly more expensive. significantly more expensive.
Implies -cover.
Sets -v. TODO: This will change. Sets -v. TODO: This will change.
-coverprofile cover.out -coverprofile cover.out
Write a coverage profile to the specified file after all tests Write a coverage profile to the specified file after all tests
have passed. have passed.
Implies -cover.
-cpu 1,2,4 -cpu 1,2,4
Specify a list of GOMAXPROCS values for which the tests or Specify a list of GOMAXPROCS values for which the tests or
......
...@@ -27,9 +27,9 @@ var usageMessage = `Usage of go test: ...@@ -27,9 +27,9 @@ var usageMessage = `Usage of go test:
-bench="": passes -test.bench to test -bench="": passes -test.bench to test
-benchmem=false: print memory allocation statistics for benchmarks -benchmem=false: print memory allocation statistics for benchmarks
-benchtime=1s: passes -test.benchtime to test -benchtime=1s: passes -test.benchtime to test
-cover=false: basic coverage; equivalent to -covermode=set -cover=false: enable coverage analysis
-covermode="": passes -test.covermode to test -covermode="set": passes -test.covermode to test if -cover
-coverprofile="": passes -test.coverprofile to test -coverprofile="": passes -test.coverprofile to test if -cover
-cpu="": passes -test.cpu to test -cpu="": passes -test.cpu to test
-cpuprofile="": passes -test.cpuprofile to test -cpuprofile="": passes -test.cpuprofile to test
-memprofile="": passes -test.memprofile to test -memprofile="": passes -test.memprofile to test
...@@ -85,7 +85,7 @@ var testFlagDefn = []*testFlagSpec{ ...@@ -85,7 +85,7 @@ var testFlagDefn = []*testFlagSpec{
{name: "bench", passToTest: true}, {name: "bench", passToTest: true},
{name: "benchmem", boolVar: new(bool), passToTest: true}, {name: "benchmem", boolVar: new(bool), passToTest: true},
{name: "benchtime", passToTest: true}, {name: "benchtime", passToTest: true},
{name: "covermode"}, // Passed to test by special arrangement. {name: "covermode"},
{name: "coverprofile", passToTest: true}, {name: "coverprofile", passToTest: true},
{name: "cpu", passToTest: true}, {name: "cpu", passToTest: true},
{name: "cpuprofile", passToTest: true}, {name: "cpuprofile", passToTest: true},
...@@ -179,12 +179,18 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -179,12 +179,18 @@ func testFlags(args []string) (packageNames, passToTest []string) {
case "blockprofile", "cpuprofile", "memprofile": case "blockprofile", "cpuprofile", "memprofile":
testProfile = true testProfile = true
case "coverprofile": case "coverprofile":
passToTest = setCoverMode("set", passToTest) testCover = true
testProfile = 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": case "outputdir":
outputDir = value outputDir = value
case "covermode":
passToTest = setCoverMode(value, passToTest)
} }
if extraWord { if extraWord {
i++ i++
...@@ -193,9 +199,11 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -193,9 +199,11 @@ func testFlags(args []string) (packageNames, passToTest []string) {
passToTest = append(passToTest, "-test."+f.name+"="+value) passToTest = append(passToTest, "-test."+f.name+"="+value)
} }
} }
// -cover is shorthand for -covermode=set. if testCover {
if testCover && testCoverMode == "" { if testCoverMode == "" {
passToTest = setCoverMode("set", passToTest) testCoverMode = "set"
}
passToTest = append(passToTest, "-test.covermode", testCoverMode)
} }
// Tell the test what directory we're running in, so it can write the profiles there. // Tell the test what directory we're running in, so it can write the profiles there.
if testProfile && outputDir == "" { if testProfile && outputDir == "" {
...@@ -208,24 +216,6 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -208,24 +216,6 @@ func testFlags(args []string) (packageNames, passToTest []string) {
return 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. // 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) { func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) {
arg := args[i] arg := args[i]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment