aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2021-09-15 15:30:18 -0700
committerJay Conrod <jayconrod@google.com>2021-09-20 18:42:32 +0000
commit51ca5706ab2074a624f8a2590a2a81e6a5821e48 (patch)
tree6a6b86512ac545ef9c6b045c391c65dd32c071e4
parent24e25afff457839ddd78b802b42b8fceda0a6904 (diff)
downloadgo-51ca5706ab2074a624f8a2590a2a81e6a5821e48.tar.gz
go-51ca5706ab2074a624f8a2590a2a81e6a5821e48.zip
[dev.fuzz] cmd/go: in 'go test' don't allow multiple packages with -fuzz
Until we have a system for managing load across multiple fuzz targets in multiple test executables, we'll only support fuzzing one target in one package at a time. Users can still run multiple 'go test -fuzz' commands concurrently, but this may overwhelm some systems unless -parallel and -p are set carefully. For #46312 Change-Id: If84c58d1b3e60498ce955eae5ad4d52100dbd4b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/350156 Trust: Jay Conrod <jayconrod@google.com> Trust: Katie Hockman <katie@golang.org> Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Katie Hockman <katie@golang.org>
-rw-r--r--src/cmd/go/internal/test/test.go3
-rw-r--r--src/cmd/go/testdata/script/test_fuzz_match.txt20
-rw-r--r--src/cmd/go/testdata/script/test_fuzz_multiple.txt51
3 files changed, 54 insertions, 20 deletions
diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go
index c36cb0b221..173e8a2ee4 100644
--- a/src/cmd/go/internal/test/test.go
+++ b/src/cmd/go/internal/test/test.go
@@ -671,6 +671,9 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
if testO != "" && len(pkgs) != 1 {
base.Fatalf("cannot use -o flag with multiple packages")
}
+ if testFuzz != "" && len(pkgs) != 1 {
+ base.Fatalf("cannot use -fuzz flag with multiple packages")
+ }
if testProfile() != "" && len(pkgs) != 1 {
base.Fatalf("cannot use %s flag with multiple packages", testProfile())
}
diff --git a/src/cmd/go/testdata/script/test_fuzz_match.txt b/src/cmd/go/testdata/script/test_fuzz_match.txt
index 47e143952a..3a2ca631ad 100644
--- a/src/cmd/go/testdata/script/test_fuzz_match.txt
+++ b/src/cmd/go/testdata/script/test_fuzz_match.txt
@@ -29,13 +29,6 @@ go test -run ThisWillNotMatch standalone_fuzz_test.go
stdout '^ok.*no tests to run'
! stdout 'no targets to fuzz'
-# Matches more than one fuzz target for fuzzing.
-! go test -fuzz Fuzz -fuzztime 1x multiple_fuzz_test.go
-! stdout 'no tests to run'
-! stdout 'no targets to fuzz'
-stdout FAIL
-stdout 'will not fuzz, -fuzz matches more than one target'
-
-- standalone_fuzz_test.go --
package standalone_fuzz
@@ -44,16 +37,3 @@ import "testing"
func Fuzz(f *testing.F) {
f.Fuzz(func (*testing.T, []byte) {})
}
-
--- multiple_fuzz_test.go --
-package multiple_fuzz
-
-import "testing"
-
-func FuzzA(f *testing.F) {
- f.Fuzz(func (*testing.T, []byte) {})
-}
-
-func FuzzB(f *testing.F) {
- f.Fuzz(func (*testing.T, []byte) {})
-}
diff --git a/src/cmd/go/testdata/script/test_fuzz_multiple.txt b/src/cmd/go/testdata/script/test_fuzz_multiple.txt
new file mode 100644
index 0000000000..6a7732f514
--- /dev/null
+++ b/src/cmd/go/testdata/script/test_fuzz_multiple.txt
@@ -0,0 +1,51 @@
+# This test checks that 'go test' prints a reasonable error when fuzzing is
+# enabled, and multiple package or multiple fuzz targets match.
+# TODO(#46312): support fuzzing multiple targets in multiple packages.
+
+# TODO(jayconrod): support shared memory on more platforms.
+[!darwin] [!linux] [!windows] skip
+
+[short] skip
+
+# With fuzzing disabled, multiple targets can be tested.
+go test ./...
+
+# With fuzzing enabled, at most one package may be tested,
+# even if only one package contains fuzz targets.
+! go test -fuzz=. ./...
+stderr '^cannot use -fuzz flag with multiple packages$'
+! go test -fuzz=. ./zero ./one
+stderr '^cannot use -fuzz flag with multiple packages$'
+go test -fuzz=. -fuzztime=1x ./one
+
+# With fuzzing enabled, at most one target in the same package may match.
+! go test -fuzz=. ./two
+stdout '^testing: will not fuzz, -fuzz matches more than one target: \[FuzzOne FuzzTwo\]$'
+go test -fuzz=FuzzTwo -fuzztime=1x ./two
+
+-- go.mod --
+module fuzz
+
+go 1.18
+-- zero/zero.go --
+package zero
+-- one/one_test.go --
+package one
+
+import "testing"
+
+func FuzzOne(f *testing.F) {
+ f.Fuzz(func(*testing.T, []byte) {})
+}
+-- two/two_test.go --
+package two
+
+import "testing"
+
+func FuzzOne(f *testing.F) {
+ f.Fuzz(func(*testing.T, []byte) {})
+}
+
+func FuzzTwo(f *testing.F) {
+ f.Fuzz(func(*testing.T, []byte) {})
+}