diff options
author | Rob Pike <r@golang.org> | 2013-06-18 17:15:26 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2013-06-18 17:15:26 -0700 |
commit | 3d989656b3c32abdbadd1536a856a4846b4c6a06 (patch) | |
tree | 29b8c941c38341ee9959f54cdc13efe069a1c13e /src/cmd/go/testflag.go | |
parent | e31c61add0a689049e5884912c403b6f75f4d95d (diff) | |
download | go-3d989656b3c32abdbadd1536a856a4846b4c6a06.tar.gz |
cmd/go: simplify flags for coverage
The single flag -cover provides the default simplest behavior.
The other flags, -covermode and -coverprofile, provide more
control. The three flags interconnect to work well.
R=rsc, adg
CC=golang-dev
https://codereview.appspot.com/10364044
Diffstat (limited to 'src/cmd/go/testflag.go')
-rw-r--r-- | src/cmd/go/testflag.go | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/src/cmd/go/testflag.go b/src/cmd/go/testflag.go index cd90a2f17..70d40778b 100644 --- a/src/cmd/go/testflag.go +++ b/src/cmd/go/testflag.go @@ -27,7 +27,8 @@ var usageMessage = `Usage of go test: -bench="": passes -test.bench to test -benchmem=false: print memory allocation statistics for benchmarks -benchtime=1s: passes -test.benchtime to test - -cover="": passes -test.cover to test + -cover=false: basic coverage; equivalent to -covermode=set + -covermode="": passes -test.covermode to test -coverprofile="": passes -test.coverprofile to test -cpu="": passes -test.cpu to test -cpuprofile="": passes -test.cpuprofile to test @@ -65,6 +66,7 @@ var testFlagDefn = []*testFlagSpec{ {name: "c", boolVar: &testC}, {name: "file", multiOK: true}, {name: "i", boolVar: &testI}, + {name: "cover", boolVar: &testCover}, // build flags. {name: "a", boolVar: &buildA}, @@ -83,7 +85,7 @@ var testFlagDefn = []*testFlagSpec{ {name: "bench", passToTest: true}, {name: "benchmem", boolVar: new(bool), passToTest: true}, {name: "benchtime", passToTest: true}, - {name: "cover", passToTest: true}, + {name: "covermode"}, // Passed to test by special arrangement. {name: "coverprofile", passToTest: true}, {name: "cpu", passToTest: true}, {name: "cpuprofile", passToTest: true}, @@ -144,7 +146,7 @@ func testFlags(args []string) (packageNames, passToTest []string) { var err error switch f.name { // bool flags. - case "a", "c", "i", "n", "x", "v", "work", "race": + case "a", "c", "i", "n", "x", "v", "race", "cover", "work": setBoolFlag(f.boolVar, value) case "p": setIntFlag(&buildP, value) @@ -174,19 +176,15 @@ func testFlags(args []string) (packageNames, passToTest []string) { testBench = true case "timeout": testTimeout = value - case "blockprofile", "coverprofile", "cpuprofile", "memprofile": + case "blockprofile", "cpuprofile", "memprofile": + testProfile = true + case "coverprofile": + passToTest = setCoverMode("set", passToTest) testProfile = true case "outputdir": outputDir = value - case "cover": - switch value { - case "set", "count", "atomic": - testCover = value - default: - fatalf("invalid flag argument for -cover: %q", value) - } - // Guarantee we see the coverage statistics. Doesn't turn -v on generally; tricky. TODO? - testV = true + case "covermode": + passToTest = setCoverMode(value, passToTest) } if extraWord { i++ @@ -195,6 +193,10 @@ func testFlags(args []string) (packageNames, passToTest []string) { passToTest = append(passToTest, "-test."+f.name+"="+value) } } + // -cover is shorthand for -covermode=set. + if testCover && testCoverMode == "" { + passToTest = setCoverMode("set", passToTest) + } // Tell the test what directory we're running in, so it can write the profiles there. if testProfile && outputDir == "" { dir, err := os.Getwd() @@ -206,6 +208,24 @@ func testFlags(args []string) (packageNames, passToTest []string) { 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] |