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