Skip to content

Commit 797d3bb

Browse files
authored
fix: rule severity is required (#4469)
1 parent 7e2840b commit 797d3bb

File tree

2 files changed

+124
-3
lines changed

2 files changed

+124
-3
lines changed

pkg/config/severity.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ type SeverityRule struct {
3434
}
3535

3636
func (s *SeverityRule) Validate() error {
37+
if s.Severity == "" {
38+
return errors.New("severity should be set")
39+
}
40+
3741
return s.BaseRule.Validate(severityRuleMinConditionsCount)
3842
}

pkg/config/severity_test.go

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,108 @@ import (
77
)
88

99
func TestSeverity_Validate(t *testing.T) {
10+
testCases := []struct {
11+
desc string
12+
severity *Severity
13+
}{
14+
{
15+
desc: "default with rules",
16+
severity: &Severity{
17+
Default: "high",
18+
Rules: []SeverityRule{
19+
{
20+
Severity: "low",
21+
BaseRule: BaseRule{
22+
Path: "test",
23+
},
24+
},
25+
},
26+
},
27+
},
28+
{
29+
desc: "default without rules",
30+
severity: &Severity{
31+
Default: "high",
32+
},
33+
},
34+
{
35+
desc: "same severity between default and rule",
36+
severity: &Severity{
37+
Default: "high",
38+
Rules: []SeverityRule{
39+
{
40+
Severity: "high",
41+
BaseRule: BaseRule{
42+
Path: "test",
43+
},
44+
},
45+
},
46+
},
47+
},
48+
}
49+
50+
for _, test := range testCases {
51+
test := test
52+
t.Run(test.desc, func(t *testing.T) {
53+
t.Parallel()
54+
55+
err := test.severity.Validate()
56+
require.NoError(t, err)
57+
})
58+
}
59+
}
60+
61+
func TestSeverity_Validate_error(t *testing.T) {
62+
testCases := []struct {
63+
desc string
64+
severity *Severity
65+
expected string
66+
}{
67+
{
68+
desc: "missing default severity",
69+
severity: &Severity{
70+
Default: "",
71+
Rules: []SeverityRule{
72+
{
73+
Severity: "low",
74+
BaseRule: BaseRule{
75+
Path: "test",
76+
},
77+
},
78+
},
79+
},
80+
expected: "can't set severity rule option: no default severity defined",
81+
},
82+
{
83+
desc: "missing rule severity",
84+
severity: &Severity{
85+
Default: "high",
86+
Rules: []SeverityRule{
87+
{
88+
BaseRule: BaseRule{
89+
Path: "test",
90+
},
91+
},
92+
},
93+
},
94+
expected: "error in severity rule #0: severity should be set",
95+
},
96+
}
97+
98+
for _, test := range testCases {
99+
test := test
100+
t.Run(test.desc, func(t *testing.T) {
101+
t.Parallel()
102+
103+
err := test.severity.Validate()
104+
require.EqualError(t, err, test.expected)
105+
})
106+
}
107+
}
108+
109+
func TestSeverityRule_Validate(t *testing.T) {
10110
rule := &SeverityRule{
111+
Severity: "low",
11112
BaseRule: BaseRule{
12113
Path: "test",
13114
},
@@ -17,20 +118,32 @@ func TestSeverity_Validate(t *testing.T) {
17118
require.NoError(t, err)
18119
}
19120

20-
func TestSeverity_Validate_error(t *testing.T) {
121+
func TestSeverityRule_Validate_error(t *testing.T) {
21122
testCases := []struct {
22123
desc string
23124
rule *SeverityRule
24125
expected string
25126
}{
26127
{
27-
desc: "empty rule",
28-
rule: &SeverityRule{},
128+
desc: "missing severity",
129+
rule: &SeverityRule{
130+
BaseRule: BaseRule{
131+
Path: "test",
132+
},
133+
},
134+
expected: "severity should be set",
135+
},
136+
{
137+
desc: "empty rule",
138+
rule: &SeverityRule{
139+
Severity: "low",
140+
},
29141
expected: "at least 1 of (text, source, path[-except], linters) should be set",
30142
},
31143
{
32144
desc: "invalid path rule",
33145
rule: &SeverityRule{
146+
Severity: "low",
34147
BaseRule: BaseRule{
35148
Path: "**test",
36149
},
@@ -40,6 +153,7 @@ func TestSeverity_Validate_error(t *testing.T) {
40153
{
41154
desc: "invalid path-except rule",
42155
rule: &SeverityRule{
156+
Severity: "low",
43157
BaseRule: BaseRule{
44158
PathExcept: "**test",
45159
},
@@ -49,6 +163,7 @@ func TestSeverity_Validate_error(t *testing.T) {
49163
{
50164
desc: "invalid text rule",
51165
rule: &SeverityRule{
166+
Severity: "low",
52167
BaseRule: BaseRule{
53168
Text: "**test",
54169
},
@@ -58,6 +173,7 @@ func TestSeverity_Validate_error(t *testing.T) {
58173
{
59174
desc: "invalid source rule",
60175
rule: &SeverityRule{
176+
Severity: "low",
61177
BaseRule: BaseRule{
62178
Source: "**test",
63179
},
@@ -67,6 +183,7 @@ func TestSeverity_Validate_error(t *testing.T) {
67183
{
68184
desc: "path and path-expect",
69185
rule: &SeverityRule{
186+
Severity: "low",
70187
BaseRule: BaseRule{
71188
Path: "test",
72189
PathExcept: "test",

0 commit comments

Comments
 (0)