github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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", "builddir", "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 "builddir": 710 // Build an executable from all the .go and .s files in a subdirectory. 711 useTmp = true 712 longdir := filepath.Join(cwd, t.goDirName()) 713 files, dirErr := ioutil.ReadDir(longdir) 714 if dirErr != nil { 715 t.err = dirErr 716 break 717 } 718 var gos []os.FileInfo 719 var asms []os.FileInfo 720 for _, file := range files { 721 switch filepath.Ext(file.Name()) { 722 case ".go": 723 gos = append(gos, file) 724 case ".s": 725 asms = append(asms, file) 726 } 727 728 } 729 var objs []string 730 cmd := []string{"go", "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"} 731 for _, file := range gos { 732 cmd = append(cmd, filepath.Join(longdir, file.Name())) 733 } 734 _, err := runcmd(cmd...) 735 if err != nil { 736 t.err = err 737 break 738 } 739 objs = append(objs, "go.o") 740 if len(asms) > 0 { 741 cmd = []string{"go", "tool", "asm", "-e", "-I", ".", "-o", "asm.o"} 742 for _, file := range asms { 743 cmd = append(cmd, filepath.Join(longdir, file.Name())) 744 } 745 _, err = runcmd(cmd...) 746 if err != nil { 747 t.err = err 748 break 749 } 750 objs = append(objs, "asm.o") 751 } 752 cmd = []string{"go", "tool", "pack", "c", "all.a"} 753 cmd = append(cmd, objs...) 754 _, err = runcmd(cmd...) 755 if err != nil { 756 t.err = err 757 break 758 } 759 cmd = []string{"go", "tool", "link", "all.a"} 760 _, err = runcmd(cmd...) 761 if err != nil { 762 t.err = err 763 break 764 } 765 766 case "buildrun": // build binary, then run binary, instead of go run. Useful for timeout tests where failure mode is infinite loop. 767 // TODO: not supported on NaCl 768 useTmp = true 769 cmd := []string{"go", "build", "-o", "a.exe"} 770 if *linkshared { 771 cmd = append(cmd, "-linkshared") 772 } 773 longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile) 774 cmd = append(cmd, flags...) 775 cmd = append(cmd, longdirgofile) 776 out, err := runcmd(cmd...) 777 if err != nil { 778 t.err = err 779 return 780 } 781 cmd = []string{"./a.exe"} 782 out, err = runcmd(append(cmd, args...)...) 783 if err != nil { 784 t.err = err 785 return 786 } 787 788 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 789 t.err = fmt.Errorf("incorrect output\n%s", out) 790 } 791 792 case "run": 793 useTmp = false 794 cmd := []string{"go", "run"} 795 if *linkshared { 796 cmd = append(cmd, "-linkshared") 797 } 798 cmd = append(cmd, flags...) 799 cmd = append(cmd, t.goFileName()) 800 out, err := runcmd(append(cmd, args...)...) 801 if err != nil { 802 t.err = err 803 return 804 } 805 if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { 806 t.err = fmt.Errorf("incorrect output\n%s", out) 807 } 808 809 case "runoutput": 810 rungatec <- true 811 defer func() { 812 <-rungatec 813 }() 814 useTmp = false 815 cmd := []string{"go", "run"} 816 if *linkshared { 817 cmd = append(cmd, "-linkshared") 818 } 819 cmd = append(cmd, t.goFileName()) 820 out, err := runcmd(append(cmd, args...)...) 821 if err != nil { 822 t.err = err 823 return 824 } 825 tfile := filepath.Join(t.tempDir, "tmp__.go") 826 if err := ioutil.WriteFile(tfile, out, 0666); err != nil { 827 t.err = fmt.Errorf("write tempfile:%s", err) 828 return 829 } 830 cmd = []string{"go", "run"} 831 if *linkshared { 832 cmd = append(cmd, "-linkshared") 833 } 834 cmd = append(cmd, tfile) 835 out, err = runcmd(cmd...) 836 if err != nil { 837 t.err = err 838 return 839 } 840 if string(out) != t.expectedOutput() { 841 t.err = fmt.Errorf("incorrect output\n%s", out) 842 } 843 844 case "errorcheckoutput": 845 useTmp = false 846 cmd := []string{"go", "run"} 847 if *linkshared { 848 cmd = append(cmd, "-linkshared") 849 } 850 cmd = append(cmd, t.goFileName()) 851 out, err := runcmd(append(cmd, args...)...) 852 if err != nil { 853 t.err = err 854 return 855 } 856 tfile := filepath.Join(t.tempDir, "tmp__.go") 857 err = ioutil.WriteFile(tfile, out, 0666) 858 if err != nil { 859 t.err = fmt.Errorf("write tempfile:%s", err) 860 return 861 } 862 cmdline := []string{"go", "tool", "compile", "-e", "-o", "a.o"} 863 cmdline = append(cmdline, flags...) 864 cmdline = append(cmdline, tfile) 865 out, err = runcmd(cmdline...) 866 if wantError { 867 if err == nil { 868 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out) 869 return 870 } 871 } else { 872 if err != nil { 873 t.err = err 874 return 875 } 876 } 877 t.err = t.errorCheck(string(out), false, tfile, "tmp__.go") 878 return 879 } 880 } 881 882 var execCmd []string 883 884 func findExecCmd() []string { 885 if execCmd != nil { 886 return execCmd 887 } 888 execCmd = []string{} // avoid work the second time 889 if goos == runtime.GOOS && goarch == runtime.GOARCH { 890 return execCmd 891 } 892 path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)) 893 if err == nil { 894 execCmd = []string{path} 895 } 896 return execCmd 897 } 898 899 func (t *test) String() string { 900 return filepath.Join(t.dir, t.gofile) 901 } 902 903 func (t *test) makeTempDir() { 904 var err error 905 t.tempDir, err = ioutil.TempDir("", "") 906 check(err) 907 if *keep { 908 log.Printf("Temporary directory is %s", t.tempDir) 909 } 910 } 911 912 func (t *test) expectedOutput() string { 913 filename := filepath.Join(t.dir, t.gofile) 914 filename = filename[:len(filename)-len(".go")] 915 filename += ".out" 916 b, _ := ioutil.ReadFile(filename) 917 return string(b) 918 } 919 920 func splitOutput(out string, wantAuto bool) []string { 921 // gc error messages continue onto additional lines with leading tabs. 922 // Split the output at the beginning of each line that doesn't begin with a tab. 923 // <autogenerated> lines are impossible to match so those are filtered out. 924 var res []string 925 for _, line := range strings.Split(out, "\n") { 926 if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows 927 line = line[:len(line)-1] 928 } 929 if strings.HasPrefix(line, "\t") { 930 res[len(res)-1] += "\n" + line 931 } else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") { 932 continue 933 } else if strings.TrimSpace(line) != "" { 934 res = append(res, line) 935 } 936 } 937 return res 938 } 939 940 func (t *test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) { 941 defer func() { 942 if *verbose && err != nil { 943 log.Printf("%s gc output:\n%s", t, outStr) 944 } 945 }() 946 var errs []error 947 out := splitOutput(outStr, wantAuto) 948 949 // Cut directory name. 950 for i := range out { 951 for j := 0; j < len(fullshort); j += 2 { 952 full, short := fullshort[j], fullshort[j+1] 953 out[i] = strings.Replace(out[i], full, short, -1) 954 } 955 } 956 957 var want []wantedError 958 for j := 0; j < len(fullshort); j += 2 { 959 full, short := fullshort[j], fullshort[j+1] 960 want = append(want, t.wantedErrors(full, short)...) 961 } 962 963 for _, we := range want { 964 var errmsgs []string 965 if we.auto { 966 errmsgs, out = partitionStrings("<autogenerated>", out) 967 } else { 968 errmsgs, out = partitionStrings(we.prefix, out) 969 } 970 if len(errmsgs) == 0 { 971 errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr)) 972 continue 973 } 974 matched := false 975 n := len(out) 976 for _, errmsg := range errmsgs { 977 // Assume errmsg says "file:line: foo". 978 // Cut leading "file:line: " to avoid accidental matching of file name instead of message. 979 text := errmsg 980 if i := strings.Index(text, " "); i >= 0 { 981 text = text[i+1:] 982 } 983 if we.re.MatchString(text) { 984 matched = true 985 } else { 986 out = append(out, errmsg) 987 } 988 } 989 if !matched { 990 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"))) 991 continue 992 } 993 } 994 995 if len(out) > 0 { 996 errs = append(errs, fmt.Errorf("Unmatched Errors:")) 997 for _, errLine := range out { 998 errs = append(errs, fmt.Errorf("%s", errLine)) 999 } 1000 } 1001 1002 if len(errs) == 0 { 1003 return nil 1004 } 1005 if len(errs) == 1 { 1006 return errs[0] 1007 } 1008 var buf bytes.Buffer 1009 fmt.Fprintf(&buf, "\n") 1010 for _, err := range errs { 1011 fmt.Fprintf(&buf, "%s\n", err.Error()) 1012 } 1013 return errors.New(buf.String()) 1014 } 1015 1016 func (t *test) updateErrors(out, file string) { 1017 base := path.Base(file) 1018 // Read in source file. 1019 src, err := ioutil.ReadFile(file) 1020 if err != nil { 1021 fmt.Fprintln(os.Stderr, err) 1022 return 1023 } 1024 lines := strings.Split(string(src), "\n") 1025 // Remove old errors. 1026 for i, ln := range lines { 1027 pos := strings.Index(ln, " // ERROR ") 1028 if pos >= 0 { 1029 lines[i] = ln[:pos] 1030 } 1031 } 1032 // Parse new errors. 1033 errors := make(map[int]map[string]bool) 1034 tmpRe := regexp.MustCompile(`autotmp_[0-9]+`) 1035 for _, errStr := range splitOutput(out, false) { 1036 colon1 := strings.Index(errStr, ":") 1037 if colon1 < 0 || errStr[:colon1] != file { 1038 continue 1039 } 1040 colon2 := strings.Index(errStr[colon1+1:], ":") 1041 if colon2 < 0 { 1042 continue 1043 } 1044 colon2 += colon1 + 1 1045 line, err := strconv.Atoi(errStr[colon1+1 : colon2]) 1046 line-- 1047 if err != nil || line < 0 || line >= len(lines) { 1048 continue 1049 } 1050 msg := errStr[colon2+2:] 1051 msg = strings.Replace(msg, file, base, -1) // normalize file mentions in error itself 1052 msg = strings.TrimLeft(msg, " \t") 1053 for _, r := range []string{`\`, `*`, `+`, `[`, `]`, `(`, `)`} { 1054 msg = strings.Replace(msg, r, `\`+r, -1) 1055 } 1056 msg = strings.Replace(msg, `"`, `.`, -1) 1057 msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`) 1058 if errors[line] == nil { 1059 errors[line] = make(map[string]bool) 1060 } 1061 errors[line][msg] = true 1062 } 1063 // Add new errors. 1064 for line, errs := range errors { 1065 var sorted []string 1066 for e := range errs { 1067 sorted = append(sorted, e) 1068 } 1069 sort.Strings(sorted) 1070 lines[line] += " // ERROR" 1071 for _, e := range sorted { 1072 lines[line] += fmt.Sprintf(` "%s$"`, e) 1073 } 1074 } 1075 // Write new file. 1076 err = ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640) 1077 if err != nil { 1078 fmt.Fprintln(os.Stderr, err) 1079 return 1080 } 1081 // Polish. 1082 exec.Command("go", "fmt", file).CombinedOutput() 1083 } 1084 1085 // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[), 1086 // That is, it needs the file name prefix followed by a : or a [, 1087 // and possibly preceded by a directory name. 1088 func matchPrefix(s, prefix string) bool { 1089 i := strings.Index(s, ":") 1090 if i < 0 { 1091 return false 1092 } 1093 j := strings.LastIndex(s[:i], "/") 1094 s = s[j+1:] 1095 if len(s) <= len(prefix) || s[:len(prefix)] != prefix { 1096 return false 1097 } 1098 switch s[len(prefix)] { 1099 case '[', ':': 1100 return true 1101 } 1102 return false 1103 } 1104 1105 func partitionStrings(prefix string, strs []string) (matched, unmatched []string) { 1106 for _, s := range strs { 1107 if matchPrefix(s, prefix) { 1108 matched = append(matched, s) 1109 } else { 1110 unmatched = append(unmatched, s) 1111 } 1112 } 1113 return 1114 } 1115 1116 type wantedError struct { 1117 reStr string 1118 re *regexp.Regexp 1119 lineNum int 1120 auto bool // match <autogenerated> line 1121 file string 1122 prefix string 1123 } 1124 1125 var ( 1126 errRx = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`) 1127 errAutoRx = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`) 1128 errQuotesRx = regexp.MustCompile(`"([^"]*)"`) 1129 lineRx = regexp.MustCompile(`LINE(([+-])([0-9]+))?`) 1130 ) 1131 1132 func (t *test) wantedErrors(file, short string) (errs []wantedError) { 1133 cache := make(map[string]*regexp.Regexp) 1134 1135 src, _ := ioutil.ReadFile(file) 1136 for i, line := range strings.Split(string(src), "\n") { 1137 lineNum := i + 1 1138 if strings.Contains(line, "////") { 1139 // double comment disables ERROR 1140 continue 1141 } 1142 var auto bool 1143 m := errAutoRx.FindStringSubmatch(line) 1144 if m != nil { 1145 auto = true 1146 } else { 1147 m = errRx.FindStringSubmatch(line) 1148 } 1149 if m == nil { 1150 continue 1151 } 1152 all := m[1] 1153 mm := errQuotesRx.FindAllStringSubmatch(all, -1) 1154 if mm == nil { 1155 log.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line) 1156 } 1157 for _, m := range mm { 1158 rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string { 1159 n := lineNum 1160 if strings.HasPrefix(m, "LINE+") { 1161 delta, _ := strconv.Atoi(m[5:]) 1162 n += delta 1163 } else if strings.HasPrefix(m, "LINE-") { 1164 delta, _ := strconv.Atoi(m[5:]) 1165 n -= delta 1166 } 1167 return fmt.Sprintf("%s:%d", short, n) 1168 }) 1169 re := cache[rx] 1170 if re == nil { 1171 var err error 1172 re, err = regexp.Compile(rx) 1173 if err != nil { 1174 log.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err) 1175 } 1176 cache[rx] = re 1177 } 1178 prefix := fmt.Sprintf("%s:%d", short, lineNum) 1179 errs = append(errs, wantedError{ 1180 reStr: rx, 1181 re: re, 1182 prefix: prefix, 1183 auto: auto, 1184 lineNum: lineNum, 1185 file: short, 1186 }) 1187 } 1188 } 1189 1190 return 1191 } 1192 1193 // defaultRunOutputLimit returns the number of runoutput tests that 1194 // can be executed in parallel. 1195 func defaultRunOutputLimit() int { 1196 const maxArmCPU = 2 1197 1198 cpu := runtime.NumCPU() 1199 if runtime.GOARCH == "arm" && cpu > maxArmCPU { 1200 cpu = maxArmCPU 1201 } 1202 return cpu 1203 } 1204 1205 // checkShouldTest runs sanity checks on the shouldTest function. 1206 func checkShouldTest() { 1207 assert := func(ok bool, _ string) { 1208 if !ok { 1209 panic("fail") 1210 } 1211 } 1212 assertNot := func(ok bool, _ string) { assert(!ok, "") } 1213 1214 // Simple tests. 1215 assert(shouldTest("// +build linux", "linux", "arm")) 1216 assert(shouldTest("// +build !windows", "linux", "arm")) 1217 assertNot(shouldTest("// +build !windows", "windows", "amd64")) 1218 1219 // A file with no build tags will always be tested. 1220 assert(shouldTest("// This is a test.", "os", "arch")) 1221 1222 // Build tags separated by a space are OR-ed together. 1223 assertNot(shouldTest("// +build arm 386", "linux", "amd64")) 1224 1225 // Build tags separated by a comma are AND-ed together. 1226 assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64")) 1227 assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386")) 1228 1229 // Build tags on multiple lines are AND-ed together. 1230 assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64")) 1231 assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64")) 1232 1233 // Test that (!a OR !b) matches anything. 1234 assert(shouldTest("// +build !windows !plan9", "windows", "amd64")) 1235 } 1236 1237 // envForDir returns a copy of the environment 1238 // suitable for running in the given directory. 1239 // The environment is the current process's environment 1240 // but with an updated $PWD, so that an os.Getwd in the 1241 // child will be faster. 1242 func envForDir(dir string) []string { 1243 env := os.Environ() 1244 for i, kv := range env { 1245 if strings.HasPrefix(kv, "PWD=") { 1246 env[i] = "PWD=" + dir 1247 return env 1248 } 1249 } 1250 env = append(env, "PWD="+dir) 1251 return env 1252 } 1253 1254 func getenv(key, def string) string { 1255 value := os.Getenv(key) 1256 if value != "" { 1257 return value 1258 } 1259 return def 1260 }