]> Cypherpunks repositories - gostls13.git/commitdiff
testing: fix invalid error message about argument of TestMain
authorKunpei Sakai <namusyaka@gmail.com>
Mon, 23 Oct 2017 06:27:04 +0000 (15:27 +0900)
committerIan Lance Taylor <iant@golang.org>
Sun, 19 Nov 2017 05:06:59 +0000 (05:06 +0000)
Also, this commit adds a test for ensuring that TestMain(t *testing.T) is a normal test.

Fixes #22388

Change-Id: Iffcb1db5cdcf34b9c822fcdb58f8926535415177
Reviewed-on: https://go-review.googlesource.com/72591
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/test/test.go
src/cmd/go/testdata/standalone_main_normal_test.go [new file with mode: 0644]
src/cmd/go/testdata/standalone_main_wrong_test.go [new file with mode: 0644]

index a1f8a7f4eb8ecd41e5ff92e650b9e378b22eabc7..8f0db27cb20181d660566ab5706bb37bdf2f4981 100644 (file)
@@ -2901,6 +2901,21 @@ func TestGoTestFooTestWorks(t *testing.T) {
        tg.run("test", "testdata/standalone_test.go")
 }
 
+// Issue 22388
+func TestGoTestMainWithWrongSignature(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.runFail("test", "testdata/standalone_main_wrong_test.go")
+       tg.grepStderr(`wrong signature for TestMain, must be: func TestMain\(m \*testing.M\)`, "detected wrong error message")
+}
+
+func TestGoTestMainAsNormalTest(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.run("test", "testdata/standalone_main_normal_test.go")
+       tg.grepBoth(okPattern, "go test did not say ok")
+}
+
 func TestGoTestFlagsAfterPackage(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
index 998607509dc0e1102b70733f6a290a5cc600a334..f8490485ddc2c5a01e06d21584f727ed05decc60 100644 (file)
@@ -1694,7 +1694,16 @@ func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
                }
                name := n.Name.String()
                switch {
-               case name == "TestMain" && isTestFunc(n, "M"):
+               case name == "TestMain":
+                       if isTestFunc(n, "T") {
+                               t.Tests = append(t.Tests, testFunc{pkg, name, "", false})
+                               *doImport, *seen = true, true
+                               continue
+                       }
+                       err := checkTestFunc(n, "M")
+                       if err != nil {
+                               return err
+                       }
                        if t.TestMain != nil {
                                return errors.New("multiple definitions of TestMain")
                        }
diff --git a/src/cmd/go/testdata/standalone_main_normal_test.go b/src/cmd/go/testdata/standalone_main_normal_test.go
new file mode 100644 (file)
index 0000000..018ce75
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package standalone_main_normal_test
+
+import "testing"
+
+func TestMain(t *testing.T) {
+}
diff --git a/src/cmd/go/testdata/standalone_main_wrong_test.go b/src/cmd/go/testdata/standalone_main_wrong_test.go
new file mode 100644 (file)
index 0000000..5999887
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package standalone_main_wrong_test
+
+import "testing"
+
+func TestMain(m *testing.Main) {
+}