github.com/m10x/go/src@v0.0.0-20220112094212-ba61592315da/testing/testing.go (about) 1 // Copyright 2009 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 testing provides support for automated testing of Go packages. 6 // It is intended to be used in concert with the "go test" command, which automates 7 // execution of any function of the form 8 // func TestXxx(*testing.T) 9 // where Xxx does not start with a lowercase letter. The function name 10 // serves to identify the test routine. 11 // 12 // Within these functions, use the Error, Fail or related methods to signal failure. 13 // 14 // To write a new test suite, create a file whose name ends _test.go that 15 // contains the TestXxx functions as described here. Put the file in the same 16 // package as the one being tested. The file will be excluded from regular 17 // package builds but will be included when the "go test" command is run. 18 // For more detail, run "go help test" and "go help testflag". 19 // 20 // A simple test function looks like this: 21 // 22 // func TestAbs(t *testing.T) { 23 // got := Abs(-1) 24 // if got != 1 { 25 // t.Errorf("Abs(-1) = %d; want 1", got) 26 // } 27 // } 28 // 29 // Benchmarks 30 // 31 // Functions of the form 32 // func BenchmarkXxx(*testing.B) 33 // are considered benchmarks, and are executed by the "go test" command when 34 // its -bench flag is provided. Benchmarks are run sequentially. 35 // 36 // For a description of the testing flags, see 37 // https://golang.org/cmd/go/#hdr-Testing_flags. 38 // 39 // A sample benchmark function looks like this: 40 // func BenchmarkRandInt(b *testing.B) { 41 // for i := 0; i < b.N; i++ { 42 // rand.Int() 43 // } 44 // } 45 // 46 // The benchmark function must run the target code b.N times. 47 // During benchmark execution, b.N is adjusted until the benchmark function lasts 48 // long enough to be timed reliably. The output 49 // BenchmarkRandInt-8 68453040 17.8 ns/op 50 // means that the loop ran 68453040 times at a speed of 17.8 ns per loop. 51 // 52 // If a benchmark needs some expensive setup before running, the timer 53 // may be reset: 54 // 55 // func BenchmarkBigLen(b *testing.B) { 56 // big := NewBig() 57 // b.ResetTimer() 58 // for i := 0; i < b.N; i++ { 59 // big.Len() 60 // } 61 // } 62 // 63 // If a benchmark needs to test performance in a parallel setting, it may use 64 // the RunParallel helper function; such benchmarks are intended to be used with 65 // the go test -cpu flag: 66 // 67 // func BenchmarkTemplateParallel(b *testing.B) { 68 // templ := template.Must(template.New("test").Parse("Hello, {{.}}!")) 69 // b.RunParallel(func(pb *testing.PB) { 70 // var buf bytes.Buffer 71 // for pb.Next() { 72 // buf.Reset() 73 // templ.Execute(&buf, "World") 74 // } 75 // }) 76 // } 77 // 78 // A detailed specification of the benchmark results format is given 79 // in https://golang.org/design/14313-benchmark-format. 80 // 81 // There are standard tools for working with benchmark results at 82 // https://golang.org/x/perf/cmd. 83 // In particular, https://golang.org/x/perf/cmd/benchstat performs 84 // statistically robust A/B comparisons. 85 // 86 // Examples 87 // 88 // The package also runs and verifies example code. Example functions may 89 // include a concluding line comment that begins with "Output:" and is compared with 90 // the standard output of the function when the tests are run. (The comparison 91 // ignores leading and trailing space.) These are examples of an example: 92 // 93 // func ExampleHello() { 94 // fmt.Println("hello") 95 // // Output: hello 96 // } 97 // 98 // func ExampleSalutations() { 99 // fmt.Println("hello, and") 100 // fmt.Println("goodbye") 101 // // Output: 102 // // hello, and 103 // // goodbye 104 // } 105 // 106 // The comment prefix "Unordered output:" is like "Output:", but matches any 107 // line order: 108 // 109 // func ExamplePerm() { 110 // for _, value := range Perm(5) { 111 // fmt.Println(value) 112 // } 113 // // Unordered output: 4 114 // // 2 115 // // 1 116 // // 3 117 // // 0 118 // } 119 // 120 // Example functions without output comments are compiled but not executed. 121 // 122 // The naming convention to declare examples for the package, a function F, a type T and 123 // method M on type T are: 124 // 125 // func Example() { ... } 126 // func ExampleF() { ... } 127 // func ExampleT() { ... } 128 // func ExampleT_M() { ... } 129 // 130 // Multiple example functions for a package/type/function/method may be provided by 131 // appending a distinct suffix to the name. The suffix must start with a 132 // lower-case letter. 133 // 134 // func Example_suffix() { ... } 135 // func ExampleF_suffix() { ... } 136 // func ExampleT_suffix() { ... } 137 // func ExampleT_M_suffix() { ... } 138 // 139 // The entire test file is presented as the example when it contains a single 140 // example function, at least one other function, type, variable, or constant 141 // declaration, and no test or benchmark functions. 142 // 143 // Fuzzing 144 // 145 // 'go test' and the testing package support fuzzing, a testing technique where 146 // a function is called with randomly generated inputs to find bugs not 147 // anticipated by unit tests. 148 // 149 // Functions of the form 150 // func FuzzXxx(*testing.F) 151 // are considered fuzz tests. 152 // 153 // For example: 154 // 155 // func FuzzHex(f *testing.F) { 156 // for _, seed := range [][]byte{{}, {0}, {9}, {0xa}, {0xf}, {1, 2, 3, 4}} { 157 // f.Add(seed) 158 // } 159 // f.Fuzz(func(t *testing.T, in []byte) { 160 // enc := hex.EncodeToString(in) 161 // out, err := hex.DecodeString(enc) 162 // if err != nil { 163 // t.Fatalf("%v: decode: %v", in, err) 164 // } 165 // if !bytes.Equal(in, out) { 166 // t.Fatalf("%v: not equal after round trip: %v", in, out) 167 // } 168 // }) 169 // } 170 // 171 // A fuzz test maintains a seed corpus, or a set of inputs which are run by 172 // default, and can seed input generation. Seed inputs may be registered by 173 // calling (*F).Add or by storing files in the directory testdata/fuzz/<Name> 174 // (where <Name> is the name of the fuzz test) within the package containing 175 // the fuzz test. Seed inputs are optional, but the fuzzing engine may find 176 // bugs more efficiently when provided with a set of small seed inputs with good 177 // code coverage. These seed inputs can also serve as regression tests for bugs 178 // identified through fuzzing. 179 // 180 // The function passed to (*F).Fuzz within the fuzz test is considered the fuzz 181 // target. A fuzz target must accept a *T parameter, followed by one or more 182 // parameters for random inputs. The types of arguments passed to (*F).Add must 183 // be identical to the types of these parameters. The fuzz target may signal 184 // that it's found a problem the same way tests do: by calling T.Fail (or any 185 // method that calls it like T.Error or T.Fatal) or by panicking. 186 // 187 // When fuzzing is enabled (by setting the -fuzz flag to a regular expression 188 // that matches a specific fuzz test), the fuzz target is called with arguments 189 // generated by repeatedly making random changes to the seed inputs. On 190 // supported platforms, 'go test' compiles the test executable with fuzzing 191 // coverage instrumentation. The fuzzing engine uses that instrumentation to 192 // find and cache inputs that expand coverage, increasing the likelihood of 193 // finding bugs. If the fuzz target fails for a given input, the fuzzing engine 194 // writes the inputs that caused the failure to a file in the directory 195 // testdata/fuzz/<Name> within the package directory. This file later serves as 196 // a seed input. If the file can't be written at that location (for example, 197 // because the directory is read-only), the fuzzing engine writes the file to 198 // the fuzz cache directory within the build cache instead. 199 // 200 // When fuzzing is disabled, the fuzz target is called with the seed inputs 201 // registered with F.Add and seed inputs from testdata/fuzz/<Name>. In this 202 // mode, the fuzz test acts much like a regular test, with subtests started 203 // with F.Fuzz instead of T.Run. 204 // 205 // TODO(#48255): write and link to documentation that will be helpful to users 206 // who are unfamiliar with fuzzing. 207 // 208 // Skipping 209 // 210 // Tests or benchmarks may be skipped at run time with a call to 211 // the Skip method of *T or *B: 212 // 213 // func TestTimeConsuming(t *testing.T) { 214 // if testing.Short() { 215 // t.Skip("skipping test in short mode.") 216 // } 217 // ... 218 // } 219 // 220 // The Skip method of *T can be used in a fuzz target if the input is invalid, 221 // but should not be considered a failing input. For example: 222 // 223 // func FuzzJSONMarshalling(f *testing.F) { 224 // f.Fuzz(func(t *testing.T, b []byte) { 225 // var v interface{} 226 // if err := json.Unmarshal(b, &v); err != nil { 227 // t.Skip() 228 // } 229 // if _, err := json.Marshal(v); err != nil { 230 // t.Error("Marshal: %v", err) 231 // } 232 // }) 233 // } 234 // 235 // Subtests and Sub-benchmarks 236 // 237 // The Run methods of T and B allow defining subtests and sub-benchmarks, 238 // without having to define separate functions for each. This enables uses 239 // like table-driven benchmarks and creating hierarchical tests. 240 // It also provides a way to share common setup and tear-down code: 241 // 242 // func TestFoo(t *testing.T) { 243 // // <setup code> 244 // t.Run("A=1", func(t *testing.T) { ... }) 245 // t.Run("A=2", func(t *testing.T) { ... }) 246 // t.Run("B=1", func(t *testing.T) { ... }) 247 // // <tear-down code> 248 // } 249 // 250 // Each subtest and sub-benchmark has a unique name: the combination of the name 251 // of the top-level test and the sequence of names passed to Run, separated by 252 // slashes, with an optional trailing sequence number for disambiguation. 253 // 254 // The argument to the -run, -bench, and -fuzz command-line flags is an unanchored regular 255 // expression that matches the test's name. For tests with multiple slash-separated 256 // elements, such as subtests, the argument is itself slash-separated, with 257 // expressions matching each name element in turn. Because it is unanchored, an 258 // empty expression matches any string. 259 // For example, using "matching" to mean "whose name contains": 260 // 261 // go test -run '' # Run all tests. 262 // go test -run Foo # Run top-level tests matching "Foo", such as "TestFooBar". 263 // go test -run Foo/A= # For top-level tests matching "Foo", run subtests matching "A=". 264 // go test -run /A=1 # For all top-level tests, run subtests matching "A=1". 265 // go test -fuzz FuzzFoo # Fuzz the target matching "FuzzFoo" 266 // 267 // The -run argument can also be used to run a specific value in the seed 268 // corpus, for debugging. For example: 269 // go test -run=FuzzFoo/9ddb952d9814 270 // 271 // The -fuzz and -run flags can both be set, in order to fuzz a target but 272 // skip the execution of all other tests. 273 // 274 // Subtests can also be used to control parallelism. A parent test will only 275 // complete once all of its subtests complete. In this example, all tests are 276 // run in parallel with each other, and only with each other, regardless of 277 // other top-level tests that may be defined: 278 // 279 // func TestGroupedParallel(t *testing.T) { 280 // for _, tc := range tests { 281 // tc := tc // capture range variable 282 // t.Run(tc.Name, func(t *testing.T) { 283 // t.Parallel() 284 // ... 285 // }) 286 // } 287 // } 288 // 289 // The race detector kills the program if it exceeds 8128 concurrent goroutines, 290 // so use care when running parallel tests with the -race flag set. 291 // 292 // Run does not return until parallel subtests have completed, providing a way 293 // to clean up after a group of parallel tests: 294 // 295 // func TestTeardownParallel(t *testing.T) { 296 // // This Run will not return until the parallel tests finish. 297 // t.Run("group", func(t *testing.T) { 298 // t.Run("Test1", parallelTest1) 299 // t.Run("Test2", parallelTest2) 300 // t.Run("Test3", parallelTest3) 301 // }) 302 // // <tear-down code> 303 // } 304 // 305 // Main 306 // 307 // It is sometimes necessary for a test or benchmark program to do extra setup or teardown 308 // before or after it executes. It is also sometimes necessary to control 309 // which code runs on the main thread. To support these and other cases, 310 // if a test file contains a function: 311 // 312 // func TestMain(m *testing.M) 313 // 314 // then the generated test will call TestMain(m) instead of running the tests or benchmarks 315 // directly. TestMain runs in the main goroutine and can do whatever setup 316 // and teardown is necessary around a call to m.Run. m.Run will return an exit 317 // code that may be passed to os.Exit. If TestMain returns, the test wrapper 318 // will pass the result of m.Run to os.Exit itself. 319 // 320 // When TestMain is called, flag.Parse has not been run. If TestMain depends on 321 // command-line flags, including those of the testing package, it should call 322 // flag.Parse explicitly. Command line flags are always parsed by the time test 323 // or benchmark functions run. 324 // 325 // A simple implementation of TestMain is: 326 // 327 // func TestMain(m *testing.M) { 328 // // call flag.Parse() here if TestMain uses flags 329 // os.Exit(m.Run()) 330 // } 331 // 332 // TestMain is a low-level primitive and should not be necessary for casual 333 // testing needs, where ordinary test functions suffice. 334 package testing 335 336 import ( 337 "bytes" 338 "errors" 339 "flag" 340 "fmt" 341 "internal/race" 342 "io" 343 "math/rand" 344 "os" 345 "reflect" 346 "runtime" 347 "runtime/debug" 348 "runtime/trace" 349 "strconv" 350 "strings" 351 "sync" 352 "sync/atomic" 353 "time" 354 "unicode" 355 "unicode/utf8" 356 ) 357 358 var initRan bool 359 360 // Init registers testing flags. These flags are automatically registered by 361 // the "go test" command before running test functions, so Init is only needed 362 // when calling functions such as Benchmark without using "go test". 363 // 364 // Init has no effect if it was already called. 365 func Init() { 366 if initRan { 367 return 368 } 369 initRan = true 370 // The short flag requests that tests run more quickly, but its functionality 371 // is provided by test writers themselves. The testing package is just its 372 // home. The all.bash installation script sets it to make installation more 373 // efficient, but by default the flag is off so a plain "go test" will do a 374 // full test of the package. 375 short = flag.Bool("test.short", false, "run smaller test suite to save time") 376 377 // The failfast flag requests that test execution stop after the first test failure. 378 failFast = flag.Bool("test.failfast", false, "do not start new tests after the first test failure") 379 380 // The directory in which to create profile files and the like. When run from 381 // "go test", the binary always runs in the source directory for the package; 382 // this flag lets "go test" tell the binary to write the files in the directory where 383 // the "go test" command is run. 384 outputDir = flag.String("test.outputdir", "", "write profiles to `dir`") 385 // Report as tests are run; default is silent for success. 386 chatty = flag.Bool("test.v", false, "verbose: print additional output") 387 count = flag.Uint("test.count", 1, "run tests and benchmarks `n` times") 388 coverProfile = flag.String("test.coverprofile", "", "write a coverage profile to `file`") 389 matchList = flag.String("test.list", "", "list tests, examples, and benchmarks matching `regexp` then exit") 390 match = flag.String("test.run", "", "run only tests and examples matching `regexp`") 391 memProfile = flag.String("test.memprofile", "", "write an allocation profile to `file`") 392 memProfileRate = flag.Int("test.memprofilerate", 0, "set memory allocation profiling `rate` (see runtime.MemProfileRate)") 393 cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to `file`") 394 blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to `file`") 395 blockProfileRate = flag.Int("test.blockprofilerate", 1, "set blocking profile `rate` (see runtime.SetBlockProfileRate)") 396 mutexProfile = flag.String("test.mutexprofile", "", "write a mutex contention profile to the named file after execution") 397 mutexProfileFraction = flag.Int("test.mutexprofilefraction", 1, "if >= 0, calls runtime.SetMutexProfileFraction()") 398 panicOnExit0 = flag.Bool("test.paniconexit0", false, "panic on call to os.Exit(0)") 399 traceFile = flag.String("test.trace", "", "write an execution trace to `file`") 400 timeout = flag.Duration("test.timeout", 0, "panic test binary after duration `d` (default 0, timeout disabled)") 401 cpuListStr = flag.String("test.cpu", "", "comma-separated `list` of cpu counts to run each test with") 402 parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "run at most `n` tests in parallel") 403 testlog = flag.String("test.testlogfile", "", "write test action log to `file` (for use only by cmd/go)") 404 shuffle = flag.String("test.shuffle", "off", "randomize the execution order of tests and benchmarks") 405 406 initBenchmarkFlags() 407 initFuzzFlags() 408 } 409 410 var ( 411 // Flags, registered during Init. 412 short *bool 413 failFast *bool 414 outputDir *string 415 chatty *bool 416 count *uint 417 coverProfile *string 418 matchList *string 419 match *string 420 memProfile *string 421 memProfileRate *int 422 cpuProfile *string 423 blockProfile *string 424 blockProfileRate *int 425 mutexProfile *string 426 mutexProfileFraction *int 427 panicOnExit0 *bool 428 traceFile *string 429 timeout *time.Duration 430 cpuListStr *string 431 parallel *int 432 shuffle *string 433 testlog *string 434 435 haveExamples bool // are there examples? 436 437 cpuList []int 438 testlogFile *os.File 439 440 numFailed uint32 // number of test failures 441 ) 442 443 type chattyPrinter struct { 444 w io.Writer 445 lastNameMu sync.Mutex // guards lastName 446 lastName string // last printed test name in chatty mode 447 } 448 449 func newChattyPrinter(w io.Writer) *chattyPrinter { 450 return &chattyPrinter{w: w} 451 } 452 453 // Updatef prints a message about the status of the named test to w. 454 // 455 // The formatted message must include the test name itself. 456 func (p *chattyPrinter) Updatef(testName, format string, args ...any) { 457 p.lastNameMu.Lock() 458 defer p.lastNameMu.Unlock() 459 460 // Since the message already implies an association with a specific new test, 461 // we don't need to check what the old test name was or log an extra CONT line 462 // for it. (We're updating it anyway, and the current message already includes 463 // the test name.) 464 p.lastName = testName 465 fmt.Fprintf(p.w, format, args...) 466 } 467 468 // Printf prints a message, generated by the named test, that does not 469 // necessarily mention that tests's name itself. 470 func (p *chattyPrinter) Printf(testName, format string, args ...any) { 471 p.lastNameMu.Lock() 472 defer p.lastNameMu.Unlock() 473 474 if p.lastName == "" { 475 p.lastName = testName 476 } else if p.lastName != testName { 477 fmt.Fprintf(p.w, "=== CONT %s\n", testName) 478 p.lastName = testName 479 } 480 481 fmt.Fprintf(p.w, format, args...) 482 } 483 484 // The maximum number of stack frames to go through when skipping helper functions for 485 // the purpose of decorating log messages. 486 const maxStackLen = 50 487 488 // common holds the elements common between T and B and 489 // captures common methods such as Errorf. 490 type common struct { 491 mu sync.RWMutex // guards this group of fields 492 output []byte // Output generated by test or benchmark. 493 w io.Writer // For flushToParent. 494 ran bool // Test or benchmark (or one of its subtests) was executed. 495 failed bool // Test or benchmark has failed. 496 skipped bool // Test or benchmark has been skipped. 497 done bool // Test is finished and all subtests have completed. 498 helperPCs map[uintptr]struct{} // functions to be skipped when writing file/line info 499 helperNames map[string]struct{} // helperPCs converted to function names 500 cleanups []func() // optional functions to be called at the end of the test 501 cleanupName string // Name of the cleanup function. 502 cleanupPc []uintptr // The stack trace at the point where Cleanup was called. 503 finished bool // Test function has completed. 504 inFuzzFn bool // Whether the fuzz target, if this is one, is running. 505 506 chatty *chattyPrinter // A copy of chattyPrinter, if the chatty flag is set. 507 bench bool // Whether the current test is a benchmark. 508 hasSub int32 // Written atomically. 509 raceErrors int // Number of races detected during test. 510 runner string // Function name of tRunner running the test. 511 512 parent *common 513 level int // Nesting depth of test or benchmark. 514 creator []uintptr // If level > 0, the stack trace at the point where the parent called t.Run. 515 name string // Name of test or benchmark. 516 start time.Time // Time test or benchmark started 517 duration time.Duration 518 barrier chan bool // To signal parallel subtests they may start. Nil when T.Parallel is not present (B) or not usable (when fuzzing). 519 signal chan bool // To signal a test is done. 520 sub []*T // Queue of subtests to be run in parallel. 521 522 tempDirMu sync.Mutex 523 tempDir string 524 tempDirErr error 525 tempDirSeq int32 526 } 527 528 // Short reports whether the -test.short flag is set. 529 func Short() bool { 530 if short == nil { 531 panic("testing: Short called before Init") 532 } 533 // Catch code that calls this from TestMain without first calling flag.Parse. 534 if !flag.Parsed() { 535 panic("testing: Short called before Parse") 536 } 537 538 return *short 539 } 540 541 // CoverMode reports what the test coverage mode is set to. The 542 // values are "set", "count", or "atomic". The return value will be 543 // empty if test coverage is not enabled. 544 func CoverMode() string { 545 return cover.Mode 546 } 547 548 // Verbose reports whether the -test.v flag is set. 549 func Verbose() bool { 550 // Same as in Short. 551 if chatty == nil { 552 panic("testing: Verbose called before Init") 553 } 554 if !flag.Parsed() { 555 panic("testing: Verbose called before Parse") 556 } 557 return *chatty 558 } 559 560 func (c *common) checkFuzzFn(name string) { 561 if c.inFuzzFn { 562 panic(fmt.Sprintf("testing: f.%s was called inside the fuzz target, use t.%s instead", name, name)) 563 } 564 } 565 566 // frameSkip searches, starting after skip frames, for the first caller frame 567 // in a function not marked as a helper and returns that frame. 568 // The search stops if it finds a tRunner function that 569 // was the entry point into the test and the test is not a subtest. 570 // This function must be called with c.mu held. 571 func (c *common) frameSkip(skip int) runtime.Frame { 572 // If the search continues into the parent test, we'll have to hold 573 // its mu temporarily. If we then return, we need to unlock it. 574 shouldUnlock := false 575 defer func() { 576 if shouldUnlock { 577 c.mu.Unlock() 578 } 579 }() 580 var pc [maxStackLen]uintptr 581 // Skip two extra frames to account for this function 582 // and runtime.Callers itself. 583 n := runtime.Callers(skip+2, pc[:]) 584 if n == 0 { 585 panic("testing: zero callers found") 586 } 587 frames := runtime.CallersFrames(pc[:n]) 588 var firstFrame, prevFrame, frame runtime.Frame 589 for more := true; more; prevFrame = frame { 590 frame, more = frames.Next() 591 if frame.Function == "runtime.gopanic" { 592 continue 593 } 594 if frame.Function == c.cleanupName { 595 frames = runtime.CallersFrames(c.cleanupPc) 596 continue 597 } 598 if firstFrame.PC == 0 { 599 firstFrame = frame 600 } 601 if frame.Function == c.runner { 602 // We've gone up all the way to the tRunner calling 603 // the test function (so the user must have 604 // called tb.Helper from inside that test function). 605 // If this is a top-level test, only skip up to the test function itself. 606 // If we're in a subtest, continue searching in the parent test, 607 // starting from the point of the call to Run which created this subtest. 608 if c.level > 1 { 609 frames = runtime.CallersFrames(c.creator) 610 parent := c.parent 611 // We're no longer looking at the current c after this point, 612 // so we should unlock its mu, unless it's the original receiver, 613 // in which case our caller doesn't expect us to do that. 614 if shouldUnlock { 615 c.mu.Unlock() 616 } 617 c = parent 618 // Remember to unlock c.mu when we no longer need it, either 619 // because we went up another nesting level, or because we 620 // returned. 621 shouldUnlock = true 622 c.mu.Lock() 623 continue 624 } 625 return prevFrame 626 } 627 // If more helper PCs have been added since we last did the conversion 628 if c.helperNames == nil { 629 c.helperNames = make(map[string]struct{}) 630 for pc := range c.helperPCs { 631 c.helperNames[pcToName(pc)] = struct{}{} 632 } 633 } 634 if _, ok := c.helperNames[frame.Function]; !ok { 635 // Found a frame that wasn't inside a helper function. 636 return frame 637 } 638 } 639 return firstFrame 640 } 641 642 // decorate prefixes the string with the file and line of the call site 643 // and inserts the final newline if needed and indentation spaces for formatting. 644 // This function must be called with c.mu held. 645 func (c *common) decorate(s string, skip int) string { 646 frame := c.frameSkip(skip) 647 file := frame.File 648 line := frame.Line 649 if file != "" { 650 // Truncate file name at last file name separator. 651 if index := strings.LastIndex(file, "/"); index >= 0 { 652 file = file[index+1:] 653 } else if index = strings.LastIndex(file, "\\"); index >= 0 { 654 file = file[index+1:] 655 } 656 } else { 657 file = "???" 658 } 659 if line == 0 { 660 line = 1 661 } 662 buf := new(strings.Builder) 663 // Every line is indented at least 4 spaces. 664 buf.WriteString(" ") 665 fmt.Fprintf(buf, "%s:%d: ", file, line) 666 lines := strings.Split(s, "\n") 667 if l := len(lines); l > 1 && lines[l-1] == "" { 668 lines = lines[:l-1] 669 } 670 for i, line := range lines { 671 if i > 0 { 672 // Second and subsequent lines are indented an additional 4 spaces. 673 buf.WriteString("\n ") 674 } 675 buf.WriteString(line) 676 } 677 buf.WriteByte('\n') 678 return buf.String() 679 } 680 681 // flushToParent writes c.output to the parent after first writing the header 682 // with the given format and arguments. 683 func (c *common) flushToParent(testName, format string, args ...any) { 684 p := c.parent 685 p.mu.Lock() 686 defer p.mu.Unlock() 687 688 c.mu.Lock() 689 defer c.mu.Unlock() 690 691 if len(c.output) > 0 { 692 format += "%s" 693 args = append(args[:len(args):len(args)], c.output) 694 c.output = c.output[:0] // but why? 695 } 696 697 if c.chatty != nil && p.w == c.chatty.w { 698 // We're flushing to the actual output, so track that this output is 699 // associated with a specific test (and, specifically, that the next output 700 // is *not* associated with that test). 701 // 702 // Moreover, if c.output is non-empty it is important that this write be 703 // atomic with respect to the output of other tests, so that we don't end up 704 // with confusing '=== CONT' lines in the middle of our '--- PASS' block. 705 // Neither humans nor cmd/test2json can parse those easily. 706 // (See https://golang.org/issue/40771.) 707 c.chatty.Updatef(testName, format, args...) 708 } else { 709 // We're flushing to the output buffer of the parent test, which will 710 // itself follow a test-name header when it is finally flushed to stdout. 711 fmt.Fprintf(p.w, format, args...) 712 } 713 } 714 715 type indenter struct { 716 c *common 717 } 718 719 func (w indenter) Write(b []byte) (n int, err error) { 720 n = len(b) 721 for len(b) > 0 { 722 end := bytes.IndexByte(b, '\n') 723 if end == -1 { 724 end = len(b) 725 } else { 726 end++ 727 } 728 // An indent of 4 spaces will neatly align the dashes with the status 729 // indicator of the parent. 730 const indent = " " 731 w.c.output = append(w.c.output, indent...) 732 w.c.output = append(w.c.output, b[:end]...) 733 b = b[end:] 734 } 735 return 736 } 737 738 // fmtDuration returns a string representing d in the form "87.00s". 739 func fmtDuration(d time.Duration) string { 740 return fmt.Sprintf("%.2fs", d.Seconds()) 741 } 742 743 // TB is the interface common to T, B, and F. 744 type TB interface { 745 Cleanup(func()) 746 Error(args ...any) 747 Errorf(format string, args ...any) 748 Fail() 749 FailNow() 750 Failed() bool 751 Fatal(args ...any) 752 Fatalf(format string, args ...any) 753 Helper() 754 Log(args ...any) 755 Logf(format string, args ...any) 756 Name() string 757 Setenv(key, value string) 758 Skip(args ...any) 759 SkipNow() 760 Skipf(format string, args ...any) 761 Skipped() bool 762 TempDir() string 763 764 // A private method to prevent users implementing the 765 // interface and so future additions to it will not 766 // violate Go 1 compatibility. 767 private() 768 } 769 770 var _ TB = (*T)(nil) 771 var _ TB = (*B)(nil) 772 773 // T is a type passed to Test functions to manage test state and support formatted test logs. 774 // 775 // A test ends when its Test function returns or calls any of the methods 776 // FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as 777 // the Parallel method, must be called only from the goroutine running the 778 // Test function. 779 // 780 // The other reporting methods, such as the variations of Log and Error, 781 // may be called simultaneously from multiple goroutines. 782 type T struct { 783 common 784 isParallel bool 785 isEnvSet bool 786 context *testContext // For running tests and subtests. 787 } 788 789 func (c *common) private() {} 790 791 // Name returns the name of the running (sub-) test or benchmark. 792 // 793 // The name will include the name of the test along with the names of 794 // any nested sub-tests. If two sibling sub-tests have the same name, 795 // Name will append a suffix to guarantee the returned name is unique. 796 func (c *common) Name() string { 797 return c.name 798 } 799 800 func (c *common) setRan() { 801 if c.parent != nil { 802 c.parent.setRan() 803 } 804 c.mu.Lock() 805 defer c.mu.Unlock() 806 c.ran = true 807 } 808 809 // Fail marks the function as having failed but continues execution. 810 func (c *common) Fail() { 811 if c.parent != nil { 812 c.parent.Fail() 813 } 814 c.mu.Lock() 815 defer c.mu.Unlock() 816 // c.done needs to be locked to synchronize checks to c.done in parent tests. 817 if c.done { 818 panic("Fail in goroutine after " + c.name + " has completed") 819 } 820 c.failed = true 821 } 822 823 // Failed reports whether the function has failed. 824 func (c *common) Failed() bool { 825 c.mu.RLock() 826 failed := c.failed 827 c.mu.RUnlock() 828 return failed || c.raceErrors+race.Errors() > 0 829 } 830 831 // FailNow marks the function as having failed and stops its execution 832 // by calling runtime.Goexit (which then runs all deferred calls in the 833 // current goroutine). 834 // Execution will continue at the next test or benchmark. 835 // FailNow must be called from the goroutine running the 836 // test or benchmark function, not from other goroutines 837 // created during the test. Calling FailNow does not stop 838 // those other goroutines. 839 func (c *common) FailNow() { 840 c.checkFuzzFn("FailNow") 841 c.Fail() 842 843 // Calling runtime.Goexit will exit the goroutine, which 844 // will run the deferred functions in this goroutine, 845 // which will eventually run the deferred lines in tRunner, 846 // which will signal to the test loop that this test is done. 847 // 848 // A previous version of this code said: 849 // 850 // c.duration = ... 851 // c.signal <- c.self 852 // runtime.Goexit() 853 // 854 // This previous version duplicated code (those lines are in 855 // tRunner no matter what), but worse the goroutine teardown 856 // implicit in runtime.Goexit was not guaranteed to complete 857 // before the test exited. If a test deferred an important cleanup 858 // function (like removing temporary files), there was no guarantee 859 // it would run on a test failure. Because we send on c.signal during 860 // a top-of-stack deferred function now, we know that the send 861 // only happens after any other stacked defers have completed. 862 c.mu.Lock() 863 c.finished = true 864 c.mu.Unlock() 865 runtime.Goexit() 866 } 867 868 // log generates the output. It's always at the same stack depth. 869 func (c *common) log(s string) { 870 c.logDepth(s, 3) // logDepth + log + public function 871 } 872 873 // logDepth generates the output at an arbitrary stack depth. 874 func (c *common) logDepth(s string, depth int) { 875 c.mu.Lock() 876 defer c.mu.Unlock() 877 if c.done { 878 // This test has already finished. Try and log this message 879 // with our parent. If we don't have a parent, panic. 880 for parent := c.parent; parent != nil; parent = parent.parent { 881 parent.mu.Lock() 882 defer parent.mu.Unlock() 883 if !parent.done { 884 parent.output = append(parent.output, parent.decorate(s, depth+1)...) 885 return 886 } 887 } 888 panic("Log in goroutine after " + c.name + " has completed: " + s) 889 } else { 890 if c.chatty != nil { 891 if c.bench { 892 // Benchmarks don't print === CONT, so we should skip the test 893 // printer and just print straight to stdout. 894 fmt.Print(c.decorate(s, depth+1)) 895 } else { 896 c.chatty.Printf(c.name, "%s", c.decorate(s, depth+1)) 897 } 898 899 return 900 } 901 c.output = append(c.output, c.decorate(s, depth+1)...) 902 } 903 } 904 905 // Log formats its arguments using default formatting, analogous to Println, 906 // and records the text in the error log. For tests, the text will be printed only if 907 // the test fails or the -test.v flag is set. For benchmarks, the text is always 908 // printed to avoid having performance depend on the value of the -test.v flag. 909 func (c *common) Log(args ...any) { 910 c.checkFuzzFn("Log") 911 c.log(fmt.Sprintln(args...)) 912 } 913 914 // Logf formats its arguments according to the format, analogous to Printf, and 915 // records the text in the error log. A final newline is added if not provided. For 916 // tests, the text will be printed only if the test fails or the -test.v flag is 917 // set. For benchmarks, the text is always printed to avoid having performance 918 // depend on the value of the -test.v flag. 919 func (c *common) Logf(format string, args ...any) { 920 c.checkFuzzFn("Logf") 921 c.log(fmt.Sprintf(format, args...)) 922 } 923 924 // Error is equivalent to Log followed by Fail. 925 func (c *common) Error(args ...any) { 926 c.checkFuzzFn("Error") 927 c.log(fmt.Sprintln(args...)) 928 c.Fail() 929 } 930 931 // Errorf is equivalent to Logf followed by Fail. 932 func (c *common) Errorf(format string, args ...any) { 933 c.checkFuzzFn("Errorf") 934 c.log(fmt.Sprintf(format, args...)) 935 c.Fail() 936 } 937 938 // Fatal is equivalent to Log followed by FailNow. 939 func (c *common) Fatal(args ...any) { 940 c.checkFuzzFn("Fatal") 941 c.log(fmt.Sprintln(args...)) 942 c.FailNow() 943 } 944 945 // Fatalf is equivalent to Logf followed by FailNow. 946 func (c *common) Fatalf(format string, args ...any) { 947 c.checkFuzzFn("Fatalf") 948 c.log(fmt.Sprintf(format, args...)) 949 c.FailNow() 950 } 951 952 // Skip is equivalent to Log followed by SkipNow. 953 func (c *common) Skip(args ...any) { 954 c.checkFuzzFn("Skip") 955 c.log(fmt.Sprintln(args...)) 956 c.SkipNow() 957 } 958 959 // Skipf is equivalent to Logf followed by SkipNow. 960 func (c *common) Skipf(format string, args ...any) { 961 c.checkFuzzFn("Skipf") 962 c.log(fmt.Sprintf(format, args...)) 963 c.SkipNow() 964 } 965 966 // SkipNow marks the test as having been skipped and stops its execution 967 // by calling runtime.Goexit. 968 // If a test fails (see Error, Errorf, Fail) and is then skipped, 969 // it is still considered to have failed. 970 // Execution will continue at the next test or benchmark. See also FailNow. 971 // SkipNow must be called from the goroutine running the test, not from 972 // other goroutines created during the test. Calling SkipNow does not stop 973 // those other goroutines. 974 func (c *common) SkipNow() { 975 c.checkFuzzFn("SkipNow") 976 c.mu.Lock() 977 c.skipped = true 978 c.finished = true 979 c.mu.Unlock() 980 runtime.Goexit() 981 } 982 983 // Skipped reports whether the test was skipped. 984 func (c *common) Skipped() bool { 985 c.mu.RLock() 986 defer c.mu.RUnlock() 987 return c.skipped 988 } 989 990 // Helper marks the calling function as a test helper function. 991 // When printing file and line information, that function will be skipped. 992 // Helper may be called simultaneously from multiple goroutines. 993 func (c *common) Helper() { 994 c.mu.Lock() 995 defer c.mu.Unlock() 996 if c.helperPCs == nil { 997 c.helperPCs = make(map[uintptr]struct{}) 998 } 999 // repeating code from callerName here to save walking a stack frame 1000 var pc [1]uintptr 1001 n := runtime.Callers(2, pc[:]) // skip runtime.Callers + Helper 1002 if n == 0 { 1003 panic("testing: zero callers found") 1004 } 1005 if _, found := c.helperPCs[pc[0]]; !found { 1006 c.helperPCs[pc[0]] = struct{}{} 1007 c.helperNames = nil // map will be recreated next time it is needed 1008 } 1009 } 1010 1011 // Cleanup registers a function to be called when the test (or subtest) and all its 1012 // subtests complete. Cleanup functions will be called in last added, 1013 // first called order. 1014 func (c *common) Cleanup(f func()) { 1015 c.checkFuzzFn("Cleanup") 1016 var pc [maxStackLen]uintptr 1017 // Skip two extra frames to account for this function and runtime.Callers itself. 1018 n := runtime.Callers(2, pc[:]) 1019 cleanupPc := pc[:n] 1020 1021 fn := func() { 1022 defer func() { 1023 c.mu.Lock() 1024 defer c.mu.Unlock() 1025 c.cleanupName = "" 1026 c.cleanupPc = nil 1027 }() 1028 1029 name := callerName(0) 1030 c.mu.Lock() 1031 c.cleanupName = name 1032 c.cleanupPc = cleanupPc 1033 c.mu.Unlock() 1034 1035 f() 1036 } 1037 1038 c.mu.Lock() 1039 defer c.mu.Unlock() 1040 c.cleanups = append(c.cleanups, fn) 1041 } 1042 1043 // TempDir returns a temporary directory for the test to use. 1044 // The directory is automatically removed by Cleanup when the test and 1045 // all its subtests complete. 1046 // Each subsequent call to t.TempDir returns a unique directory; 1047 // if the directory creation fails, TempDir terminates the test by calling Fatal. 1048 func (c *common) TempDir() string { 1049 c.checkFuzzFn("TempDir") 1050 // Use a single parent directory for all the temporary directories 1051 // created by a test, each numbered sequentially. 1052 c.tempDirMu.Lock() 1053 var nonExistent bool 1054 if c.tempDir == "" { // Usually the case with js/wasm 1055 nonExistent = true 1056 } else { 1057 _, err := os.Stat(c.tempDir) 1058 nonExistent = os.IsNotExist(err) 1059 if err != nil && !nonExistent { 1060 c.Fatalf("TempDir: %v", err) 1061 } 1062 } 1063 1064 if nonExistent { 1065 c.Helper() 1066 1067 // Drop unusual characters (such as path separators or 1068 // characters interacting with globs) from the directory name to 1069 // avoid surprising os.MkdirTemp behavior. 1070 mapper := func(r rune) rune { 1071 if r < utf8.RuneSelf { 1072 const allowed = "!#$%&()+,-.=@^_{}~ " 1073 if '0' <= r && r <= '9' || 1074 'a' <= r && r <= 'z' || 1075 'A' <= r && r <= 'Z' { 1076 return r 1077 } 1078 if strings.ContainsRune(allowed, r) { 1079 return r 1080 } 1081 } else if unicode.IsLetter(r) || unicode.IsNumber(r) { 1082 return r 1083 } 1084 return -1 1085 } 1086 pattern := strings.Map(mapper, c.Name()) 1087 c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern) 1088 if c.tempDirErr == nil { 1089 c.Cleanup(func() { 1090 if err := removeAll(c.tempDir); err != nil { 1091 c.Errorf("TempDir RemoveAll cleanup: %v", err) 1092 } 1093 }) 1094 } 1095 } 1096 c.tempDirMu.Unlock() 1097 1098 if c.tempDirErr != nil { 1099 c.Fatalf("TempDir: %v", c.tempDirErr) 1100 } 1101 seq := atomic.AddInt32(&c.tempDirSeq, 1) 1102 dir := fmt.Sprintf("%s%c%03d", c.tempDir, os.PathSeparator, seq) 1103 if err := os.Mkdir(dir, 0777); err != nil { 1104 c.Fatalf("TempDir: %v", err) 1105 } 1106 return dir 1107 } 1108 1109 // removeAll is like os.RemoveAll, but retries Windows "Access is denied." 1110 // errors up to an arbitrary timeout. 1111 // 1112 // Those errors have been known to occur spuriously on at least the 1113 // windows-amd64-2012 builder (https://go.dev/issue/50051), and can only occur 1114 // legitimately if the test leaves behind a temp file that either is still open 1115 // or the test otherwise lacks permission to delete. In the case of legitimate 1116 // failures, a failing test may take a bit longer to fail, but once the test is 1117 // fixed the extra latency will go away. 1118 func removeAll(path string) error { 1119 const arbitraryTimeout = 2 * time.Second 1120 var ( 1121 start time.Time 1122 nextSleep = 1 * time.Millisecond 1123 ) 1124 for { 1125 err := os.RemoveAll(path) 1126 if !isWindowsAccessDenied(err) { 1127 return err 1128 } 1129 if start.IsZero() { 1130 start = time.Now() 1131 } else if d := time.Since(start) + nextSleep; d >= arbitraryTimeout { 1132 return err 1133 } 1134 time.Sleep(nextSleep) 1135 nextSleep += time.Duration(rand.Int63n(int64(nextSleep))) 1136 } 1137 } 1138 1139 // Setenv calls os.Setenv(key, value) and uses Cleanup to 1140 // restore the environment variable to its original value 1141 // after the test. 1142 // 1143 // This cannot be used in parallel tests. 1144 func (c *common) Setenv(key, value string) { 1145 c.checkFuzzFn("Setenv") 1146 prevValue, ok := os.LookupEnv(key) 1147 1148 if err := os.Setenv(key, value); err != nil { 1149 c.Fatalf("cannot set environment variable: %v", err) 1150 } 1151 1152 if ok { 1153 c.Cleanup(func() { 1154 os.Setenv(key, prevValue) 1155 }) 1156 } else { 1157 c.Cleanup(func() { 1158 os.Unsetenv(key) 1159 }) 1160 } 1161 } 1162 1163 // panicHanding is an argument to runCleanup. 1164 type panicHandling int 1165 1166 const ( 1167 normalPanic panicHandling = iota 1168 recoverAndReturnPanic 1169 ) 1170 1171 // runCleanup is called at the end of the test. 1172 // If catchPanic is true, this will catch panics, and return the recovered 1173 // value if any. 1174 func (c *common) runCleanup(ph panicHandling) (panicVal any) { 1175 if ph == recoverAndReturnPanic { 1176 defer func() { 1177 panicVal = recover() 1178 }() 1179 } 1180 1181 // Make sure that if a cleanup function panics, 1182 // we still run the remaining cleanup functions. 1183 defer func() { 1184 c.mu.Lock() 1185 recur := len(c.cleanups) > 0 1186 c.mu.Unlock() 1187 if recur { 1188 c.runCleanup(normalPanic) 1189 } 1190 }() 1191 1192 for { 1193 var cleanup func() 1194 c.mu.Lock() 1195 if len(c.cleanups) > 0 { 1196 last := len(c.cleanups) - 1 1197 cleanup = c.cleanups[last] 1198 c.cleanups = c.cleanups[:last] 1199 } 1200 c.mu.Unlock() 1201 if cleanup == nil { 1202 return nil 1203 } 1204 cleanup() 1205 } 1206 } 1207 1208 // callerName gives the function name (qualified with a package path) 1209 // for the caller after skip frames (where 0 means the current function). 1210 func callerName(skip int) string { 1211 var pc [1]uintptr 1212 n := runtime.Callers(skip+2, pc[:]) // skip + runtime.Callers + callerName 1213 if n == 0 { 1214 panic("testing: zero callers found") 1215 } 1216 return pcToName(pc[0]) 1217 } 1218 1219 func pcToName(pc uintptr) string { 1220 pcs := []uintptr{pc} 1221 frames := runtime.CallersFrames(pcs) 1222 frame, _ := frames.Next() 1223 return frame.Function 1224 } 1225 1226 // Parallel signals that this test is to be run in parallel with (and only with) 1227 // other parallel tests. When a test is run multiple times due to use of 1228 // -test.count or -test.cpu, multiple instances of a single test never run in 1229 // parallel with each other. 1230 func (t *T) Parallel() { 1231 if t.isParallel { 1232 panic("testing: t.Parallel called multiple times") 1233 } 1234 if t.isEnvSet { 1235 panic("testing: t.Parallel called after t.Setenv; cannot set environment variables in parallel tests") 1236 } 1237 t.isParallel = true 1238 if t.parent.barrier == nil { 1239 // T.Parallel has no effect when fuzzing. 1240 // Multiple processes may run in parallel, but only one input can run at a 1241 // time per process so we can attribute crashes to specific inputs. 1242 return 1243 } 1244 1245 // We don't want to include the time we spend waiting for serial tests 1246 // in the test duration. Record the elapsed time thus far and reset the 1247 // timer afterwards. 1248 t.duration += time.Since(t.start) 1249 1250 // Add to the list of tests to be released by the parent. 1251 t.parent.sub = append(t.parent.sub, t) 1252 t.raceErrors += race.Errors() 1253 1254 if t.chatty != nil { 1255 // Unfortunately, even though PAUSE indicates that the named test is *no 1256 // longer* running, cmd/test2json interprets it as changing the active test 1257 // for the purpose of log parsing. We could fix cmd/test2json, but that 1258 // won't fix existing deployments of third-party tools that already shell 1259 // out to older builds of cmd/test2json — so merely fixing cmd/test2json 1260 // isn't enough for now. 1261 t.chatty.Updatef(t.name, "=== PAUSE %s\n", t.name) 1262 } 1263 1264 t.signal <- true // Release calling test. 1265 <-t.parent.barrier // Wait for the parent test to complete. 1266 t.context.waitParallel() 1267 1268 if t.chatty != nil { 1269 t.chatty.Updatef(t.name, "=== CONT %s\n", t.name) 1270 } 1271 1272 t.start = time.Now() 1273 t.raceErrors += -race.Errors() 1274 } 1275 1276 // Setenv calls os.Setenv(key, value) and uses Cleanup to 1277 // restore the environment variable to its original value 1278 // after the test. 1279 // 1280 // This cannot be used in parallel tests. 1281 func (t *T) Setenv(key, value string) { 1282 if t.isParallel { 1283 panic("testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests") 1284 } 1285 1286 t.isEnvSet = true 1287 1288 t.common.Setenv(key, value) 1289 } 1290 1291 // InternalTest is an internal type but exported because it is cross-package; 1292 // it is part of the implementation of the "go test" command. 1293 type InternalTest struct { 1294 Name string 1295 F func(*T) 1296 } 1297 1298 var errNilPanicOrGoexit = errors.New("test executed panic(nil) or runtime.Goexit") 1299 1300 func tRunner(t *T, fn func(t *T)) { 1301 t.runner = callerName(0) 1302 1303 // When this goroutine is done, either because fn(t) 1304 // returned normally or because a test failure triggered 1305 // a call to runtime.Goexit, record the duration and send 1306 // a signal saying that the test is done. 1307 defer func() { 1308 if t.Failed() { 1309 atomic.AddUint32(&numFailed, 1) 1310 } 1311 1312 if t.raceErrors+race.Errors() > 0 { 1313 t.Errorf("race detected during execution of test") 1314 } 1315 1316 // Check if the test panicked or Goexited inappropriately. 1317 // 1318 // If this happens in a normal test, print output but continue panicking. 1319 // tRunner is called in its own goroutine, so this terminates the process. 1320 // 1321 // If this happens while fuzzing, recover from the panic and treat it like a 1322 // normal failure. It's important that the process keeps running in order to 1323 // find short inputs that cause panics. 1324 err := recover() 1325 signal := true 1326 1327 t.mu.RLock() 1328 finished := t.finished 1329 t.mu.RUnlock() 1330 if !finished && err == nil { 1331 err = errNilPanicOrGoexit 1332 for p := t.parent; p != nil; p = p.parent { 1333 p.mu.RLock() 1334 finished = p.finished 1335 p.mu.RUnlock() 1336 if finished { 1337 t.Errorf("%v: subtest may have called FailNow on a parent test", err) 1338 err = nil 1339 signal = false 1340 break 1341 } 1342 } 1343 } 1344 1345 if err != nil && t.context.isFuzzing { 1346 prefix := "panic: " 1347 if err == errNilPanicOrGoexit { 1348 prefix = "" 1349 } 1350 t.Errorf("%s%s\n%s\n", prefix, err, string(debug.Stack())) 1351 t.mu.Lock() 1352 t.finished = true 1353 t.mu.Unlock() 1354 err = nil 1355 } 1356 1357 // Use a deferred call to ensure that we report that the test is 1358 // complete even if a cleanup function calls t.FailNow. See issue 41355. 1359 didPanic := false 1360 defer func() { 1361 if didPanic { 1362 return 1363 } 1364 if err != nil { 1365 panic(err) 1366 } 1367 // Only report that the test is complete if it doesn't panic, 1368 // as otherwise the test binary can exit before the panic is 1369 // reported to the user. See issue 41479. 1370 t.signal <- signal 1371 }() 1372 1373 doPanic := func(err any) { 1374 t.Fail() 1375 if r := t.runCleanup(recoverAndReturnPanic); r != nil { 1376 t.Logf("cleanup panicked with %v", r) 1377 } 1378 // Flush the output log up to the root before dying. 1379 for root := &t.common; root.parent != nil; root = root.parent { 1380 root.mu.Lock() 1381 root.duration += time.Since(root.start) 1382 d := root.duration 1383 root.mu.Unlock() 1384 root.flushToParent(root.name, "--- FAIL: %s (%s)\n", root.name, fmtDuration(d)) 1385 if r := root.parent.runCleanup(recoverAndReturnPanic); r != nil { 1386 fmt.Fprintf(root.parent.w, "cleanup panicked with %v", r) 1387 } 1388 } 1389 didPanic = true 1390 panic(err) 1391 } 1392 if err != nil { 1393 doPanic(err) 1394 } 1395 1396 t.duration += time.Since(t.start) 1397 1398 if len(t.sub) > 0 { 1399 // Run parallel subtests. 1400 // Decrease the running count for this test. 1401 t.context.release() 1402 // Release the parallel subtests. 1403 close(t.barrier) 1404 // Wait for subtests to complete. 1405 for _, sub := range t.sub { 1406 <-sub.signal 1407 } 1408 cleanupStart := time.Now() 1409 err := t.runCleanup(recoverAndReturnPanic) 1410 t.duration += time.Since(cleanupStart) 1411 if err != nil { 1412 doPanic(err) 1413 } 1414 if !t.isParallel { 1415 // Reacquire the count for sequential tests. See comment in Run. 1416 t.context.waitParallel() 1417 } 1418 } else if t.isParallel { 1419 // Only release the count for this test if it was run as a parallel 1420 // test. See comment in Run method. 1421 t.context.release() 1422 } 1423 t.report() // Report after all subtests have finished. 1424 1425 // Do not lock t.done to allow race detector to detect race in case 1426 // the user does not appropriately synchronize a goroutine. 1427 t.done = true 1428 if t.parent != nil && atomic.LoadInt32(&t.hasSub) == 0 { 1429 t.setRan() 1430 } 1431 }() 1432 defer func() { 1433 if len(t.sub) == 0 { 1434 t.runCleanup(normalPanic) 1435 } 1436 }() 1437 1438 t.start = time.Now() 1439 t.raceErrors = -race.Errors() 1440 fn(t) 1441 1442 // code beyond here will not be executed when FailNow is invoked 1443 t.mu.Lock() 1444 t.finished = true 1445 t.mu.Unlock() 1446 } 1447 1448 // Run runs f as a subtest of t called name. It runs f in a separate goroutine 1449 // and blocks until f returns or calls t.Parallel to become a parallel test. 1450 // Run reports whether f succeeded (or at least did not fail before calling t.Parallel). 1451 // 1452 // Run may be called simultaneously from multiple goroutines, but all such calls 1453 // must return before the outer test function for t returns. 1454 func (t *T) Run(name string, f func(t *T)) bool { 1455 atomic.StoreInt32(&t.hasSub, 1) 1456 testName, ok, _ := t.context.match.fullName(&t.common, name) 1457 if !ok || shouldFailFast() { 1458 return true 1459 } 1460 // Record the stack trace at the point of this call so that if the subtest 1461 // function - which runs in a separate stack - is marked as a helper, we can 1462 // continue walking the stack into the parent test. 1463 var pc [maxStackLen]uintptr 1464 n := runtime.Callers(2, pc[:]) 1465 t = &T{ 1466 common: common{ 1467 barrier: make(chan bool), 1468 signal: make(chan bool, 1), 1469 name: testName, 1470 parent: &t.common, 1471 level: t.level + 1, 1472 creator: pc[:n], 1473 chatty: t.chatty, 1474 }, 1475 context: t.context, 1476 } 1477 t.w = indenter{&t.common} 1478 1479 if t.chatty != nil { 1480 t.chatty.Updatef(t.name, "=== RUN %s\n", t.name) 1481 } 1482 // Instead of reducing the running count of this test before calling the 1483 // tRunner and increasing it afterwards, we rely on tRunner keeping the 1484 // count correct. This ensures that a sequence of sequential tests runs 1485 // without being preempted, even when their parent is a parallel test. This 1486 // may especially reduce surprises if *parallel == 1. 1487 go tRunner(t, f) 1488 if !<-t.signal { 1489 // At this point, it is likely that FailNow was called on one of the 1490 // parent tests by one of the subtests. Continue aborting up the chain. 1491 runtime.Goexit() 1492 } 1493 return !t.failed 1494 } 1495 1496 // Deadline reports the time at which the test binary will have 1497 // exceeded the timeout specified by the -timeout flag. 1498 // 1499 // The ok result is false if the -timeout flag indicates “no timeout” (0). 1500 func (t *T) Deadline() (deadline time.Time, ok bool) { 1501 deadline = t.context.deadline 1502 return deadline, !deadline.IsZero() 1503 } 1504 1505 // testContext holds all fields that are common to all tests. This includes 1506 // synchronization primitives to run at most *parallel tests. 1507 type testContext struct { 1508 match *matcher 1509 deadline time.Time 1510 1511 // isFuzzing is true in the context used when generating random inputs 1512 // for fuzz targets. isFuzzing is false when running normal tests and 1513 // when running fuzz tests as unit tests (without -fuzz or when -fuzz 1514 // does not match). 1515 isFuzzing bool 1516 1517 mu sync.Mutex 1518 1519 // Channel used to signal tests that are ready to be run in parallel. 1520 startParallel chan bool 1521 1522 // running is the number of tests currently running in parallel. 1523 // This does not include tests that are waiting for subtests to complete. 1524 running int 1525 1526 // numWaiting is the number tests waiting to be run in parallel. 1527 numWaiting int 1528 1529 // maxParallel is a copy of the parallel flag. 1530 maxParallel int 1531 } 1532 1533 func newTestContext(maxParallel int, m *matcher) *testContext { 1534 return &testContext{ 1535 match: m, 1536 startParallel: make(chan bool), 1537 maxParallel: maxParallel, 1538 running: 1, // Set the count to 1 for the main (sequential) test. 1539 } 1540 } 1541 1542 func (c *testContext) waitParallel() { 1543 c.mu.Lock() 1544 if c.running < c.maxParallel { 1545 c.running++ 1546 c.mu.Unlock() 1547 return 1548 } 1549 c.numWaiting++ 1550 c.mu.Unlock() 1551 <-c.startParallel 1552 } 1553 1554 func (c *testContext) release() { 1555 c.mu.Lock() 1556 if c.numWaiting == 0 { 1557 c.running-- 1558 c.mu.Unlock() 1559 return 1560 } 1561 c.numWaiting-- 1562 c.mu.Unlock() 1563 c.startParallel <- true // Pick a waiting test to be run. 1564 } 1565 1566 // No one should be using func Main anymore. 1567 // See the doc comment on func Main and use MainStart instead. 1568 var errMain = errors.New("testing: unexpected use of func Main") 1569 1570 type matchStringOnly func(pat, str string) (bool, error) 1571 1572 func (f matchStringOnly) MatchString(pat, str string) (bool, error) { return f(pat, str) } 1573 func (f matchStringOnly) StartCPUProfile(w io.Writer) error { return errMain } 1574 func (f matchStringOnly) StopCPUProfile() {} 1575 func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain } 1576 func (f matchStringOnly) ImportPath() string { return "" } 1577 func (f matchStringOnly) StartTestLog(io.Writer) {} 1578 func (f matchStringOnly) StopTestLog() error { return errMain } 1579 func (f matchStringOnly) SetPanicOnExit0(bool) {} 1580 func (f matchStringOnly) CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error { 1581 return errMain 1582 } 1583 func (f matchStringOnly) RunFuzzWorker(func(corpusEntry) error) error { return errMain } 1584 func (f matchStringOnly) ReadCorpus(string, []reflect.Type) ([]corpusEntry, error) { 1585 return nil, errMain 1586 } 1587 func (f matchStringOnly) CheckCorpus([]any, []reflect.Type) error { return nil } 1588 func (f matchStringOnly) ResetCoverage() {} 1589 func (f matchStringOnly) SnapshotCoverage() {} 1590 1591 // Main is an internal function, part of the implementation of the "go test" command. 1592 // It was exported because it is cross-package and predates "internal" packages. 1593 // It is no longer used by "go test" but preserved, as much as possible, for other 1594 // systems that simulate "go test" using Main, but Main sometimes cannot be updated as 1595 // new functionality is added to the testing package. 1596 // Systems simulating "go test" should be updated to use MainStart. 1597 func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) { 1598 os.Exit(MainStart(matchStringOnly(matchString), tests, benchmarks, nil, examples).Run()) 1599 } 1600 1601 // M is a type passed to a TestMain function to run the actual tests. 1602 type M struct { 1603 deps testDeps 1604 tests []InternalTest 1605 benchmarks []InternalBenchmark 1606 fuzzTargets []InternalFuzzTarget 1607 examples []InternalExample 1608 1609 timer *time.Timer 1610 afterOnce sync.Once 1611 1612 numRun int 1613 1614 // value to pass to os.Exit, the outer test func main 1615 // harness calls os.Exit with this code. See #34129. 1616 exitCode int 1617 } 1618 1619 // testDeps is an internal interface of functionality that is 1620 // passed into this package by a test's generated main package. 1621 // The canonical implementation of this interface is 1622 // testing/internal/testdeps's TestDeps. 1623 type testDeps interface { 1624 ImportPath() string 1625 MatchString(pat, str string) (bool, error) 1626 SetPanicOnExit0(bool) 1627 StartCPUProfile(io.Writer) error 1628 StopCPUProfile() 1629 StartTestLog(io.Writer) 1630 StopTestLog() error 1631 WriteProfileTo(string, io.Writer, int) error 1632 CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error 1633 RunFuzzWorker(func(corpusEntry) error) error 1634 ReadCorpus(string, []reflect.Type) ([]corpusEntry, error) 1635 CheckCorpus([]any, []reflect.Type) error 1636 ResetCoverage() 1637 SnapshotCoverage() 1638 } 1639 1640 // MainStart is meant for use by tests generated by 'go test'. 1641 // It is not meant to be called directly and is not subject to the Go 1 compatibility document. 1642 // It may change signature from release to release. 1643 func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M { 1644 Init() 1645 return &M{ 1646 deps: deps, 1647 tests: tests, 1648 benchmarks: benchmarks, 1649 fuzzTargets: fuzzTargets, 1650 examples: examples, 1651 } 1652 } 1653 1654 // Run runs the tests. It returns an exit code to pass to os.Exit. 1655 func (m *M) Run() (code int) { 1656 defer func() { 1657 code = m.exitCode 1658 }() 1659 1660 // Count the number of calls to m.Run. 1661 // We only ever expected 1, but we didn't enforce that, 1662 // and now there are tests in the wild that call m.Run multiple times. 1663 // Sigh. golang.org/issue/23129. 1664 m.numRun++ 1665 1666 // TestMain may have already called flag.Parse. 1667 if !flag.Parsed() { 1668 flag.Parse() 1669 } 1670 1671 if *parallel < 1 { 1672 fmt.Fprintln(os.Stderr, "testing: -parallel can only be given a positive integer") 1673 flag.Usage() 1674 m.exitCode = 2 1675 return 1676 } 1677 if *matchFuzz != "" && *fuzzCacheDir == "" { 1678 fmt.Fprintln(os.Stderr, "testing: -test.fuzzcachedir must be set if -test.fuzz is set") 1679 flag.Usage() 1680 m.exitCode = 2 1681 return 1682 } 1683 1684 if len(*matchList) != 0 { 1685 listTests(m.deps.MatchString, m.tests, m.benchmarks, m.fuzzTargets, m.examples) 1686 m.exitCode = 0 1687 return 1688 } 1689 1690 if *shuffle != "off" { 1691 var n int64 1692 var err error 1693 if *shuffle == "on" { 1694 n = time.Now().UnixNano() 1695 } else { 1696 n, err = strconv.ParseInt(*shuffle, 10, 64) 1697 if err != nil { 1698 fmt.Fprintln(os.Stderr, `testing: -shuffle should be "off", "on", or a valid integer:`, err) 1699 m.exitCode = 2 1700 return 1701 } 1702 } 1703 fmt.Println("-test.shuffle", n) 1704 rng := rand.New(rand.NewSource(n)) 1705 rng.Shuffle(len(m.tests), func(i, j int) { m.tests[i], m.tests[j] = m.tests[j], m.tests[i] }) 1706 rng.Shuffle(len(m.benchmarks), func(i, j int) { m.benchmarks[i], m.benchmarks[j] = m.benchmarks[j], m.benchmarks[i] }) 1707 } 1708 1709 parseCpuList() 1710 1711 m.before() 1712 defer m.after() 1713 1714 // Run tests, examples, and benchmarks unless this is a fuzz worker process. 1715 // Workers start after this is done by their parent process, and they should 1716 // not repeat this work. 1717 if !*isFuzzWorker { 1718 deadline := m.startAlarm() 1719 haveExamples = len(m.examples) > 0 1720 testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline) 1721 fuzzTargetsRan, fuzzTargetsOk := runFuzzTests(m.deps, m.fuzzTargets, deadline) 1722 exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples) 1723 m.stopAlarm() 1724 if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" { 1725 fmt.Fprintln(os.Stderr, "testing: warning: no tests to run") 1726 } 1727 if !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 { 1728 fmt.Println("FAIL") 1729 m.exitCode = 1 1730 return 1731 } 1732 } 1733 1734 fuzzingOk := runFuzzing(m.deps, m.fuzzTargets) 1735 if !fuzzingOk { 1736 fmt.Println("FAIL") 1737 if *isFuzzWorker { 1738 m.exitCode = fuzzWorkerExitCode 1739 } else { 1740 m.exitCode = 1 1741 } 1742 return 1743 } 1744 1745 m.exitCode = 0 1746 if !*isFuzzWorker { 1747 fmt.Println("PASS") 1748 } 1749 return 1750 } 1751 1752 func (t *T) report() { 1753 if t.parent == nil { 1754 return 1755 } 1756 dstr := fmtDuration(t.duration) 1757 format := "--- %s: %s (%s)\n" 1758 if t.Failed() { 1759 t.flushToParent(t.name, format, "FAIL", t.name, dstr) 1760 } else if t.chatty != nil { 1761 if t.Skipped() { 1762 t.flushToParent(t.name, format, "SKIP", t.name, dstr) 1763 } else { 1764 t.flushToParent(t.name, format, "PASS", t.name, dstr) 1765 } 1766 } 1767 } 1768 1769 func listTests(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) { 1770 if _, err := matchString(*matchList, "non-empty"); err != nil { 1771 fmt.Fprintf(os.Stderr, "testing: invalid regexp in -test.list (%q): %s\n", *matchList, err) 1772 os.Exit(1) 1773 } 1774 1775 for _, test := range tests { 1776 if ok, _ := matchString(*matchList, test.Name); ok { 1777 fmt.Println(test.Name) 1778 } 1779 } 1780 for _, bench := range benchmarks { 1781 if ok, _ := matchString(*matchList, bench.Name); ok { 1782 fmt.Println(bench.Name) 1783 } 1784 } 1785 for _, fuzzTarget := range fuzzTargets { 1786 if ok, _ := matchString(*matchList, fuzzTarget.Name); ok { 1787 fmt.Println(fuzzTarget.Name) 1788 } 1789 } 1790 for _, example := range examples { 1791 if ok, _ := matchString(*matchList, example.Name); ok { 1792 fmt.Println(example.Name) 1793 } 1794 } 1795 } 1796 1797 // RunTests is an internal function but exported because it is cross-package; 1798 // it is part of the implementation of the "go test" command. 1799 func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) { 1800 var deadline time.Time 1801 if *timeout > 0 { 1802 deadline = time.Now().Add(*timeout) 1803 } 1804 ran, ok := runTests(matchString, tests, deadline) 1805 if !ran && !haveExamples { 1806 fmt.Fprintln(os.Stderr, "testing: warning: no tests to run") 1807 } 1808 return ok 1809 } 1810 1811 func runTests(matchString func(pat, str string) (bool, error), tests []InternalTest, deadline time.Time) (ran, ok bool) { 1812 ok = true 1813 for _, procs := range cpuList { 1814 runtime.GOMAXPROCS(procs) 1815 for i := uint(0); i < *count; i++ { 1816 if shouldFailFast() { 1817 break 1818 } 1819 if i > 0 && !ran { 1820 // There were no tests to run on the first 1821 // iteration. This won't change, so no reason 1822 // to keep trying. 1823 break 1824 } 1825 ctx := newTestContext(*parallel, newMatcher(matchString, *match, "-test.run")) 1826 ctx.deadline = deadline 1827 t := &T{ 1828 common: common{ 1829 signal: make(chan bool, 1), 1830 barrier: make(chan bool), 1831 w: os.Stdout, 1832 }, 1833 context: ctx, 1834 } 1835 if Verbose() { 1836 t.chatty = newChattyPrinter(t.w) 1837 } 1838 tRunner(t, func(t *T) { 1839 for _, test := range tests { 1840 t.Run(test.Name, test.F) 1841 } 1842 }) 1843 select { 1844 case <-t.signal: 1845 default: 1846 panic("internal error: tRunner exited without sending on t.signal") 1847 } 1848 ok = ok && !t.Failed() 1849 ran = ran || t.ran 1850 } 1851 } 1852 return ran, ok 1853 } 1854 1855 // before runs before all testing. 1856 func (m *M) before() { 1857 if *memProfileRate > 0 { 1858 runtime.MemProfileRate = *memProfileRate 1859 } 1860 if *cpuProfile != "" { 1861 f, err := os.Create(toOutputDir(*cpuProfile)) 1862 if err != nil { 1863 fmt.Fprintf(os.Stderr, "testing: %s\n", err) 1864 return 1865 } 1866 if err := m.deps.StartCPUProfile(f); err != nil { 1867 fmt.Fprintf(os.Stderr, "testing: can't start cpu profile: %s\n", err) 1868 f.Close() 1869 return 1870 } 1871 // Could save f so after can call f.Close; not worth the effort. 1872 } 1873 if *traceFile != "" { 1874 f, err := os.Create(toOutputDir(*traceFile)) 1875 if err != nil { 1876 fmt.Fprintf(os.Stderr, "testing: %s\n", err) 1877 return 1878 } 1879 if err := trace.Start(f); err != nil { 1880 fmt.Fprintf(os.Stderr, "testing: can't start tracing: %s\n", err) 1881 f.Close() 1882 return 1883 } 1884 // Could save f so after can call f.Close; not worth the effort. 1885 } 1886 if *blockProfile != "" && *blockProfileRate >= 0 { 1887 runtime.SetBlockProfileRate(*blockProfileRate) 1888 } 1889 if *mutexProfile != "" && *mutexProfileFraction >= 0 { 1890 runtime.SetMutexProfileFraction(*mutexProfileFraction) 1891 } 1892 if *coverProfile != "" && cover.Mode == "" { 1893 fmt.Fprintf(os.Stderr, "testing: cannot use -test.coverprofile because test binary was not built with coverage enabled\n") 1894 os.Exit(2) 1895 } 1896 if *testlog != "" { 1897 // Note: Not using toOutputDir. 1898 // This file is for use by cmd/go, not users. 1899 var f *os.File 1900 var err error 1901 if m.numRun == 1 { 1902 f, err = os.Create(*testlog) 1903 } else { 1904 f, err = os.OpenFile(*testlog, os.O_WRONLY, 0) 1905 if err == nil { 1906 f.Seek(0, io.SeekEnd) 1907 } 1908 } 1909 if err != nil { 1910 fmt.Fprintf(os.Stderr, "testing: %s\n", err) 1911 os.Exit(2) 1912 } 1913 m.deps.StartTestLog(f) 1914 testlogFile = f 1915 } 1916 if *panicOnExit0 { 1917 m.deps.SetPanicOnExit0(true) 1918 } 1919 } 1920 1921 // after runs after all testing. 1922 func (m *M) after() { 1923 m.afterOnce.Do(func() { 1924 m.writeProfiles() 1925 }) 1926 1927 // Restore PanicOnExit0 after every run, because we set it to true before 1928 // every run. Otherwise, if m.Run is called multiple times the behavior of 1929 // os.Exit(0) will not be restored after the second run. 1930 if *panicOnExit0 { 1931 m.deps.SetPanicOnExit0(false) 1932 } 1933 } 1934 1935 func (m *M) writeProfiles() { 1936 if *testlog != "" { 1937 if err := m.deps.StopTestLog(); err != nil { 1938 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err) 1939 os.Exit(2) 1940 } 1941 if err := testlogFile.Close(); err != nil { 1942 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err) 1943 os.Exit(2) 1944 } 1945 } 1946 if *cpuProfile != "" { 1947 m.deps.StopCPUProfile() // flushes profile to disk 1948 } 1949 if *traceFile != "" { 1950 trace.Stop() // flushes trace to disk 1951 } 1952 if *memProfile != "" { 1953 f, err := os.Create(toOutputDir(*memProfile)) 1954 if err != nil { 1955 fmt.Fprintf(os.Stderr, "testing: %s\n", err) 1956 os.Exit(2) 1957 } 1958 runtime.GC() // materialize all statistics 1959 if err = m.deps.WriteProfileTo("allocs", f, 0); err != nil { 1960 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *memProfile, err) 1961 os.Exit(2) 1962 } 1963 f.Close() 1964 } 1965 if *blockProfile != "" && *blockProfileRate >= 0 { 1966 f, err := os.Create(toOutputDir(*blockProfile)) 1967 if err != nil { 1968 fmt.Fprintf(os.Stderr, "testing: %s\n", err) 1969 os.Exit(2) 1970 } 1971 if err = m.deps.WriteProfileTo("block", f, 0); err != nil { 1972 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err) 1973 os.Exit(2) 1974 } 1975 f.Close() 1976 } 1977 if *mutexProfile != "" && *mutexProfileFraction >= 0 { 1978 f, err := os.Create(toOutputDir(*mutexProfile)) 1979 if err != nil { 1980 fmt.Fprintf(os.Stderr, "testing: %s\n", err) 1981 os.Exit(2) 1982 } 1983 if err = m.deps.WriteProfileTo("mutex", f, 0); err != nil { 1984 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *mutexProfile, err) 1985 os.Exit(2) 1986 } 1987 f.Close() 1988 } 1989 if cover.Mode != "" { 1990 coverReport() 1991 } 1992 } 1993 1994 // toOutputDir returns the file name relocated, if required, to outputDir. 1995 // Simple implementation to avoid pulling in path/filepath. 1996 func toOutputDir(path string) string { 1997 if *outputDir == "" || path == "" { 1998 return path 1999 } 2000 // On Windows, it's clumsy, but we can be almost always correct 2001 // by just looking for a drive letter and a colon. 2002 // Absolute paths always have a drive letter (ignoring UNC). 2003 // Problem: if path == "C:A" and outputdir == "C:\Go" it's unclear 2004 // what to do, but even then path/filepath doesn't help. 2005 // TODO: Worth doing better? Probably not, because we're here only 2006 // under the management of go test. 2007 if runtime.GOOS == "windows" && len(path) >= 2 { 2008 letter, colon := path[0], path[1] 2009 if ('a' <= letter && letter <= 'z' || 'A' <= letter && letter <= 'Z') && colon == ':' { 2010 // If path starts with a drive letter we're stuck with it regardless. 2011 return path 2012 } 2013 } 2014 if os.IsPathSeparator(path[0]) { 2015 return path 2016 } 2017 return fmt.Sprintf("%s%c%s", *outputDir, os.PathSeparator, path) 2018 } 2019 2020 // startAlarm starts an alarm if requested. 2021 func (m *M) startAlarm() time.Time { 2022 if *timeout <= 0 { 2023 return time.Time{} 2024 } 2025 2026 deadline := time.Now().Add(*timeout) 2027 m.timer = time.AfterFunc(*timeout, func() { 2028 m.after() 2029 debug.SetTraceback("all") 2030 panic(fmt.Sprintf("test timed out after %v", *timeout)) 2031 }) 2032 return deadline 2033 } 2034 2035 // stopAlarm turns off the alarm. 2036 func (m *M) stopAlarm() { 2037 if *timeout > 0 { 2038 m.timer.Stop() 2039 } 2040 } 2041 2042 func parseCpuList() { 2043 for _, val := range strings.Split(*cpuListStr, ",") { 2044 val = strings.TrimSpace(val) 2045 if val == "" { 2046 continue 2047 } 2048 cpu, err := strconv.Atoi(val) 2049 if err != nil || cpu <= 0 { 2050 fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu\n", val) 2051 os.Exit(1) 2052 } 2053 cpuList = append(cpuList, cpu) 2054 } 2055 if cpuList == nil { 2056 cpuList = append(cpuList, runtime.GOMAXPROCS(-1)) 2057 } 2058 } 2059 2060 func shouldFailFast() bool { 2061 return *failFast && atomic.LoadUint32(&numFailed) > 0 2062 }