aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2017-12-13 21:46:03 -0500
committerRuss Cox <rsc@golang.org>2017-12-14 14:57:01 +0000
commit94d7c884c38561dba467feef3acf6ada50713e59 (patch)
treefeee894c601fffbe88f71c7f5fa43e312d77064e /src/testing/testing.go
parente28a0d397b3118d518a0ef3c4cd68c0577d3d9a6 (diff)
downloadgo-94d7c884c38561dba467feef3acf6ada50713e59.tar.xz
testing: do not crash when m.Run is called twice and -test.testlogfile is used
Tests exist that call m.Run in a loop‽ Now we have one too. Fixes #23129. Change-Id: I8cbecb724f239ae14ad45d75e67d12c80e41c994 Reviewed-on: https://go-review.googlesource.com/83956 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/testing/testing.go')
-rw-r--r--src/testing/testing.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 13937b6ad4..3a4e256b49 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -914,6 +914,8 @@ type M struct {
timer *time.Timer
afterOnce sync.Once
+
+ numRun int
}
// testDeps is an internal interface of functionality that is
@@ -945,6 +947,12 @@ func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchma
// Run runs the tests. It returns an exit code to pass to os.Exit.
func (m *M) Run() int {
+ // Count the number of calls to m.Run.
+ // We only ever expected 1, but we didn't enforce that,
+ // and now there are tests in the wild that call m.Run multiple times.
+ // Sigh. golang.org/issue/23129.
+ m.numRun++
+
// TestMain may have already called flag.Parse.
if !flag.Parsed() {
flag.Parse()
@@ -1110,7 +1118,16 @@ func (m *M) before() {
if *testlog != "" {
// Note: Not using toOutputDir.
// This file is for use by cmd/go, not users.
- f, err := os.Create(*testlog)
+ var f *os.File
+ var err error
+ if m.numRun == 1 {
+ f, err = os.Create(*testlog)
+ } else {
+ f, err = os.OpenFile(*testlog, os.O_WRONLY, 0)
+ if err == nil {
+ f.Seek(0, io.SeekEnd)
+ }
+ }
if err != nil {
fmt.Fprintf(os.Stderr, "testing: %s\n", err)
os.Exit(2)