Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AIP-191): valid characters in filename #948

Merged
merged 5 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/rules/0191/filenames.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ 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

**Incorrect** filenames for this rule:

- `v1.proto`
- `v1beta1.proto`
- `library.service.proto`
- `library#.proto`
- `library$.proto`

**Correct** filenames for this rule:

Expand Down
3 changes: 2 additions & 1 deletion rules/aip0191/aip0191.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-zA-Z0-9\\_\\/]*$`)
7 changes: 7 additions & 0 deletions rules/aip0191/filenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
5 changes: 5 additions & 0 deletions rules/aip0191/filenames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ 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"}}},
{"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"}}},
Expand Down