From 6ab049b96578547b78f919810d01f94d94cc1981 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 15 May 2019 15:10:32 -0400 Subject: [PATCH] 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 Reviewed-by: Ian Lance Taylor --- src/testing/testing.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/testing/testing.go b/src/testing/testing.go index 2f05203f27..753de9f27c 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -367,11 +367,9 @@ func Short() bool { if short == nil { panic("testing: Short called before Init") } - // Catch code that calls this from TestMain without first - // calling flag.Parse. This shouldn't really be a panic. + // Catch code that calls this from TestMain without first calling flag.Parse. if !flag.Parsed() { - fmt.Fprintf(os.Stderr, "testing: Short called before flag.Parse\n") - os.Exit(2) + panic("testing: Short called before Parse") } return *short @@ -386,13 +384,12 @@ func CoverMode() string { // Verbose reports whether the -test.v flag is set. func Verbose() bool { + // Same as in Short. if chatty == nil { panic("testing: Verbose called before Init") } - // Same as in Short. if !flag.Parsed() { - fmt.Fprintf(os.Stderr, "testing: Verbose called before flag.Parse\n") - os.Exit(2) + panic("testing: Verbose called before Parse") } return *chatty } -- 2.50.0