aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorCaleb Spare <cespare@gmail.com>2019-05-08 13:38:14 -0700
committerBryan C. Mills <bcmills@google.com>2019-05-10 18:10:48 +0000
commit49a1a01bb106f0e65a5147be5eb5f8dd60323e39 (patch)
treead9ac4c81129221c86a0d642379cdb90963910ca /src/testing
parent5833aa507bd25df20b2aecf9e2334dccfa2dac76 (diff)
downloadgo-49a1a01bb106f0e65a5147be5eb5f8dd60323e39.tar.xz
cmd/go: move automatic testing.Init call into generated test code
In CL 173722, we moved the flag registration in the testing package into an Init function. In order to avoid needing changes to user code, we called Init automatically as part of testing.MainStart. However, that isn't early enough if flag.Parse is called before the tests run, as part of package initialization. Fix this by injecting a bit of code to call testing.Init into test packages. This runs before any other initialization code in the user's test package, so testing.Init will be called before any user code can call flag.Parse. Fixes #31859 Change-Id: Ib42cd8d3819150c49a3cecf7eef2472319d0c7e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/176098 Run-TryBot: Caleb Spare <cespare@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/testing.go5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 7db7c630c2..2f05203f27 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -1078,6 +1078,11 @@ type testDeps interface {
// It is not meant to be called directly and is not subject to the Go 1 compatibility document.
// It may change signature from release to release.
func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M {
+ // In most cases, Init has already been called by the testinginit code
+ // that 'go test' injects into test packages.
+ // Call it again here to handle cases such as:
+ // - test packages that don't import "testing" (such as example-only packages)
+ // - direct use of MainStart (though that isn't well-supported)
Init()
return &M{
deps: deps,