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 (
"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.
......
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