]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: convert all tests using testdata/standalone_* to script framework
authorMichael Matloob <matloob@golang.org>
Tue, 7 Jan 2020 21:07:20 +0000 (16:07 -0500)
committerMichael Matloob <matloob@golang.org>
Wed, 19 Feb 2020 20:48:08 +0000 (20:48 +0000)
Part of converting all tests to script framework to improve
test parallelism.

Updates #36320
Updates #17751

Change-Id: Iea6ba91d37c6f0d4994ae64e629c349c4eae511a
Reviewed-on: https://go-review.googlesource.com/c/go/+/213678
Reviewed-by: Jay Conrod <jayconrod@google.com>
21 files changed:
src/cmd/go/go_test.go
src/cmd/go/testdata/script/test_main.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_benchmark_labels.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_no_benchmarks.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_no_subtests.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_no_subtests_failure.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_no_subtests_parallel.txt [moved from src/cmd/go/testdata/standalone_parallel_sub_test.go with 62% similarity]
src/cmd/go/testdata/script/test_match_no_tests.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_no_tests_with_subtests.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_only_benchmarks.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_only_subtests.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_only_subtests_parallel.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_match_only_tests.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_parallel_number.txt [new file with mode: 0644]
src/cmd/go/testdata/standalone_benchmark_test.go [deleted file]
src/cmd/go/testdata/standalone_fail_sub_test.go [deleted file]
src/cmd/go/testdata/standalone_main_normal_test.go [deleted file]
src/cmd/go/testdata/standalone_main_wrong_test.go [deleted file]
src/cmd/go/testdata/standalone_sub_test.go [deleted file]
src/cmd/go/testdata/standalone_test.go [deleted file]
src/cmd/go/testdata/standalone_testmain_flag_test.go [deleted file]

index eea833ec0176856a727c4988da493a6d9fcca21c..2c952474c53e137a7648e6b2c734ec0f76f7a2fc 100644 (file)
@@ -2386,33 +2386,6 @@ func TestGoBuildTestOnly(t *testing.T) {
        tg.run("install", "./testonly...")
 }
 
-func TestGoTestFooTestWorks(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "testdata/standalone_test.go")
-}
-
-func TestGoTestTestMainSeesTestingFlags(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "testdata/standalone_testmain_flag_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 TestGoTestXtestonlyWorks(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
@@ -3300,96 +3273,6 @@ const (
        okPattern        = `(?m)^ok`
 )
 
-func TestMatchesNoTests(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       // TODO: tg.parallel()
-       tg.run("test", "-run", "ThisWillNotMatch", "testdata/standalone_test.go")
-       tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
-}
-
-func TestMatchesNoBenchmarksIsOK(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       // TODO: tg.parallel()
-       tg.run("test", "-run", "^$", "-bench", "ThisWillNotMatch", "testdata/standalone_benchmark_test.go")
-       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
-       tg.grepBoth(okPattern, "go test did not say ok")
-}
-
-func TestMatchesOnlyBenchmarkIsOK(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       // TODO: tg.parallel()
-       tg.run("test", "-run", "^$", "-bench", ".", "testdata/standalone_benchmark_test.go")
-       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
-       tg.grepBoth(okPattern, "go test did not say ok")
-}
-
-func TestBenchmarkLabelsOutsideGOPATH(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       // TODO: tg.parallel()
-       tg.run("test", "-run", "^$", "-bench", ".", "testdata/standalone_benchmark_test.go")
-       tg.grepStdout(`(?m)^goos: `+runtime.GOOS, "go test did not print goos")
-       tg.grepStdout(`(?m)^goarch: `+runtime.GOARCH, "go test did not print goarch")
-       tg.grepBothNot(`(?m)^pkg:`, "go test did say pkg:")
-}
-
-func TestMatchesOnlyTestIsOK(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       // TODO: tg.parallel()
-       tg.run("test", "-run", "Test", "testdata/standalone_test.go")
-       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
-       tg.grepBoth(okPattern, "go test did not say ok")
-}
-
-func TestMatchesNoTestsWithSubtests(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "-run", "ThisWillNotMatch", "testdata/standalone_sub_test.go")
-       tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
-}
-
-func TestMatchesNoSubtestsMatch(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "-run", "Test/ThisWillNotMatch", "testdata/standalone_sub_test.go")
-       tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
-}
-
-func TestMatchesNoSubtestsDoesNotOverrideFailure(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.runFail("test", "-run", "TestThatFails/ThisWillNotMatch", "testdata/standalone_fail_sub_test.go")
-       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
-       tg.grepBoth("FAIL", "go test did not say FAIL")
-}
-
-func TestMatchesOnlySubtestIsOK(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "-run", "Test/Sub", "testdata/standalone_sub_test.go")
-       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
-       tg.grepBoth(okPattern, "go test did not say ok")
-}
-
-func TestMatchesNoSubtestsParallel(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "-run", "Test/Sub/ThisWillNotMatch", "testdata/standalone_parallel_sub_test.go")
-       tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
-}
-
-func TestMatchesOnlySubtestParallelIsOK(t *testing.T) {
-       tg := testgo(t)
-       defer tg.cleanup()
-       tg.run("test", "-run", "Test/Sub/Nested", "testdata/standalone_parallel_sub_test.go")
-       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
-       tg.grepBoth(okPattern, "go test did not say ok")
-}
-
 // Issue 18845
 func TestBenchTimeout(t *testing.T) {
        tooSlow(t)
@@ -3889,18 +3772,6 @@ func TestExecBuildX(t *testing.T) {
        tg.must(robustio.RemoveAll(matches[1]))
 }
 
-func TestParallelNumber(t *testing.T) {
-       tooSlow(t)
-       for _, n := range [...]string{"-1", "0"} {
-               t.Run(n, func(t *testing.T) {
-                       tg := testgo(t)
-                       defer tg.cleanup()
-                       tg.runFail("test", "-parallel", n, "testdata/standalone_parallel_sub_test.go")
-                       tg.grepBoth("-parallel can only be given", "go test -parallel with N<1 did not error")
-               })
-       }
-}
-
 func TestWrongGOOSErrorBeforeLoadError(t *testing.T) {
        skipIfGccgo(t, "gccgo assumes cross-compilation is always possible")
        tg := testgo(t)
diff --git a/src/cmd/go/testdata/script/test_main.txt b/src/cmd/go/testdata/script/test_main.txt
new file mode 100644 (file)
index 0000000..e255eab
--- /dev/null
@@ -0,0 +1,66 @@
+# Test TestMain
+go test standalone_main_normal_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout '^ok'
+
+# Test TestMain sees testing flags
+go test standalone_testmain_flag_test.go
+stdout '^ok.*\[no tests to run\]'
+
+# Test TestMain with wrong signature (Issue #22388)
+! go test standalone_main_wrong_test.go
+stderr 'wrong signature for TestMain, must be: func TestMain\(m \*testing.M\)'
+
+-- standalone_main_normal_test.go --
+// 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) {
+}
+-- standalone_main_wrong_test.go --
+// 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) {
+}
+-- standalone_testmain_flag_test.go --
+// Copyright 2019 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_testmain_flag_test
+
+import (
+       "flag"
+       "fmt"
+       "os"
+       "testing"
+)
+
+func TestMain(m *testing.M) {
+       // A TestMain should be able to access testing flags if it calls
+       // flag.Parse without needing to use testing.Init.
+       flag.Parse()
+       found := false
+       flag.VisitAll(func(f *flag.Flag) {
+               if f.Name == "test.count" {
+                       found = true
+               }
+       })
+       if !found {
+               fmt.Println("testing flags not registered")
+               os.Exit(1)
+       }
+       os.Exit(m.Run())
+}
diff --git a/src/cmd/go/testdata/script/test_match_benchmark_labels.txt b/src/cmd/go/testdata/script/test_match_benchmark_labels.txt
new file mode 100644 (file)
index 0000000..13c4007
--- /dev/null
@@ -0,0 +1,18 @@
+# Benchmark labels, file outside gopath
+# TODO(matloob): This test was called TestBenchmarkLabelsOutsideGOPATH
+# why "OutsideGOPATH"? Does the go command need to be run outside GOPATH?
+# Do the files need to exist outside GOPATH?
+cp $GOPATH/src/standalone_benchmark_test.go $WORK/tmp/standalone_benchmark_test.go
+go test -run '^$' -bench . $WORK/tmp/standalone_benchmark_test.go
+stdout '^goos: '$GOOS
+stdout '^goarch: '$GOARCH
+! stdout '^pkg:'
+! stderr '^pkg:'
+
+-- standalone_benchmark_test.go --
+package standalone_benchmark
+
+import "testing"
+
+func Benchmark(b *testing.B) {
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_no_benchmarks.txt b/src/cmd/go/testdata/script/test_match_no_benchmarks.txt
new file mode 100644 (file)
index 0000000..30f4be8
--- /dev/null
@@ -0,0 +1,13 @@
+# Matches no benchmarks
+go test -run '^$' -bench ThisWillNotMatch standalone_benchmark_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout '^ok'
+
+-- standalone_benchmark_test.go --
+package standalone_benchmark
+
+import "testing"
+
+func Benchmark(b *testing.B) {
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_no_subtests.txt b/src/cmd/go/testdata/script/test_match_no_subtests.txt
new file mode 100644 (file)
index 0000000..7abb1eb
--- /dev/null
@@ -0,0 +1,12 @@
+# The subtests don't match
+go test -run Test/ThisWillNotMatch standalone_sub_test.go
+stdout '^ok.*\[no tests to run\]'
+
+-- standalone_sub_test.go --
+package standalone_sub_test
+
+import "testing"
+
+func Test(t *testing.T) {
+       t.Run("Sub", func(t *testing.T) {})
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_no_subtests_failure.txt b/src/cmd/go/testdata/script/test_match_no_subtests_failure.txt
new file mode 100644 (file)
index 0000000..b3c5b92
--- /dev/null
@@ -0,0 +1,15 @@
+# Matches no subtests, but parent test still fails
+! go test -run TestThatFails/ThisWillNotMatch standalone_fail_sub_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout 'FAIL'
+
+-- standalone_fail_sub_test.go --
+package standalone_fail_sub_test
+
+import "testing"
+
+func TestThatFails(t *testing.T) {
+       t.Run("Sub", func(t *testing.T) {})
+       t.Fail()
+}
\ No newline at end of file
similarity index 62%
rename from src/cmd/go/testdata/standalone_parallel_sub_test.go
rename to src/cmd/go/testdata/script/test_match_no_subtests_parallel.txt
index d326de0a5aca0fb9a9fd9337b19fa90a082bb271..11c734c4c3fe71cab0655fbf3140a18d6da6f1ec 100644 (file)
@@ -1,3 +1,8 @@
+# Matches no subtests, parallel
+go test -run Test/Sub/ThisWillNotMatch standalone_parallel_sub_test.go
+stdout '^ok.*\[no tests to run\]'
+
+-- standalone_parallel_sub_test.go --
 package standalone_parallel_sub_test
 
 import "testing"
diff --git a/src/cmd/go/testdata/script/test_match_no_tests.txt b/src/cmd/go/testdata/script/test_match_no_tests.txt
new file mode 100644 (file)
index 0000000..1ad2097
--- /dev/null
@@ -0,0 +1,11 @@
+# Matches no tests
+go test -run ThisWillNotMatch standalone_test.go
+stdout '^ok.*\[no tests to run\]'
+
+-- standalone_test.go --
+package standalone_test
+
+import "testing"
+
+func Test(t *testing.T) {
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_no_tests_with_subtests.txt b/src/cmd/go/testdata/script/test_match_no_tests_with_subtests.txt
new file mode 100644 (file)
index 0000000..0d94918
--- /dev/null
@@ -0,0 +1,12 @@
+# Matches no tests with subtests
+go test -run ThisWillNotMatch standalone_sub_test.go
+stdout '^ok.*\[no tests to run\]'
+
+-- standalone_sub_test.go --
+package standalone_sub_test
+
+import "testing"
+
+func Test(t *testing.T) {
+       t.Run("Sub", func(t *testing.T) {})
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_only_benchmarks.txt b/src/cmd/go/testdata/script/test_match_only_benchmarks.txt
new file mode 100644 (file)
index 0000000..5dfb96e
--- /dev/null
@@ -0,0 +1,13 @@
+# Matches only benchmarks
+go test -run '^$' -bench . standalone_benchmark_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout '^ok'
+
+-- standalone_benchmark_test.go --
+package standalone_benchmark
+
+import "testing"
+
+func Benchmark(b *testing.B) {
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_only_subtests.txt b/src/cmd/go/testdata/script/test_match_only_subtests.txt
new file mode 100644 (file)
index 0000000..beea895
--- /dev/null
@@ -0,0 +1,14 @@
+# Matches only subtests
+go test -run Test/Sub standalone_sub_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout '^ok'
+
+-- standalone_sub_test.go --
+package standalone_sub_test
+
+import "testing"
+
+func Test(t *testing.T) {
+       t.Run("Sub", func(t *testing.T) {})
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_match_only_subtests_parallel.txt b/src/cmd/go/testdata/script/test_match_only_subtests_parallel.txt
new file mode 100644 (file)
index 0000000..11872c2
--- /dev/null
@@ -0,0 +1,21 @@
+# Matches only subtests, parallel
+go test -run Test/Sub/Nested standalone_parallel_sub_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout '^ok'
+
+-- standalone_parallel_sub_test.go --
+package standalone_parallel_sub_test
+
+import "testing"
+
+func Test(t *testing.T) {
+       ch := make(chan bool, 1)
+       t.Run("Sub", func(t *testing.T) {
+               t.Parallel()
+               <-ch
+               t.Run("Nested", func(t *testing.T) {})
+       })
+       // Ensures that Sub will finish after its t.Run call already returned.
+       ch <- true
+}
diff --git a/src/cmd/go/testdata/script/test_match_only_tests.txt b/src/cmd/go/testdata/script/test_match_only_tests.txt
new file mode 100644 (file)
index 0000000..9185793
--- /dev/null
@@ -0,0 +1,13 @@
+# Matches only tests
+go test -run Test standalone_test.go
+! stdout '^ok.*\[no tests to run\]'
+! stderr '^ok.*\[no tests to run\]'
+stdout '^ok'
+
+-- standalone_test.go --
+package standalone_test
+
+import "testing"
+
+func Test(t *testing.T) {
+}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/test_parallel_number.txt b/src/cmd/go/testdata/script/test_parallel_number.txt
new file mode 100644 (file)
index 0000000..4eb9794
--- /dev/null
@@ -0,0 +1,25 @@
+[short] skip
+
+# go test -parallel -1 shouldn't work
+! go test -parallel -1 standalone_parallel_sub_test.go
+stdout '-parallel can only be given'
+
+# go test -parallel 0 shouldn't work
+! go test -parallel 0 standalone_parallel_sub_test.go
+stdout '-parallel can only be given'
+
+-- standalone_parallel_sub_test.go --
+package standalone_parallel_sub_test
+
+import "testing"
+
+func Test(t *testing.T) {
+       ch := make(chan bool, 1)
+       t.Run("Sub", func(t *testing.T) {
+               t.Parallel()
+               <-ch
+               t.Run("Nested", func(t *testing.T) {})
+       })
+       // Ensures that Sub will finish after its t.Run call already returned.
+       ch <- true
+}
diff --git a/src/cmd/go/testdata/standalone_benchmark_test.go b/src/cmd/go/testdata/standalone_benchmark_test.go
deleted file mode 100644 (file)
index 4850f98..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-package standalone_benchmark
-
-import "testing"
-
-func Benchmark(b *testing.B) {
-}
diff --git a/src/cmd/go/testdata/standalone_fail_sub_test.go b/src/cmd/go/testdata/standalone_fail_sub_test.go
deleted file mode 100644 (file)
index ac483f9..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-package standalone_fail_sub_test
-
-import "testing"
-
-func TestThatFails(t *testing.T) {
-       t.Run("Sub", func(t *testing.T) {})
-       t.Fail()
-}
diff --git a/src/cmd/go/testdata/standalone_main_normal_test.go b/src/cmd/go/testdata/standalone_main_normal_test.go
deleted file mode 100644 (file)
index 018ce75..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// 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
deleted file mode 100644 (file)
index 5999887..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// 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) {
-}
diff --git a/src/cmd/go/testdata/standalone_sub_test.go b/src/cmd/go/testdata/standalone_sub_test.go
deleted file mode 100644 (file)
index f6c31db..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-package standalone_sub_test
-
-import "testing"
-
-func Test(t *testing.T) {
-       t.Run("Sub", func(t *testing.T) {})
-}
diff --git a/src/cmd/go/testdata/standalone_test.go b/src/cmd/go/testdata/standalone_test.go
deleted file mode 100644 (file)
index 59cf918..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-package standalone_test
-
-import "testing"
-
-func Test(t *testing.T) {
-}
diff --git a/src/cmd/go/testdata/standalone_testmain_flag_test.go b/src/cmd/go/testdata/standalone_testmain_flag_test.go
deleted file mode 100644 (file)
index a59555b..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 2019 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_testmain_flag_test
-
-import (
-       "flag"
-       "fmt"
-       "os"
-       "testing"
-)
-
-func TestMain(m *testing.M) {
-       // A TestMain should be able to access testing flags if it calls
-       // flag.Parse without needing to use testing.Init.
-       flag.Parse()
-       found := false
-       flag.VisitAll(func(f *flag.Flag) {
-               if f.Name == "test.count" {
-                       found = true
-               }
-       })
-       if !found {
-               fmt.Println("testing flags not registered")
-               os.Exit(1)
-       }
-       os.Exit(m.Run())
-}