Repository: googleapis/api-linter
Pull request #365 (2019-11-19 23:50:18)

[AIP-191] Require java_package option.


Patch:
diff --git a/docs/rules/0191/java-multiple-files.md b/docs/rules/0191/java-multiple-files.md
index a58420e..426f6a6 100644
--- a/docs/rules/0191/java-multiple-files.md
+++ b/docs/rules/0191/java-multiple-files.md
@@ -26,7 +26,11 @@ file annotation is not present, or set to `false`.
 // Incorrect.
 syntax = "proto3";
 
+package google.example.v1;
+
 // Needs `option java_multiple_files = true;`.
+option java_package = "com.google.example.v1";
+option java_outer_classname = "LibraryProto";
 
 message Book {}
 ```
@@ -37,7 +41,11 @@ message Book {}
 // Correct.
 syntax = "proto3";
 
+package google.example.v1;
+
+option java_package = "com.google.example.v1";
 option java_multiple_files = true;
+option java_outer_classname = "LibraryProto";
 
 message Book {}
 ```
@@ -52,6 +60,11 @@ Remember to also include an [aip.dev/not-precedent][] comment explaining why.
 //     aip.dev/not-precedent: We need to do this because reasons. --)
 syntax = "proto3";
 
+package google.example.v1;
+
+option java_package = "com.google.example.v1";
+option java_outer_classname = "LibraryProto";
+
 message Book {}
 ```
 
diff --git a/docs/rules/0191/java-package.md b/docs/rules/0191/java-package.md
new file mode 100644
index 0000000..9d00c33
--- /dev/null
+++ b/docs/rules/0191/java-package.md
@@ -0,0 +1,72 @@
+---
+rule:
+  aip: 191
+  name: [core, '0191', java-package]
+  summary: All proto files must set `option java_package`.
+permalink: /191/java-package
+redirect_from:
+  - /0191/java-package
+---
+
+# Java package annotation
+
+This rule enforces that every proto file for a public API surface sets
+`option java_package`, as mandated in [AIP-191][].
+
+## Details
+
+This rule looks at each proto file, and complains if the `java_package` file
+annotation is not present, or set to empty string.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+syntax = "proto3";
+
+package google.example.v1;
+
+// Needs `option java_package = "com.google.example.v1";`.
+option java_multiple_files = true;
+option java_outer_classname = "LibraryProto";
+
+message Book {}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+syntax = "proto3";
+
+package google.example.v1;
+
+option java_package = "com.google.example.v1";
+option java_multiple_files = true;
+option java_outer_classname = "LibraryProto";
+
+message Book {}
+```
+
+## Disabling
+
+If you need to violate this rule, use a comment at the top of the file.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0191::java-package=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+syntax = "proto3";
+
+package google.example.v1;
+
+option java_multiple_files = true;
+option java_outer_classname = "LibraryProto";
+
+message Book {}
+```
+
+[aip-191]: https://aip.dev/191
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0191/aip0191.go b/rules/aip0191/aip0191.go
index 0d4ead3..c0a0685 100644
--- a/rules/aip0191/aip0191.go
+++ b/rules/aip0191/aip0191.go
@@ -19,6 +19,7 @@ import (
 	"regexp"
 
 	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
 )
 
 // AddRules adds all of the AIP-191 rules to the provided registry.
@@ -27,8 +28,13 @@ func AddRules(r lint.RuleRegistry) error {
 		191,
 		filename,
 		javaMultipleFiles,
+		javaPackage,
 		syntax,
 	)
 }
 
+func hasPackage(f *desc.FileDescriptor) bool {
+	return f.GetPackage() != ""
+}
+
 var versionRegexp = regexp.MustCompile("^v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]*)?$")
diff --git a/rules/aip0191/java_multiple_files.go b/rules/aip0191/java_multiple_files.go
index 1eb7103..1a63c34 100644
--- a/rules/aip0191/java_multiple_files.go
+++ b/rules/aip0191/java_multiple_files.go
@@ -21,10 +21,8 @@ import (
 )
 
 var javaMultipleFiles = &lint.FileRule{
-	Name: lint.NewRuleName(191, "java-multiple-files"),
-	OnlyIf: func(f *desc.FileDescriptor) bool {
-		return f.GetPackage() != ""
-	},
+	Name:   lint.NewRuleName(191, "java-multiple-files"),
+	OnlyIf: hasPackage,
 	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
 		if f.GetFileOptions().GetJavaMultipleFiles() == false {
 			return []lint.Problem{{
diff --git a/rules/aip0191/java_package.go b/rules/aip0191/java_package.go
new file mode 100644
index 0000000..9f7ac19
--- /dev/null
+++ b/rules/aip0191/java_package.go
@@ -0,0 +1,36 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 		https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0191
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var javaPackage = &lint.FileRule{
+	Name:   lint.NewRuleName(191, "java-package"),
+	OnlyIf: hasPackage,
+	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
+		if f.GetFileOptions().GetJavaPackage() == "" {
+			return []lint.Problem{{
+				Descriptor: f,
+				Location:   locations.FilePackage(f),
+				Message:    "Proto files must set `option java_package`.",
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0191/java_package_test.go b/rules/aip0191/java_package_test.go
new file mode 100644
index 0000000..871eb26
--- /dev/null
+++ b/rules/aip0191/java_package_test.go
@@ -0,0 +1,41 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 		https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0191
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestJavaPackage(t *testing.T) {
+	for _, test := range []struct {
+		name       string
+		statements []string
+		problems   testutils.Problems
+	}{
+		{"Valid", []string{"package foo.v1;", `option java_package = "com.foo.v1";`}, testutils.Problems{}},
+		{"Invalid", []string{"package foo.v1;", ""}, testutils.Problems{{Message: "java_package"}}},
+		{"Ignored", []string{"", ""}, testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3String(t, strings.Join(test.statements, "\n"))
+			if diff := test.problems.SetDescriptor(f).Diff(javaPackage.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}