forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob_test.go
413 lines (391 loc) · 9.41 KB
/
glob_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package actionlint
import (
"fmt"
"strings"
"testing"
)
func TestInvalidGlobPatternMessage(t *testing.T) {
want := "42: this is message"
err := &InvalidGlobPattern{"this is message", 42}
have := err.Error()
if want != have {
t.Fatalf("want %q but have %q", want, have)
}
}
func TestValidateGlobOK(t *testing.T) {
testCases := []string{
"a",
"abc/def",
"[ab]",
"[a-z]",
"[a-zA-Z_]",
"[xA-Zy0-9z]",
"[xy][A-Z][yz][0-9][zx]",
"xy[A-Z]yz[0-9]zx",
"*",
"**",
"*/*",
"*/**",
"a?",
"a+",
`\+`,
`\\`,
`\!`, // escaped as "!"
`foo\!bar`, // escaped as "foo!bar"
`\++\\?`,
"[a-z]+",
"[a-z]?",
"*.*.*-**",
"!a",
"a!",
"a!+", // this is ok because ! has no special meaning
"a!?",
"!*",
// examples in official documents
"feature/*",
"feature/**",
"main",
"releases/mona-the-octcat",
"*",
"**",
"*feature",
"v2*",
"v[12].[0-9]+.[0-9]+",
}
for _, input := range testCases {
t.Run(input, func(t *testing.T) {
if errs := ValidateRefGlob(input); len(errs) > 0 {
t.Errorf("ref glob %q caused errors: %#v", input, errs)
}
if errs := ValidatePathGlob(input); len(errs) > 0 {
t.Errorf("path glob %q caused errors: %#v", input, errs)
}
})
}
}
func TestValidateGlobPathOnlyOK(t *testing.T) {
testCases := []string{
".",
"/foo",
"/foo/",
"/foo/bar",
`\[\?\*\+\\`,
"foo bar",
"~/foo",
"foo:bar",
"foo^bar",
// examples in official document
"*.jsx?",
"*.js",
"**.js",
"docs/*",
"docs/**",
"docs/**/*.md",
"**/docs/**",
"**/README.md",
"**/*src/**",
"*/*-post.md",
"**/migrate-*.sql",
"!README.md",
}
for _, input := range testCases {
t.Run(input, func(t *testing.T) {
if errs := ValidatePathGlob(input); len(errs) > 0 {
t.Fatalf("path glob %q caused errors: %#v", input, errs)
}
})
}
}
func TestValidateGlobSyntaxError(t *testing.T) {
testCases := []struct {
what string
input string
expected string
expectedAll []string
}{
{
what: "empty",
input: "",
expected: "glob pattern cannot be empty",
},
{
what: "nothing after negate",
input: "!",
expected: "at least one character must follow !",
},
{
what: "? as first character",
input: "?",
expected: "the preceding character must not be special character",
},
{
what: "? following special character",
input: "*?",
expected: "the preceding character must not be special character",
},
{
what: "? following ?",
input: "a??",
expected: "the preceding character must not be special character",
},
{
what: "+ as first character",
input: "+",
expected: "the preceding character must not be special character",
},
{
what: "+ following special character",
input: "*+",
expected: "the preceding character must not be special character",
},
{
what: "+ following +",
input: "a++",
expected: "the preceding character must not be special character",
},
{
what: "newline in pattern",
input: "\n",
expected: "newline cannot be contained",
},
{
what: `newline with \r in pattern`,
input: "\r",
expected: `'\r'`,
},
{
what: `newline with \r\n in pattern`,
input: "\r\n",
expected: `'\n'`,
},
{
what: "empty match",
input: "[]",
expected: "character match must not be empty",
},
{
what: "missing [ with empty match",
input: "[",
expected: "missing ]",
},
{
what: "missing [",
input: "[a",
expected: "missing ]",
},
{
what: "missing [ with range",
input: "[a-c",
expected: "missing ]",
},
{
what: "missing end of range",
input: "[a-]",
expected: "end of range is missing",
},
{
what: "EOF inside range",
input: "[a-",
expected: "missing ]",
},
{
what: "invalid range",
input: "[b-a]",
expected: "start of range 'b' (98) is larger than end of range 'a' (97)",
},
{
what: "single character match",
input: "[x]",
expected: "character match with single character is useless",
},
{
what: "multiple errors",
input: "+?[][a-]*+\n[b",
expectedAll: []string{
"the preceding character must not be special character",
"the preceding character must not be special character",
"character match must not be empty",
"end of range is missing",
"the preceding character must not be special character",
"newline cannot be contained",
"missing ]",
},
},
{
what: "preceding character is negate for ?",
input: "!?",
expected: "the preceding character must not be special character",
},
{
what: "preceding character is negate for +",
input: "!+",
expected: "the preceding character must not be special character",
},
}
for _, kind := range []string{"ref", "path"} {
for _, tc := range testCases {
t.Run(kind+"/"+tc.what, func(t *testing.T) {
var errs []InvalidGlobPattern
if kind == "ref" {
errs = ValidateRefGlob(tc.input)
} else {
errs = ValidatePathGlob(tc.input)
}
expected := tc.expectedAll
if len(expected) == 0 {
expected = []string{tc.expected}
}
if len(errs) != len(expected) {
t.Fatalf("wanted %d errors from %s glob %q but got %d errors", len(expected), kind, tc.input, len(errs))
}
for i := range errs {
err := errs[i]
want, have := expected[i], err.Message
if !strings.Contains(have, want) {
t.Errorf("%dth error message at col:%d from %s glob %q does not contain expected string:\n want: %s\n have: %s", i+1, err.Column, kind, tc.input, want, have)
}
}
})
}
}
}
func TestValidateGlobGitRefNameInvalidCharacter(t *testing.T) {
testCases := []struct {
what string
input string
expected string
expectedAll []string
}{
{
what: "start with /",
input: "/foo",
expected: "ref name must not start with /",
},
{
what: "end with /",
input: "foo/",
expected: "ref name must not end with / and .",
},
{
what: "end with .",
input: "foo.",
expected: "ref name must not end with / and .",
},
{
what: "escaped special chars",
input: `\[\?\*`,
expectedAll: []string{
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
},
},
{
what: "escaped non-special character",
input: `\d`,
expected: "only special characters [, ?, +, *, \\ ! can be escaped with \\",
},
{
what: "prohibited characters for ref names",
input: " ~^:",
expectedAll: []string{
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
},
},
{
what: "regular expression string",
input: `/^v\d+\.\d+\.(x|\d+)$/`,
expectedAll: []string{
"ref name must not start with /",
"ref name cannot contain spaces, ~, ^, :, [, ?, *",
"only special characters [, ?, +, *, \\ ! can be escaped with \\",
"only special characters [, ?, +, *, \\ ! can be escaped with \\",
"only special characters [, ?, +, *, \\ ! can be escaped with \\",
"only special characters [, ?, +, *, \\ ! can be escaped with \\",
"only special characters [, ?, +, *, \\ ! can be escaped with \\",
"ref name must not end with / and .",
},
},
}
for _, tc := range testCases {
t.Run(tc.what, func(t *testing.T) {
errs := ValidateRefGlob(tc.input)
expected := tc.expectedAll
if len(expected) == 0 {
expected = []string{tc.expected}
}
if len(errs) != len(expected) {
t.Fatalf("wanted %d errors from %q but got %d errors", len(expected), tc.input, len(errs))
}
for i := range errs {
err := errs[i]
want, have := expected[i], err.Message
if !strings.Contains(have, want) {
t.Errorf("%dth error message at col:%d from %q does not contain expected string:\n want: %s\n have: %s", i+1, err.Column, tc.input, want, have)
}
}
})
}
}
func TestValidateGlobErrorColumn(t *testing.T) {
testCases := []struct {
input string
col int
}{
{"", 0},
{"!", 1},
{"?", 1},
{"+", 1},
{"*?", 2},
{"a++", 3},
{"a\n", 0}, // fallback
{"[]", 2},
{"[0", 2},
{"[0-]", 4},
{"[0-", 3},
{"[b-a]", 4},
{"/foo", 1},
{"foo/", 4},
{"foo.", 4},
{`\[`, 2},
{`foo bar`, 4},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
errs := ValidateRefGlob(tc.input)
if len(errs) != 1 {
t.Fatalf("wanted 1 error from %q but got %d errors: %#v", tc.input, len(errs), errs)
}
want, have := tc.col, errs[0].Column
if want != have {
t.Errorf("error position is unexpected. wanted col:%d but have col:%d: %s", want, have, errs[0].Message)
}
})
}
}
func TestValidateGlobQuoteCharacterInErrorMessage(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"!", "'!'"},
{`\d`, `'\'`},
{"\n", `'\n'`},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%q shows %s", tc.input, tc.expected), func(t *testing.T) {
errs := ValidateRefGlob(tc.input)
if len(errs) == 0 {
t.Fatalf("no error found in %q", tc.input)
}
m := errs[0].Message
if !strings.Contains(m, tc.expected) {
t.Fatalf("error message %q does not contain %q", m, tc.expected)
}
})
}
}