]> Cypherpunks repositories - gostls13.git/commitdiff
doc/progs: build test programs in temp directory
authorMatthew Dempsky <mdempsky@google.com>
Sat, 18 Apr 2015 04:04:39 +0000 (21:04 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 18 Apr 2015 05:29:19 +0000 (05:29 +0000)
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>
doc/progs/run.go

index a664f07871b959e2d35f0eb64f94fa7a2432194f..035e663872ddb43c9ac58224c879fab264e24dc2 100755 (executable)
@@ -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.