From: Matthew Dempsky Date: Sat, 18 Apr 2015 04:04:39 +0000 (-0700) Subject: doc/progs: build test programs in temp directory X-Git-Tag: go1.5beta1~1020 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f06795d9b742cf3292a0f254646c23603fc6419b;p=gostls13.git 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 --- diff --git a/doc/progs/run.go b/doc/progs/run.go index a664f07871..035e663872 100755 --- a/doc/progs/run.go +++ b/doc/progs/run.go @@ -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.