Commit f06795d9 authored by Matthew Dempsky's avatar Matthew Dempsky Committed by Brad Fitzpatrick

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/9110Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 4540e162
...@@ -9,8 +9,10 @@ import ( ...@@ -9,8 +9,10 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
...@@ -39,6 +41,12 @@ func main() { ...@@ -39,6 +41,12 @@ func main() {
onlyTest(flag.Args()...) 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. // ratec limits the number of tests running concurrently.
// None of the tests are intensive, so don't bother // None of the tests are intensive, so don't bother
// trying to manually adjust for slow builders. // trying to manually adjust for slow builders.
...@@ -49,7 +57,7 @@ func main() { ...@@ -49,7 +57,7 @@ func main() {
tt := tt tt := tt
ratec <- true ratec <- true
go func() { go func() {
errc <- test(tt.file, tt.want) errc <- test(tmpdir, tt.file, tt.want)
<-ratec <-ratec
}() }()
} }
...@@ -61,30 +69,32 @@ func main() { ...@@ -61,30 +69,32 @@ func main() {
rc = 1 rc = 1
} }
} }
os.Remove(tmpdir)
os.Exit(rc) os.Exit(rc)
} }
// test builds the test in the given file. // test builds the test in the given file.
// If want is non-empty, test also runs the test // If want is non-empty, test also runs the test
// and checks that the output matches the regexp want. // 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. // 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() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return fmt.Errorf("go build %s.go failed: %v\nOutput:\n%s", file, err, out) 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. // Only run the test if we have output to check.
if want == "" { if want == "" {
return nil return nil
} }
cmd = exec.Command("./" + file) cmd = exec.Command(prog)
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
if err != nil { 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. // Canonicalize output.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment