]> Cypherpunks repositories - gostls13.git/commitdiff
testing: tests and benchmarks can assume flag.Parsed
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 8 May 2020 13:14:15 +0000 (14:14 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 8 May 2020 21:21:40 +0000 (21:21 +0000)
testing.M.Run has this bit of code:

if !flag.Parsed() {
flag.Parse()
}

It makes sense, and it's common knowledge for many Go developers that
test flags are automatically parsed by the time tests and benchmarks are
run. However, the docs didn't clarify that. The previous wording only
mentioned that flag.Parse isn't run before TestMain, which doesn't
necessarily mean that it's run afterwards.

Fixes #38952.

Change-Id: I85f7a9dce637a23c5cb9abc485d47415c1a1ca27
Reviewed-on: https://go-review.googlesource.com/c/go/+/232806
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/testing/testing.go

index 90c15a2cff314ae681051c81a4cc569a7feeb787..216e46ee81609c3d1c1171214ce8f2f84e15808f 100644 (file)
 // directly. TestMain runs in the main goroutine and can do whatever setup
 // and teardown is necessary around a call to m.Run. m.Run will return an exit
 // code that may be passed to os.Exit. If TestMain returns, the test wrapper
-// will pass the result of m.Run to os.Exit itself. When TestMain is called,
-// flag.Parse has not been run. If TestMain depends on command-line flags,
-// including those of the testing package, it should call flag.Parse explicitly.
+// will pass the result of m.Run to os.Exit itself.
+//
+// When TestMain is called, flag.Parse has not been run. If TestMain depends on
+// command-line flags, including those of the testing package, it should call
+// flag.Parse explicitly. Command line flags are always parsed by the time test
+// or benchmark functions run.
 //
 // A simple implementation of TestMain is:
 //