Skip to content

Commit f714220

Browse files
committed
cmd/go: use build cache for tests when GOCACHE is unset
Before this CL, if you had GOCACHE=/some/dir, then the cmd/go tests used it. But if you were relying on the implicit behavior that GOCACHE being empty meant an appropriate system-specific cache directory, then the cmd/go tests ran with no cache at all, which makes them about 4X slower. During all.bash GOCACHE is set to a fresh temporary directory and is therefore already getting proper caching; this CL mainly helps people running 'go test cmd/go' by hand. Change-Id: I7c322ca79b877c1d0a3b448b95d5354fbfcba7f8 Reviewed-on: https://go-review.googlesource.com/118320 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 44b826b commit f714220

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/cmd/go/go_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func init() {
9999
var testGOROOT string
100100

101101
var testCC string
102+
var testGOCACHE string
102103

103104
// The TestMain function creates a go command for testing purposes and
104105
// deletes it after the tests have been run.
@@ -175,6 +176,13 @@ func TestMain(m *testing.M) {
175176
}
176177
}
177178

179+
out, err = exec.Command(gotool, "env", "GOCACHE").CombinedOutput()
180+
if err != nil {
181+
fmt.Fprintf(os.Stderr, "could not find testing GOCACHE: %v\n%s", err, out)
182+
os.Exit(2)
183+
}
184+
testGOCACHE = strings.TrimSpace(string(out))
185+
178186
// As of Sept 2017, MSan is only supported on linux/amd64.
179187
// https://github.com/google/sanitizers/wiki/MemorySanitizer#getting-memorysanitizer
180188
canMSan = canCgo && runtime.GOOS == "linux" && runtime.GOARCH == "amd64"
@@ -198,7 +206,7 @@ func TestMain(m *testing.M) {
198206
}
199207
os.Setenv("HOME", "/test-go-home-does-not-exist")
200208
if os.Getenv("GOCACHE") == "" {
201-
os.Setenv("GOCACHE", "off") // because $HOME is gone
209+
os.Setenv("GOCACHE", testGOCACHE) // because $HOME is gone
202210
}
203211

204212
r := m.Run()
@@ -3261,9 +3269,9 @@ func TestGoTestCoverMultiPackage(t *testing.T) {
32613269
tg := testgo(t)
32623270
defer tg.cleanup()
32633271
tg.run("test", "-cover", "./testdata/testcover/...")
3264-
tg.grepStdout(`\?.*testdata/testcover/pkg1.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3265-
tg.grepStdout(`ok.*testdata/testcover/pkg2.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3266-
tg.grepStdout(`ok.*testdata/testcover/pkg3.*\d\.\d\d\ds.*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
3272+
tg.grepStdout(`\?.*testdata/testcover/pkg1.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3273+
tg.grepStdout(`ok.*testdata/testcover/pkg2.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3274+
tg.grepStdout(`ok.*testdata/testcover/pkg3.*(\d\.\d\d\ds|cached).*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
32673275
}
32683276

32693277
// issue 24570
@@ -3272,9 +3280,9 @@ func TestGoTestCoverprofileMultiPackage(t *testing.T) {
32723280
defer tg.cleanup()
32733281
tg.creatingTemp("testdata/cover.out")
32743282
tg.run("test", "-coverprofile=testdata/cover.out", "./testdata/testcover/...")
3275-
tg.grepStdout(`\?.*testdata/testcover/pkg1.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3276-
tg.grepStdout(`ok.*testdata/testcover/pkg2.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3277-
tg.grepStdout(`ok.*testdata/testcover/pkg3.*\d\.\d\d\ds.*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
3283+
tg.grepStdout(`\?.*testdata/testcover/pkg1.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3284+
tg.grepStdout(`ok.*testdata/testcover/pkg2.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3285+
tg.grepStdout(`ok.*testdata/testcover/pkg3.*(\d\.\d\d\ds|cached).*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
32783286
if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
32793287
t.Error(err)
32803288
} else {

0 commit comments

Comments
 (0)