Skip to content

Commit 888a0c8

Browse files
committed
testing: add TB.TempDir
Fixes #35998 Change-Id: I87c6bf4e34e832be68862ca16ecfa6ea12048d31 Reviewed-on: https://go-review.googlesource.com/c/go/+/226877 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 2bed279 commit 888a0c8

File tree

6 files changed

+94
-7
lines changed

6 files changed

+94
-7
lines changed

src/go/build/deps_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ var pkgDeps = map[string][]string{
199199
"runtime/trace": {"L0", "context", "fmt"},
200200
"text/tabwriter": {"L2"},
201201

202-
"testing": {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
202+
"testing": {"L2", "flag", "fmt", "internal/race", "io/ioutil", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
203203
"testing/iotest": {"L2", "log"},
204204
"testing/quick": {"L2", "flag", "fmt", "reflect", "time"},
205205
"internal/obscuretestdata": {"L2", "OS", "encoding/base64"},

src/io/ioutil/export_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package ioutil
6+
7+
var ErrPatternHasSeparator = errPatternHasSeparator

src/io/ioutil/ioutil_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package ioutil
5+
package ioutil_test
66

77
import (
88
"bytes"
9+
. "io/ioutil"
910
"os"
1011
"path/filepath"
1112
"testing"

src/io/ioutil/tempfile_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package ioutil
5+
package ioutil_test
66

77
import (
8+
. "io/ioutil"
89
"os"
910
"path/filepath"
1011
"regexp"
@@ -59,7 +60,7 @@ func TestTempFile_BadPattern(t *testing.T) {
5960
tests := []struct {
6061
pattern string
6162
wantErr bool
62-
} {
63+
}{
6364
{"ioutil*test", false},
6465
{"ioutil_test*foo", false},
6566
{"ioutil_test" + sep + "foo", true},
@@ -80,7 +81,7 @@ func TestTempFile_BadPattern(t *testing.T) {
8081
if err == nil {
8182
t.Errorf("Expected an error for pattern %q", tt.pattern)
8283
}
83-
if g, w := err, errPatternHasSeparator; g != w {
84+
if g, w := err, ErrPatternHasSeparator; g != w {
8485
t.Errorf("Error mismatch: got %#v, want %#v for pattern %q", g, w, tt.pattern)
8586
}
8687
} else if err != nil {
@@ -166,7 +167,7 @@ func TestTempDir_BadPattern(t *testing.T) {
166167
tests := []struct {
167168
pattern string
168169
wantErr bool
169-
} {
170+
}{
170171
{"ioutil*test", false},
171172
{"ioutil_test*foo", false},
172173
{"ioutil_test" + sep + "foo", true},
@@ -182,7 +183,7 @@ func TestTempDir_BadPattern(t *testing.T) {
182183
if err == nil {
183184
t.Errorf("Expected an error for pattern %q", tt.pattern)
184185
}
185-
if g, w := err, errPatternHasSeparator; g != w {
186+
if g, w := err, ErrPatternHasSeparator; g != w {
186187
t.Errorf("Error mismatch: got %#v, want %#v for pattern %q", g, w, tt.pattern)
187188
}
188189
} else if err != nil {

src/testing/testing.go

+30
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ import (
239239
"fmt"
240240
"internal/race"
241241
"io"
242+
"io/ioutil"
242243
"os"
243244
"runtime"
244245
"runtime/debug"
@@ -362,6 +363,10 @@ type common struct {
362363
barrier chan bool // To signal parallel subtests they may start.
363364
signal chan bool // To signal a test is done.
364365
sub []*T // Queue of subtests to be run in parallel.
366+
367+
tempDirOnce sync.Once
368+
tempDir string
369+
tempDirErr error
365370
}
366371

367372
// Short reports whether the -test.short flag is set.
@@ -561,6 +566,7 @@ type TB interface {
561566
SkipNow()
562567
Skipf(format string, args ...interface{})
563568
Skipped() bool
569+
TempDir() string
564570

565571
// A private method to prevent users implementing the
566572
// interface and so future additions to it will not
@@ -791,6 +797,30 @@ func (c *common) Cleanup(f func()) {
791797
}
792798
}
793799

800+
// TempDir returns a temporary directory for the test to use.
801+
// It is lazily created on first access, and calls t.Fatal if the directory
802+
// creation fails.
803+
// Subsequent calls to t.TempDir return the same directory.
804+
// The directory is automatically removed by Cleanup when the test and
805+
// all its subtests complete.
806+
func (c *common) TempDir() string {
807+
c.tempDirOnce.Do(func() {
808+
c.Helper()
809+
c.tempDir, c.tempDirErr = ioutil.TempDir("", c.Name())
810+
if c.tempDirErr == nil {
811+
c.Cleanup(func() {
812+
if err := os.RemoveAll(c.tempDir); err != nil {
813+
c.Errorf("TempDir RemoveAll cleanup: %v", err)
814+
}
815+
})
816+
}
817+
})
818+
if c.tempDirErr != nil {
819+
c.Fatalf("TempDir: %v", c.tempDirErr)
820+
}
821+
return c.tempDir
822+
}
823+
794824
// panicHanding is an argument to runCleanup.
795825
type panicHandling int
796826

src/testing/testing_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package testing_test
66

77
import (
8+
"io/ioutil"
89
"os"
910
"testing"
1011
)
@@ -16,3 +17,50 @@ import (
1617
func TestMain(m *testing.M) {
1718
os.Exit(m.Run())
1819
}
20+
21+
func TestTempDir(t *testing.T) {
22+
dirCh := make(chan string, 1)
23+
t.Cleanup(func() {
24+
// Verify directory has been removed.
25+
select {
26+
case dir := <-dirCh:
27+
fi, err := os.Stat(dir)
28+
if os.IsNotExist(err) {
29+
// All good
30+
return
31+
}
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
t.Errorf("directory %q stil exists: %v, isDir=%v", dir, fi, fi.IsDir())
36+
default:
37+
if !t.Failed() {
38+
t.Fatal("never received dir channel")
39+
}
40+
}
41+
})
42+
43+
dir := t.TempDir()
44+
if dir == "" {
45+
t.Fatal("expected dir")
46+
}
47+
dir2 := t.TempDir()
48+
if dir != dir2 {
49+
t.Fatal("directory changed between calls")
50+
}
51+
dirCh <- dir
52+
fi, err := os.Stat(dir)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
if !fi.IsDir() {
57+
t.Errorf("dir %q is not a dir", dir)
58+
}
59+
fis, err := ioutil.ReadDir(dir)
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
if len(fis) > 0 {
64+
t.Errorf("unexpected %d files in TempDir: %v", len(fis), fis)
65+
}
66+
}

0 commit comments

Comments
 (0)