Skip to content

Commit a23a896

Browse files
committed
fix: rule severity is required
1 parent c0f89fb commit a23a896

File tree

2 files changed

+110
-3
lines changed

2 files changed

+110
-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: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,94 @@ 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+
36+
for _, test := range testCases {
37+
test := test
38+
t.Run(test.desc, func(t *testing.T) {
39+
t.Parallel()
40+
41+
err := test.severity.Validate()
42+
require.NoError(t, err)
43+
})
44+
}
45+
}
46+
47+
func TestSeverity_Validate_error(t *testing.T) {
48+
testCases := []struct {
49+
desc string
50+
severity *Severity
51+
expected string
52+
}{
53+
{
54+
desc: "missing default severity",
55+
severity: &Severity{
56+
Default: "",
57+
Rules: []SeverityRule{
58+
{
59+
Severity: "low",
60+
BaseRule: BaseRule{
61+
Path: "test",
62+
},
63+
},
64+
},
65+
},
66+
expected: "can't set severity rule option: no default severity defined",
67+
},
68+
{
69+
desc: "missing rule severity",
70+
severity: &Severity{
71+
Default: "high",
72+
Rules: []SeverityRule{
73+
{
74+
BaseRule: BaseRule{
75+
Path: "test",
76+
},
77+
},
78+
},
79+
},
80+
expected: "error in severity rule #0: severity should be set",
81+
},
82+
}
83+
84+
for _, test := range testCases {
85+
test := test
86+
t.Run(test.desc, func(t *testing.T) {
87+
t.Parallel()
88+
89+
err := test.severity.Validate()
90+
require.EqualError(t, err, test.expected)
91+
})
92+
}
93+
}
94+
95+
func TestSeverityRule_Validate(t *testing.T) {
1096
rule := &SeverityRule{
97+
Severity: "low",
1198
BaseRule: BaseRule{
1299
Path: "test",
13100
},
@@ -17,20 +104,32 @@ func TestSeverity_Validate(t *testing.T) {
17104
require.NoError(t, err)
18105
}
19106

20-
func TestSeverity_Validate_error(t *testing.T) {
107+
func TestSeverityRule_Validate_error(t *testing.T) {
21108
testCases := []struct {
22109
desc string
23110
rule *SeverityRule
24111
expected string
25112
}{
26113
{
27-
desc: "empty rule",
28-
rule: &SeverityRule{},
114+
desc: "missing severity",
115+
rule: &SeverityRule{
116+
BaseRule: BaseRule{
117+
Path: "test",
118+
},
119+
},
120+
expected: "severity should be set",
121+
},
122+
{
123+
desc: "empty rule",
124+
rule: &SeverityRule{
125+
Severity: "low",
126+
},
29127
expected: "at least 1 of (text, source, path[-except], linters) should be set",
30128
},
31129
{
32130
desc: "invalid path rule",
33131
rule: &SeverityRule{
132+
Severity: "low",
34133
BaseRule: BaseRule{
35134
Path: "**test",
36135
},
@@ -40,6 +139,7 @@ func TestSeverity_Validate_error(t *testing.T) {
40139
{
41140
desc: "invalid path-except rule",
42141
rule: &SeverityRule{
142+
Severity: "low",
43143
BaseRule: BaseRule{
44144
PathExcept: "**test",
45145
},
@@ -49,6 +149,7 @@ func TestSeverity_Validate_error(t *testing.T) {
49149
{
50150
desc: "invalid text rule",
51151
rule: &SeverityRule{
152+
Severity: "low",
52153
BaseRule: BaseRule{
53154
Text: "**test",
54155
},
@@ -58,6 +159,7 @@ func TestSeverity_Validate_error(t *testing.T) {
58159
{
59160
desc: "invalid source rule",
60161
rule: &SeverityRule{
162+
Severity: "low",
61163
BaseRule: BaseRule{
62164
Source: "**test",
63165
},
@@ -67,6 +169,7 @@ func TestSeverity_Validate_error(t *testing.T) {
67169
{
68170
desc: "path and path-expect",
69171
rule: &SeverityRule{
172+
Severity: "low",
70173
BaseRule: BaseRule{
71174
Path: "test",
72175
PathExcept: "test",

0 commit comments

Comments
 (0)