@@ -4,11 +4,16 @@ import (
4
4
"testing"
5
5
6
6
"github.com/stretchr/testify/assert"
7
+ "github.com/stretchr/testify/require"
7
8
)
8
9
9
10
func TestGetExcludePatterns (t * testing.T ) {
10
- assert . Equal ( t , GetExcludePatterns (nil ), DefaultExcludePatterns )
11
+ patterns := GetExcludePatterns (nil )
11
12
13
+ assert .Equal (t , DefaultExcludePatterns , patterns )
14
+ }
15
+
16
+ func TestGetExcludePatterns_includes (t * testing.T ) {
12
17
include := []string {DefaultExcludePatterns [0 ].ID , DefaultExcludePatterns [1 ].ID }
13
18
14
19
exclude := GetExcludePatterns (include )
@@ -19,3 +24,182 @@ func TestGetExcludePatterns(t *testing.T) {
19
24
assert .Contains (t , DefaultExcludePatterns , p )
20
25
}
21
26
}
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
+ }
0 commit comments