From 5ce83d3bdaa97012150e7e20941d1a0ceb2cd7db Mon Sep 17 00:00:00 2001 From: Katie Hockman Date: Thu, 17 Sep 2020 10:28:18 -0400 Subject: [PATCH] [dev.fuzz] testing: small cleanup to running targets Change-Id: Idcf90c5acbf7dbba2ea01d21d893214a5c2028c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/255517 Trust: Katie Hockman Run-TryBot: Katie Hockman TryBot-Result: Go Bot Reviewed-by: Jay Conrod --- src/cmd/go/testdata/script/test_fuzz.txt | 10 ++++++++++ src/testing/fuzz.go | 18 +++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cmd/go/testdata/script/test_fuzz.txt b/src/cmd/go/testdata/script/test_fuzz.txt index 68e5041822..24350ee450 100644 --- a/src/cmd/go/testdata/script/test_fuzz.txt +++ b/src/cmd/go/testdata/script/test_fuzz.txt @@ -8,6 +8,16 @@ go test success_fuzz_test.go stdout ok ! stdout FAIL +# Test that calling f.Fatal while fuzzing causes a non-zero exit status. +! go test -fuzz Fuzz fail_fuzz_test.go +! stdout ^ok +stdout FAIL + +# Test that successful fuzzing exits cleanly. +go test -fuzz Fuzz success_fuzz_test.go +stdout ok +! stdout FAIL + [short] stop # Test that calling panic(nil) in a fuzz target causes a non-zero exit status. diff --git a/src/testing/fuzz.go b/src/testing/fuzz.go index f5162115b4..ee7f68e544 100644 --- a/src/testing/fuzz.go +++ b/src/testing/fuzz.go @@ -78,20 +78,16 @@ func (f *F) run(name string, fn func(f *F)) (ran, ok bool) { context: f.context, } if innerF.chatty != nil { - if f.fuzz { - innerF.chatty.Updatef(name, "--- FUZZ: %s\n", name) - } else { - innerF.chatty.Updatef(name, "=== RUN %s\n", name) - } + innerF.chatty.Updatef(name, "=== RUN %s\n", name) } - go runTarget(innerF, fn) + go innerF.runTarget(fn) <-innerF.signal return innerF.ran, !innerF.failed } // runTarget runs the given target, handling panics and exits // within the test, and reporting errors. -func runTarget(f *F, fn func(f *F)) { +func (f *F) runTarget(fn func(f *F)) { defer func() { err := recover() // If the function has recovered but the test hasn't finished, @@ -202,7 +198,8 @@ func runFuzzing(matchString func(pat, str string) (bool, error), fuzzTargets []I } f := &F{ common: common{ - w: os.Stdout, + signal: make(chan bool), + w: os.Stdout, }, context: ctx, fuzz: true, @@ -227,8 +224,11 @@ func runFuzzing(matchString func(pat, str string) (bool, error), fuzzTargets []I } if Verbose() { f.chatty = newChattyPrinter(f.w) + f.chatty.Updatef(f.name, "--- FUZZ: %s\n", f.name) } - return f.run(ft.Name, ft.Fn) + go f.runTarget(ft.Fn) + <-f.signal + return f.ran, !f.failed } // Fuzz runs a single fuzz target. It is useful for creating -- 2.48.1