Commit 6ab049b9 authored by Russ Cox's avatar Russ Cox

testing: panic on calls to Short/Verbose before Parse

CL 121936 added this diagnostic to avoid a panic accessing *short.
(Hence the "This shouldn't really be a panic" comment.)

That CL was right to produce a clearer error than a plain memory fault,
but I think wrong to print+exit instead of panicking. I just ran into
one of these in a real program, and there is no indication anywhere
of how the program reached this point. The panic will show that.
So change print+exit to a panic with a helpful message, in contrast
to the original panic with an unhelpful message and the current
helpful message without stack trace.

Change-Id: Ib2bae1dead4ccde92f00fa3a34c05241ff7690c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/177419
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 80b393e8
...@@ -367,11 +367,9 @@ func Short() bool { ...@@ -367,11 +367,9 @@ func Short() bool {
if short == nil { if short == nil {
panic("testing: Short called before Init") panic("testing: Short called before Init")
} }
// Catch code that calls this from TestMain without first // Catch code that calls this from TestMain without first calling flag.Parse.
// calling flag.Parse. This shouldn't really be a panic.
if !flag.Parsed() { if !flag.Parsed() {
fmt.Fprintf(os.Stderr, "testing: Short called before flag.Parse\n") panic("testing: Short called before Parse")
os.Exit(2)
} }
return *short return *short
...@@ -386,13 +384,12 @@ func CoverMode() string { ...@@ -386,13 +384,12 @@ func CoverMode() string {
// Verbose reports whether the -test.v flag is set. // Verbose reports whether the -test.v flag is set.
func Verbose() bool { func Verbose() bool {
// Same as in Short.
if chatty == nil { if chatty == nil {
panic("testing: Verbose called before Init") panic("testing: Verbose called before Init")
} }
// Same as in Short.
if !flag.Parsed() { if !flag.Parsed() {
fmt.Fprintf(os.Stderr, "testing: Verbose called before flag.Parse\n") panic("testing: Verbose called before Parse")
os.Exit(2)
} }
return *chatty return *chatty
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment