aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorCherry Zhang <cherryyz@google.com>2020-05-21 14:08:32 -0400
committerCherry Zhang <cherryyz@google.com>2020-05-21 14:08:32 -0400
commit6097f7cf7a9a9bf877f6f49112ffcc2d0f0e7e75 (patch)
treec42265e0cfb4ac2bbcf4a9ed9432748997d2362a /src/testing
parent8e4ab9cb4c28649be36f82a02d04eb1cb31d02c9 (diff)
parentc847589ad06a1acfcceaac7b230c0d5a826caab8 (diff)
downloadgo-6097f7cf7a9a9bf877f6f49112ffcc2d0f0e7e75.tar.xz
[dev.link] all: merge branch 'master' into dev.link
Change-Id: I85b653b621ad8cb2ef27886210ea2c4b7409b60d
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/testing.go15
-rw-r--r--src/testing/testing_test.go8
2 files changed, 17 insertions, 6 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 216e46ee81..608bb39671 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -372,6 +372,7 @@ type common struct {
tempDirOnce sync.Once
tempDir string
tempDirErr error
+ tempDirSeq int32
}
// Short reports whether the -test.short flag is set.
@@ -821,12 +822,13 @@ var tempDirReplacer struct {
}
// TempDir returns a temporary directory for the test to use.
-// It is lazily created on first access, and calls t.Fatal if the directory
-// creation fails.
-// Subsequent calls to t.TempDir return the same directory.
// The directory is automatically removed by Cleanup when the test and
// all its subtests complete.
+// Each subsequent call to t.TempDir returns a unique directory;
+// if the directory creation fails, TempDir terminates the test by calling Fatal.
func (c *common) TempDir() string {
+ // Use a single parent directory for all the temporary directories
+ // created by a test, each numbered sequentially.
c.tempDirOnce.Do(func() {
c.Helper()
@@ -849,7 +851,12 @@ func (c *common) TempDir() string {
if c.tempDirErr != nil {
c.Fatalf("TempDir: %v", c.tempDirErr)
}
- return c.tempDir
+ seq := atomic.AddInt32(&c.tempDirSeq, 1)
+ dir := fmt.Sprintf("%s%c%03d", c.tempDir, os.PathSeparator, seq)
+ if err := os.Mkdir(dir, 0777); err != nil {
+ c.Fatalf("TempDir: %v", err)
+ }
+ return dir
}
// panicHanding is an argument to runCleanup.
diff --git a/src/testing/testing_test.go b/src/testing/testing_test.go
index 1340dae5c4..dbef7066e0 100644
--- a/src/testing/testing_test.go
+++ b/src/testing/testing_test.go
@@ -7,6 +7,7 @@ package testing_test
import (
"io/ioutil"
"os"
+ "path/filepath"
"testing"
)
@@ -55,8 +56,11 @@ func testTempDir(t *testing.T) {
t.Fatal("expected dir")
}
dir2 := t.TempDir()
- if dir != dir2 {
- t.Fatal("directory changed between calls")
+ if dir == dir2 {
+ t.Fatal("subsequent calls to TempDir returned the same directory")
+ }
+ if filepath.Dir(dir) != filepath.Dir(dir2) {
+ t.Fatalf("calls to TempDir do not share a parent; got %q, %q", dir, dir2)
}
dirCh <- dir
fi, err := os.Stat(dir)