tempDirOnce sync.Once
tempDir string
tempDirErr error
+ tempDirSeq int32
}
// Short reports whether the -test.short flag is set.
// The directory is automatically removed by Cleanup when the test and
// all its subtests complete.
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()
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.
import (
"io/ioutil"
"os"
+ "path/filepath"
"testing"
)
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)