Skip to content

Commit

Permalink
Merge pull request #420 from profhenry/419_configuring_severity_does_…
Browse files Browse the repository at this point in the history
…not_work_for_MaxLineLengthRule

Fixed configuring severity for max line length rule
  • Loading branch information
yoheimuta authored Jul 7, 2024
2 parents b7dd32c + 145d2ec commit 1f1793c
Show file tree
Hide file tree
Showing 47 changed files with 230 additions and 89 deletions.
2 changes: 1 addition & 1 deletion _example/plugin/customrules/simpleRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func (r SimpleRule) Severity() rule.Severity {
// Apply applies the rule to the proto.
func (r SimpleRule) Apply(proto *parser.Proto) ([]report.Failure, error) {
return []report.Failure{
report.Failuref(meta.Position{}, r.ID(), "Custom Rule, verbose=%v, fixMode=%v", r.verbose, r.fixMode),
report.Failuref(meta.Position{}, r.ID(), r.severity, "Custom Rule, verbose=%v, fixMode=%v", r.verbose, r.fixMode),
}, nil
}
2 changes: 1 addition & 1 deletion internal/addon/plugin/externalRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r externalRule) Apply(p *parser.Proto) ([]report.Failure, error) {

var fs []report.Failure
for _, f := range resp.Failures {
fs = append(fs, report.FailureWithSeverityf(meta.Position{
fs = append(fs, report.Failuref(meta.Position{
Filename: relPath,
Offset: int(f.Pos.Offset),
Line: int(f.Pos.Line),
Expand Down
4 changes: 4 additions & 0 deletions internal/addon/rules/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func NewRuleWithSeverity(
}

// Severity returns the configured severity.
// If no severity is set, the default severity will be ERROR
func (r RuleWithSeverity) Severity() rule.Severity {
if r.severity == "" {
return rule.SeverityError
}
return r.severity
}
1 change: 1 addition & 0 deletions internal/addon/rules/enumFieldNamesPrefixRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func TestEnumFieldNamesPrefixRule_Apply(t *testing.T) {
Column: 10,
},
"ENUM_FIELD_NAMES_PREFIX",
string(rule.SeverityError),
`EnumField name "BAR_UNSPECIFIED" should have the prefix "FOO_BAR"`,
),
},
Expand Down
2 changes: 2 additions & 0 deletions internal/addon/rules/enumFieldNamesUpperSnakeCaseRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestEnumFieldNamesUpperSnakeCaseRule_Apply(t *testing.T) {
Column: 10,
},
"ENUM_FIELD_NAMES_UPPER_SNAKE_CASE",
string(rule.SeverityError),
`EnumField name "fIRST_VALUE" must be CAPITALS_WITH_UNDERSCORES like "FIRST_VALUE"`,
),
report.Failuref(
Expand All @@ -96,6 +97,7 @@ func TestEnumFieldNamesUpperSnakeCaseRule_Apply(t *testing.T) {
Column: 20,
},
"ENUM_FIELD_NAMES_UPPER_SNAKE_CASE",
string(rule.SeverityError),
`EnumField name "secondValue" must be CAPITALS_WITH_UNDERSCORES like "SECOND_VALUE"`,
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestEnumFieldNamesZeroValueEndWithRule_Apply(t *testing.T) {
Column: 10,
},
"ENUM_FIELD_NAMES_ZERO_VALUE_END_WITH",
string(rule.SeverityError),
`EnumField name "FIRST_VALUE" with zero value should have the suffix "UNSPECIFIED"`,
),
},
Expand Down
3 changes: 3 additions & 0 deletions internal/addon/rules/enumFieldsHaveCommentRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestEnumFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 15,
},
"ENUM_FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`EnumField "EnumFieldName" should have a comment`,
),
},
Expand Down Expand Up @@ -141,6 +142,7 @@ func TestEnumFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 15,
},
"ENUM_FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`EnumField "EnumFieldName" should have a comment of the form "// EnumFieldName ..."`,
),
},
Expand All @@ -166,6 +168,7 @@ func TestEnumFieldsHaveCommentRule_Apply(t *testing.T) {
report.Failuref(
meta.Position{},
"ENUM_FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`EnumField "EnumFieldName" should have a comment of the form "// EnumFieldName ..."`,
),
},
Expand Down
2 changes: 2 additions & 0 deletions internal/addon/rules/enumNamesUpperCamelCaseRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestEnumNamesUpperCamelCaseRule_Apply(t *testing.T) {
Column: 10,
},
"ENUM_NAMES_UPPER_CAMEL_CASE",
string(rule.SeverityError),
`Enum name "enumName" must be UpperCamelCase like "EnumName"`,
),
report.Failuref(
Expand All @@ -89,6 +90,7 @@ func TestEnumNamesUpperCamelCaseRule_Apply(t *testing.T) {
Column: 20,
},
"ENUM_NAMES_UPPER_CAMEL_CASE",
string(rule.SeverityError),
`Enum name "Enum_name" must be UpperCamelCase like "EnumName"`,
),
},
Expand Down
4 changes: 4 additions & 0 deletions internal/addon/rules/enumsHaveCommentRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestEnumsHaveCommentRule_Apply(t *testing.T) {
Column: 15,
},
"ENUMS_HAVE_COMMENT",
string(rule.SeverityError),
`Enum "EnumName" should have a comment`,
),
},
Expand Down Expand Up @@ -131,6 +132,7 @@ func TestEnumsHaveCommentRule_Apply(t *testing.T) {
Column: 15,
},
"ENUMS_HAVE_COMMENT",
string(rule.SeverityError),
`Enum "EnumName" should have a comment of the form "// EnumName ..."`,
),
},
Expand Down Expand Up @@ -158,11 +160,13 @@ func TestEnumsHaveCommentRule_Apply(t *testing.T) {
report.Failuref(
meta.Position{},
"ENUMS_HAVE_COMMENT",
string(rule.SeverityError),
`Enum "EnumName" should have a comment of the form "// EnumName ..."`,
),
report.Failuref(
meta.Position{},
"ENUMS_HAVE_COMMENT",
string(rule.SeverityError),
`Enum "EnumName2" should have a comment of the form "// EnumName2 ..."`,
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
Column: 10,
},
"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
string(rule.SeverityError),
`Field name "reason_for_error" should not include a preposition "for"`,
),
report.Failuref(
Expand All @@ -134,6 +135,7 @@ func TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
Column: 20,
},
"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
string(rule.SeverityError),
`Field name "cpu_usage_at_time_of_failure" should not include a preposition "at"`,
),
report.Failuref(
Expand All @@ -144,6 +146,7 @@ func TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
Column: 20,
},
"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
string(rule.SeverityError),
`Field name "cpu_usage_at_time_of_failure" should not include a preposition "of"`,
),
report.Failuref(
Expand All @@ -154,6 +157,7 @@ func TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
Column: 30,
},
"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
string(rule.SeverityError),
`Field name "name_of_song" should not include a preposition "of"`,
),
report.Failuref(
Expand All @@ -164,6 +168,7 @@ func TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
Column: 45,
},
"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
string(rule.SeverityError),
`Field name "name_of_song2" should not include a preposition "of"`,
),
},
Expand Down Expand Up @@ -214,6 +219,7 @@ func TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
Column: 20,
},
"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
string(rule.SeverityError),
`Field name "version_of_support_end" should not include a preposition "of"`,
),
},
Expand Down
3 changes: 3 additions & 0 deletions internal/addon/rules/fieldNamesLowerSnakeCaseRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestFieldNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 10,
},
"FIELD_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`Field name "song_Name" must be underscore_separated_names like "song_name"`,
),
report.Failuref(
Expand All @@ -121,6 +122,7 @@ func TestFieldNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 30,
},
"FIELD_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`Field name "MapFieldName" must be underscore_separated_names like "map_field_name"`,
),
report.Failuref(
Expand All @@ -131,6 +133,7 @@ func TestFieldNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 45,
},
"FIELD_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`Field name "OneofFieldName" must be underscore_separated_names like "oneof_field_name"`,
),
},
Expand Down
9 changes: 9 additions & 0 deletions internal/addon/rules/fieldsHaveCommentRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 15,
},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "FieldName" should have a comment`,
),
report.Failuref(
Expand All @@ -193,6 +194,7 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 30,
},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "MapFieldName" should have a comment`,
),
report.Failuref(
Expand All @@ -203,6 +205,7 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 45,
},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "OneofFieldName" should have a comment`,
),
},
Expand Down Expand Up @@ -279,6 +282,7 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 15,
},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "FieldName" should have a comment of the form "// FieldName ..."`,
),
report.Failuref(
Expand All @@ -289,6 +293,7 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 30,
},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "MapFieldName" should have a comment of the form "// MapFieldName ..."`,
),
report.Failuref(
Expand All @@ -299,6 +304,7 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
Column: 45,
},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "OneofFieldName" should have a comment of the form "// OneofFieldName ..."`,
),
},
Expand Down Expand Up @@ -340,16 +346,19 @@ func TestFieldsHaveCommentRule_Apply(t *testing.T) {
report.Failuref(
meta.Position{},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "FieldName2" should have a comment of the form "// FieldName2 ..."`,
),
report.Failuref(
meta.Position{},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "MapFieldName2" should have a comment of the form "// MapFieldName2 ..."`,
),
report.Failuref(
meta.Position{},
"FIELDS_HAVE_COMMENT",
string(rule.SeverityError),
`Field "OneofFieldName2" should have a comment of the form "// OneofFieldName2 ..."`,
),
},
Expand Down
1 change: 1 addition & 0 deletions internal/addon/rules/fileHasCommentRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestFileHasCommentRule_Apply(t *testing.T) {
Column: 15,
},
"FILE_HAS_COMMENT",
string(rule.SeverityError),
`File should start with a doc comment`,
),
},
Expand Down
4 changes: 4 additions & 0 deletions internal/addon/rules/fileNamesLowerSnakeCaseRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestFileNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 1,
},
"FILE_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`File name "lowerSnakeCase.proto" should be lower_snake_case.proto like "lower_snake_case.proto".`,
),
},
Expand All @@ -90,6 +91,7 @@ func TestFileNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 1,
},
"FILE_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`File name "lowerSnakeCase.txt" should be lower_snake_case.proto like "lower_snake_case.proto".`,
),
},
Expand All @@ -110,6 +112,7 @@ func TestFileNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 1,
},
"FILE_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`File name "dot.separated.proto" should be lower_snake_case.proto like "dot_separated.proto".`,
),
},
Expand All @@ -130,6 +133,7 @@ func TestFileNamesLowerSnakeCaseRule_Apply(t *testing.T) {
Column: 1,
},
"FILE_NAMES_LOWER_SNAKE_CASE",
string(rule.SeverityError),
`File name "user-role.proto" should be lower_snake_case.proto like "user_role.proto".`,
),
},
Expand Down
6 changes: 6 additions & 0 deletions internal/addon/rules/importsSortedRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestImportsSortedRule_Apply(t *testing.T) {
Column: 1,
},
"IMPORTS_SORTED",
string(rule.SeverityError),
`Imports are not sorted.`,
),
report.Failuref(
Expand All @@ -57,6 +58,7 @@ func TestImportsSortedRule_Apply(t *testing.T) {
Column: 1,
},
"IMPORTS_SORTED",
string(rule.SeverityError),
`Imports are not sorted.`,
),
},
Expand All @@ -73,6 +75,7 @@ func TestImportsSortedRule_Apply(t *testing.T) {
Column: 1,
},
"IMPORTS_SORTED",
string(rule.SeverityError),
`Imports are not sorted.`,
),
report.Failuref(
Expand All @@ -83,6 +86,7 @@ func TestImportsSortedRule_Apply(t *testing.T) {
Column: 1,
},
"IMPORTS_SORTED",
string(rule.SeverityError),
`Imports are not sorted.`,
),
report.Failuref(
Expand All @@ -93,6 +97,7 @@ func TestImportsSortedRule_Apply(t *testing.T) {
Column: 1,
},
"IMPORTS_SORTED",
string(rule.SeverityError),
`Imports are not sorted.`,
),
report.Failuref(
Expand All @@ -103,6 +108,7 @@ func TestImportsSortedRule_Apply(t *testing.T) {
Column: 1,
},
"IMPORTS_SORTED",
string(rule.SeverityError),
`Imports are not sorted.`,
),
},
Expand Down
Loading

0 comments on commit 1f1793c

Please sign in to comment.