Skip to content

Commit 3452e2e

Browse files
authored
dev: add test on ExcludeRule and SeverityRule (#4420)
1 parent 9ecfc05 commit 3452e2e

File tree

3 files changed

+288
-1
lines changed

3 files changed

+288
-1
lines changed

pkg/config/issues.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
)
@@ -141,41 +142,55 @@ func (b *BaseRule) Validate(minConditionsCount int) error {
141142
if err := validateOptionalRegex(b.Path); err != nil {
142143
return fmt.Errorf("invalid path regex: %w", err)
143144
}
145+
144146
if err := validateOptionalRegex(b.PathExcept); err != nil {
145147
return fmt.Errorf("invalid path-except regex: %w", err)
146148
}
149+
147150
if err := validateOptionalRegex(b.Text); err != nil {
148151
return fmt.Errorf("invalid text regex: %w", err)
149152
}
153+
150154
if err := validateOptionalRegex(b.Source); err != nil {
151155
return fmt.Errorf("invalid source regex: %w", err)
152156
}
157+
158+
if b.Path != "" && b.PathExcept != "" {
159+
return errors.New("path and path-except should not be set at the same time")
160+
}
161+
153162
nonBlank := 0
154163
if len(b.Linters) > 0 {
155164
nonBlank++
156165
}
166+
157167
// Filtering by path counts as one condition, regardless how it is done (one or both).
158168
// Otherwise, a rule with Path and PathExcept set would pass validation
159169
// whereas before the introduction of path-except that wouldn't have been precise enough.
160170
if b.Path != "" || b.PathExcept != "" {
161171
nonBlank++
162172
}
173+
163174
if b.Text != "" {
164175
nonBlank++
165176
}
177+
166178
if b.Source != "" {
167179
nonBlank++
168180
}
181+
169182
if nonBlank < minConditionsCount {
170183
return fmt.Errorf("at least %d of (text, source, path[-except], linters) should be set", minConditionsCount)
171184
}
185+
172186
return nil
173187
}
174188

175189
func validateOptionalRegex(value string) error {
176190
if value == "" {
177191
return nil
178192
}
193+
179194
_, err := regexp.Compile(value)
180195
return err
181196
}

pkg/config/issues_test.go

+185-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestGetExcludePatterns(t *testing.T) {
10-
assert.Equal(t, GetExcludePatterns(nil), DefaultExcludePatterns)
11+
patterns := GetExcludePatterns(nil)
1112

13+
assert.Equal(t, DefaultExcludePatterns, patterns)
14+
}
15+
16+
func TestGetExcludePatterns_includes(t *testing.T) {
1217
include := []string{DefaultExcludePatterns[0].ID, DefaultExcludePatterns[1].ID}
1318

1419
exclude := GetExcludePatterns(include)
@@ -19,3 +24,182 @@ func TestGetExcludePatterns(t *testing.T) {
1924
assert.Contains(t, DefaultExcludePatterns, p)
2025
}
2126
}
27+
28+
func TestExcludeRule_Validate(t *testing.T) {
29+
testCases := []struct {
30+
desc string
31+
rule *ExcludeRule
32+
expected string
33+
}{
34+
{
35+
desc: "empty rule",
36+
rule: &ExcludeRule{},
37+
expected: "at least 2 of (text, source, path[-except], linters) should be set",
38+
},
39+
{
40+
desc: "only path rule",
41+
rule: &ExcludeRule{
42+
BaseRule{
43+
Path: "test",
44+
},
45+
},
46+
expected: "at least 2 of (text, source, path[-except], linters) should be set",
47+
},
48+
{
49+
desc: "only path-except rule",
50+
rule: &ExcludeRule{
51+
BaseRule{
52+
PathExcept: "test",
53+
},
54+
},
55+
expected: "at least 2 of (text, source, path[-except], linters) should be set",
56+
},
57+
{
58+
desc: "only text rule",
59+
rule: &ExcludeRule{
60+
BaseRule{
61+
Text: "test",
62+
},
63+
},
64+
expected: "at least 2 of (text, source, path[-except], linters) should be set",
65+
},
66+
{
67+
desc: "only source rule",
68+
rule: &ExcludeRule{
69+
BaseRule{
70+
Source: "test",
71+
},
72+
},
73+
expected: "at least 2 of (text, source, path[-except], linters) should be set",
74+
},
75+
{
76+
desc: "invalid path rule",
77+
rule: &ExcludeRule{
78+
BaseRule{
79+
Path: "**test",
80+
},
81+
},
82+
expected: "invalid path regex: error parsing regexp: missing argument to repetition operator: `*`",
83+
},
84+
{
85+
desc: "invalid path-except rule",
86+
rule: &ExcludeRule{
87+
BaseRule{
88+
PathExcept: "**test",
89+
},
90+
},
91+
expected: "invalid path-except regex: error parsing regexp: missing argument to repetition operator: `*`",
92+
},
93+
{
94+
desc: "invalid text rule",
95+
rule: &ExcludeRule{
96+
BaseRule{
97+
Text: "**test",
98+
},
99+
},
100+
expected: "invalid text regex: error parsing regexp: missing argument to repetition operator: `*`",
101+
},
102+
{
103+
desc: "invalid source rule",
104+
rule: &ExcludeRule{
105+
BaseRule{
106+
Source: "**test",
107+
},
108+
},
109+
expected: "invalid source regex: error parsing regexp: missing argument to repetition operator: `*`",
110+
},
111+
{
112+
desc: "path and path-expect",
113+
rule: &ExcludeRule{
114+
BaseRule{
115+
Path: "test",
116+
PathExcept: "test",
117+
},
118+
},
119+
expected: "path and path-except should not be set at the same time",
120+
},
121+
}
122+
123+
for _, test := range testCases {
124+
test := test
125+
t.Run(test.desc, func(t *testing.T) {
126+
t.Parallel()
127+
128+
err := test.rule.Validate()
129+
require.EqualError(t, err, test.expected)
130+
})
131+
}
132+
}
133+
134+
func TestExcludeRule_Validate_error(t *testing.T) {
135+
testCases := []struct {
136+
desc string
137+
rule *ExcludeRule
138+
}{
139+
{
140+
desc: "path and linter",
141+
rule: &ExcludeRule{
142+
BaseRule{
143+
Path: "test",
144+
Linters: []string{"a"},
145+
},
146+
},
147+
},
148+
{
149+
desc: "path-except and linter",
150+
rule: &ExcludeRule{
151+
BaseRule{
152+
PathExcept: "test",
153+
Linters: []string{"a"},
154+
},
155+
},
156+
},
157+
{
158+
desc: "text and linter",
159+
rule: &ExcludeRule{
160+
BaseRule{
161+
Text: "test",
162+
Linters: []string{"a"},
163+
},
164+
},
165+
},
166+
{
167+
desc: "source and linter",
168+
rule: &ExcludeRule{
169+
BaseRule{
170+
Source: "test",
171+
Linters: []string{"a"},
172+
},
173+
},
174+
},
175+
{
176+
desc: "path and text",
177+
rule: &ExcludeRule{
178+
BaseRule{
179+
Path: "test",
180+
Text: "test",
181+
},
182+
},
183+
},
184+
{
185+
desc: "path and text and linter",
186+
rule: &ExcludeRule{
187+
BaseRule{
188+
Path: "test",
189+
Text: "test",
190+
Linters: []string{"a"},
191+
},
192+
},
193+
},
194+
}
195+
196+
for _, test := range testCases {
197+
test := test
198+
t.Run(test.desc, func(t *testing.T) {
199+
t.Parallel()
200+
201+
err := test.rule.Validate()
202+
require.NoError(t, err)
203+
})
204+
}
205+
}

pkg/config/severity_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestSeverity_Validate(t *testing.T) {
10+
rule := &SeverityRule{
11+
BaseRule: BaseRule{
12+
Path: "test",
13+
},
14+
}
15+
16+
err := rule.Validate()
17+
require.NoError(t, err)
18+
}
19+
20+
func TestSeverity_Validate_error(t *testing.T) {
21+
testCases := []struct {
22+
desc string
23+
rule *SeverityRule
24+
expected string
25+
}{
26+
{
27+
desc: "empty rule",
28+
rule: &SeverityRule{},
29+
expected: "at least 1 of (text, source, path[-except], linters) should be set",
30+
},
31+
{
32+
desc: "invalid path rule",
33+
rule: &SeverityRule{
34+
BaseRule: BaseRule{
35+
Path: "**test",
36+
},
37+
},
38+
expected: "invalid path regex: error parsing regexp: missing argument to repetition operator: `*`",
39+
},
40+
{
41+
desc: "invalid path-except rule",
42+
rule: &SeverityRule{
43+
BaseRule: BaseRule{
44+
PathExcept: "**test",
45+
},
46+
},
47+
expected: "invalid path-except regex: error parsing regexp: missing argument to repetition operator: `*`",
48+
},
49+
{
50+
desc: "invalid text rule",
51+
rule: &SeverityRule{
52+
BaseRule: BaseRule{
53+
Text: "**test",
54+
},
55+
},
56+
expected: "invalid text regex: error parsing regexp: missing argument to repetition operator: `*`",
57+
},
58+
{
59+
desc: "invalid source rule",
60+
rule: &SeverityRule{
61+
BaseRule: BaseRule{
62+
Source: "**test",
63+
},
64+
},
65+
expected: "invalid source regex: error parsing regexp: missing argument to repetition operator: `*`",
66+
},
67+
{
68+
desc: "path and path-expect",
69+
rule: &SeverityRule{
70+
BaseRule: BaseRule{
71+
Path: "test",
72+
PathExcept: "test",
73+
},
74+
},
75+
expected: "path and path-except should not be set at the same time",
76+
},
77+
}
78+
79+
for _, test := range testCases {
80+
test := test
81+
t.Run(test.desc, func(t *testing.T) {
82+
t.Parallel()
83+
84+
err := test.rule.Validate()
85+
require.EqualError(t, err, test.expected)
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)