From aa22c42d704f9c6d9d8e65f972ac819678629ff2 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 16 Feb 2016 07:13:10 -0800 Subject: [PATCH] cmd/go: avoid race on test environment Fixes #14337. Change-Id: I58aef7e08d936b0712da577dd1ce5c9ed5d8bfd2 Reviewed-on: https://go-review.googlesource.com/19513 Reviewed-by: Russ Cox --- src/cmd/dist/test.go | 5 +++++ src/cmd/go/go_test.go | 26 +++++++++++++++++++++++++- src/cmd/go/main.go | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index 156b868109..36c829d1b9 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -947,6 +947,11 @@ func (t *tester) raceTest(dt *distTest) error { t.addCmd(dt, "src", "go", "test", "-race", "-i", "runtime/race", "flag", "os/exec") t.addCmd(dt, "src", "go", "test", "-race", "-run=Output", "runtime/race") t.addCmd(dt, "src", "go", "test", "-race", "-short", "-run=TestParse|TestEcho", "flag", "os/exec") + // We don't want the following line, because it + // slows down all.bash (by 10 seconds on my laptop). + // The race builder should catch any error here, but doesn't. + // TODO(iant): Figure out how to catch this. + // t.addCmd(dt, "src", "go", "test", "-race", "-run=TestParallelTest", "cmd/go") if t.cgoEnabled { env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ()) cmd := t.addCmd(dt, "misc/cgo/test", "go", "test", "-race", "-short") diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index c60971efed..39e0f3e56d 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -10,6 +10,7 @@ import ( "fmt" "go/build" "go/format" + "internal/race" "internal/testenv" "io" "io/ioutil" @@ -69,7 +70,11 @@ func TestMain(m *testing.M) { flag.Parse() if canRun { - out, err := exec.Command("go", "build", "-tags", "testgo", "-o", "testgo"+exeSuffix).CombinedOutput() + args := []string{"build", "-tags", "testgo", "-o", "testgo" + exeSuffix} + if race.Enabled { + args = append(args, "-race") + } + out, err := exec.Command("go", args...).CombinedOutput() if err != nil { fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out) os.Exit(2) @@ -2735,3 +2740,22 @@ func TestIssue13655(t *testing.T) { tg.grepStdout("runtime/internal/sys", "did not find required dependency of "+pkg+" on runtime/internal/sys") } } + +// For issue 14337. +func TestParallelTest(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.makeTempdir() + const testSrc = `package package_test + import ( + "testing" + ) + func TestTest(t *testing.T) { + }` + tg.tempFile("src/p1/p1_test.go", strings.Replace(testSrc, "package_test", "p1_test", 1)) + tg.tempFile("src/p2/p2_test.go", strings.Replace(testSrc, "package_test", "p2_test", 1)) + tg.tempFile("src/p3/p3_test.go", strings.Replace(testSrc, "package_test", "p3_test", 1)) + tg.tempFile("src/p4/p4_test.go", strings.Replace(testSrc, "package_test", "p4_test", 1)) + tg.setenv("GOPATH", tg.path(".")) + tg.run("test", "-p=4", "p1", "p2", "p3", "p4") +} diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index d384594722..f9b979da7f 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -454,7 +454,9 @@ func envForDir(dir string, base []string) []string { // mergeEnvLists merges the two environment lists such that // variables with the same name in "in" replace those in "out". +// This always returns a newly allocated slice. func mergeEnvLists(in, out []string) []string { + out = append([]string(nil), out...) NextVar: for _, inkv := range in { k := strings.SplitAfterN(inkv, "=", 2)[0] -- 2.48.1