Skip to content

Commit 2ac1ab7

Browse files
committed
check: use Go 1.15's TempDir() code in preference to our own if available.
1 parent 6ceb58a commit 2ac1ab7

File tree

5 files changed

+85
-76
lines changed

5 files changed

+85
-76
lines changed

check.go

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ package check
88

99
import (
1010
"fmt"
11-
"io/ioutil"
1211
"os"
1312
"path"
14-
"path/filepath"
1513
"reflect"
1614
"regexp"
1715
"runtime"
18-
"strconv"
1916
"strings"
20-
"sync"
2117
"testing"
2218
"time"
2319
)
@@ -109,46 +105,11 @@ type C struct {
109105
// -----------------------------------------------------------------------
110106
// Handling of temporary files and directories.
111107

112-
type tempDir struct {
113-
sync.Mutex
114-
path string
115-
counter int
116-
}
117-
118-
func (td *tempDir) newPath() string {
119-
td.Lock()
120-
defer td.Unlock()
121-
if td.path == "" {
122-
path, err := ioutil.TempDir("", "check-")
123-
if err != nil {
124-
panic("Couldn't create temporary directory: " + err.Error())
125-
}
126-
td.path = path
127-
}
128-
result := filepath.Join(td.path, strconv.Itoa(td.counter))
129-
td.counter++
130-
return result
131-
}
132-
133-
func (td *tempDir) removeAll() {
134-
td.Lock()
135-
defer td.Unlock()
136-
if td.path != "" {
137-
err := os.RemoveAll(td.path)
138-
if err != nil {
139-
fmt.Fprintf(os.Stderr, "WARNING: Error cleaning up temporaries: "+err.Error())
140-
}
141-
}
142-
}
143-
144108
// Create a new temporary directory which is automatically removed after
145109
// the suite finishes running.
146110
func (c *C) MkDir() string {
147-
path := c.tempDir.newPath()
148-
if err := os.Mkdir(path, 0700); err != nil {
149-
panic(fmt.Sprintf("Couldn't create temporary directory %s: %s", path, err.Error()))
150-
}
151-
return path
111+
c.Helper()
112+
return c.TempDir()
152113
}
153114

154115
// -----------------------------------------------------------------------
@@ -223,25 +184,15 @@ type suiteRunner struct {
223184
keepDir bool
224185
}
225186

226-
type RunConf struct {
227-
KeepWorkDir bool
228-
}
229-
230187
// Create a new suiteRunner able to run all methods in the given suite.
231-
func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
232-
var conf RunConf
233-
if runConf != nil {
234-
conf = *runConf
235-
}
236-
188+
func newSuiteRunner(suite interface{}) *suiteRunner {
237189
suiteType := reflect.TypeOf(suite)
238190
suiteNumMethods := suiteType.NumMethod()
239191
suiteValue := reflect.ValueOf(suite)
240192

241193
runner := &suiteRunner{
242194
suite: suite,
243195
tempDir: &tempDir{},
244-
keepDir: conf.KeepWorkDir,
245196
tests: make([]*methodType, 0, suiteNumMethods),
246197
}
247198

@@ -269,11 +220,7 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
269220
// Run all methods in the given suite.
270221
func (runner *suiteRunner) run(t *testing.T) {
271222
t.Cleanup(func() {
272-
if runner.keepDir {
273-
t.Log("WORK =", runner.tempDir.path)
274-
} else {
275-
runner.tempDir.removeAll()
276-
}
223+
runner.tempDir.removeAll()
277224
})
278225

279226
c := C{T: t, startTime: time.Now()}

run.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,64 +26,59 @@ func Suite(suite interface{}) interface{} {
2626

2727
var (
2828
oldListFlag = flag.Bool("gocheck.list", false, "List the names of all tests that will be run")
29-
oldWorkFlag = flag.Bool("gocheck.work", false, "Display and do not remove the test working directory")
3029

3130
newListFlag = flag.Bool("check.list", false, "List the names of all tests that will be run")
32-
newWorkFlag = flag.Bool("check.work", false, "Display and do not remove the test working directory")
3331
)
3432

3533
// TestingT runs all test suites registered with the Suite function,
3634
// printing results to stdout, and reporting any failures back to
3735
// the "testing" package.
3836
func TestingT(t *testing.T) {
3937
t.Helper()
40-
conf := &RunConf{
41-
KeepWorkDir: *oldWorkFlag || *newWorkFlag,
42-
}
4338
if *oldListFlag || *newListFlag {
4439
w := bufio.NewWriter(os.Stdout)
45-
for _, name := range ListAll(conf) {
40+
for _, name := range ListAll() {
4641
fmt.Fprintln(w, name)
4742
}
4843
w.Flush()
4944
return
5045
}
51-
RunAll(t, conf)
46+
RunAll(t)
5247
}
5348

5449
// RunAll runs all test suites registered with the Suite function, using the
5550
// provided run configuration.
56-
func RunAll(t *testing.T, runConf *RunConf) {
51+
func RunAll(t *testing.T) {
5752
t.Helper()
5853
for _, suite := range allSuites {
5954
t.Run(suiteName(suite), func(t *testing.T) {
60-
Run(t, suite, runConf)
55+
Run(t, suite)
6156
})
6257
}
6358
}
6459

6560
// Run runs the provided test suite using the provided run configuration.
66-
func Run(t *testing.T, suite interface{}, runConf *RunConf) {
61+
func Run(t *testing.T, suite interface{}) {
6762
t.Helper()
68-
runner := newSuiteRunner(suite, runConf)
63+
runner := newSuiteRunner(suite)
6964
runner.run(t)
7065
}
7166

7267
// ListAll returns the names of all the test functions registered with the
7368
// Suite function that will be run with the provided run configuration.
74-
func ListAll(runConf *RunConf) []string {
69+
func ListAll() []string {
7570
var names []string
7671
for _, suite := range allSuites {
77-
names = append(names, List(suite, runConf)...)
72+
names = append(names, List(suite)...)
7873
}
7974
return names
8075
}
8176

8277
// List returns the names of the test functions in the given
8378
// suite that will be run with the provided run configuration.
84-
func List(suite interface{}, runConf *RunConf) []string {
79+
func List(suite interface{}) []string {
8580
var names []string
86-
runner := newSuiteRunner(suite, runConf)
81+
runner := newSuiteRunner(suite)
8782
for _, t := range runner.tests {
8883
names = append(names, t.String())
8984
}

tempdir.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build go1.15
2+
3+
package check
4+
5+
type tempDir struct{}
6+
7+
func (td *tempDir) removeAll() {
8+
// nothing
9+
}

tempdir114.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// +build !go1.15
2+
3+
package check
4+
5+
import (
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
10+
"strconv"
11+
"sync"
12+
)
13+
14+
type tempDir struct {
15+
sync.Mutex
16+
path string
17+
counter int
18+
}
19+
20+
func (td *tempDir) newPath() string {
21+
td.Lock()
22+
defer td.Unlock()
23+
if td.path == "" {
24+
path, err := ioutil.TempDir("", "check-")
25+
if err != nil {
26+
panic("Couldn't create temporary directory: " + err.Error())
27+
}
28+
td.path = path
29+
}
30+
result := filepath.Join(td.path, strconv.Itoa(td.counter))
31+
td.counter++
32+
return result
33+
}
34+
35+
func (td *tempDir) removeAll() {
36+
td.Lock()
37+
defer td.Unlock()
38+
if td.path != "" {
39+
err := os.RemoveAll(td.path)
40+
if err != nil {
41+
fmt.Fprintf(os.Stderr, "WARNING: Error cleaning up temporaries: "+err.Error())
42+
}
43+
}
44+
}
45+
46+
// TempDir returns a temporary directory for the test to use. The
47+
// directory is automatically cleaned up when the suite is complete.
48+
// Each subsequent call to c.TempDir returns a unique directory; if
49+
// the directory creation fails, TempDir terminates the test by
50+
// calling Fatal.
51+
func (c *C) TempDir() string {
52+
c.Helper()
53+
path := c.tempDir.newPath()
54+
if err := os.Mkdir(path, 0700); err != nil {
55+
c.Fatalf("Couldn't create temporary directory %s: %s", path, err.Error())
56+
}
57+
return path
58+
}

testhelper_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ func TestHelperSuite(t *testing.T) {
2222
}
2323
switch *helperRunFlag {
2424
case "FailHelper":
25-
check.Run(t, &FailHelper{}, nil)
25+
check.Run(t, &FailHelper{})
2626
case "SuccessHelper":
27-
check.Run(t, &SuccessHelper{}, nil)
27+
check.Run(t, &SuccessHelper{})
2828
case "FixtureHelper":
2929
suite := &FixtureHelper{}
3030
if helperPanicFlag != nil {
3131
suite.panicOn = *helperPanicFlag
3232
}
33-
check.Run(t, suite, nil)
33+
check.Run(t, suite)
3434
case "integrationTestHelper":
35-
check.Run(t, &integrationTestHelper{}, nil)
35+
check.Run(t, &integrationTestHelper{})
3636
default:
3737
t.Skip()
3838
}

0 commit comments

Comments
 (0)