Skip to content
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
9 changes: 9 additions & 0 deletions pkg/config/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func (s *Severity) Validate() error {
}

for i, rule := range s.Rules {
if s.Default == rule.Severity {
return fmt.Errorf("error in severity rule #%d: the default severity (%q) is the same as the rule severity, this rule can be removed",
i, s.Default)
}

if err := rule.Validate(); err != nil {
return fmt.Errorf("error in severity rule #%d: %w", i, err)
}
Expand All @@ -34,5 +39,9 @@ type SeverityRule struct {
}

func (s *SeverityRule) Validate() error {
if s.Severity == "" {
return errors.New("severity should be set")
}

return s.BaseRule.Validate(severityRuleMinConditionsCount)
}
124 changes: 121 additions & 3 deletions pkg/config/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,109 @@ import (
)

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",
},
},
}

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",
},
{
desc: "same severity between default and rule",
severity: &Severity{
Default: "high",
Rules: []SeverityRule{
{
Severity: "high",
BaseRule: BaseRule{
Path: "test",
},
},
},
},
expected: `error in severity rule #0: the default severity ("high") is the same as the rule severity, this rule can be removed`,
},
}

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{
Severity: "low",
BaseRule: BaseRule{
Path: "test",
},
Expand All @@ -17,20 +119,32 @@ func TestSeverity_Validate(t *testing.T) {
require.NoError(t, err)
}

func TestSeverity_Validate_error(t *testing.T) {
func TestSeverityRule_Validate_error(t *testing.T) {
testCases := []struct {
desc string
rule *SeverityRule
expected string
}{
{
desc: "empty rule",
rule: &SeverityRule{},
desc: "missing severity",
rule: &SeverityRule{
BaseRule: BaseRule{
Path: "test",
},
},
expected: "severity should be set",
},
{
desc: "empty rule",
rule: &SeverityRule{
Severity: "low",
},
expected: "at least 1 of (text, source, path[-except], linters) should be set",
},
{
desc: "invalid path rule",
rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{
Path: "**test",
},
Expand All @@ -40,6 +154,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{
desc: "invalid path-except rule",
rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{
PathExcept: "**test",
},
Expand All @@ -49,6 +164,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{
desc: "invalid text rule",
rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{
Text: "**test",
},
Expand All @@ -58,6 +174,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{
desc: "invalid source rule",
rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{
Source: "**test",
},
Expand All @@ -67,6 +184,7 @@ func TestSeverity_Validate_error(t *testing.T) {
{
desc: "path and path-expect",
rule: &SeverityRule{
Severity: "low",
BaseRule: BaseRule{
Path: "test",
PathExcept: "test",
Expand Down