Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(497)

Unified Diff: src/cmd/go/testflag.go

Issue 5495055: code review 5495055: go: implement test command (Closed)
Patch Set: diff -r e4e0c7c99d9d https://go.googlecode.com/hg/ Created 13 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/cmd/go/test.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/cmd/go/testflag.go
===================================================================
copy from src/cmd/gotest/flag.go
copy to src/cmd/go/testflag.go
--- a/src/cmd/gotest/flag.go
+++ b/src/cmd/go/testflag.go
@@ -11,14 +11,15 @@
"strings"
)
-// The flag handling part of gotest is large and distracting.
+// The flag handling part of go test is large and distracting.
// We can't use the flag package because some of the flags from
// our command line are for us, and some are for 6.out, and
// some are for both.
-var usageMessage = `Usage of %s:
+var usageMessage = `Usage of go test:
-c=false: compile but do not run the test binary
- -file=file:
+ -file=file_test.go: specify file to use for tests;
+ use multiple times for multiple files
-x=false: print command lines as they are executed
// These flags can be passed with or without a "test." prefix: -v or -test.v.
@@ -36,13 +37,14 @@
`
// usage prints a usage message and exits.
-func usage() {
- fmt.Fprintf(os.Stdout, usageMessage, os.Args[0])
- os.Exit(2)
+func testUsage() {
+ fmt.Fprint(os.Stderr, usageMessage)
+ exitStatus = 2
+ exit()
}
-// flagSpec defines a flag we know about.
-type flagSpec struct {
+// testFlagSpec defines a flag we know about.
+type testFlagSpec struct {
name string
isBool bool
passToTest bool // pass to Test
@@ -50,9 +52,9 @@
present bool // flag has been seen
}
-// flagDefn is the set of flags we process.
-var flagDefn = []*flagSpec{
- // gotest-local.
+// testFlagDefn is the set of flags we process.
+var testFlagDefn = []*testFlagSpec{
+ // local.
{name: "c", isBool: true},
{name: "file", multiOK: true},
{name: "x", isBool: true},
@@ -71,39 +73,40 @@
{name: "v", isBool: true, passToTest: true},
}
-// flags processes the command line, grabbing -x and -c, rewriting known flags
+// testFlags processes the command line, grabbing -x and -c, rewriting known flags
// to have "test" before them, and reading the command line for the 6.out.
-// Unfortunately for us, we need to do our own flag processing because gotest
+// Unfortunately for us, we need to do our own flag processing because go test
// grabs some flags but otherwise its command line is just a holding place for
-// 6.out's arguments.
-func flags() {
- for i := 1; i < len(os.Args); i++ {
- arg := os.Args[i]
- f, value, extraWord := flag(i)
+// test.out's arguments.
+func testFlags(args []string) (passToTest []string) {
+ for i := 0; i < len(args); i++ {
+ arg := args[i]
+ f, value, extraWord := testFlag(args, i)
if f == nil {
args = append(args, arg)
continue
}
switch f.name {
case "c":
- setBoolFlag(&cFlag, value)
+ setBoolFlag(&testC, value)
case "x":
- setBoolFlag(&xFlag, value)
+ setBoolFlag(&testX, value)
case "file":
- fileNames = append(fileNames, value)
+ testFiles = append(testFiles, value)
}
if extraWord {
i++
}
if f.passToTest {
- args = append(args, "-test."+f.name+"="+value)
+ passToTest = append(passToTest, "-test."+f.name+"="+value)
}
}
+ return
}
-// flag sees if argument i is a known flag and returns its definition, value, and whether it consumed an extra word.
-func flag(i int) (f *flagSpec, value string, extra bool) {
- arg := os.Args[i]
+// 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]
if strings.HasPrefix(arg, "--") { // reduce two minuses to one
arg = arg[1:]
}
@@ -124,7 +127,7 @@
value = name[equals+1:]
name = name[:equals]
}
- for _, f = range flagDefn {
+ for _, f = range testFlagDefn {
if name == f.name {
// Booleans are special because they have modes -x, -x=true, -x=false.
if f.isBool {
@@ -137,10 +140,10 @@
} else { // Non-booleans must have a value.
extra = equals < 0
if extra {
- if i+1 >= len(os.Args) {
+ if i+1 >= len(args) {
usage()
}
- value = os.Args[i+1]
+ value = args[i+1]
}
}
if f.present && !f.multiOK {
@@ -158,7 +161,7 @@
func setBoolFlag(flag *bool, value string) {
x, err := strconv.ParseBool(value)
if err != nil {
- fmt.Fprintf(os.Stderr, "gotest: illegal bool flag value %s\n", value)
+ fmt.Fprintf(os.Stderr, "go test: illegal bool flag value %s\n", value)
usage()
}
*flag = x
« no previous file with comments | « src/cmd/go/test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b