Added -gocheck.work flag, analogous to go test -work
diff --git a/gocheck.go b/gocheck.go
index 50af42e..7ddf507 100644
--- a/gocheck.go
+++ b/gocheck.go
@@ -117,36 +117,36 @@
type tempDir struct {
sync.Mutex
- _path string
- _counter int
+ path string
+ counter int
}
func (td *tempDir) newPath() string {
td.Lock()
defer td.Unlock()
- if td._path == "" {
+ if td.path == "" {
var err error
for i := 0; i != 100; i++ {
path := fmt.Sprintf("%s/gocheck-%d", os.TempDir(), rand.Int())
if err = os.Mkdir(path, 0700); err == nil {
- td._path = path
+ td.path = path
break
}
}
- if td._path == "" {
+ if td.path == "" {
panic("Couldn't create temporary directory: " + err.Error())
}
}
- result := path.Join(td._path, strconv.Itoa(td._counter))
- td._counter += 1
+ result := path.Join(td.path, strconv.Itoa(td.counter))
+ td.counter += 1
return result
}
func (td *tempDir) removeAll() {
td.Lock()
defer td.Unlock()
- if td._path != "" {
- err := os.RemoveAll(td._path)
+ if td.path != "" {
+ err := os.RemoveAll(td.path)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: Error cleaning up temporaries: "+err.Error())
}
@@ -392,8 +392,9 @@
Panicked int
FixturePanicked int
ExpectedFailures int
- Missed int // Not even tried to run, related to a panic in the fixture.
- RunError error // Houston, we've got a problem.
+ Missed int // Not even tried to run, related to a panic in the fixture.
+ RunError error // Houston, we've got a problem.
+ WorkDir string // If KeepWorkDir is true
}
type resultTracker struct {
@@ -492,6 +493,7 @@
tests []*methodType
tracker *resultTracker
tempDir *tempDir
+ keepDir bool
output *outputWriter
reportedProblemLast bool
benchTime time.Duration
@@ -504,6 +506,7 @@
Filter string
Benchmark bool
BenchmarkTime time.Duration // Defaults to 1 second
+ KeepWorkDir bool
}
// Create a new suiteRunner able to run all methods in the given suite.
@@ -528,9 +531,10 @@
output: newOutputWriter(conf.Output, conf.Stream, conf.Verbose),
tracker: newResultTracker(),
benchTime: conf.BenchmarkTime,
+ tempDir: &tempDir{},
+ keepDir: conf.KeepWorkDir,
+ tests: make([]*methodType, 0, suiteNumMethods),
}
- runner.tests = make([]*methodType, 0, suiteNumMethods)
- runner.tempDir = new(tempDir)
if runner.benchTime == 0 {
runner.benchTime = 1 * time.Second
}
@@ -597,7 +601,11 @@
runner.skipTests(missedSt, runner.tests)
}
runner.tracker.waitAndStop()
- runner.tempDir.removeAll()
+ if runner.keepDir {
+ runner.tracker.result.WorkDir = runner.tempDir.path
+ } else {
+ runner.tempDir.removeAll()
+ }
}
return &runner.tracker.result
}
diff --git a/run.go b/run.go
index dc75d77..cc9a299 100644
--- a/run.go
+++ b/run.go
@@ -32,6 +32,7 @@
benchFlag = flag.Bool("gocheck.b", false, "Run benchmarks")
benchTime = flag.Duration("gocheck.btime", 1 * time.Second, "approximate run time for each benchmark")
listFlag = flag.Bool("gocheck.list", false, "List the names of all tests that will be run")
+ workFlag = flag.Bool("gocheck.work", false, "Display and do not remove the test working directory")
)
// Run all test suites registered with the Suite() function, printing
@@ -44,6 +45,7 @@
Stream: *streamFlag,
Benchmark: *benchFlag,
BenchmarkTime: *benchTime,
+ KeepWorkDir: *workFlag,
}
if *listFlag {
w := bufio.NewWriter(os.Stdout)
@@ -86,9 +88,8 @@
return names
}
-// List prints the names of the test functions in the given
-// suite that will be run with the provided run configuration
-// to the given Writer.
+// 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 {
var names []string
runner := newSuiteRunner(suite, runConf)
@@ -109,6 +110,11 @@
r.FixturePanicked += other.FixturePanicked
r.ExpectedFailures += other.ExpectedFailures
r.Missed += other.Missed
+ if r.WorkDir != "" && other.WorkDir != "" {
+ r.WorkDir += ":" + other.WorkDir
+ } else if other.WorkDir != "" {
+ r.WorkDir = other.WorkDir
+ }
}
func (r *Result) Passed() bool {
@@ -148,5 +154,8 @@
if r.Missed != 0 {
value += fmt.Sprintf(", %d MISSED", r.Missed)
}
+ if r.WorkDir != "" {
+ value += "\nWORK=" + r.WorkDir
+ }
return value
}
diff --git a/run_test.go b/run_test.go
index 6793e4c..e47c0ef 100644
--- a/run_test.go
+++ b/run_test.go
@@ -6,6 +6,7 @@
import (
"errors"
. "launchpad.net/gocheck"
+ "os"
"sync"
)
@@ -396,3 +397,24 @@
c.Assert(output.value, Matches, expected)
}
+
+// -----------------------------------------------------------------------
+// Verify that that the keep work dir request indeed does so.
+
+type WorkDirSuite struct {}
+
+func (s *WorkDirSuite) Test(c *C) {
+ c.MkDir()
+}
+
+func (s *RunS) TestKeepWorkDir(c *C) {
+ output := String{}
+ runConf := RunConf{Output: &output, Verbose: true, KeepWorkDir: true}
+ result := Run(&WorkDirSuite{}, &runConf)
+
+ c.Assert(result.String(), Matches, ".*\nWORK=" + result.WorkDir)
+
+ stat, err := os.Stat(result.WorkDir)
+ c.Assert(err, IsNil)
+ c.Assert(stat.IsDir(), Equals, true)
+}