1
0
mirror of https://github.com/golang/go synced 2024-11-12 00:30:22 -07:00

doc/progs: build test programs in temp directory

This avoids a race condition with go1.go wanting to examine files in
the current directory with filepath.Walk(".", walkFn).

Fixes #10497.

Change-Id: I2159f40a08d1a768195dbb7ea3c27e38cf9740bb
Reviewed-on: https://go-review.googlesource.com/9110
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Matthew Dempsky 2015-04-17 21:04:39 -07:00 committed by Brad Fitzpatrick
parent 4540e162b1
commit f06795d9b7

View File

@ -9,8 +9,10 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
@ -39,6 +41,12 @@ func main() {
onlyTest(flag.Args()...)
}
tmpdir, err := ioutil.TempDir("", "go-progs")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// ratec limits the number of tests running concurrently.
// None of the tests are intensive, so don't bother
// trying to manually adjust for slow builders.
@ -49,7 +57,7 @@ func main() {
tt := tt
ratec <- true
go func() {
errc <- test(tt.file, tt.want)
errc <- test(tmpdir, tt.file, tt.want)
<-ratec
}()
}
@ -61,30 +69,32 @@ func main() {
rc = 1
}
}
os.Remove(tmpdir)
os.Exit(rc)
}
// test builds the test in the given file.
// If want is non-empty, test also runs the test
// and checks that the output matches the regexp want.
func test(file, want string) error {
func test(tmpdir, file, want string) error {
// Build the program.
cmd := exec.Command("go", "build", file+".go")
prog := filepath.Join(tmpdir, file)
cmd := exec.Command("go", "build", "-o", prog, file+".go")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("go build %s.go failed: %v\nOutput:\n%s", file, err, out)
}
defer os.Remove(file)
defer os.Remove(prog)
// Only run the test if we have output to check.
if want == "" {
return nil
}
cmd = exec.Command("./" + file)
cmd = exec.Command(prog)
out, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("./%s failed: %v\nOutput:\n%s", file, err, out)
return fmt.Errorf("%s failed: %v\nOutput:\n%s", file, err, out)
}
// Canonicalize output.