github.com/activestate/go@v0.0.0-20170614201249-0b81c023a722/test/run.go (about) 1 // skip 2 3 // Copyright 2012 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Run runs tests in the test directory. 8 // 9 // TODO(bradfitz): docs of some sort, once we figure out how we're changing 10 // headers of files 11 package main 12 13 import ( 14 "bytes" 15 "errors" 16 "flag" 17 "fmt" 18 "hash/fnv" 19 "io" 20 "io/ioutil" 21 "log" 22 "os" 23 "os/exec" 24 "path" 25 "path/filepath" 26 "regexp" 27 "runtime" 28 "sort" 29 "strconv" 30 "strings" 31 "time" 32 "unicode" 33 ) 34 35 var ( 36 verbose = flag.Bool("v", false, "verbose. if set, parallelism is set to 1.") 37 keep = flag.Bool("k", false, "keep. keep temporary directory.") 38 numParallel = flag.Int("n", runtime.NumCPU(), "number of parallel tests to run") 39 summary = flag.Bool("summary", false, "show summary of results") 40 showSkips = flag.Bool("show_skips", false, "show skipped tests") 41 runSkips = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)") 42 linkshared = flag.Bool("linkshared", false, "") 43 updateErrors = flag.Bool("update_errors", false, "update error messages in test file based on compiler output") 44 runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run") 45 46 shard = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.") 47 shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.") 48 ) 49 50 var ( 51 goos, goarch string 52 53 // dirs are the directories to look for *.go files in. 54 // TODO(bradfitz): just use all directories? 55 dirs = []string{".", "ken", "chan", "interface", "syntax", "dwarf", "fixedbugs"} 56 57 // ratec controls the max number of tests running at a time. 58 ratec chan bool 59 60 // toRun is the channel of tests to run. 61 // It is nil until the first test is started. 62 toRun chan *test 63 64 // rungatec controls the max number of runoutput tests 65 // executed in parallel as they can each consume a lot of memory. 66 rungatec chan bool 67 ) 68 69 // maxTests is an upper bound on the total number of tests. 70 // It is used as a channel buffer size to make sure sends don't block. 71 const maxTests = 5000 72 73 func main() { 74 flag.Parse() 75 76 goos = getenv("GOOS", runtime.GOOS) 77 goarch = getenv("GOARCH", runtime.GOARCH) 78 79 findExecCmd() 80 81 // Disable parallelism if printing or if using a simulator. 82 if *verbose || len(findExecCmd()) > 0 { 83 *numParallel = 1 84 } 85 86 ratec = make(chan bool, *numParallel) 87 rungatec = make(chan bool, *runoutputLimit) 88 89 var tests []*test 90 if flag.NArg() > 0 { 91 for _, arg := range flag.Args() { 92 if arg == "-" || arg == "--" { 93 // Permit running: 94 // $ go run run.go - env.go 95 // $ go run run.go -- env.go 96 // $ go run run.go - ./fixedbugs 97 // $ go run run.go -- ./fixedbugs 98 continue 99 } 100 if fi, err := os.Stat(arg); err == nil && fi.IsDir() { 101 for _, baseGoFile := range goFiles(arg) { 102 tests = append(tests, startTest(arg, baseGoFile)) 103 } 104 } else if strings.HasSuffix(arg, ".go") { 105 dir, file := filepath.Split(arg) 106 tests = append(tests, startTest(dir, file)) 107 } else { 108 log.Fatalf("can't yet deal with non-directory and non-go file %q", arg) 109 } 110 } 111 } else { 112 for _, dir := range dirs { 113 for _, baseGoFile := range goFiles(dir) { 114 tests = append(tests, startTest(dir, baseGoFile)) 115 } 116 } 117 } 118 119 failed := false 120 resCount := map[string]int{} 121 for _, test := range tests { 122 <-test.donec 123 status := "ok " 124 errStr := "" 125 if e, isSkip := test.err.(skipError); isSkip { 126 test.err = nil 127 errStr = "unexpected skip for " + path.Join(test.dir, test.gofile) + ": " + string(e) 128 status = "FAIL" 129 } 130 if test.err != nil { 131 status = "FAIL" 132 errStr = test.err.Error() 133 } 134 if status == "FAIL" { 135 failed = true 136 } 137 resCount[status]++ 138 dt := fmt.Sprintf("%.3fs", test.dt.Seconds()) 139 if status == "FAIL" { 140 fmt.Printf("# go run run.go -- %s\n%s\nFAIL\t%s\t%s\n", 141 path.Join(test.dir, test.gofile), 142 errStr, test.goFileName(), dt) 143 continue 144 } 145 if !*verbose { 146 continue 147 } 148 fmt.Printf("%s\t%s\t%s\n", status, test.goFileName(), dt) 149 } 150 151 if *summary { 152 for k, v := range resCount { 153 fmt.Printf("%5d %s\n", v, k) 154 } 155 } 156 157 if failed { 158 os.Exit(1) 159 } 160 } 161 162 func toolPath(name string) string { 163 p := filepath.Join(os.Getenv("GOROOT"), "bin", "tool", name) 164 if _, err := os.Stat(p); err != nil { 165 log.Fatalf("didn't find binary at %s", p) 166 } 167 return p 168 } 169 170 func shardMatch(name string) bool { 171 if *shards == 0 { 172 return true 173 } 174 h := fnv.New32() 175 io.WriteString(h, name) 176 return int(h.Sum32()%uint32(*shards)) == *shard 177 } 178 179 func goFiles(dir string) []string { 180 f, err := os.Open(dir) 181 check(err) 182 dirnames, err := f.Readdirnames(-1) 183 check(err) 184 names := []string{} 185 for _, name := range dirnames { 186 if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) { 187 names = append(names, name) 188 } 189 } 190 sort.Strings(names) 191 return names 192 } 193 194 type runCmd func(...string) ([]byte, error) 195 196 func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) { 197 cmd := []string{"go", "tool", "compile", "-e"} 198 cmd = append(cmd, flags...) 199 if *linkshared { 200 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink") 201 } 202 cmd = append(cmd, longname) 203 return runcmd(cmd...) 204 } 205 206 func compileInDir(runcmd runCmd, dir string, flags []string, names ...string) (out []byte, err error) { 207 cmd := []string{"go", "tool", "compile", "-e", "-D", ".", "-I", "."} 208 cmd = append(cmd, flags...) 209 if *linkshared { 210 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink") 211 } 212 for _, name := range names { 213 cmd = append(cmd, filepath.Join(dir, name)) 214 } 215 return runcmd(cmd...) 216 } 217 218 func linkFile(runcmd runCmd, goname string) (err error) { 219 pfile := strings.Replace(goname, ".go", ".o", -1) 220 cmd := []string{"go", "tool", "link", "-w", "-o", "a.exe", "-L", "."} 221 if *linkshared { 222 cmd = append(cmd, "-linkshared", "-installsuffix=dynlink") 223 } 224 cmd = append(cmd, pfile) 225 _, err = runcmd(cmd...) 226 return 227 } 228 229 // skipError describes why a test was skipped. 230 type skipError string 231 232 func (s skipError) Error() string { return string(s) } 233 234 func check(err error) { 235 if err != nil { 236 log.Fatal(err) 237 } 238 } 239 240 // test holds the state of a test. 241 type test struct { 242 dir, gofile string 243 donec chan bool // closed when done 244 dt time.Duration 245 246 src string 247 248 tempDir string 249 err error 250 } 251 252 // startTest 253 func startTest(dir, gofile string) *test { 254 t := &test{ 255 dir: dir, 256 gofile: gofile, 257 donec: make(chan bool, 1), 258 } 259 if toRun == nil { 260 toRun = make(chan *test, maxTests) 261 go runTests() 262 } 263 select { 264 case toRun <- t: 265 default: 266 panic("toRun buffer size (maxTests) is too small") 267 } 268 return t 269 } 270 271 // runTests runs tests in parallel, but respecting the order they 272 // were enqueued on the toRun channel. 273 func runTests() { 274 for { 275 ratec <- true 276 t := <-toRun 277 go func() { 278 t.run() 279 <-ratec 280 }() 281 } 282 } 283 284 var cwd, _ = os.Getwd() 285 286 func (t *test) goFileName() string { 287 return filepath.Join(t.dir, t.gofile) 288 } 289 290 func (t *test) goDirName() string { 291 return filepath.Join(t.dir, strings.Replace(t.gofile, ".go", ".dir", -1)) 292 } 293 294 func goDirFiles(longdir string) (filter []os.FileInfo, err error) { 295 files, dirErr := ioutil.ReadDir(longdir) 296 if dirErr != nil { 297 return nil, dirErr 298 } 299 for _, gofile := range files { 300 if filepath.Ext(gofile.Name()) == ".go" { 301 filter = append(filter, gofile) 302 } 303 } 304 return 305 } 306 307 var packageRE = regexp.MustCompile(`(?m)^package (\w+)`) 308 309 // If singlefilepkgs is set, each file is considered a separate package 310 // even if the package names are the same. 311 func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) { 312 files, err := goDirFiles(longdir) 313 if err != nil { 314 return nil, err 315 } 316 var pkgs [][]string 317 m := make(map[string]int) 318 for _, file := range files { 319 name := file.Name() 320 data, err := ioutil.ReadFile(filepath.Join(longdir, name)) 321 if err != nil { 322 return nil, err 323 } 324 pkgname := packageRE.FindStringSubmatch(string(data)) 325 if pkgname == nil { 326 return nil, fmt.Errorf("cannot find package name in %s", name) 327 } 328 i, ok := m[pkgname[1]] 329 if singlefilepkgs || !ok { 330 i = len(pkgs) 331 pkgs = append(pkgs, nil) 332 m[pkgname[1]] = i 333 } 334 pkgs[i] = append(pkgs[i], name) 335 } 336 return pkgs, nil 337 } 338 339 type context struct { 340 GOOS string 341 GOARCH string 342 } 343 344 // shouldTest looks for build tags in a source file and returns 345 // whether the file should be used according to the tags. 346 func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) { 347 if *runSkips { 348 return true, "" 349 } 350 for _, line := range strings.Split(src, "\n") { 351 line = strings.TrimSpace(line) 352 if strings.HasPrefix(line, "//") { 353 line = line[2:] 354 } else { 355 continue 356 } 357 line = strings.TrimSpace(line) 358 if len(line) == 0 || line[0] != '+' { 359 continue 360 } 361 ctxt := &context{ 362 GOOS: goos, 363 GOARCH: goarch, 364 } 365 words := strings.Fields(line) 366 if words[0] == "+build" { 367 ok := false 368 for _, word := range words[1:] { 369 if ctxt.match(word) { 370 ok = true 371 break 372 } 373 } 374 if !ok { 375 // no matching tag found. 376 return false, line 377 } 378 } 379 } 380 // no build tags 381 return true, "" 382 } 383 384 func (ctxt *context) match(name string) bool { 385 if name == "" { 386 return false 387 } 388 if i := strings.Index(name, ","); i >= 0 { 389 // comma-separated list 390 return ctxt.match(name[:i]) && ctxt.match(name[i+1:]) 391 } 392 if strings.HasPrefix(name, "!!") { // bad syntax, reject always 393 return false 394 } 395 if strings.HasPrefix(name, "!") { // negation 396 return len(name) > 1 && !ctxt.match(name[1:]) 397 } 398 399 // Tags must be letters, digits, underscores or dots. 400 // Unlike in Go identifiers, all digits are fine (e.g., "386"). 401 for _, c := range name { 402 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' { 403 return false 404 } 405 } 406 407 if name == ctxt.GOOS || name == ctxt.GOARCH { 408 return true 409 } 410 411 if name == "test_run" { 412 return true 413 } 414 415 return false 416 } 417 418 func init() { checkShouldTest() } 419 420 // run runs a test. 421 func (t *test) run() { 422 start := time.Now() 423 defer func() { 424 t.dt = time.Since(start) 425 close(t.donec) 426 }() 427 428 srcBytes, err := ioutil.ReadFile(t.goFileName()) 429 if err != nil { 430 t.err = err 431 return 432 } 433 t.src = string(srcBytes) 434 if t.src[0] == '\n' { 435 t.err = skipError("starts with newline") 436 return 437 } 438 439 // Execution recipe stops at first blank line. 440 pos := strings.Index(t.src, "\n\n") 441 if pos == -1 { 442 t.err = errors.New("double newline not found") 443 return 444 } 445 action := t.src[:pos] 446 if nl := strings.Index(action, "\n"); nl >= 0 && strings.Contains(action[:nl], "+build") { 447 // skip first line 448 action = action[nl+1:] 449 } 450 if strings.HasPrefix(action, "//") { 451 action = action[2:] 452 } 453 454 // Check for build constraints only up to the actual code. 455 pkgPos := strings.Index(t.src, "\npackage") 456 if pkgPos == -1 { 457 pkgPos = pos // some files are intentionally malformed 458 } 459 if ok, why := shouldTest(t.src[:pkgPos], goos, goarch); !ok { 460 if *showSkips { 461 fmt.Printf("%-20s %-20s: %s\n", "skip", t.goFileName(), why) 462 } 463 return 464 } 465 466 var args, flags []string 467 var tim int 468 wantError := false 469 wantAuto := false 470 singlefilepkgs := false 471 f := strings.Fields(action) 472 if len(f) > 0 { 473 action = f[0] 474 args = f[1:] 475 } 476 477 // TODO: Clean up/simplify this switch statement. 478 switch action { 479 case "rundircmpout": 480 action = "rundir" 481 case "cmpout": 482 action = "run" // the run case already looks for <dir>/<test>.out files 483 case "compile", "compiledir", "build", "run", "buildrun", "runoutput", "rundir": 484 // nothing to do 485 case "errorcheckandrundir": 486 wantError = false // should be no error if also will run 487 case "errorcheckwithauto": 488 action = "errorcheck" 489 wantAuto = true 490 wantError = true 491 case "errorcheck", "errorcheckdir", "errorcheckoutput": 492 wantError = true 493 case "skip": 494 if *runSkips { 495 break 496 } 497 return 498 default: 499 t.err = skipError("skipped; unknown pattern: " + action) 500 return 501 } 502 503 // collect flags 504 for len(args) > 0 && strings.HasPrefix(args[0], "-") { 505 switch args[0] { 506 case "-0": 507 wantError = false 508 case "-s": 509 singlefilepkgs = true 510 case "-t": // timeout in seconds 511 args = args[1:] 512 var err error 513 tim, err = strconv.Atoi(args[0]) 514 if err != nil { 515 t.err = fmt.Errorf("need number of seconds for -t timeout, got %s instead", args[0]) 516 } 517 518 default: 519 flags = append(flags, args[0]) 520 } 521 args = args[1:] 522 } 523 524 t.makeTempDir() 525 if !*keep { 526 defer os.RemoveAll(t.tempDir) 527 } 528 529 err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644) 530 check(err) 531 532 // A few tests (of things like the environment) require these to be set. 533 if os.Getenv("GOOS") == "" { 534 os.Setenv("GOOS", runtime.GOOS) 535 } 536 if os.Getenv("GOARCH") == "" { 537 os.Setenv("GOARCH", runtime.GOARCH) 538 } 539 540 useTmp := true 541 runcmd := func(args ...string) ([]byte, error) { 542 cmd := exec.Command(args[0], args[1:]...) 543 var buf bytes.Buffer 544 cmd.Stdout = &buf 545 cmd.Stderr = &buf 546 if useTmp { 547 cmd.Dir = t.tempDir 548 cmd.Env = envForDir(cmd.Dir) 549 } else { 550 cmd.Env = os.Environ() 551 } 552 553 var err error 554 555 if tim != 0 { 556 err = cmd.Start() 557 // This command-timeout code adapted from cmd/go/test.go 558 if err == nil { 559 tick := time.NewTimer(time.Duration(tim) * time.Second) 560 done := make(chan error) 561 go func() { 562 done <- cmd.Wait() 563 }() 564 select { 565 case err = <-done: 566 // ok 567 case <-tick.C: 568 cmd.Process.Kill() 569 err = <-done 570 // err = errors.New("Test timeout") 571 } 572 tick.Stop() 573 } 574 } else { 575 err = cmd.Run() 576 } 577 if err != nil { 578 err = fmt.Errorf("%s\n%s", err, buf.Bytes()) 579 } 580 return buf.Bytes(), err 581 } 582 583 long := filepath.Join(cwd, t.goFileName()) 584 switch action { 585 default: 586 t.err = fmt.Errorf("unimplemented action %q", action) 587 588 case "errorcheck": 589 // TODO(gri) remove need for -C (disable printing of columns in error messages) 590 cmdline := []string{"go", "tool", "compile", "-C", "-e", "-o", "a.o"} 591 // No need to add -dynlink even if linkshared if we're just checking for errors... 592 cmdline = append(cmdline, flags...) 593 cmdline = append(cmdline, long) 594 out, err := runcmd(cmdline...) 595 if wantError { 596 if err == nil { 597 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 598 return 599 } 600 } else { 601 if err != nil { 602 t.err = err 603 return 604 } 605 } 606 if *updateErrors { 607 t.updateErrors(string(out), long) 608 } 609 t.err = t.errorCheck(string(out), wantAuto, long, t.gofile) 610 return 611 612 case "compile": 613 _, t.err = compileFile(runcmd, long, flags) 614 615 case "compiledir": 616 // Compile all files in the directory in lexicographic order. 617 longdir := filepath.Join(cwd, t.goDirName()) 618 pkgs, err := goDirPackages(longdir, singlefilepkgs) 619 if err != nil { 620 t.err = err 621 return 622 } 623 for _, gofiles := range pkgs { 624 _, t.err = compileInDir(runcmd, longdir, flags, gofiles...) 625 if t.err != nil { 626 return 627 } 628 } 629 630 case "errorcheckdir", "errorcheckandrundir": 631 // errorcheck all files in lexicographic order 632 // useful for finding importing errors 633 longdir := filepath.Join(cwd, t.goDirName()) 634 pkgs, err := goDirPackages(longdir, singlefilepkgs) 635 if err != nil { 636 t.err = err 637 return 638 } 639 for i, gofiles := range pkgs { 640 out, err := compileInDir(runcmd, longdir, flags, gofiles...) 641 if i == len(pkgs)-1 { 642 if wantError && err == nil { 643 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 644 return 645 } else if !wantError && err != nil { 646 t.err = err 647 return 648 } 649 } else if err != nil { 650 t.err = err 651 return 652 } 653 var fullshort []string 654 for _, name := range gofiles { 655 fullshort = append(fullshort, filepath.Join(longdir, name), name) 656 } 657 t.err = t.errorCheck(string(out), wantAuto, fullshort...) 658 if t.err != nil { 659 break 660 } 661 } 662 if action == "errorcheckdir" { 663 return 664 } 665 fallthrough 666 667 case "rundir": 668 // Compile all files in the directory in lexicographic order. 669 // then link as if the last file is the main package and run it 670 longdir := filepath.Join(cwd, t.goDirName()) 671 pkgs, err := goDirPackages(longdir, singlefilepkgs) 672 if err != nil { 673 t.err = err 674 return 675 } 676 for i, gofiles := range pkgs { 677 _, err := compileInDir(runcmd, longdir, flags, gofiles...) 678 if err != nil { 679 t.err = err 680 return 681 } 682 if i == len(pkgs)-1 { 683 err = linkFile(runcmd, gofiles[0]) 684 if err != nil { 685 t.err = err 686 return 687 } 688 var cmd []string 689 cmd = append(cmd, findExecCmd()...) 690 cmd = append(cmd, filepath.Join(t.tempDir, "a.exe")) 691 cmd = append(cmd, args...) 692 out, err := runcmd(cmd...) 693 if err != nil { 694 t.err = err 695 return 696 } 697 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 698 t.err = fmt.Errorf("incorrect output\n%s", out) 699 } 700 } 701 } 702 703 case "build": 704 _, err := runcmd("go", "build", "-o", "a.exe", long) 705 if err != nil { 706 t.err = err 707 } 708 709 case "buildrun": // build binary, then run binary, instead of go run. Useful for timeout tests where failure mode is infinite loop. 710 // TODO: not supported on NaCl 711 useTmp = true 712 cmd := []string{"go", "build", "-o", "a.exe"} 713 if *linkshared { 714 cmd = append(cmd, "-linkshared") 715 } 716 longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile) 717 cmd = append(cmd, flags...) 718 cmd = append(cmd, longdirgofile) 719 out, err := runcmd(cmd...) 720 if err != nil { 721 t.err = err 722 return 723 } 724 cmd = []string{"./a.exe"} 725 out, err = runcmd(append(cmd, args...)...) 726 if err != nil { 727 t.err = err 728 return 729 } 730 731 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 732 t.err = fmt.Errorf("incorrect output\n%s", out) 733 } 734 735 case "run": 736 useTmp = false 737 cmd := []string{"go", "run"} 738 if *linkshared { 739 cmd = append(cmd, "-linkshared") 740 } 741 cmd = append(cmd, flags...) 742 cmd = append(cmd, t.goFileName()) 743 out, err := runcmd(append(cmd, args...)...) 744 if err != nil { 745 t.err = err 746 return 747 } 748 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 749 t.err = fmt.Errorf("incorrect output\n%s", out) 750 } 751 752 case "runoutput": 753 rungatec <- true 754 defer func() { 755 <-rungatec 756 }() 757 useTmp = false 758 cmd := []string{"go", "run"} 759 if *linkshared { 760 cmd = append(cmd, "-linkshared") 761 } 762 cmd = append(cmd, t.goFileName()) 763 out, err := runcmd(append(cmd, args...)...) 764 if err != nil { 765 t.err = err 766 return 767 } 768 tfile := filepath.Join(t.tempDir, "tmp__.go") 769 if err := ioutil.WriteFile(tfile, out, 0666); err != nil { 770 t.err = fmt.Errorf("write tempfile:%s", err) 771 return 772 } 773 cmd = []string{"go", "run"} 774 if *linkshared { 775 cmd = append(cmd, "-linkshared") 776 } 777 cmd = append(cmd, tfile) 778 out, err = runcmd(cmd...) 779 if err != nil { 780 t.err = err 781 return 782 } 783 if string(out) != t.expectedOutput() { 784 t.err = fmt.Errorf("incorrect output\n%s", out) 785 } 786 787 case "errorcheckoutput": 788 useTmp = false 789 cmd := []string{"go", "run"} 790 if *linkshared { 791 cmd = append(cmd, "-linkshared") 792 } 793 cmd = append(cmd, t.goFileName()) 794 out, err := runcmd(append(cmd, args...)...) 795 if err != nil { 796 t.err = err 797 return 798 } 799 tfile := filepath.Join(t.tempDir, "tmp__.go") 800 err = ioutil.WriteFile(tfile, out, 0666) 801 if err != nil { 802 t.err = fmt.Errorf("write tempfile:%s", err) 803 return 804 } 805 cmdline := []string{"go", "tool", "compile", "-e", "-o", "a.o"} 806 cmdline = append(cmdline, flags...) 807 cmdline = append(cmdline, tfile) 808 out, err = runcmd(cmdline...) 809 if wantError { 810 if err == nil { 811 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 812 return 813 } 814 } else { 815 if err != nil { 816 t.err = err 817 return 818 } 819 } 820 t.err = t.errorCheck(string(out), false, tfile, "tmp__.go") 821 return 822 } 823 } 824 825 var execCmd []string 826 827 func findExecCmd() []string { 828 if execCmd != nil { 829 return execCmd 830 } 831 execCmd = []string{} // avoid work the second time 832 if goos == runtime.GOOS && goarch == runtime.GOARCH { 833 return execCmd 834 } 835 path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)) 836 if err == nil { 837 execCmd = []string{path} 838 } 839 return execCmd 840 } 841 842 func (t *test) String() string { 843 return filepath.Join(t.dir, t.gofile) 844 } 845 846 func (t *test) makeTempDir() { 847 var err error 848 t.tempDir, err = ioutil.TempDir("", "") 849 check(err) 850 if *keep { 851 log.Printf("Temporary directory is %s", t.tempDir) 852 } 853 } 854 855 func (t *test) expectedOutput() string { 856 filename := filepath.Join(t.dir, t.gofile) 857 filename = filename[:len(filename)-len(".go")] 858 filename += ".out" 859 b, _ := ioutil.ReadFile(filename) 860 return string(b) 861 } 862 863 func splitOutput(out string, wantAuto bool) []string { 864 // gc error messages continue onto additional lines with leading tabs. 865 // Split the output at the beginning of each line that doesn't begin with a tab. 866 // <autogenerated> lines are impossible to match so those are filtered out. 867 var res []string 868 for _, line := range strings.Split(out, "\n") { 869 if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows 870 line = line[:len(line)-1] 871 } 872 if strings.HasPrefix(line, "\t") { 873 res[len(res)-1] += "\n" + line 874 } else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") { 875 continue 876 } else if strings.TrimSpace(line) != "" { 877 res = append(res, line) 878 } 879 } 880 return res 881 } 882 883 func (t *test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) { 884 defer func() { 885 if *verbose && err != nil { 886 log.Printf("%s gc output:\n%s", t, outStr) 887 } 888 }() 889 var errs []error 890 out := splitOutput(outStr, wantAuto) 891 892 // Cut directory name. 893 for i := range out { 894 for j := 0; j < len(fullshort); j += 2 { 895 full, short := fullshort[j], fullshort[j+1] 896 out[i] = strings.Replace(out[i], full, short, -1) 897 } 898 } 899 900 var want []wantedError 901 for j := 0; j < len(fullshort); j += 2 { 902 full, short := fullshort[j], fullshort[j+1] 903 want = append(want, t.wantedErrors(full, short)...) 904 } 905 906 for _, we := range want { 907 var errmsgs []string 908 if we.auto { 909 errmsgs, out = partitionStrings("<autogenerated>", out) 910 } else { 911 errmsgs, out = partitionStrings(we.prefix, out) 912 } 913 if len(errmsgs) == 0 { 914 errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr)) 915 continue 916 } 917 matched := false 918 n := len(out) 919 for _, errmsg := range errmsgs { 920 // Assume errmsg says "file:line: foo". 921 // Cut leading "file:line: " to avoid accidental matching of file name instead of message. 922 text := errmsg 923 if i := strings.Index(text, " "); i >= 0 { 924 text = text[i+1:] 925 } 926 if we.re.MatchString(text) { 927 matched = true 928 } else { 929 out = append(out, errmsg) 930 } 931 } 932 if !matched { 933 errs = append(errs, fmt.Errorf("%s:%d: no match for %#q in:\n\t%s", we.file, we.lineNum, we.reStr, strings.Join(out[n:], "\n\t"))) 934 continue 935 } 936 } 937 938 if len(out) > 0 { 939 errs = append(errs, fmt.Errorf("Unmatched Errors:")) 940 for _, errLine := range out { 941 errs = append(errs, fmt.Errorf("%s", errLine)) 942 } 943 } 944 945 if len(errs) == 0 { 946 return nil 947 } 948 if len(errs) == 1 { 949 return errs[0] 950 } 951 var buf bytes.Buffer 952 fmt.Fprintf(&buf, "\n") 953 for _, err := range errs { 954 fmt.Fprintf(&buf, "%s\n", err.Error()) 955 } 956 return errors.New(buf.String()) 957 } 958 959 func (t *test) updateErrors(out, file string) { 960 base := path.Base(file) 961 // Read in source file. 962 src, err := ioutil.ReadFile(file) 963 if err != nil { 964 fmt.Fprintln(os.Stderr, err) 965 return 966 } 967 lines := strings.Split(string(src), "\n") 968 // Remove old errors. 969 for i, ln := range lines { 970 pos := strings.Index(ln, " // ERROR ") 971 if pos >= 0 { 972 lines[i] = ln[:pos] 973 } 974 } 975 // Parse new errors. 976 errors := make(map[int]map[string]bool) 977 tmpRe := regexp.MustCompile(`autotmp_[0-9]+`) 978 for _, errStr := range splitOutput(out, false) { 979 colon1 := strings.Index(errStr, ":") 980 if colon1 < 0 || errStr[:colon1] != file { 981 continue 982 } 983 colon2 := strings.Index(errStr[colon1+1:], ":") 984 if colon2 < 0 { 985 continue 986 } 987 colon2 += colon1 + 1 988 line, err := strconv.Atoi(errStr[colon1+1 : colon2]) 989 line-- 990 if err != nil || line < 0 || line >= len(lines) { 991 continue 992 } 993 msg := errStr[colon2+2:] 994 msg = strings.Replace(msg, file, base, -1) // normalize file mentions in error itself 995 msg = strings.TrimLeft(msg, " \t") 996 for _, r := range []string{`\`, `*`, `+`, `[`, `]`, `(`, `)`} { 997 msg = strings.Replace(msg, r, `\`+r, -1) 998 } 999 msg = strings.Replace(msg, `"`, `.`, -1) 1000 msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`) 1001 if errors[line] == nil { 1002 errors[line] = make(map[string]bool) 1003 } 1004 errors[line][msg] = true 1005 } 1006 // Add new errors. 1007 for line, errs := range errors { 1008 var sorted []string 1009 for e := range errs { 1010 sorted = append(sorted, e) 1011 } 1012 sort.Strings(sorted) 1013 lines[line] += " // ERROR" 1014 for _, e := range sorted { 1015 lines[line] += fmt.Sprintf(` "%s$"`, e) 1016 } 1017 } 1018 // Write new file. 1019 err = ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640) 1020 if err != nil { 1021 fmt.Fprintln(os.Stderr, err) 1022 return 1023 } 1024 // Polish. 1025 exec.Command("go", "fmt", file).CombinedOutput() 1026 } 1027 1028 // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[), 1029 // That is, it needs the file name prefix followed by a : or a [, 1030 // and possibly preceded by a directory name. 1031 func matchPrefix(s, prefix string) bool { 1032 i := strings.Index(s, ":") 1033 if i < 0 { 1034 return false 1035 } 1036 j := strings.LastIndex(s[:i], "/") 1037 s = s[j+1:] 1038 if len(s) <= len(prefix) || s[:len(prefix)] != prefix { 1039 return false 1040 } 1041 switch s[len(prefix)] { 1042 case '[', ':': 1043 return true 1044 } 1045 return false 1046 } 1047 1048 func partitionStrings(prefix string, strs []string) (matched, unmatched []string) { 1049 for _, s := range strs { 1050 if matchPrefix(s, prefix) { 1051 matched = append(matched, s) 1052 } else { 1053 unmatched = append(unmatched, s) 1054 } 1055 } 1056 return 1057 } 1058 1059 type wantedError struct { 1060 reStr string 1061 re *regexp.Regexp 1062 lineNum int 1063 auto bool // match <autogenerated> line 1064 file string 1065 prefix string 1066 } 1067 1068 var ( 1069 errRx = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`) 1070 errAutoRx = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`) 1071 errQuotesRx = regexp.MustCompile(`"([^"]*)"`) 1072 lineRx = regexp.MustCompile(`LINE(([+-])([0-9]+))?`) 1073 ) 1074 1075 func (t *test) wantedErrors(file, short string) (errs []wantedError) { 1076 cache := make(map[string]*regexp.Regexp) 1077 1078 src, _ := ioutil.ReadFile(file) 1079 for i, line := range strings.Split(string(src), "\n") { 1080 lineNum := i + 1 1081 if strings.Contains(line, "////") { 1082 // double comment disables ERROR 1083 continue 1084 } 1085 var auto bool 1086 m := errAutoRx.FindStringSubmatch(line) 1087 if m != nil { 1088 auto = true 1089 } else { 1090 m = errRx.FindStringSubmatch(line) 1091 } 1092 if m == nil { 1093 continue 1094 } 1095 all := m[1] 1096 mm := errQuotesRx.FindAllStringSubmatch(all, -1) 1097 if mm == nil { 1098 log.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line) 1099 } 1100 for _, m := range mm { 1101 rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string { 1102 n := lineNum 1103 if strings.HasPrefix(m, "LINE+") { 1104 delta, _ := strconv.Atoi(m[5:]) 1105 n += delta 1106 } else if strings.HasPrefix(m, "LINE-") { 1107 delta, _ := strconv.Atoi(m[5:]) 1108 n -= delta 1109 } 1110 return fmt.Sprintf("%s:%d", short, n) 1111 }) 1112 re := cache[rx] 1113 if re == nil { 1114 var err error 1115 re, err = regexp.Compile(rx) 1116 if err != nil { 1117 log.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err) 1118 } 1119 cache[rx] = re 1120 } 1121 prefix := fmt.Sprintf("%s:%d", short, lineNum) 1122 errs = append(errs, wantedError{ 1123 reStr: rx, 1124 re: re, 1125 prefix: prefix, 1126 auto: auto, 1127 lineNum: lineNum, 1128 file: short, 1129 }) 1130 } 1131 } 1132 1133 return 1134 } 1135 1136 // defaultRunOutputLimit returns the number of runoutput tests that 1137 // can be executed in parallel. 1138 func defaultRunOutputLimit() int { 1139 const maxArmCPU = 2 1140 1141 cpu := runtime.NumCPU() 1142 if runtime.GOARCH == "arm" && cpu > maxArmCPU { 1143 cpu = maxArmCPU 1144 } 1145 return cpu 1146 } 1147 1148 // checkShouldTest runs sanity checks on the shouldTest function. 1149 func checkShouldTest() { 1150 assert := func(ok bool, _ string) { 1151 if !ok { 1152 panic("fail") 1153 } 1154 } 1155 assertNot := func(ok bool, _ string) { assert(!ok, "") } 1156 1157 // Simple tests. 1158 assert(shouldTest("// +build linux", "linux", "arm")) 1159 assert(shouldTest("// +build !windows", "linux", "arm")) 1160 assertNot(shouldTest("// +build !windows", "windows", "amd64")) 1161 1162 // A file with no build tags will always be tested. 1163 assert(shouldTest("// This is a test.", "os", "arch")) 1164 1165 // Build tags separated by a space are OR-ed together. 1166 assertNot(shouldTest("// +build arm 386", "linux", "amd64")) 1167 1168 // Build tags separated by a comma are AND-ed together. 1169 assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64")) 1170 assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386")) 1171 1172 // Build tags on multiple lines are AND-ed together. 1173 assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64")) 1174 assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64")) 1175 1176 // Test that (!a OR !b) matches anything. 1177 assert(shouldTest("// +build !windows !plan9", "windows", "amd64")) 1178 } 1179 1180 // envForDir returns a copy of the environment 1181 // suitable for running in the given directory. 1182 // The environment is the current process's environment 1183 // but with an updated $PWD, so that an os.Getwd in the 1184 // child will be faster. 1185 func envForDir(dir string) []string { 1186 env := os.Environ() 1187 for i, kv := range env { 1188 if strings.HasPrefix(kv, "PWD=") { 1189 env[i] = "PWD=" + dir 1190 return env 1191 } 1192 } 1193 env = append(env, "PWD="+dir) 1194 return env 1195 } 1196 1197 func getenv(key, def string) string { 1198 value := os.Getenv(key) 1199 if value != "" { 1200 return value 1201 } 1202 return def 1203 }