diff --git a/docs/rules/0191/filenames.md b/docs/rules/0191/filenames.md index 48f9cfba2..601a785a4 100644 --- a/docs/rules/0191/filenames.md +++ b/docs/rules/0191/filenames.md @@ -21,6 +21,7 @@ paths), it is important not to have odd paths. This rule currently complains if the filename: - ...is set to the proto version. +- ...contains invalid cahracters. ## Examples @@ -28,6 +29,11 @@ This rule currently complains if the filename: - `v1.proto` - `v1beta1.proto` +- `library.service.proto` +- `library#.proto` +- `library$.proto` +- `library service.proto` +- `library_Service.proto` **Correct** filenames for this rule: diff --git a/rules/aip0191/aip0191.go b/rules/aip0191/aip0191.go index 42cd4db89..f89f7d099 100644 --- a/rules/aip0191/aip0191.go +++ b/rules/aip0191/aip0191.go @@ -44,4 +44,5 @@ func hasPackage(f *desc.FileDescriptor) bool { return f.GetPackage() != "" } -var versionRegexp = regexp.MustCompile("^v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]*)?$") +var versionRegexp = regexp.MustCompile(`^v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]*)?$`) +var validCharacterRegexp = regexp.MustCompile(`^[a-z0-9\\_\\/]*$`) diff --git a/rules/aip0191/filenames.go b/rules/aip0191/filenames.go index 0c801af8b..195ef6bfc 100644 --- a/rules/aip0191/filenames.go +++ b/rules/aip0191/filenames.go @@ -34,6 +34,13 @@ var filename = &lint.FileRule{ Location: locations.FilePackage(f), }} } + if !validCharacterRegexp.MatchString(fn) { + return []lint.Problem{{ + Message: "The filename has invalid characters.", + Descriptor: f, + Location: locations.FilePackage(f), + }} + } return nil }, } diff --git a/rules/aip0191/filenames_test.go b/rules/aip0191/filenames_test.go index a39d515cc..9849b0f15 100644 --- a/rules/aip0191/filenames_test.go +++ b/rules/aip0191/filenames_test.go @@ -29,6 +29,12 @@ func TestFilename(t *testing.T) { }{ {"Valid", "library.proto", testutils.Problems{}}, {"ValidDirectory", "google/library.proto", testutils.Problems{}}, + {"ValidFileNameWithSnakeCase", "library_test.proto", testutils.Problems{}}, + {"InvalidFileNameNotSnakeCase", "library.test.proto", testutils.Problems{{Message: "invalid characters"}}}, + {"InvalidCharacterUpperCase", "library_Test.proto", testutils.Problems{{Message: "invalid characters"}}}, + {"InvalidCharacterDollar", "library_$test.proto", testutils.Problems{{Message: "invalid characters"}}}, + {"InvalidCharacterSpace", "library test.proto", testutils.Problems{{Message: "invalid characters"}}}, + {"InvalidCharacterHash", "library_#test.proto", testutils.Problems{{Message: "invalid characters"}}}, {"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"}}},