Repository: googleapis/api-linter
Pull request #249 (2019-10-21 00:59:21)

[AIP-191] Prohibit proto versions as filenames.


Patch:
diff --git a/rules/aip0191/aip0191.go b/rules/aip0191/aip0191.go
index fbdee1d..3296b41 100644
--- a/rules/aip0191/aip0191.go
+++ b/rules/aip0191/aip0191.go
@@ -16,12 +16,17 @@
 package aip0191
 
 import (
+	"regexp"
+
 	"github.com/googleapis/api-linter/lint"
 )
 
 // AddRules adds all of the AIP-191 rules to the provided registry.
 func AddRules(r lint.RuleRegistry) {
 	r.Register(
+		filename,
 		syntax,
 	)
 }
+
+var versionRegexp = regexp.MustCompile("^v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]*)?$")
diff --git a/rules/aip0191/filename.go b/rules/aip0191/filename.go
new file mode 100644
index 0000000..3aaa68b
--- /dev/null
+++ b/rules/aip0191/filename.go
@@ -0,0 +1,39 @@
+// 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 (
+	"path/filepath"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var filename = &lint.FileRule{
+	Name: lint.NewRuleName("core", "0191", "filename"),
+	URI:  "https://aip.dev/191#guidance",
+	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
+		fn := strings.ReplaceAll(filepath.Base(f.GetName()), ".proto", "")
+		if versionRegexp.MatchString(fn) {
+			return []lint.Problem{{
+				Message:    "The proto version must not be used as the filename.",
+				Descriptor: f,
+				Location:   lint.PackageLocation(f),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0191/filename_test.go b/rules/aip0191/filename_test.go
new file mode 100644
index 0000000..a39d515
--- /dev/null
+++ b/rules/aip0191/filename_test.go
@@ -0,0 +1,52 @@
+// 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"
+	"github.com/jhump/protoreflect/desc/builder"
+)
+
+func TestFilename(t *testing.T) {
+	tests := []struct {
+		testName string
+		filename string
+		problems testutils.Problems
+	}{
+		{"Valid", "library.proto", testutils.Problems{}},
+		{"ValidDirectory", "google/library.proto", testutils.Problems{}},
+		{"InvalidStable", "v1.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidBigStable", "v20.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidStableDirectory", "google/library/v1.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidAlphaUnnumbered", "v1alpha.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidAlphaNumbered", "v1alpha1.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidBetaUnnumbered", "v1beta.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidBetaNumbered", "v1beta1.proto", testutils.Problems{{Message: "proto version"}}},
+		{"InvalidPoint", "v1p1beta1.proto", testutils.Problems{{Message: "proto version"}}},
+	}
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			f, err := builder.NewFile(test.filename).Build()
+			if err != nil {
+				t.Fatalf("Failed to build file.")
+			}
+			if diff := test.problems.SetDescriptor(f).Diff(filename.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}