github.com/fsouza/go@v0.0.0-20160225033436-e14546fefa5e/testflag.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "flag" 9 "fmt" 10 "os" 11 "strconv" 12 "strings" 13 ) 14 15 // The flag handling part of go test is large and distracting. 16 // We can't use the flag package because some of the flags from 17 // our command line are for us, and some are for 6.out, and 18 // some are for both. 19 20 // testFlagSpec defines a flag we know about. 21 type testFlagSpec struct { 22 name string 23 boolVar *bool 24 flagValue flag.Value 25 passToTest bool // pass to Test 26 multiOK bool // OK to have multiple instances 27 present bool // flag has been seen 28 } 29 30 // testFlagDefn is the set of flags we process. 31 var testFlagDefn = []*testFlagSpec{ 32 // local. 33 {name: "c", boolVar: &testC}, 34 {name: "i", boolVar: &buildI}, 35 {name: "o"}, 36 {name: "cover", boolVar: &testCover}, 37 {name: "covermode"}, 38 {name: "coverpkg"}, 39 {name: "exec"}, 40 41 // passed to 6.out, adding a "test." prefix to the name if necessary: -v becomes -test.v. 42 {name: "bench", passToTest: true}, 43 {name: "benchmem", boolVar: new(bool), passToTest: true}, 44 {name: "benchtime", passToTest: true}, 45 {name: "count", passToTest: true}, 46 {name: "coverprofile", passToTest: true}, 47 {name: "cpu", passToTest: true}, 48 {name: "cpuprofile", passToTest: true}, 49 {name: "memprofile", passToTest: true}, 50 {name: "memprofilerate", passToTest: true}, 51 {name: "blockprofile", passToTest: true}, 52 {name: "blockprofilerate", passToTest: true}, 53 {name: "outputdir", passToTest: true}, 54 {name: "parallel", passToTest: true}, 55 {name: "run", passToTest: true}, 56 {name: "short", boolVar: new(bool), passToTest: true}, 57 {name: "timeout", passToTest: true}, 58 {name: "trace", passToTest: true}, 59 {name: "v", boolVar: &testV, passToTest: true}, 60 } 61 62 // add build flags to testFlagDefn 63 func init() { 64 var cmd Command 65 addBuildFlags(&cmd) 66 cmd.Flag.VisitAll(func(f *flag.Flag) { 67 if f.Name == "v" { 68 // test overrides the build -v flag 69 return 70 } 71 testFlagDefn = append(testFlagDefn, &testFlagSpec{ 72 name: f.Name, 73 flagValue: f.Value, 74 }) 75 }) 76 } 77 78 // testFlags processes the command line, grabbing -x and -c, rewriting known flags 79 // to have "test" before them, and reading the command line for the 6.out. 80 // Unfortunately for us, we need to do our own flag processing because go test 81 // grabs some flags but otherwise its command line is just a holding place for 82 // pkg.test's arguments. 83 // We allow known flags both before and after the package name list, 84 // to allow both 85 // go test fmt -custom-flag-for-fmt-test 86 // go test -x math 87 func testFlags(args []string) (packageNames, passToTest []string) { 88 inPkg := false 89 outputDir := "" 90 var explicitArgs []string 91 for i := 0; i < len(args); i++ { 92 if !strings.HasPrefix(args[i], "-") { 93 if !inPkg && packageNames == nil { 94 // First package name we've seen. 95 inPkg = true 96 } 97 if inPkg { 98 packageNames = append(packageNames, args[i]) 99 continue 100 } 101 } 102 103 if inPkg { 104 // Found an argument beginning with "-"; end of package list. 105 inPkg = false 106 } 107 108 f, value, extraWord := testFlag(args, i) 109 if f == nil { 110 // This is a flag we do not know; we must assume 111 // that any args we see after this might be flag 112 // arguments, not package names. 113 inPkg = false 114 if packageNames == nil { 115 // make non-nil: we have seen the empty package list 116 packageNames = []string{} 117 } 118 if args[i] == "-args" || args[i] == "--args" { 119 // -args or --args signals that everything that follows 120 // should be passed to the test. 121 explicitArgs = args[i+1:] 122 break 123 } 124 passToTest = append(passToTest, args[i]) 125 continue 126 } 127 if f.flagValue != nil { 128 if err := f.flagValue.Set(value); err != nil { 129 fatalf("invalid flag argument for -%s: %v", f.name, err) 130 } 131 } else { 132 // Test-only flags. 133 // Arguably should be handled by f.flagValue, but aren't. 134 var err error 135 switch f.name { 136 // bool flags. 137 case "c", "i", "v", "cover": 138 setBoolFlag(f.boolVar, value) 139 case "o": 140 testO = value 141 testNeedBinary = true 142 case "exec": 143 execCmd, err = splitQuotedFields(value) 144 if err != nil { 145 fatalf("invalid flag argument for -%s: %v", f.name, err) 146 } 147 case "bench": 148 // record that we saw the flag; don't care about the value 149 testBench = true 150 case "timeout": 151 testTimeout = value 152 case "blockprofile", "cpuprofile", "memprofile", "trace": 153 testProfile = true 154 testNeedBinary = true 155 case "coverpkg": 156 testCover = true 157 if value == "" { 158 testCoverPaths = nil 159 } else { 160 testCoverPaths = strings.Split(value, ",") 161 } 162 case "coverprofile": 163 testCover = true 164 testProfile = true 165 case "covermode": 166 switch value { 167 case "set", "count", "atomic": 168 testCoverMode = value 169 default: 170 fatalf("invalid flag argument for -covermode: %q", value) 171 } 172 testCover = true 173 case "outputdir": 174 outputDir = value 175 } 176 } 177 if extraWord { 178 i++ 179 } 180 if f.passToTest { 181 passToTest = append(passToTest, "-test."+f.name+"="+value) 182 } 183 } 184 185 if testCoverMode == "" { 186 testCoverMode = "set" 187 if buildRace { 188 // Default coverage mode is atomic when -race is set. 189 testCoverMode = "atomic" 190 } 191 } 192 193 // Tell the test what directory we're running in, so it can write the profiles there. 194 if testProfile && outputDir == "" { 195 dir, err := os.Getwd() 196 if err != nil { 197 fatalf("error from os.Getwd: %s", err) 198 } 199 passToTest = append(passToTest, "-test.outputdir", dir) 200 } 201 202 passToTest = append(passToTest, explicitArgs...) 203 return 204 } 205 206 // testFlag sees if argument i is a known flag and returns its definition, value, and whether it consumed an extra word. 207 func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) { 208 arg := args[i] 209 if strings.HasPrefix(arg, "--") { // reduce two minuses to one 210 arg = arg[1:] 211 } 212 switch arg { 213 case "-?", "-h", "-help": 214 usage() 215 } 216 if arg == "" || arg[0] != '-' { 217 return 218 } 219 name := arg[1:] 220 // If there's already "test.", drop it for now. 221 name = strings.TrimPrefix(name, "test.") 222 equals := strings.Index(name, "=") 223 if equals >= 0 { 224 value = name[equals+1:] 225 name = name[:equals] 226 } 227 for _, f = range testFlagDefn { 228 if name == f.name { 229 // Booleans are special because they have modes -x, -x=true, -x=false. 230 if f.boolVar != nil || isBoolFlag(f.flagValue) { 231 if equals < 0 { // otherwise, it's been set and will be verified in setBoolFlag 232 value = "true" 233 } else { 234 // verify it parses 235 setBoolFlag(new(bool), value) 236 } 237 } else { // Non-booleans must have a value. 238 extra = equals < 0 239 if extra { 240 if i+1 >= len(args) { 241 testSyntaxError("missing argument for flag " + f.name) 242 } 243 value = args[i+1] 244 } 245 } 246 if f.present && !f.multiOK { 247 testSyntaxError(f.name + " flag may be set only once") 248 } 249 f.present = true 250 return 251 } 252 } 253 f = nil 254 return 255 } 256 257 // isBoolFlag reports whether v is a bool flag. 258 func isBoolFlag(v flag.Value) bool { 259 vv, ok := v.(interface { 260 IsBoolFlag() bool 261 }) 262 if ok { 263 return vv.IsBoolFlag() 264 } 265 return false 266 } 267 268 // setBoolFlag sets the addressed boolean to the value. 269 func setBoolFlag(flag *bool, value string) { 270 x, err := strconv.ParseBool(value) 271 if err != nil { 272 testSyntaxError("illegal bool flag value " + value) 273 } 274 *flag = x 275 } 276 277 // setIntFlag sets the addressed integer to the value. 278 func setIntFlag(flag *int, value string) { 279 x, err := strconv.Atoi(value) 280 if err != nil { 281 testSyntaxError("illegal int flag value " + value) 282 } 283 *flag = x 284 } 285 286 func testSyntaxError(msg string) { 287 fmt.Fprintf(os.Stderr, "go test: %s\n", msg) 288 fmt.Fprintf(os.Stderr, `run "go help test" or "go help testflag" for more information`+"\n") 289 os.Exit(2) 290 }