github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/cmd/go/test.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 "bytes" 9 "errors" 10 "fmt" 11 "go/ast" 12 "go/build" 13 "go/doc" 14 "go/parser" 15 "go/token" 16 "os" 17 "os/exec" 18 "path" 19 "path/filepath" 20 "regexp" 21 "runtime" 22 "sort" 23 "strings" 24 "text/template" 25 "time" 26 "unicode" 27 "unicode/utf8" 28 ) 29 30 // Break init loop. 31 func init() { 32 cmdTest.Run = runTest 33 } 34 35 const testUsage = "test [build/test flags] [packages] [build/test flags & test binary flags]" 36 37 var cmdTest = &Command{ 38 CustomFlags: true, 39 UsageLine: testUsage, 40 Short: "test packages", 41 Long: ` 42 'Go test' automates testing the packages named by the import paths. 43 It prints a summary of the test results in the format: 44 45 ok archive/tar 0.011s 46 FAIL archive/zip 0.022s 47 ok compress/gzip 0.033s 48 ... 49 50 followed by detailed output for each failed package. 51 52 'Go test' recompiles each package along with any files with names matching 53 the file pattern "*_test.go". 54 Files whose names begin with "_" (including "_test.go") or "." are ignored. 55 These additional files can contain test functions, benchmark functions, and 56 example functions. See 'go help testfunc' for more. 57 Each listed package causes the execution of a separate test binary. 58 59 Test files that declare a package with the suffix "_test" will be compiled as a 60 separate package, and then linked and run with the main test binary. 61 62 The go tool will ignore a directory named "testdata", making it available 63 to hold ancillary data needed by the tests. 64 65 By default, go test needs no arguments. It compiles and tests the package 66 with source in the current directory, including tests, and runs the tests. 67 68 The package is built in a temporary directory so it does not interfere with the 69 non-test installation. 70 71 ` + strings.TrimSpace(testFlag1) + ` See 'go help testflag' for details. 72 73 For more about build flags, see 'go help build'. 74 For more about specifying packages, see 'go help packages'. 75 76 See also: go build, go vet. 77 `, 78 } 79 80 const testFlag1 = ` 81 In addition to the build flags, the flags handled by 'go test' itself are: 82 83 -args 84 Pass the remainder of the command line (everything after -args) 85 to the test binary, uninterpreted and unchanged. 86 Because this flag consumes the remainder of the command line, 87 the package list (if present) must appear before this flag. 88 89 -c 90 Compile the test binary to pkg.test but do not run it 91 (where pkg is the last element of the package's import path). 92 The file name can be changed with the -o flag. 93 94 -exec xprog 95 Run the test binary using xprog. The behavior is the same as 96 in 'go run'. See 'go help run' for details. 97 98 -i 99 Install packages that are dependencies of the test. 100 Do not run the test. 101 102 -o file 103 Compile the test binary to the named file. 104 The test still runs (unless -c or -i is specified). 105 106 The test binary also accepts flags that control execution of the test; these 107 flags are also accessible by 'go test'. 108 ` 109 110 var helpTestflag = &Command{ 111 UsageLine: "testflag", 112 Short: "description of testing flags", 113 Long: ` 114 The 'go test' command takes both flags that apply to 'go test' itself 115 and flags that apply to the resulting test binary. 116 117 Several of the flags control profiling and write an execution profile 118 suitable for "go tool pprof"; run "go tool pprof -h" for more 119 information. The --alloc_space, --alloc_objects, and --show_bytes 120 options of pprof control how the information is presented. 121 122 The following flags are recognized by the 'go test' command and 123 control the execution of any test: 124 125 ` + strings.TrimSpace(testFlag2) + ` 126 `, 127 } 128 129 const testFlag2 = ` 130 -bench regexp 131 Run (sub)benchmarks matching a regular expression. 132 The given regular expression is split into smaller ones by 133 top-level '/', where each must match the corresponding part of a 134 benchmark's identifier. 135 By default, no benchmarks run. To run all benchmarks, 136 use '-bench .' or '-bench=.'. 137 138 -benchmem 139 Print memory allocation statistics for benchmarks. 140 141 -benchtime t 142 Run enough iterations of each benchmark to take t, specified 143 as a time.Duration (for example, -benchtime 1h30s). 144 The default is 1 second (1s). 145 146 -blockprofile block.out 147 Write a goroutine blocking profile to the specified file 148 when all tests are complete. 149 Writes test binary as -c would. 150 151 -blockprofilerate n 152 Control the detail provided in goroutine blocking profiles by 153 calling runtime.SetBlockProfileRate with n. 154 See 'go doc runtime.SetBlockProfileRate'. 155 The profiler aims to sample, on average, one blocking event every 156 n nanoseconds the program spends blocked. By default, 157 if -test.blockprofile is set without this flag, all blocking events 158 are recorded, equivalent to -test.blockprofilerate=1. 159 160 -count n 161 Run each test and benchmark n times (default 1). 162 If -cpu is set, run n times for each GOMAXPROCS value. 163 Examples are always run once. 164 165 -cover 166 Enable coverage analysis. 167 168 -covermode set,count,atomic 169 Set the mode for coverage analysis for the package[s] 170 being tested. The default is "set" unless -race is enabled, 171 in which case it is "atomic". 172 The values: 173 set: bool: does this statement run? 174 count: int: how many times does this statement run? 175 atomic: int: count, but correct in multithreaded tests; 176 significantly more expensive. 177 Sets -cover. 178 179 -coverpkg pkg1,pkg2,pkg3 180 Apply coverage analysis in each test to the given list of packages. 181 The default is for each test to analyze only the package being tested. 182 Packages are specified as import paths. 183 Sets -cover. 184 185 -coverprofile cover.out 186 Write a coverage profile to the file after all tests have passed. 187 Sets -cover. 188 189 -cpu 1,2,4 190 Specify a list of GOMAXPROCS values for which the tests or 191 benchmarks should be executed. The default is the current value 192 of GOMAXPROCS. 193 194 -cpuprofile cpu.out 195 Write a CPU profile to the specified file before exiting. 196 Writes test binary as -c would. 197 198 -memprofile mem.out 199 Write a memory profile to the file after all tests have passed. 200 Writes test binary as -c would. 201 202 -memprofilerate n 203 Enable more precise (and expensive) memory profiles by setting 204 runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'. 205 To profile all memory allocations, use -test.memprofilerate=1 206 and pass --alloc_space flag to the pprof tool. 207 208 -outputdir directory 209 Place output files from profiling in the specified directory, 210 by default the directory in which "go test" is running. 211 212 -parallel n 213 Allow parallel execution of test functions that call t.Parallel. 214 The value of this flag is the maximum number of tests to run 215 simultaneously; by default, it is set to the value of GOMAXPROCS. 216 Note that -parallel only applies within a single test binary. 217 The 'go test' command may run tests for different packages 218 in parallel as well, according to the setting of the -p flag 219 (see 'go help build'). 220 221 -run regexp 222 Run only those tests and examples matching the regular expression. 223 For tests the regular expression is split into smaller ones by 224 top-level '/', where each must match the corresponding part of a 225 test's identifier. 226 227 -short 228 Tell long-running tests to shorten their run time. 229 It is off by default but set during all.bash so that installing 230 the Go tree can run a sanity check but not spend time running 231 exhaustive tests. 232 233 -timeout t 234 If a test runs longer than t, panic. 235 The default is 10 minutes (10m). 236 237 -trace trace.out 238 Write an execution trace to the specified file before exiting. 239 240 -v 241 Verbose output: log all tests as they are run. Also print all 242 text from Log and Logf calls even if the test succeeds. 243 244 Each of these flags is also recognized with an optional 'test.' prefix, 245 as in -test.v. When invoking the generated test binary (the result of 246 'go test -c') directly, however, the prefix is mandatory. 247 248 The 'go test' command rewrites or removes recognized flags, 249 as appropriate, both before and after the optional package list, 250 before invoking the test binary. 251 252 For instance, the command 253 254 go test -v -myflag testdata -cpuprofile=prof.out -x 255 256 will compile the test binary and then run it as 257 258 pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out 259 260 (The -x flag is removed because it applies only to the go command's 261 execution, not to the test itself.) 262 263 The test flags that generate profiles (other than for coverage) also 264 leave the test binary in pkg.test for use when analyzing the profiles. 265 266 When 'go test' runs a test binary, it does so from within the 267 corresponding package's source code directory. Depending on the test, 268 it may be necessary to do the same when invoking a generated test 269 binary directly. 270 271 The command-line package list, if present, must appear before any 272 flag not known to the go test command. Continuing the example above, 273 the package list would have to appear before -myflag, but could appear 274 on either side of -v. 275 276 To keep an argument for a test binary from being interpreted as a 277 known flag or a package name, use -args (see 'go help test') which 278 passes the remainder of the command line through to the test binary 279 uninterpreted and unaltered. 280 281 For instance, the command 282 283 go test -v -args -x -v 284 285 will compile the test binary and then run it as 286 287 pkg.test -test.v -x -v 288 289 Similarly, 290 291 go test -args math 292 293 will compile the test binary and then run it as 294 295 pkg.test math 296 297 In the first example, the -x and the second -v are passed through to the 298 test binary unchanged and with no effect on the go command itself. 299 In the second example, the argument math is passed through to the test 300 binary, instead of being interpreted as the package list. 301 ` 302 303 var helpTestfunc = &Command{ 304 UsageLine: "testfunc", 305 Short: "description of testing functions", 306 Long: ` 307 The 'go test' command expects to find test, benchmark, and example functions 308 in the "*_test.go" files corresponding to the package under test. 309 310 A test function is one named TestXXX (where XXX is any alphanumeric string 311 not starting with a lower case letter) and should have the signature, 312 313 func TestXXX(t *testing.T) { ... } 314 315 A benchmark function is one named BenchmarkXXX and should have the signature, 316 317 func BenchmarkXXX(b *testing.B) { ... } 318 319 An example function is similar to a test function but, instead of using 320 *testing.T to report success or failure, prints output to os.Stdout. 321 If the last comment in the function starts with "Output:" then the output 322 is compared exactly against the comment (see examples below). If the last 323 comment begins with "Unordered output:" then the output is compared to the 324 comment, however the order of the lines is ignored. An example with no such 325 comment, or with no text after "Output:" is compiled but not executed. 326 327 Godoc displays the body of ExampleXXX to demonstrate the use 328 of the function, constant, or variable XXX. An example of a method M with 329 receiver type T or *T is named ExampleT_M. There may be multiple examples 330 for a given function, constant, or variable, distinguished by a trailing _xxx, 331 where xxx is a suffix not beginning with an upper case letter. 332 333 Here is an example of an example: 334 335 func ExamplePrintln() { 336 Println("The output of\nthis example.") 337 // Output: The output of 338 // this example. 339 } 340 341 Here is another example where the ordering of the output is ignored: 342 343 func ExamplePerm() { 344 for _, value := range Perm(4) { 345 fmt.Println(value) 346 } 347 348 // Unordered output: 4 349 // 2 350 // 1 351 // 3 352 // 0 353 } 354 355 The entire test file is presented as the example when it contains a single 356 example function, at least one other function, type, variable, or constant 357 declaration, and no test or benchmark functions. 358 359 See the documentation of the testing package for more information. 360 `, 361 } 362 363 var ( 364 testC bool // -c flag 365 testCover bool // -cover flag 366 testCoverMode string // -covermode flag 367 testCoverPaths []string // -coverpkg flag 368 testCoverPkgs []*Package // -coverpkg flag 369 testO string // -o flag 370 testProfile bool // some profiling flag 371 testNeedBinary bool // profile needs to keep binary around 372 testV bool // -v flag 373 testTimeout string // -timeout flag 374 testArgs []string 375 testBench bool 376 testStreamOutput bool // show output as it is generated 377 testShowPass bool // show passing output 378 379 testKillTimeout = 10 * time.Minute 380 ) 381 382 var testMainDeps = map[string]bool{ 383 // Dependencies for testmain. 384 "testing": true, 385 "regexp": true, 386 "os": true, 387 } 388 389 func runTest(cmd *Command, args []string) { 390 var pkgArgs []string 391 pkgArgs, testArgs = testFlags(args) 392 393 findExecCmd() // initialize cached result 394 395 instrumentInit() 396 buildModeInit() 397 pkgs := packagesForBuild(pkgArgs) 398 if len(pkgs) == 0 { 399 fatalf("no packages to test") 400 } 401 402 if testC && len(pkgs) != 1 { 403 fatalf("cannot use -c flag with multiple packages") 404 } 405 if testO != "" && len(pkgs) != 1 { 406 fatalf("cannot use -o flag with multiple packages") 407 } 408 if testProfile && len(pkgs) != 1 { 409 fatalf("cannot use test profile flag with multiple packages") 410 } 411 412 // If a test timeout was given and is parseable, set our kill timeout 413 // to that timeout plus one minute. This is a backup alarm in case 414 // the test wedges with a goroutine spinning and its background 415 // timer does not get a chance to fire. 416 if dt, err := time.ParseDuration(testTimeout); err == nil && dt > 0 { 417 testKillTimeout = dt + 1*time.Minute 418 } 419 420 // show passing test output (after buffering) with -v flag. 421 // must buffer because tests are running in parallel, and 422 // otherwise the output will get mixed. 423 testShowPass = testV 424 425 // stream test output (no buffering) when no package has 426 // been given on the command line (implicit current directory) 427 // or when benchmarking. 428 // Also stream if we're showing output anyway with a 429 // single package under test or if parallelism is set to 1. 430 // In these cases, streaming the output produces the same result 431 // as not streaming, just more immediately. 432 testStreamOutput = len(pkgArgs) == 0 || testBench || 433 (testShowPass && (len(pkgs) == 1 || buildP == 1)) 434 435 var b builder 436 b.init() 437 438 if buildI { 439 buildV = testV 440 441 deps := make(map[string]bool) 442 for dep := range testMainDeps { 443 deps[dep] = true 444 } 445 446 for _, p := range pkgs { 447 // Dependencies for each test. 448 for _, path := range p.Imports { 449 deps[path] = true 450 } 451 for _, path := range p.vendored(p.TestImports) { 452 deps[path] = true 453 } 454 for _, path := range p.vendored(p.XTestImports) { 455 deps[path] = true 456 } 457 } 458 459 // translate C to runtime/cgo 460 if deps["C"] { 461 delete(deps, "C") 462 deps["runtime/cgo"] = true 463 if goos == runtime.GOOS && goarch == runtime.GOARCH && !buildRace && !buildMSan { 464 deps["cmd/cgo"] = true 465 } 466 } 467 // Ignore pseudo-packages. 468 delete(deps, "unsafe") 469 470 all := []string{} 471 for path := range deps { 472 if !build.IsLocalImport(path) { 473 all = append(all, path) 474 } 475 } 476 sort.Strings(all) 477 478 a := &action{} 479 for _, p := range packagesForBuild(all) { 480 a.deps = append(a.deps, b.action(modeInstall, modeInstall, p)) 481 } 482 b.do(a) 483 if !testC || a.failed { 484 return 485 } 486 b.init() 487 } 488 489 var builds, runs, prints []*action 490 491 if testCoverPaths != nil { 492 // Load packages that were asked about for coverage. 493 // packagesForBuild exits if the packages cannot be loaded. 494 testCoverPkgs = packagesForBuild(testCoverPaths) 495 496 // Warn about -coverpkg arguments that are not actually used. 497 used := make(map[string]bool) 498 for _, p := range pkgs { 499 used[p.ImportPath] = true 500 for _, dep := range p.Deps { 501 used[dep] = true 502 } 503 } 504 for _, p := range testCoverPkgs { 505 if !used[p.ImportPath] { 506 fmt.Fprintf(os.Stderr, "warning: no packages being tested depend on %s\n", p.ImportPath) 507 } 508 } 509 510 // Mark all the coverage packages for rebuilding with coverage. 511 for _, p := range testCoverPkgs { 512 // There is nothing to cover in package unsafe; it comes from the compiler. 513 if p.ImportPath == "unsafe" { 514 continue 515 } 516 p.Stale = true // rebuild 517 p.StaleReason = "rebuild for coverage" 518 p.fake = true // do not warn about rebuild 519 p.coverMode = testCoverMode 520 var coverFiles []string 521 coverFiles = append(coverFiles, p.GoFiles...) 522 coverFiles = append(coverFiles, p.CgoFiles...) 523 coverFiles = append(coverFiles, p.TestGoFiles...) 524 p.coverVars = declareCoverVars(p.ImportPath, coverFiles...) 525 } 526 } 527 528 // Prepare build + run + print actions for all packages being tested. 529 for _, p := range pkgs { 530 buildTest, runTest, printTest, err := b.test(p) 531 if err != nil { 532 str := err.Error() 533 if strings.HasPrefix(str, "\n") { 534 str = str[1:] 535 } 536 failed := fmt.Sprintf("FAIL\t%s [setup failed]\n", p.ImportPath) 537 538 if p.ImportPath != "" { 539 errorf("# %s\n%s\n%s", p.ImportPath, str, failed) 540 } else { 541 errorf("%s\n%s", str, failed) 542 } 543 continue 544 } 545 builds = append(builds, buildTest) 546 runs = append(runs, runTest) 547 prints = append(prints, printTest) 548 } 549 550 // Ultimately the goal is to print the output. 551 root := &action{deps: prints} 552 553 // Force the printing of results to happen in order, 554 // one at a time. 555 for i, a := range prints { 556 if i > 0 { 557 a.deps = append(a.deps, prints[i-1]) 558 } 559 } 560 561 // Force benchmarks to run in serial. 562 if !testC && testBench { 563 // The first run must wait for all builds. 564 // Later runs must wait for the previous run's print. 565 for i, run := range runs { 566 if i == 0 { 567 run.deps = append(run.deps, builds...) 568 } else { 569 run.deps = append(run.deps, prints[i-1]) 570 } 571 } 572 } 573 574 // If we are building any out-of-date packages other 575 // than those under test, warn. 576 okBuild := map[*Package]bool{} 577 for _, p := range pkgs { 578 okBuild[p] = true 579 } 580 warned := false 581 for _, a := range actionList(root) { 582 if a.p == nil || okBuild[a.p] { 583 continue 584 } 585 okBuild[a.p] = true // warn at most once 586 587 // Don't warn about packages being rebuilt because of 588 // things like coverage analysis. 589 for _, p1 := range a.p.imports { 590 if p1.fake { 591 a.p.fake = true 592 } 593 } 594 595 if a.f != nil && !okBuild[a.p] && !a.p.fake && !a.p.local { 596 if !warned { 597 fmt.Fprintf(os.Stderr, "warning: building out-of-date packages:\n") 598 warned = true 599 } 600 fmt.Fprintf(os.Stderr, "\t%s\n", a.p.ImportPath) 601 } 602 } 603 if warned { 604 args := strings.Join(pkgArgs, " ") 605 if args != "" { 606 args = " " + args 607 } 608 extraOpts := "" 609 if buildRace { 610 extraOpts = "-race " 611 } 612 if buildMSan { 613 extraOpts = "-msan " 614 } 615 fmt.Fprintf(os.Stderr, "installing these packages with 'go test %s-i%s' will speed future tests.\n\n", extraOpts, args) 616 } 617 618 b.do(root) 619 } 620 621 func contains(x []string, s string) bool { 622 for _, t := range x { 623 if t == s { 624 return true 625 } 626 } 627 return false 628 } 629 630 var windowsBadWords = []string{ 631 "install", 632 "patch", 633 "setup", 634 "update", 635 } 636 637 func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, err error) { 638 if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 { 639 build := b.action(modeBuild, modeBuild, p) 640 run := &action{p: p, deps: []*action{build}} 641 print := &action{f: (*builder).notest, p: p, deps: []*action{run}} 642 return build, run, print, nil 643 } 644 645 // Build Package structs describing: 646 // ptest - package + test files 647 // pxtest - package of external test files 648 // pmain - pkg.test binary 649 var ptest, pxtest, pmain *Package 650 651 var imports, ximports []*Package 652 var stk importStack 653 stk.push(p.ImportPath + " (test)") 654 for i, path := range p.TestImports { 655 p1 := loadImport(path, p.Dir, p, &stk, p.build.TestImportPos[path], useVendor) 656 if p1.Error != nil { 657 return nil, nil, nil, p1.Error 658 } 659 if len(p1.DepsErrors) > 0 { 660 err := p1.DepsErrors[0] 661 err.Pos = "" // show full import stack 662 return nil, nil, nil, err 663 } 664 if contains(p1.Deps, p.ImportPath) || p1.ImportPath == p.ImportPath { 665 // Same error that loadPackage returns (via reusePackage) in pkg.go. 666 // Can't change that code, because that code is only for loading the 667 // non-test copy of a package. 668 err := &PackageError{ 669 ImportStack: testImportStack(stk[0], p1, p.ImportPath), 670 Err: "import cycle not allowed in test", 671 isImportCycle: true, 672 } 673 return nil, nil, nil, err 674 } 675 p.TestImports[i] = p1.ImportPath 676 imports = append(imports, p1) 677 } 678 stk.pop() 679 stk.push(p.ImportPath + "_test") 680 pxtestNeedsPtest := false 681 for i, path := range p.XTestImports { 682 p1 := loadImport(path, p.Dir, p, &stk, p.build.XTestImportPos[path], useVendor) 683 if p1.Error != nil { 684 return nil, nil, nil, p1.Error 685 } 686 if len(p1.DepsErrors) > 0 { 687 err := p1.DepsErrors[0] 688 err.Pos = "" // show full import stack 689 return nil, nil, nil, err 690 } 691 if p1.ImportPath == p.ImportPath { 692 pxtestNeedsPtest = true 693 } else { 694 ximports = append(ximports, p1) 695 } 696 p.XTestImports[i] = p1.ImportPath 697 } 698 stk.pop() 699 700 // Use last element of import path, not package name. 701 // They differ when package name is "main". 702 // But if the import path is "command-line-arguments", 703 // like it is during 'go run', use the package name. 704 var elem string 705 if p.ImportPath == "command-line-arguments" { 706 elem = p.Name 707 } else { 708 _, elem = path.Split(p.ImportPath) 709 } 710 testBinary := elem + ".test" 711 712 // The ptest package needs to be importable under the 713 // same import path that p has, but we cannot put it in 714 // the usual place in the temporary tree, because then 715 // other tests will see it as the real package. 716 // Instead we make a _test directory under the import path 717 // and then repeat the import path there. We tell the 718 // compiler and linker to look in that _test directory first. 719 // 720 // That is, if the package under test is unicode/utf8, 721 // then the normal place to write the package archive is 722 // $WORK/unicode/utf8.a, but we write the test package archive to 723 // $WORK/unicode/utf8/_test/unicode/utf8.a. 724 // We write the external test package archive to 725 // $WORK/unicode/utf8/_test/unicode/utf8_test.a. 726 testDir := filepath.Join(b.work, filepath.FromSlash(p.ImportPath+"/_test")) 727 ptestObj := buildToolchain.pkgpath(testDir, p) 728 729 // Create the directory for the .a files. 730 ptestDir, _ := filepath.Split(ptestObj) 731 if err := b.mkdir(ptestDir); err != nil { 732 return nil, nil, nil, err 733 } 734 735 // Should we apply coverage analysis locally, 736 // only for this package and only for this test? 737 // Yes, if -cover is on but -coverpkg has not specified 738 // a list of packages for global coverage. 739 localCover := testCover && testCoverPaths == nil 740 741 // Test package. 742 if len(p.TestGoFiles) > 0 || localCover || p.Name == "main" { 743 ptest = new(Package) 744 *ptest = *p 745 ptest.GoFiles = nil 746 ptest.GoFiles = append(ptest.GoFiles, p.GoFiles...) 747 ptest.GoFiles = append(ptest.GoFiles, p.TestGoFiles...) 748 ptest.target = "" 749 ptest.Imports = stringList(p.Imports, p.TestImports) 750 ptest.imports = append(append([]*Package{}, p.imports...), imports...) 751 ptest.pkgdir = testDir 752 ptest.fake = true 753 ptest.forceLibrary = true 754 ptest.Stale = true 755 ptest.StaleReason = "rebuild for test" 756 ptest.build = new(build.Package) 757 *ptest.build = *p.build 758 m := map[string][]token.Position{} 759 for k, v := range p.build.ImportPos { 760 m[k] = append(m[k], v...) 761 } 762 for k, v := range p.build.TestImportPos { 763 m[k] = append(m[k], v...) 764 } 765 ptest.build.ImportPos = m 766 767 if localCover { 768 ptest.coverMode = testCoverMode 769 var coverFiles []string 770 coverFiles = append(coverFiles, ptest.GoFiles...) 771 coverFiles = append(coverFiles, ptest.CgoFiles...) 772 ptest.coverVars = declareCoverVars(ptest.ImportPath, coverFiles...) 773 } 774 } else { 775 ptest = p 776 } 777 778 // External test package. 779 if len(p.XTestGoFiles) > 0 { 780 pxtest = &Package{ 781 Name: p.Name + "_test", 782 ImportPath: p.ImportPath + "_test", 783 localPrefix: p.localPrefix, 784 Root: p.Root, 785 Dir: p.Dir, 786 GoFiles: p.XTestGoFiles, 787 Imports: p.XTestImports, 788 build: &build.Package{ 789 ImportPos: p.build.XTestImportPos, 790 }, 791 imports: ximports, 792 pkgdir: testDir, 793 fake: true, 794 external: true, 795 Stale: true, 796 } 797 if pxtestNeedsPtest { 798 pxtest.imports = append(pxtest.imports, ptest) 799 } 800 } 801 802 // Action for building pkg.test. 803 pmain = &Package{ 804 Name: "main", 805 Dir: testDir, 806 GoFiles: []string{"_testmain.go"}, 807 ImportPath: "testmain", 808 Root: p.Root, 809 build: &build.Package{Name: "main"}, 810 pkgdir: testDir, 811 fake: true, 812 Stale: true, 813 omitDWARF: !testC && !testNeedBinary, 814 } 815 816 // The generated main also imports testing, regexp, and os. 817 stk.push("testmain") 818 for dep := range testMainDeps { 819 if dep == ptest.ImportPath { 820 pmain.imports = append(pmain.imports, ptest) 821 } else { 822 p1 := loadImport(dep, "", nil, &stk, nil, 0) 823 if p1.Error != nil { 824 return nil, nil, nil, p1.Error 825 } 826 pmain.imports = append(pmain.imports, p1) 827 } 828 } 829 830 if testCoverPkgs != nil { 831 // Add imports, but avoid duplicates. 832 seen := map[*Package]bool{p: true, ptest: true} 833 for _, p1 := range pmain.imports { 834 seen[p1] = true 835 } 836 for _, p1 := range testCoverPkgs { 837 if !seen[p1] { 838 seen[p1] = true 839 pmain.imports = append(pmain.imports, p1) 840 } 841 } 842 } 843 844 // Do initial scan for metadata needed for writing _testmain.go 845 // Use that metadata to update the list of imports for package main. 846 // The list of imports is used by recompileForTest and by the loop 847 // afterward that gathers t.Cover information. 848 t, err := loadTestFuncs(ptest) 849 if err != nil { 850 return nil, nil, nil, err 851 } 852 if len(ptest.GoFiles) > 0 { 853 pmain.imports = append(pmain.imports, ptest) 854 t.ImportTest = true 855 } 856 if pxtest != nil { 857 pmain.imports = append(pmain.imports, pxtest) 858 t.ImportXtest = true 859 } 860 861 if ptest != p && localCover { 862 // We have made modifications to the package p being tested 863 // and are rebuilding p (as ptest), writing it to the testDir tree. 864 // Arrange to rebuild, writing to that same tree, all packages q 865 // such that the test depends on q, and q depends on p. 866 // This makes sure that q sees the modifications to p. 867 // Strictly speaking, the rebuild is only necessary if the 868 // modifications to p change its export metadata, but 869 // determining that is a bit tricky, so we rebuild always. 870 // 871 // This will cause extra compilation, so for now we only do it 872 // when testCover is set. The conditions are more general, though, 873 // and we may find that we need to do it always in the future. 874 recompileForTest(pmain, p, ptest, testDir) 875 } 876 877 if buildContext.GOOS == "darwin" { 878 if buildContext.GOARCH == "arm" || buildContext.GOARCH == "arm64" { 879 t.NeedCgo = true 880 } 881 } 882 883 for _, cp := range pmain.imports { 884 if len(cp.coverVars) > 0 { 885 t.Cover = append(t.Cover, coverInfo{cp, cp.coverVars}) 886 } 887 } 888 889 if !buildN { 890 // writeTestmain writes _testmain.go. This must happen after recompileForTest, 891 // because recompileForTest modifies XXX. 892 if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), t); err != nil { 893 return nil, nil, nil, err 894 } 895 } 896 897 computeStale(pmain) 898 899 if ptest != p { 900 a := b.action(modeBuild, modeBuild, ptest) 901 a.objdir = testDir + string(filepath.Separator) + "_obj_test" + string(filepath.Separator) 902 a.objpkg = ptestObj 903 a.target = ptestObj 904 a.link = false 905 } 906 907 if pxtest != nil { 908 a := b.action(modeBuild, modeBuild, pxtest) 909 a.objdir = testDir + string(filepath.Separator) + "_obj_xtest" + string(filepath.Separator) 910 a.objpkg = buildToolchain.pkgpath(testDir, pxtest) 911 a.target = a.objpkg 912 } 913 914 a := b.action(modeBuild, modeBuild, pmain) 915 a.objdir = testDir + string(filepath.Separator) 916 a.objpkg = filepath.Join(testDir, "main.a") 917 a.target = filepath.Join(testDir, testBinary) + exeSuffix 918 if goos == "windows" { 919 // There are many reserved words on Windows that, 920 // if used in the name of an executable, cause Windows 921 // to try to ask for extra permissions. 922 // The word list includes setup, install, update, and patch, 923 // but it does not appear to be defined anywhere. 924 // We have run into this trying to run the 925 // go.codereview/patch tests. 926 // For package names containing those words, use test.test.exe 927 // instead of pkgname.test.exe. 928 // Note that this file name is only used in the Go command's 929 // temporary directory. If the -c or other flags are 930 // given, the code below will still use pkgname.test.exe. 931 // There are two user-visible effects of this change. 932 // First, you can actually run 'go test' in directories that 933 // have names that Windows thinks are installer-like, 934 // without getting a dialog box asking for more permissions. 935 // Second, in the Windows process listing during go test, 936 // the test shows up as test.test.exe, not pkgname.test.exe. 937 // That second one is a drawback, but it seems a small 938 // price to pay for the test running at all. 939 // If maintaining the list of bad words is too onerous, 940 // we could just do this always on Windows. 941 for _, bad := range windowsBadWords { 942 if strings.Contains(testBinary, bad) { 943 a.target = filepath.Join(testDir, "test.test") + exeSuffix 944 break 945 } 946 } 947 } 948 buildAction = a 949 950 if testC || testNeedBinary { 951 // -c or profiling flag: create action to copy binary to ./test.out. 952 target := filepath.Join(cwd, testBinary+exeSuffix) 953 if testO != "" { 954 target = testO 955 if !filepath.IsAbs(target) { 956 target = filepath.Join(cwd, target) 957 } 958 } 959 buildAction = &action{ 960 f: (*builder).install, 961 deps: []*action{buildAction}, 962 p: pmain, 963 target: target, 964 } 965 runAction = buildAction // make sure runAction != nil even if not running test 966 } 967 if testC { 968 printAction = &action{p: p, deps: []*action{runAction}} // nop 969 } else { 970 // run test 971 runAction = &action{ 972 f: (*builder).runTest, 973 deps: []*action{buildAction}, 974 p: p, 975 ignoreFail: true, 976 } 977 cleanAction := &action{ 978 f: (*builder).cleanTest, 979 deps: []*action{runAction}, 980 p: p, 981 } 982 printAction = &action{ 983 f: (*builder).printTest, 984 deps: []*action{cleanAction}, 985 p: p, 986 } 987 } 988 989 return buildAction, runAction, printAction, nil 990 } 991 992 func testImportStack(top string, p *Package, target string) []string { 993 stk := []string{top, p.ImportPath} 994 Search: 995 for p.ImportPath != target { 996 for _, p1 := range p.imports { 997 if p1.ImportPath == target || contains(p1.Deps, target) { 998 stk = append(stk, p1.ImportPath) 999 p = p1 1000 continue Search 1001 } 1002 } 1003 // Can't happen, but in case it does... 1004 stk = append(stk, "<lost path to cycle>") 1005 break 1006 } 1007 return stk 1008 } 1009 1010 func recompileForTest(pmain, preal, ptest *Package, testDir string) { 1011 // The "test copy" of preal is ptest. 1012 // For each package that depends on preal, make a "test copy" 1013 // that depends on ptest. And so on, up the dependency tree. 1014 testCopy := map[*Package]*Package{preal: ptest} 1015 for _, p := range packageList([]*Package{pmain}) { 1016 // Copy on write. 1017 didSplit := false 1018 split := func() { 1019 if didSplit { 1020 return 1021 } 1022 didSplit = true 1023 if p.pkgdir != testDir { 1024 p1 := new(Package) 1025 testCopy[p] = p1 1026 *p1 = *p 1027 p1.imports = make([]*Package, len(p.imports)) 1028 copy(p1.imports, p.imports) 1029 p = p1 1030 p.pkgdir = testDir 1031 p.target = "" 1032 p.fake = true 1033 p.Stale = true 1034 p.StaleReason = "depends on package being tested" 1035 } 1036 } 1037 1038 // Update p.deps and p.imports to use at test copies. 1039 for i, dep := range p.deps { 1040 if p1 := testCopy[dep]; p1 != nil && p1 != dep { 1041 split() 1042 p.deps[i] = p1 1043 } 1044 } 1045 for i, imp := range p.imports { 1046 if p1 := testCopy[imp]; p1 != nil && p1 != imp { 1047 split() 1048 p.imports[i] = p1 1049 } 1050 } 1051 } 1052 } 1053 1054 var coverIndex = 0 1055 1056 // isTestFile reports whether the source file is a set of tests and should therefore 1057 // be excluded from coverage analysis. 1058 func isTestFile(file string) bool { 1059 // We don't cover tests, only the code they test. 1060 return strings.HasSuffix(file, "_test.go") 1061 } 1062 1063 // declareCoverVars attaches the required cover variables names 1064 // to the files, to be used when annotating the files. 1065 func declareCoverVars(importPath string, files ...string) map[string]*CoverVar { 1066 coverVars := make(map[string]*CoverVar) 1067 for _, file := range files { 1068 if isTestFile(file) { 1069 continue 1070 } 1071 coverVars[file] = &CoverVar{ 1072 File: filepath.Join(importPath, file), 1073 Var: fmt.Sprintf("GoCover_%d", coverIndex), 1074 } 1075 coverIndex++ 1076 } 1077 return coverVars 1078 } 1079 1080 // runTest is the action for running a test binary. 1081 func (b *builder) runTest(a *action) error { 1082 args := stringList(findExecCmd(), a.deps[0].target, testArgs) 1083 a.testOutput = new(bytes.Buffer) 1084 1085 if buildN || buildX { 1086 b.showcmd("", "%s", strings.Join(args, " ")) 1087 if buildN { 1088 return nil 1089 } 1090 } 1091 1092 if a.failed { 1093 // We were unable to build the binary. 1094 a.failed = false 1095 fmt.Fprintf(a.testOutput, "FAIL\t%s [build failed]\n", a.p.ImportPath) 1096 setExitStatus(1) 1097 return nil 1098 } 1099 1100 cmd := exec.Command(args[0], args[1:]...) 1101 cmd.Dir = a.p.Dir 1102 cmd.Env = envForDir(cmd.Dir, origEnv) 1103 var buf bytes.Buffer 1104 if testStreamOutput { 1105 cmd.Stdout = os.Stdout 1106 cmd.Stderr = os.Stderr 1107 } else { 1108 cmd.Stdout = &buf 1109 cmd.Stderr = &buf 1110 } 1111 1112 // If there are any local SWIG dependencies, we want to load 1113 // the shared library from the build directory. 1114 if a.p.usesSwig() { 1115 env := cmd.Env 1116 found := false 1117 prefix := "LD_LIBRARY_PATH=" 1118 for i, v := range env { 1119 if strings.HasPrefix(v, prefix) { 1120 env[i] = v + ":." 1121 found = true 1122 break 1123 } 1124 } 1125 if !found { 1126 env = append(env, "LD_LIBRARY_PATH=.") 1127 } 1128 cmd.Env = env 1129 } 1130 1131 t0 := time.Now() 1132 err := cmd.Start() 1133 1134 // This is a last-ditch deadline to detect and 1135 // stop wedged test binaries, to keep the builders 1136 // running. 1137 if err == nil { 1138 tick := time.NewTimer(testKillTimeout) 1139 startSigHandlers() 1140 done := make(chan error) 1141 go func() { 1142 done <- cmd.Wait() 1143 }() 1144 Outer: 1145 select { 1146 case err = <-done: 1147 // ok 1148 case <-tick.C: 1149 if signalTrace != nil { 1150 // Send a quit signal in the hope that the program will print 1151 // a stack trace and exit. Give it five seconds before resorting 1152 // to Kill. 1153 cmd.Process.Signal(signalTrace) 1154 select { 1155 case err = <-done: 1156 fmt.Fprintf(&buf, "*** Test killed with %v: ran too long (%v).\n", signalTrace, testKillTimeout) 1157 break Outer 1158 case <-time.After(5 * time.Second): 1159 } 1160 } 1161 cmd.Process.Kill() 1162 err = <-done 1163 fmt.Fprintf(&buf, "*** Test killed: ran too long (%v).\n", testKillTimeout) 1164 } 1165 tick.Stop() 1166 } 1167 out := buf.Bytes() 1168 t := fmt.Sprintf("%.3fs", time.Since(t0).Seconds()) 1169 if err == nil { 1170 if testShowPass { 1171 a.testOutput.Write(out) 1172 } 1173 fmt.Fprintf(a.testOutput, "ok \t%s\t%s%s\n", a.p.ImportPath, t, coveragePercentage(out)) 1174 return nil 1175 } 1176 1177 setExitStatus(1) 1178 if len(out) > 0 { 1179 a.testOutput.Write(out) 1180 // assume printing the test binary's exit status is superfluous 1181 } else { 1182 fmt.Fprintf(a.testOutput, "%s\n", err) 1183 } 1184 fmt.Fprintf(a.testOutput, "FAIL\t%s\t%s\n", a.p.ImportPath, t) 1185 1186 return nil 1187 } 1188 1189 // coveragePercentage returns the coverage results (if enabled) for the 1190 // test. It uncovers the data by scanning the output from the test run. 1191 func coveragePercentage(out []byte) string { 1192 if !testCover { 1193 return "" 1194 } 1195 // The string looks like 1196 // test coverage for encoding/binary: 79.9% of statements 1197 // Extract the piece from the percentage to the end of the line. 1198 re := regexp.MustCompile(`coverage: (.*)\n`) 1199 matches := re.FindSubmatch(out) 1200 if matches == nil { 1201 // Probably running "go test -cover" not "go test -cover fmt". 1202 // The coverage output will appear in the output directly. 1203 return "" 1204 } 1205 return fmt.Sprintf("\tcoverage: %s", matches[1]) 1206 } 1207 1208 // cleanTest is the action for cleaning up after a test. 1209 func (b *builder) cleanTest(a *action) error { 1210 if buildWork { 1211 return nil 1212 } 1213 run := a.deps[0] 1214 testDir := filepath.Join(b.work, filepath.FromSlash(run.p.ImportPath+"/_test")) 1215 os.RemoveAll(testDir) 1216 return nil 1217 } 1218 1219 // printTest is the action for printing a test result. 1220 func (b *builder) printTest(a *action) error { 1221 clean := a.deps[0] 1222 run := clean.deps[0] 1223 os.Stdout.Write(run.testOutput.Bytes()) 1224 run.testOutput = nil 1225 return nil 1226 } 1227 1228 // notest is the action for testing a package with no test files. 1229 func (b *builder) notest(a *action) error { 1230 fmt.Printf("? \t%s\t[no test files]\n", a.p.ImportPath) 1231 return nil 1232 } 1233 1234 // isTestFunc tells whether fn has the type of a testing function. arg 1235 // specifies the parameter type we look for: B, M or T. 1236 func isTestFunc(fn *ast.FuncDecl, arg string) bool { 1237 if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 || 1238 fn.Type.Params.List == nil || 1239 len(fn.Type.Params.List) != 1 || 1240 len(fn.Type.Params.List[0].Names) > 1 { 1241 return false 1242 } 1243 ptr, ok := fn.Type.Params.List[0].Type.(*ast.StarExpr) 1244 if !ok { 1245 return false 1246 } 1247 // We can't easily check that the type is *testing.M 1248 // because we don't know how testing has been imported, 1249 // but at least check that it's *M or *something.M. 1250 // Same applies for B and T. 1251 if name, ok := ptr.X.(*ast.Ident); ok && name.Name == arg { 1252 return true 1253 } 1254 if sel, ok := ptr.X.(*ast.SelectorExpr); ok && sel.Sel.Name == arg { 1255 return true 1256 } 1257 return false 1258 } 1259 1260 // isTest tells whether name looks like a test (or benchmark, according to prefix). 1261 // It is a Test (say) if there is a character after Test that is not a lower-case letter. 1262 // We don't want TesticularCancer. 1263 func isTest(name, prefix string) bool { 1264 if !strings.HasPrefix(name, prefix) { 1265 return false 1266 } 1267 if len(name) == len(prefix) { // "Test" is ok 1268 return true 1269 } 1270 rune, _ := utf8.DecodeRuneInString(name[len(prefix):]) 1271 return !unicode.IsLower(rune) 1272 } 1273 1274 type coverInfo struct { 1275 Package *Package 1276 Vars map[string]*CoverVar 1277 } 1278 1279 // loadTestFuncs returns the testFuncs describing the tests that will be run. 1280 func loadTestFuncs(ptest *Package) (*testFuncs, error) { 1281 t := &testFuncs{ 1282 Package: ptest, 1283 } 1284 for _, file := range ptest.TestGoFiles { 1285 if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.ImportTest, &t.NeedTest); err != nil { 1286 return nil, err 1287 } 1288 } 1289 for _, file := range ptest.XTestGoFiles { 1290 if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.ImportXtest, &t.NeedXtest); err != nil { 1291 return nil, err 1292 } 1293 } 1294 return t, nil 1295 } 1296 1297 // writeTestmain writes the _testmain.go file for t to the file named out. 1298 func writeTestmain(out string, t *testFuncs) error { 1299 f, err := os.Create(out) 1300 if err != nil { 1301 return err 1302 } 1303 defer f.Close() 1304 1305 if err := testmainTmpl.Execute(f, t); err != nil { 1306 return err 1307 } 1308 1309 return nil 1310 } 1311 1312 type testFuncs struct { 1313 Tests []testFunc 1314 Benchmarks []testFunc 1315 Examples []testFunc 1316 TestMain *testFunc 1317 Package *Package 1318 ImportTest bool 1319 NeedTest bool 1320 ImportXtest bool 1321 NeedXtest bool 1322 NeedCgo bool 1323 Cover []coverInfo 1324 } 1325 1326 func (t *testFuncs) CoverMode() string { 1327 return testCoverMode 1328 } 1329 1330 func (t *testFuncs) CoverEnabled() bool { 1331 return testCover 1332 } 1333 1334 // Covered returns a string describing which packages are being tested for coverage. 1335 // If the covered package is the same as the tested package, it returns the empty string. 1336 // Otherwise it is a comma-separated human-readable list of packages beginning with 1337 // " in", ready for use in the coverage message. 1338 func (t *testFuncs) Covered() string { 1339 if testCoverPaths == nil { 1340 return "" 1341 } 1342 return " in " + strings.Join(testCoverPaths, ", ") 1343 } 1344 1345 // Tested returns the name of the package being tested. 1346 func (t *testFuncs) Tested() string { 1347 return t.Package.Name 1348 } 1349 1350 type testFunc struct { 1351 Package string // imported package name (_test or _xtest) 1352 Name string // function name 1353 Output string // output, for examples 1354 Unordered bool // output is allowed to be unordered. 1355 } 1356 1357 var testFileSet = token.NewFileSet() 1358 1359 func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error { 1360 f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments) 1361 if err != nil { 1362 return expandScanner(err) 1363 } 1364 for _, d := range f.Decls { 1365 n, ok := d.(*ast.FuncDecl) 1366 if !ok { 1367 continue 1368 } 1369 if n.Recv != nil { 1370 continue 1371 } 1372 name := n.Name.String() 1373 switch { 1374 case name == "TestMain" && isTestFunc(n, "M"): 1375 if t.TestMain != nil { 1376 return errors.New("multiple definitions of TestMain") 1377 } 1378 t.TestMain = &testFunc{pkg, name, "", false} 1379 *doImport, *seen = true, true 1380 case isTest(name, "Test"): 1381 err := checkTestFunc(n, "T") 1382 if err != nil { 1383 return err 1384 } 1385 t.Tests = append(t.Tests, testFunc{pkg, name, "", false}) 1386 *doImport, *seen = true, true 1387 case isTest(name, "Benchmark"): 1388 err := checkTestFunc(n, "B") 1389 if err != nil { 1390 return err 1391 } 1392 t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, "", false}) 1393 *doImport, *seen = true, true 1394 } 1395 } 1396 ex := doc.Examples(f) 1397 sort.Sort(byOrder(ex)) 1398 for _, e := range ex { 1399 *doImport = true // import test file whether executed or not 1400 if e.Output == "" && !e.EmptyOutput { 1401 // Don't run examples with no output. 1402 continue 1403 } 1404 t.Examples = append(t.Examples, testFunc{pkg, "Example" + e.Name, e.Output, e.Unordered}) 1405 *seen = true 1406 } 1407 return nil 1408 } 1409 1410 func checkTestFunc(fn *ast.FuncDecl, arg string) error { 1411 if !isTestFunc(fn, arg) { 1412 name := fn.Name.String() 1413 pos := testFileSet.Position(fn.Pos()) 1414 return fmt.Errorf("%s: wrong signature for %s, must be: func %s(%s *testing.%s)", pos, name, name, strings.ToLower(arg), arg) 1415 } 1416 return nil 1417 } 1418 1419 type byOrder []*doc.Example 1420 1421 func (x byOrder) Len() int { return len(x) } 1422 func (x byOrder) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 1423 func (x byOrder) Less(i, j int) bool { return x[i].Order < x[j].Order } 1424 1425 var testmainTmpl = template.Must(template.New("main").Parse(` 1426 package main 1427 1428 import ( 1429 {{if not .TestMain}} 1430 "os" 1431 {{end}} 1432 "regexp" 1433 "testing" 1434 1435 {{if .ImportTest}} 1436 {{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}} 1437 {{end}} 1438 {{if .ImportXtest}} 1439 {{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}} 1440 {{end}} 1441 {{range $i, $p := .Cover}} 1442 _cover{{$i}} {{$p.Package.ImportPath | printf "%q"}} 1443 {{end}} 1444 1445 {{if .NeedCgo}} 1446 _ "runtime/cgo" 1447 {{end}} 1448 ) 1449 1450 var tests = []testing.InternalTest{ 1451 {{range .Tests}} 1452 {"{{.Name}}", {{.Package}}.{{.Name}}}, 1453 {{end}} 1454 } 1455 1456 var benchmarks = []testing.InternalBenchmark{ 1457 {{range .Benchmarks}} 1458 {"{{.Name}}", {{.Package}}.{{.Name}}}, 1459 {{end}} 1460 } 1461 1462 var examples = []testing.InternalExample{ 1463 {{range .Examples}} 1464 {"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}}, 1465 {{end}} 1466 } 1467 1468 var matchPat string 1469 var matchRe *regexp.Regexp 1470 1471 func matchString(pat, str string) (result bool, err error) { 1472 if matchRe == nil || matchPat != pat { 1473 matchPat = pat 1474 matchRe, err = regexp.Compile(matchPat) 1475 if err != nil { 1476 return 1477 } 1478 } 1479 return matchRe.MatchString(str), nil 1480 } 1481 1482 {{if .CoverEnabled}} 1483 1484 // Only updated by init functions, so no need for atomicity. 1485 var ( 1486 coverCounters = make(map[string][]uint32) 1487 coverBlocks = make(map[string][]testing.CoverBlock) 1488 ) 1489 1490 func init() { 1491 {{range $i, $p := .Cover}} 1492 {{range $file, $cover := $p.Vars}} 1493 coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:]) 1494 {{end}} 1495 {{end}} 1496 } 1497 1498 func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) { 1499 if 3*len(counter) != len(pos) || len(counter) != len(numStmts) { 1500 panic("coverage: mismatched sizes") 1501 } 1502 if coverCounters[fileName] != nil { 1503 // Already registered. 1504 return 1505 } 1506 coverCounters[fileName] = counter 1507 block := make([]testing.CoverBlock, len(counter)) 1508 for i := range counter { 1509 block[i] = testing.CoverBlock{ 1510 Line0: pos[3*i+0], 1511 Col0: uint16(pos[3*i+2]), 1512 Line1: pos[3*i+1], 1513 Col1: uint16(pos[3*i+2]>>16), 1514 Stmts: numStmts[i], 1515 } 1516 } 1517 coverBlocks[fileName] = block 1518 } 1519 {{end}} 1520 1521 func main() { 1522 {{if .CoverEnabled}} 1523 testing.RegisterCover(testing.Cover{ 1524 Mode: {{printf "%q" .CoverMode}}, 1525 Counters: coverCounters, 1526 Blocks: coverBlocks, 1527 CoveredPackages: {{printf "%q" .Covered}}, 1528 }) 1529 {{end}} 1530 m := testing.MainStart(matchString, tests, benchmarks, examples) 1531 {{with .TestMain}} 1532 {{.Package}}.{{.Name}}(m) 1533 {{else}} 1534 os.Exit(m.Run()) 1535 {{end}} 1536 } 1537 1538 `))