compiler/protogen: disable warnings in tests

Change-Id: I80ff3c31dc1039a833e6c962d5f3fc6e0aa46f6a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/222377
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/compiler/protogen/protogen.go b/compiler/protogen/protogen.go
index 9299404..ee8d0f7 100644
--- a/compiler/protogen/protogen.go
+++ b/compiler/protogen/protogen.go
@@ -295,7 +295,7 @@
 		switch {
 		case packageImportPath != "":
 			// Command line: import_path=quux/bar
-			log.Printf("WARNING: Deprecated use of the 'import_path' command-line argument. In %q, please specify:\n"+
+			warn("Deprecated use of the 'import_path' command-line argument. In %q, please specify:\n"+
 				"\toption go_package = %q;\n"+
 				"A future release of protoc-gen-go will no longer support the 'import_path' argument.\n"+
 				"See "+goPackageDocURL+" for more information.\n"+
@@ -304,14 +304,14 @@
 			// Command line: M=foo.proto=quux/bar
 		case packageName != "" && importPath == "":
 			// Source file: option go_package = "quux";
-			log.Printf("WARNING: Deprecated use of 'go_package' option without a full import path in %q, please specify:\n"+
+			warn("Deprecated use of 'go_package' option without a full import path in %q, please specify:\n"+
 				"\toption go_package = %q;\n"+
 				"A future release of protoc-gen-go will require the import path be specified.\n"+
 				"See "+goPackageDocURL+" for more information.\n"+
 				"\n", fdesc.GetName(), goPkgOpt)
 		case packageName == "" && importPath == "":
 			// No Go package information provided.
-			log.Printf("WARNING: Missing 'go_package' option in %q, please specify:\n"+
+			warn("Missing 'go_package' option in %q, please specify:\n"+
 				"\toption go_package = %q;\n"+
 				"A future release of protoc-gen-go will require this be specified.\n"+
 				"See "+goPackageDocURL+" for more information.\n"+
@@ -534,7 +534,7 @@
 	rawPkg, impPath := goPackageOptionRaw(opt)
 	pkg = cleanPackageName(rawPkg)
 	if string(pkg) != rawPkg && impPath != "" {
-		log.Printf("WARNING: Malformed 'go_package' option in %q, please specify:\n"+
+		warn("Malformed 'go_package' option in %q, please specify:\n"+
 			"\toption go_package = %q;\n"+
 			"A future release of protoc-gen-go will reject this.\n"+
 			"See "+goPackageDocURL+" for more information.\n"+
@@ -1346,3 +1346,11 @@
 	}
 	return string(b)
 }
+
+var warnings = true
+
+func warn(format string, a ...interface{}) {
+	if warnings {
+		log.Printf("WARNING: "+format, a...)
+	}
+}
diff --git a/compiler/protogen/protogen_test.go b/compiler/protogen/protogen_test.go
index 419305f..5cc3d66 100644
--- a/compiler/protogen/protogen_test.go
+++ b/compiler/protogen/protogen_test.go
@@ -18,6 +18,10 @@
 	"google.golang.org/protobuf/types/pluginpb"
 )
 
+func init() {
+	warnings = false // avoid spam in tests
+}
+
 func TestPluginParameters(t *testing.T) {
 	var flags flag.FlagSet
 	value := flags.Int("integer", 0, "")