fix: rule severity is required (#4469)

This commit is contained in:
Ludovic Fernandez 2024-03-09 15:29:15 +01:00 committed by GitHub
parent 7e2840b92e
commit 797d3bb0c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 124 additions and 3 deletions

View File

@ -34,5 +34,9 @@ type SeverityRule struct {
} }
func (s *SeverityRule) Validate() error { func (s *SeverityRule) Validate() error {
if s.Severity == "" {
return errors.New("severity should be set")
}
return s.BaseRule.Validate(severityRuleMinConditionsCount) return s.BaseRule.Validate(severityRuleMinConditionsCount)
} }

View File

@ -7,7 +7,108 @@ import (
) )
func TestSeverity_Validate(t *testing.T) { func TestSeverity_Validate(t *testing.T) {
testCases := []struct {
desc string
severity *Severity
}{
{
desc: "default with rules",
severity: &Severity{
Default: "high",
Rules: []SeverityRule{
{
Severity: "low",
BaseRule: BaseRule{
Path: "test",
},
},
},
},
},
{
desc: "default without rules",
severity: &Severity{
Default: "high",
},
},
{
desc: "same severity between default and rule",
severity: &Severity{
Default: "high",
Rules: []SeverityRule{
{
Severity: "high",
BaseRule: BaseRule{
Path: "test",
},
},
},
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
err := test.severity.Validate()
require.NoError(t, err)
})
}
}
func TestSeverity_Validate_error(t *testing.T) {
testCases := []struct {
desc string
severity *Severity
expected string
}{
{
desc: "missing default severity",
severity: &Severity{
Default: "",
Rules: []SeverityRule{
{
Severity: "low",
BaseRule: BaseRule{
Path: "test",
},
},
},
},
expected: "can't set severity rule option: no default severity defined",
},
{
desc: "missing rule severity",
severity: &Severity{
Default: "high",
Rules: []SeverityRule{
{
BaseRule: BaseRule{
Path: "test",
},
},
},
},
expected: "error in severity rule #0: severity should be set",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
err := test.severity.Validate()
require.EqualError(t, err, test.expected)
})
}
}
func TestSeverityRule_Validate(t *testing.T) {
rule := &SeverityRule{ rule := &SeverityRule{
Severity: "low",
BaseRule: BaseRule{ BaseRule: BaseRule{
Path: "test", Path: "test",
}, },
@ -17,20 +118,32 @@ func TestSeverity_Validate(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func TestSeverity_Validate_error(t *testing.T) { func TestSeverityRule_Validate_error(t *testing.T) {
testCases := []struct { testCases := []struct {
desc string desc string
rule *SeverityRule rule *SeverityRule
expected string expected string
}{ }{
{
desc: "missing severity",
rule: &SeverityRule{
BaseRule: BaseRule{
Path: "test",
},
},
expected: "severity should be set",
},
{ {
desc: "empty rule", desc: "empty rule",
rule: &SeverityRule{}, rule: &SeverityRule{
Severity: "low",
},
expected: "at least 1 of (text, source, path[-except], linters) should be set", expected: "at least 1 of (text, source, path[-except], linters) should be set",
}, },
{ {
desc: "invalid path rule", desc: "invalid path rule",
rule: &SeverityRule{ rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{ BaseRule: BaseRule{
Path: "**test", Path: "**test",
}, },
@ -40,6 +153,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{ {
desc: "invalid path-except rule", desc: "invalid path-except rule",
rule: &SeverityRule{ rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{ BaseRule: BaseRule{
PathExcept: "**test", PathExcept: "**test",
}, },
@ -49,6 +163,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{ {
desc: "invalid text rule", desc: "invalid text rule",
rule: &SeverityRule{ rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{ BaseRule: BaseRule{
Text: "**test", Text: "**test",
}, },
@ -58,6 +173,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{ {
desc: "invalid source rule", desc: "invalid source rule",
rule: &SeverityRule{ rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{ BaseRule: BaseRule{
Source: "**test", Source: "**test",
}, },
@ -67,6 +183,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{ {
desc: "path and path-expect", desc: "path and path-expect",
rule: &SeverityRule{ rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{ BaseRule: BaseRule{
Path: "test", Path: "test",
PathExcept: "test", PathExcept: "test",