Repository: googleapis/api-linter
Pull request #358 (2019-11-19 00:51:20)

[AIP-191] Require `option java_multiple_files = true;`.
~This should not be merged until googleapis/aip#360 is.~ That PR is merged.


Patch:
diff --git a/docs/rules/0191/java-multiple-files.md b/docs/rules/0191/java-multiple-files.md
new file mode 100644
index 0000000..a58420e
--- /dev/null
+++ b/docs/rules/0191/java-multiple-files.md
@@ -0,0 +1,59 @@
+---
+rule:
+  aip: 191
+  name: [core, '0191', java-multiple-files]
+  summary: All proto files must set `option java_multiple_files = true`.
+permalink: /191/java-multiple-files
+redirect_from:
+  - /0191/java-multiple-files
+---
+
+# Java multiple files annotation
+
+This rule enforces that every proto file for a public API surface sets
+`option java_multiple_files = true;`, as mandated in [AIP-191][].
+
+## Details
+
+This rule looks at each proto file, and complains if the `java_multiple_files`
+file annotation is not present, or set to `false`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+syntax = "proto3";
+
+// Needs `option java_multiple_files = true;`.
+
+message Book {}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+syntax = "proto3";
+
+option java_multiple_files = true;
+
+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-multiple-files=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+syntax = "proto3";
+
+message Book {}
+```
+
+[aip-191]: https://aip.dev/191
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/lint/rule.go b/lint/rule.go
index 042d8f9..9d79724 100644
--- a/lint/rule.go
+++ b/lint/rule.go
@@ -43,6 +43,10 @@ type FileRule struct {
 	// Problems it finds.
 	LintFile func(*desc.FileDescriptor) []Problem
 
+	// OnlyIf accepts a FileDescriptor and determines whether this rule
+	// is applicable.
+	OnlyIf func(*desc.FileDescriptor) bool
+
 	noPositional struct{}
 }
 
@@ -54,7 +58,10 @@ func (r *FileRule) GetName() RuleName {
 // Lint forwards the FileDescriptor to the LintFile method defined on the
 // FileRule.
 func (r *FileRule) Lint(fd *desc.FileDescriptor) []Problem {
-	return r.LintFile(fd)
+	if r.OnlyIf == nil || r.OnlyIf(fd) {
+		return r.LintFile(fd)
+	}
+	return nil
 }
 
 // MessageRule defines a lint rule that is run on each message in the file.
diff --git a/rules/aip0191/aip0191.go b/rules/aip0191/aip0191.go
index a6dca94..0d4ead3 100644
--- a/rules/aip0191/aip0191.go
+++ b/rules/aip0191/aip0191.go
@@ -26,6 +26,7 @@ func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		191,
 		filename,
+		javaMultipleFiles,
 		syntax,
 	)
 }
diff --git a/rules/aip0191/java_multiple_files.go b/rules/aip0191/java_multiple_files.go
new file mode 100644
index 0000000..1eb7103
--- /dev/null
+++ b/rules/aip0191/java_multiple_files.go
@@ -0,0 +1,38 @@
+// 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 javaMultipleFiles = &lint.FileRule{
+	Name: lint.NewRuleName(191, "java-multiple-files"),
+	OnlyIf: func(f *desc.FileDescriptor) bool {
+		return f.GetPackage() != ""
+	},
+	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
+		if f.GetFileOptions().GetJavaMultipleFiles() == false {
+			return []lint.Problem{{
+				Descriptor: f,
+				Location:   locations.FilePackage(f),
+				Message:    "Proto files must set `option java_multiple_files = true;`",
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/lint/rule_test.go b/lint/rule_test.go
index f82a5f2..88ed290 100644
--- a/lint/rule_test.go
+++ b/lint/rule_test.go
@@ -34,6 +34,9 @@ func TestFileRule(t *testing.T) {
 		t.Run(test.testName, func(t *testing.T) {
 			rule := &FileRule{
 				Name: RuleName("test"),
+				OnlyIf: func(fd *desc.FileDescriptor) bool {
+					return fd.GetName() == "test.proto"
+				},
 				LintFile: func(fd *desc.FileDescriptor) []Problem {
 					return test.problems
 				},
diff --git a/rules/aip0191/java_multiple_files_test.go b/rules/aip0191/java_multiple_files_test.go
new file mode 100644
index 0000000..7de0678
--- /dev/null
+++ b/rules/aip0191/java_multiple_files_test.go
@@ -0,0 +1,44 @@
+// 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 (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestJavaMultipleFiles(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		Package string
+		Opt      string
+		problems testutils.Problems
+	}{
+		{"Valid", "package foo.v1;", "option java_multiple_files = true;", testutils.Problems{}},
+		{"Invalid", "package foo.v1;", "", testutils.Problems{{Message: "java_multiple_files"}}},
+		{"Ignored", "", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				{{.Package}}
+				{{.Opt}}
+			`, test)
+			if diff := test.problems.SetDescriptor(f).Diff(javaMultipleFiles.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}