-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing: delay flag registration; move to an Init function
Any code that imports the testing package forces the testing flags to be defined, even in non-test binaries. People work around this today by defining a copy of the testing.TB interface just to avoid importing testing. Fix this by moving flag registration into a new function, testing.Init. Delay calling Init until the testing binary begins to run, in testing.MainStart. Init is exported for cases where users need the testing flags to be defined outside of a "go test" context. In particular, this may be needed where testing.Benchmark is called outside of a test. Fixes #21051 Change-Id: Ib7e02459e693c26ae1ba71bbae7d455a91118ee3 Reviewed-on: https://go-review.googlesource.com/c/go/+/173722 Reviewed-by: Brad Fitzpatrick <[email protected]>
- Loading branch information
Showing
6 changed files
with
118 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters