github.com/euank/go@v0.0.0-20160829210321-495514729181/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", "bugs"} 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) (out []byte, err error) { 197 cmd := []string{"go", "tool", "compile", "-e"} 198 if *linkshared { 199 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink") 200 } 201 cmd = append(cmd, longname) 202 return runcmd(cmd...) 203 } 204 205 func compileInDir(runcmd runCmd, dir string, flags []string, names ...string) (out []byte, err error) { 206 cmd := []string{"go", "tool", "compile", "-e", "-D", ".", "-I", "."} 207 cmd = append(cmd, flags...) 208 if *linkshared { 209 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink") 210 } 211 for _, name := range names { 212 cmd = append(cmd, filepath.Join(dir, name)) 213 } 214 return runcmd(cmd...) 215 } 216 217 func linkFile(runcmd runCmd, goname string) (err error) { 218 pfile := strings.Replace(goname, ".go", ".o", -1) 219 cmd := []string{"go", "tool", "link", "-w", "-o", "a.exe", "-L", "."} 220 if *linkshared { 221 cmd = append(cmd, "-linkshared", "-installsuffix=dynlink") 222 } 223 cmd = append(cmd, pfile) 224 _, err = runcmd(cmd...) 225 return 226 } 227 228 // skipError describes why a test was skipped. 229 type skipError string 230 231 func (s skipError) Error() string { return string(s) } 232 233 func check(err error) { 234 if err != nil { 235 log.Fatal(err) 236 } 237 } 238 239 // test holds the state of a test. 240 type test struct { 241 dir, gofile string 242 donec chan bool // closed when done 243 dt time.Duration 244 245 src string 246 action string // "compile", "build", etc. 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 t.action = "skip" 461 if *showSkips { 462 fmt.Printf("%-20s %-20s: %s\n", t.action, t.goFileName(), why) 463 } 464 return 465 } 466 467 var args, flags []string 468 wantError := false 469 singlefilepkgs := false 470 f := strings.Fields(action) 471 if len(f) > 0 { 472 action = f[0] 473 args = f[1:] 474 } 475 476 // TODO: Clean up/simplify this switch statement. 477 switch action { 478 case "rundircmpout": 479 action = "rundir" 480 t.action = "rundir" 481 case "cmpout": 482 action = "run" // the run case already looks for <dir>/<test>.out files 483 fallthrough 484 case "compile", "compiledir", "build", "run", "runoutput", "rundir": 485 t.action = action 486 case "errorcheckandrundir": 487 wantError = false // should be no error if also will run 488 fallthrough 489 case "errorcheck", "errorcheckdir", "errorcheckoutput": 490 t.action = action 491 wantError = true 492 case "skip": 493 if *runSkips { 494 break 495 } 496 t.action = "skip" 497 return 498 default: 499 t.err = skipError("skipped; unknown pattern: " + action) 500 t.action = "??" 501 return 502 } 503 504 // collect flags 505 for len(args) > 0 && strings.HasPrefix(args[0], "-") { 506 switch args[0] { 507 case "-0": 508 wantError = false 509 case "-s": 510 singlefilepkgs = true 511 default: 512 flags = append(flags, args[0]) 513 } 514 args = args[1:] 515 } 516 517 t.makeTempDir() 518 if !*keep { 519 defer os.RemoveAll(t.tempDir) 520 } 521 522 err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644) 523 check(err) 524 525 // A few tests (of things like the environment) require these to be set. 526 if os.Getenv("GOOS") == "" { 527 os.Setenv("GOOS", runtime.GOOS) 528 } 529 if os.Getenv("GOARCH") == "" { 530 os.Setenv("GOARCH", runtime.GOARCH) 531 } 532 533 useTmp := true 534 runcmd := func(args ...string) ([]byte, error) { 535 cmd := exec.Command(args[0], args[1:]...) 536 var buf bytes.Buffer 537 cmd.Stdout = &buf 538 cmd.Stderr = &buf 539 if useTmp { 540 cmd.Dir = t.tempDir 541 cmd.Env = envForDir(cmd.Dir) 542 } else { 543 cmd.Env = os.Environ() 544 } 545 err := cmd.Run() 546 if err != nil { 547 err = fmt.Errorf("%s\n%s", err, buf.Bytes()) 548 } 549 return buf.Bytes(), err 550 } 551 552 long := filepath.Join(cwd, t.goFileName()) 553 switch action { 554 default: 555 t.err = fmt.Errorf("unimplemented action %q", action) 556 557 case "errorcheck": 558 cmdline := []string{"go", "tool", "compile", "-e", "-o", "a.o"} 559 // No need to add -dynlink even if linkshared if we're just checking for errors... 560 cmdline = append(cmdline, flags...) 561 cmdline = append(cmdline, long) 562 out, err := runcmd(cmdline...) 563 if wantError { 564 if err == nil { 565 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 566 return 567 } 568 } else { 569 if err != nil { 570 t.err = err 571 return 572 } 573 } 574 if *updateErrors { 575 t.updateErrors(string(out), long) 576 } 577 t.err = t.errorCheck(string(out), long, t.gofile) 578 return 579 580 case "compile": 581 _, t.err = compileFile(runcmd, long) 582 583 case "compiledir": 584 // Compile all files in the directory in lexicographic order. 585 longdir := filepath.Join(cwd, t.goDirName()) 586 pkgs, err := goDirPackages(longdir, singlefilepkgs) 587 if err != nil { 588 t.err = err 589 return 590 } 591 for _, gofiles := range pkgs { 592 _, t.err = compileInDir(runcmd, longdir, flags, gofiles...) 593 if t.err != nil { 594 return 595 } 596 } 597 598 case "errorcheckdir", "errorcheckandrundir": 599 // errorcheck all files in lexicographic order 600 // useful for finding importing errors 601 longdir := filepath.Join(cwd, t.goDirName()) 602 pkgs, err := goDirPackages(longdir, singlefilepkgs) 603 if err != nil { 604 t.err = err 605 return 606 } 607 for i, gofiles := range pkgs { 608 out, err := compileInDir(runcmd, longdir, flags, gofiles...) 609 if i == len(pkgs)-1 { 610 if wantError && err == nil { 611 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 612 return 613 } else if !wantError && err != nil { 614 t.err = err 615 return 616 } 617 } else if err != nil { 618 t.err = err 619 return 620 } 621 var fullshort []string 622 for _, name := range gofiles { 623 fullshort = append(fullshort, filepath.Join(longdir, name), name) 624 } 625 t.err = t.errorCheck(string(out), fullshort...) 626 if t.err != nil { 627 break 628 } 629 } 630 if action == "errorcheckdir" { 631 return 632 } 633 fallthrough 634 635 case "rundir": 636 // Compile all files in the directory in lexicographic order. 637 // then link as if the last file is the main package and run it 638 longdir := filepath.Join(cwd, t.goDirName()) 639 pkgs, err := goDirPackages(longdir, singlefilepkgs) 640 if err != nil { 641 t.err = err 642 return 643 } 644 for i, gofiles := range pkgs { 645 _, err := compileInDir(runcmd, longdir, flags, gofiles...) 646 if err != nil { 647 t.err = err 648 return 649 } 650 if i == len(pkgs)-1 { 651 err = linkFile(runcmd, gofiles[0]) 652 if err != nil { 653 t.err = err 654 return 655 } 656 var cmd []string 657 cmd = append(cmd, findExecCmd()...) 658 cmd = append(cmd, filepath.Join(t.tempDir, "a.exe")) 659 cmd = append(cmd, args...) 660 out, err := runcmd(cmd...) 661 if err != nil { 662 t.err = err 663 return 664 } 665 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 666 t.err = fmt.Errorf("incorrect output\n%s", out) 667 } 668 } 669 } 670 671 case "build": 672 _, err := runcmd("go", "build", "-o", "a.exe", long) 673 if err != nil { 674 t.err = err 675 } 676 677 case "run": 678 useTmp = false 679 cmd := []string{"go", "run"} 680 if *linkshared { 681 cmd = append(cmd, "-linkshared") 682 } 683 cmd = append(cmd, t.goFileName()) 684 out, err := runcmd(append(cmd, args...)...) 685 if err != nil { 686 t.err = err 687 return 688 } 689 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 690 t.err = fmt.Errorf("incorrect output\n%s", out) 691 } 692 693 case "runoutput": 694 rungatec <- true 695 defer func() { 696 <-rungatec 697 }() 698 useTmp = false 699 cmd := []string{"go", "run"} 700 if *linkshared { 701 cmd = append(cmd, "-linkshared") 702 } 703 cmd = append(cmd, t.goFileName()) 704 out, err := runcmd(append(cmd, args...)...) 705 if err != nil { 706 t.err = err 707 return 708 } 709 tfile := filepath.Join(t.tempDir, "tmp__.go") 710 if err := ioutil.WriteFile(tfile, out, 0666); err != nil { 711 t.err = fmt.Errorf("write tempfile:%s", err) 712 return 713 } 714 cmd = []string{"go", "run"} 715 if *linkshared { 716 cmd = append(cmd, "-linkshared") 717 } 718 cmd = append(cmd, tfile) 719 out, err = runcmd(cmd...) 720 if err != nil { 721 t.err = err 722 return 723 } 724 if string(out) != t.expectedOutput() { 725 t.err = fmt.Errorf("incorrect output\n%s", out) 726 } 727 728 case "errorcheckoutput": 729 useTmp = false 730 cmd := []string{"go", "run"} 731 if *linkshared { 732 cmd = append(cmd, "-linkshared") 733 } 734 cmd = append(cmd, t.goFileName()) 735 out, err := runcmd(append(cmd, args...)...) 736 if err != nil { 737 t.err = err 738 return 739 } 740 tfile := filepath.Join(t.tempDir, "tmp__.go") 741 err = ioutil.WriteFile(tfile, out, 0666) 742 if err != nil { 743 t.err = fmt.Errorf("write tempfile:%s", err) 744 return 745 } 746 cmdline := []string{"go", "tool", "compile", "-e", "-o", "a.o"} 747 cmdline = append(cmdline, flags...) 748 cmdline = append(cmdline, tfile) 749 out, err = runcmd(cmdline...) 750 if wantError { 751 if err == nil { 752 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 753 return 754 } 755 } else { 756 if err != nil { 757 t.err = err 758 return 759 } 760 } 761 t.err = t.errorCheck(string(out), tfile, "tmp__.go") 762 return 763 } 764 } 765 766 var execCmd []string 767 768 func findExecCmd() []string { 769 if execCmd != nil { 770 return execCmd 771 } 772 execCmd = []string{} // avoid work the second time 773 if goos == runtime.GOOS && goarch == runtime.GOARCH { 774 return execCmd 775 } 776 path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)) 777 if err == nil { 778 execCmd = []string{path} 779 } 780 return execCmd 781 } 782 783 func (t *test) String() string { 784 return filepath.Join(t.dir, t.gofile) 785 } 786 787 func (t *test) makeTempDir() { 788 var err error 789 t.tempDir, err = ioutil.TempDir("", "") 790 check(err) 791 if *keep { 792 log.Printf("Temporary directory is %s", t.tempDir) 793 } 794 } 795 796 func (t *test) expectedOutput() string { 797 filename := filepath.Join(t.dir, t.gofile) 798 filename = filename[:len(filename)-len(".go")] 799 filename += ".out" 800 b, _ := ioutil.ReadFile(filename) 801 return string(b) 802 } 803 804 func splitOutput(out string) []string { 805 // gc error messages continue onto additional lines with leading tabs. 806 // Split the output at the beginning of each line that doesn't begin with a tab. 807 // <autogenerated> lines are impossible to match so those are filtered out. 808 var res []string 809 for _, line := range strings.Split(out, "\n") { 810 if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows 811 line = line[:len(line)-1] 812 } 813 if strings.HasPrefix(line, "\t") { 814 res[len(res)-1] += "\n" + line 815 } else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "<autogenerated>") || strings.HasPrefix(line, "#") { 816 continue 817 } else if strings.TrimSpace(line) != "" { 818 res = append(res, line) 819 } 820 } 821 return res 822 } 823 824 func (t *test) errorCheck(outStr string, fullshort ...string) (err error) { 825 defer func() { 826 if *verbose && err != nil { 827 log.Printf("%s gc output:\n%s", t, outStr) 828 } 829 }() 830 var errs []error 831 out := splitOutput(outStr) 832 833 // Cut directory name. 834 for i := range out { 835 for j := 0; j < len(fullshort); j += 2 { 836 full, short := fullshort[j], fullshort[j+1] 837 out[i] = strings.Replace(out[i], full, short, -1) 838 } 839 } 840 841 var want []wantedError 842 for j := 0; j < len(fullshort); j += 2 { 843 full, short := fullshort[j], fullshort[j+1] 844 want = append(want, t.wantedErrors(full, short)...) 845 } 846 847 for _, we := range want { 848 var errmsgs []string 849 errmsgs, out = partitionStrings(we.prefix, out) 850 if len(errmsgs) == 0 { 851 errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr)) 852 continue 853 } 854 matched := false 855 n := len(out) 856 for _, errmsg := range errmsgs { 857 if we.re.MatchString(errmsg) { 858 matched = true 859 } else { 860 out = append(out, errmsg) 861 } 862 } 863 if !matched { 864 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"))) 865 continue 866 } 867 } 868 869 if len(out) > 0 { 870 errs = append(errs, fmt.Errorf("Unmatched Errors:")) 871 for _, errLine := range out { 872 errs = append(errs, fmt.Errorf("%s", errLine)) 873 } 874 } 875 876 if len(errs) == 0 { 877 return nil 878 } 879 if len(errs) == 1 { 880 return errs[0] 881 } 882 var buf bytes.Buffer 883 fmt.Fprintf(&buf, "\n") 884 for _, err := range errs { 885 fmt.Fprintf(&buf, "%s\n", err.Error()) 886 } 887 return errors.New(buf.String()) 888 } 889 890 func (t *test) updateErrors(out, file string) { 891 base := path.Base(file) 892 // Read in source file. 893 src, err := ioutil.ReadFile(file) 894 if err != nil { 895 fmt.Fprintln(os.Stderr, err) 896 return 897 } 898 lines := strings.Split(string(src), "\n") 899 // Remove old errors. 900 for i, ln := range lines { 901 pos := strings.Index(ln, " // ERROR ") 902 if pos >= 0 { 903 lines[i] = ln[:pos] 904 } 905 } 906 // Parse new errors. 907 errors := make(map[int]map[string]bool) 908 tmpRe := regexp.MustCompile(`autotmp_[0-9]+`) 909 for _, errStr := range splitOutput(out) { 910 colon1 := strings.Index(errStr, ":") 911 if colon1 < 0 || errStr[:colon1] != file { 912 continue 913 } 914 colon2 := strings.Index(errStr[colon1+1:], ":") 915 if colon2 < 0 { 916 continue 917 } 918 colon2 += colon1 + 1 919 line, err := strconv.Atoi(errStr[colon1+1 : colon2]) 920 line-- 921 if err != nil || line < 0 || line >= len(lines) { 922 continue 923 } 924 msg := errStr[colon2+2:] 925 msg = strings.Replace(msg, file, base, -1) // normalize file mentions in error itself 926 msg = strings.TrimLeft(msg, " \t") 927 for _, r := range []string{`\`, `*`, `+`, `[`, `]`, `(`, `)`} { 928 msg = strings.Replace(msg, r, `\`+r, -1) 929 } 930 msg = strings.Replace(msg, `"`, `.`, -1) 931 msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`) 932 if errors[line] == nil { 933 errors[line] = make(map[string]bool) 934 } 935 errors[line][msg] = true 936 } 937 // Add new errors. 938 for line, errs := range errors { 939 var sorted []string 940 for e := range errs { 941 sorted = append(sorted, e) 942 } 943 sort.Strings(sorted) 944 lines[line] += " // ERROR" 945 for _, e := range sorted { 946 lines[line] += fmt.Sprintf(` "%s$"`, e) 947 } 948 } 949 // Write new file. 950 err = ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640) 951 if err != nil { 952 fmt.Fprintln(os.Stderr, err) 953 return 954 } 955 // Polish. 956 exec.Command("go", "fmt", file).CombinedOutput() 957 } 958 959 // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[), 960 // That is, it needs the file name prefix followed by a : or a [, 961 // and possibly preceded by a directory name. 962 func matchPrefix(s, prefix string) bool { 963 i := strings.Index(s, ":") 964 if i < 0 { 965 return false 966 } 967 j := strings.LastIndex(s[:i], "/") 968 s = s[j+1:] 969 if len(s) <= len(prefix) || s[:len(prefix)] != prefix { 970 return false 971 } 972 switch s[len(prefix)] { 973 case '[', ':': 974 return true 975 } 976 return false 977 } 978 979 func partitionStrings(prefix string, strs []string) (matched, unmatched []string) { 980 for _, s := range strs { 981 if matchPrefix(s, prefix) { 982 matched = append(matched, s) 983 } else { 984 unmatched = append(unmatched, s) 985 } 986 } 987 return 988 } 989 990 type wantedError struct { 991 reStr string 992 re *regexp.Regexp 993 lineNum int 994 file string 995 prefix string 996 } 997 998 var ( 999 errRx = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`) 1000 errQuotesRx = regexp.MustCompile(`"([^"]*)"`) 1001 lineRx = regexp.MustCompile(`LINE(([+-])([0-9]+))?`) 1002 ) 1003 1004 func (t *test) wantedErrors(file, short string) (errs []wantedError) { 1005 cache := make(map[string]*regexp.Regexp) 1006 1007 src, _ := ioutil.ReadFile(file) 1008 for i, line := range strings.Split(string(src), "\n") { 1009 lineNum := i + 1 1010 if strings.Contains(line, "////") { 1011 // double comment disables ERROR 1012 continue 1013 } 1014 m := errRx.FindStringSubmatch(line) 1015 if m == nil { 1016 continue 1017 } 1018 all := m[1] 1019 mm := errQuotesRx.FindAllStringSubmatch(all, -1) 1020 if mm == nil { 1021 log.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line) 1022 } 1023 for _, m := range mm { 1024 rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string { 1025 n := lineNum 1026 if strings.HasPrefix(m, "LINE+") { 1027 delta, _ := strconv.Atoi(m[5:]) 1028 n += delta 1029 } else if strings.HasPrefix(m, "LINE-") { 1030 delta, _ := strconv.Atoi(m[5:]) 1031 n -= delta 1032 } 1033 return fmt.Sprintf("%s:%d", short, n) 1034 }) 1035 re := cache[rx] 1036 if re == nil { 1037 var err error 1038 re, err = regexp.Compile(rx) 1039 if err != nil { 1040 log.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err) 1041 } 1042 cache[rx] = re 1043 } 1044 prefix := fmt.Sprintf("%s:%d", short, lineNum) 1045 errs = append(errs, wantedError{ 1046 reStr: rx, 1047 re: re, 1048 prefix: prefix, 1049 lineNum: lineNum, 1050 file: short, 1051 }) 1052 } 1053 } 1054 1055 return 1056 } 1057 1058 // defaultRunOutputLimit returns the number of runoutput tests that 1059 // can be executed in parallel. 1060 func defaultRunOutputLimit() int { 1061 const maxArmCPU = 2 1062 1063 cpu := runtime.NumCPU() 1064 if runtime.GOARCH == "arm" && cpu > maxArmCPU { 1065 cpu = maxArmCPU 1066 } 1067 return cpu 1068 } 1069 1070 // checkShouldTest runs sanity checks on the shouldTest function. 1071 func checkShouldTest() { 1072 assert := func(ok bool, _ string) { 1073 if !ok { 1074 panic("fail") 1075 } 1076 } 1077 assertNot := func(ok bool, _ string) { assert(!ok, "") } 1078 1079 // Simple tests. 1080 assert(shouldTest("// +build linux", "linux", "arm")) 1081 assert(shouldTest("// +build !windows", "linux", "arm")) 1082 assertNot(shouldTest("// +build !windows", "windows", "amd64")) 1083 1084 // A file with no build tags will always be tested. 1085 assert(shouldTest("// This is a test.", "os", "arch")) 1086 1087 // Build tags separated by a space are OR-ed together. 1088 assertNot(shouldTest("// +build arm 386", "linux", "amd64")) 1089 1090 // Build tags separated by a comma are AND-ed together. 1091 assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64")) 1092 assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386")) 1093 1094 // Build tags on multiple lines are AND-ed together. 1095 assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64")) 1096 assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64")) 1097 1098 // Test that (!a OR !b) matches anything. 1099 assert(shouldTest("// +build !windows !plan9", "windows", "amd64")) 1100 } 1101 1102 // envForDir returns a copy of the environment 1103 // suitable for running in the given directory. 1104 // The environment is the current process's environment 1105 // but with an updated $PWD, so that an os.Getwd in the 1106 // child will be faster. 1107 func envForDir(dir string) []string { 1108 env := os.Environ() 1109 for i, kv := range env { 1110 if strings.HasPrefix(kv, "PWD=") { 1111 env[i] = "PWD=" + dir 1112 return env 1113 } 1114 } 1115 env = append(env, "PWD="+dir) 1116 return env 1117 } 1118 1119 func getenv(key, def string) string { 1120 value := os.Getenv(key) 1121 if value != "" { 1122 return value 1123 } 1124 return def 1125 }