Skip to content

Commit

Permalink
check: use Go 1.15's TempDir() code in preference to our own if avail…
Browse files Browse the repository at this point in the history
…able.
  • Loading branch information
jhenstridge committed Dec 12, 2020
1 parent 6ceb58a commit 2ac1ab7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 76 deletions.
61 changes: 4 additions & 57 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ package check

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -109,46 +105,11 @@ type C struct {
// -----------------------------------------------------------------------
// Handling of temporary files and directories.

type tempDir struct {
sync.Mutex
path string
counter int
}

func (td *tempDir) newPath() string {
td.Lock()
defer td.Unlock()
if td.path == "" {
path, err := ioutil.TempDir("", "check-")
if err != nil {
panic("Couldn't create temporary directory: " + err.Error())
}
td.path = path
}
result := filepath.Join(td.path, strconv.Itoa(td.counter))
td.counter++
return result
}

func (td *tempDir) removeAll() {
td.Lock()
defer td.Unlock()
if td.path != "" {
err := os.RemoveAll(td.path)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: Error cleaning up temporaries: "+err.Error())
}
}
}

// Create a new temporary directory which is automatically removed after
// the suite finishes running.
func (c *C) MkDir() string {
path := c.tempDir.newPath()
if err := os.Mkdir(path, 0700); err != nil {
panic(fmt.Sprintf("Couldn't create temporary directory %s: %s", path, err.Error()))
}
return path
c.Helper()
return c.TempDir()
}

// -----------------------------------------------------------------------
Expand Down Expand Up @@ -223,25 +184,15 @@ type suiteRunner struct {
keepDir bool
}

type RunConf struct {
KeepWorkDir bool
}

// Create a new suiteRunner able to run all methods in the given suite.
func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
var conf RunConf
if runConf != nil {
conf = *runConf
}

func newSuiteRunner(suite interface{}) *suiteRunner {
suiteType := reflect.TypeOf(suite)
suiteNumMethods := suiteType.NumMethod()
suiteValue := reflect.ValueOf(suite)

runner := &suiteRunner{
suite: suite,
tempDir: &tempDir{},
keepDir: conf.KeepWorkDir,
tests: make([]*methodType, 0, suiteNumMethods),
}

Expand Down Expand Up @@ -269,11 +220,7 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
// Run all methods in the given suite.
func (runner *suiteRunner) run(t *testing.T) {
t.Cleanup(func() {
if runner.keepDir {
t.Log("WORK =", runner.tempDir.path)
} else {
runner.tempDir.removeAll()
}
runner.tempDir.removeAll()
})

c := C{T: t, startTime: time.Now()}
Expand Down
25 changes: 10 additions & 15 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,59 @@ func Suite(suite interface{}) interface{} {

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

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

// TestingT runs all test suites registered with the Suite function,
// printing results to stdout, and reporting any failures back to
// the "testing" package.
func TestingT(t *testing.T) {
t.Helper()
conf := &RunConf{
KeepWorkDir: *oldWorkFlag || *newWorkFlag,
}
if *oldListFlag || *newListFlag {
w := bufio.NewWriter(os.Stdout)
for _, name := range ListAll(conf) {
for _, name := range ListAll() {
fmt.Fprintln(w, name)
}
w.Flush()
return
}
RunAll(t, conf)
RunAll(t)
}

// RunAll runs all test suites registered with the Suite function, using the
// provided run configuration.
func RunAll(t *testing.T, runConf *RunConf) {
func RunAll(t *testing.T) {
t.Helper()
for _, suite := range allSuites {
t.Run(suiteName(suite), func(t *testing.T) {
Run(t, suite, runConf)
Run(t, suite)
})
}
}

// Run runs the provided test suite using the provided run configuration.
func Run(t *testing.T, suite interface{}, runConf *RunConf) {
func Run(t *testing.T, suite interface{}) {
t.Helper()
runner := newSuiteRunner(suite, runConf)
runner := newSuiteRunner(suite)
runner.run(t)
}

// ListAll returns the names of all the test functions registered with the
// Suite function that will be run with the provided run configuration.
func ListAll(runConf *RunConf) []string {
func ListAll() []string {
var names []string
for _, suite := range allSuites {
names = append(names, List(suite, runConf)...)
names = append(names, List(suite)...)
}
return names
}

// List returns the names of the test functions in the given
// suite that will be run with the provided run configuration.
func List(suite interface{}, runConf *RunConf) []string {
func List(suite interface{}) []string {
var names []string
runner := newSuiteRunner(suite, runConf)
runner := newSuiteRunner(suite)
for _, t := range runner.tests {
names = append(names, t.String())
}
Expand Down
9 changes: 9 additions & 0 deletions tempdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build go1.15

package check

type tempDir struct{}

func (td *tempDir) removeAll() {
// nothing
}
58 changes: 58 additions & 0 deletions tempdir114.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// +build !go1.15

package check

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"
)

type tempDir struct {
sync.Mutex
path string
counter int
}

func (td *tempDir) newPath() string {
td.Lock()
defer td.Unlock()
if td.path == "" {
path, err := ioutil.TempDir("", "check-")
if err != nil {
panic("Couldn't create temporary directory: " + err.Error())
}
td.path = path
}
result := filepath.Join(td.path, strconv.Itoa(td.counter))
td.counter++
return result
}

func (td *tempDir) removeAll() {
td.Lock()
defer td.Unlock()
if td.path != "" {
err := os.RemoveAll(td.path)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: Error cleaning up temporaries: "+err.Error())
}
}
}

// TempDir returns a temporary directory for the test to use. The
// directory is automatically cleaned up when the suite is complete.
// Each subsequent call to c.TempDir returns a unique directory; if
// the directory creation fails, TempDir terminates the test by
// calling Fatal.
func (c *C) TempDir() string {
c.Helper()
path := c.tempDir.newPath()
if err := os.Mkdir(path, 0700); err != nil {
c.Fatalf("Couldn't create temporary directory %s: %s", path, err.Error())
}
return path
}
8 changes: 4 additions & 4 deletions testhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func TestHelperSuite(t *testing.T) {
}
switch *helperRunFlag {
case "FailHelper":
check.Run(t, &FailHelper{}, nil)
check.Run(t, &FailHelper{})
case "SuccessHelper":
check.Run(t, &SuccessHelper{}, nil)
check.Run(t, &SuccessHelper{})
case "FixtureHelper":
suite := &FixtureHelper{}
if helperPanicFlag != nil {
suite.panicOn = *helperPanicFlag
}
check.Run(t, suite, nil)
check.Run(t, suite)
case "integrationTestHelper":
check.Run(t, &integrationTestHelper{}, nil)
check.Run(t, &integrationTestHelper{})
default:
t.Skip()
}
Expand Down

0 comments on commit 2ac1ab7

Please sign in to comment.