aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKatie Hockman <katie@golang.org>2020-09-17 10:28:18 -0400
committerFilippo Valsorda <filippo@golang.org>2020-12-04 19:17:29 +0100
commit5ce83d3bdaa97012150e7e20941d1a0ceb2cd7db (patch)
treeb297d10663425f380858faedd4c8f5e456af9e10 /src
parent2a9036fe7e3c8f62bf80c857534dc142a4248f37 (diff)
downloadgo-5ce83d3bdaa97012150e7e20941d1a0ceb2cd7db.tar.xz
[dev.fuzz] testing: small cleanup to running targets
Change-Id: Idcf90c5acbf7dbba2ea01d21d893214a5c2028c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/255517 Trust: Katie Hockman <katie@golang.org> Run-TryBot: Katie Hockman <katie@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/go/testdata/script/test_fuzz.txt10
-rw-r--r--src/testing/fuzz.go18
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