[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>
diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go
index c36cb0b..173e8a2 100644
--- a/src/cmd/go/internal/test/test.go
+++ b/src/cmd/go/internal/test/test.go
@@ -671,6 +671,9 @@
 	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 47e1439..3a2ca63 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 @@
 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 @@
 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 0000000..6a7732f
--- /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) {})
+}