cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.57.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/comments/comments_test.go

507lines · modecode

1package comments_test
2
3import (
4 "errors"
5 "fmt"
6 "testing"
7 "time"
8
9 "github.com/cloudflare/pint/internal/comments"
10
11 "github.com/stretchr/testify/require"
12)
13
14func TestParse(t *testing.T) {
15 type testCaseT struct {
16 input string
17 output []comments.Comment
18 }
19
20 parseUntil := func(s string) time.Time {
21 until, err := time.Parse(time.RFC3339, s)
22 require.NoError(t, err)
23 return until
24 }
25
26 errUntil := func(s string) error {
27 _, err := time.Parse("2006-01-02", s)
28 require.Error(t, err)
29 return err
30 }
31
32 testCases := []testCaseT{
33 {
34 input: "code\n",
35 },
36 {
37 input: "code # bob\n",
38 },
39 {
40 input: "code # bob\ncode # alice\n",
41 },
42 {
43 input: "# pint bamboozle me this",
44 },
45 {
46 input: "# pint/xxx bamboozle me this",
47 },
48 {
49 input: "# pint bambo[]ozle me this",
50 },
51 {
52 input: "# pint ignore/file \t this file",
53 output: []comments.Comment{
54 {
55 Type: comments.InvalidComment,
56 Value: comments.Invalid{Err: comments.CommentError{
57 Line: 1,
58 Err: fmt.Errorf(`unexpected comment suffix: "this file"`),
59 }},
60 },
61 },
62 },
63 {
64 input: "# pint ignore/file",
65 output: []comments.Comment{
66 {Type: comments.IgnoreFileType},
67 },
68 },
69 {
70 input: "# pint ignore/line this line",
71 output: []comments.Comment{
72 {
73 Type: comments.InvalidComment,
74 Value: comments.Invalid{Err: comments.CommentError{
75 Line: 1,
76 Err: fmt.Errorf(`unexpected comment suffix: "this line"`),
77 }},
78 },
79 },
80 },
81 {
82 input: "# pint ignore/line",
83 output: []comments.Comment{
84 {Type: comments.IgnoreLineType},
85 },
86 },
87 {
88 input: "# pint ignore/begin here",
89 output: []comments.Comment{
90 {
91 Type: comments.InvalidComment,
92 Value: comments.Invalid{Err: comments.CommentError{
93 Line: 1,
94 Err: fmt.Errorf(`unexpected comment suffix: "here"`),
95 }},
96 },
97 },
98 },
99 {
100 input: "# pint ignore/begin",
101 output: []comments.Comment{
102 {Type: comments.IgnoreBeginType},
103 },
104 },
105 {
106 input: "# pint ignore/end here",
107 output: []comments.Comment{
108 {
109 Type: comments.InvalidComment,
110 Value: comments.Invalid{Err: comments.CommentError{
111 Line: 1,
112 Err: fmt.Errorf(`unexpected comment suffix: "here"`),
113 }},
114 },
115 },
116 },
117 {
118 input: "# pint ignore/end",
119 output: []comments.Comment{
120 {Type: comments.IgnoreEndType},
121 },
122 },
123 {
124 input: "# pint ignore/next-line\there",
125 output: []comments.Comment{
126 {
127 Type: comments.InvalidComment,
128 Value: comments.Invalid{Err: comments.CommentError{
129 Line: 1,
130 Err: fmt.Errorf(`unexpected comment suffix: "here"`),
131 }},
132 },
133 },
134 },
135 {
136 input: "# pint ignore/next-line",
137 output: []comments.Comment{
138 {Type: comments.IgnoreNextLineType},
139 },
140 },
141 {
142 input: "# pint file/owner",
143 output: []comments.Comment{
144 {
145 Type: comments.InvalidComment,
146 Value: comments.Invalid{Err: comments.CommentError{
147 Line: 1,
148 Err: fmt.Errorf("missing file/owner value"),
149 }},
150 },
151 },
152 },
153 {
154 input: "# pint file/owner bob and alice",
155 output: []comments.Comment{
156 {
157 Type: comments.FileOwnerType,
158 Value: comments.Owner{Name: "bob and alice"},
159 },
160 },
161 },
162 {
163 input: "# pint rule/owner",
164 output: []comments.Comment{
165 {
166 Type: comments.InvalidComment,
167 Value: comments.Invalid{Err: comments.CommentError{
168 Line: 1,
169 Err: fmt.Errorf("missing rule/owner value"),
170 }},
171 },
172 },
173 },
174 {
175 input: "# pint rule/owner bob and alice",
176 output: []comments.Comment{
177 {
178 Type: comments.RuleOwnerType,
179 Value: comments.Owner{Name: "bob and alice"},
180 },
181 },
182 },
183 {
184 input: "# pint file/disable",
185 output: []comments.Comment{
186 {
187 Type: comments.InvalidComment,
188 Value: comments.Invalid{Err: comments.CommentError{
189 Line: 1,
190 Err: fmt.Errorf("missing file/disable value"),
191 }},
192 },
193 },
194 },
195 {
196 input: `# pint file/disable promql/series(http_errors_total{label="this has spaces"})`,
197 output: []comments.Comment{
198 {
199 Type: comments.FileDisableType,
200 Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces"})`},
201 },
202 },
203 },
204 {
205 input: "# pint disable",
206 output: []comments.Comment{
207 {
208 Type: comments.InvalidComment,
209 Value: comments.Invalid{Err: comments.CommentError{
210 Line: 1,
211 Err: fmt.Errorf("missing disable value"),
212 }},
213 },
214 },
215 },
216 {
217 input: `# pint disable promql/series(http_errors_total{label="this has spaces"})`,
218 output: []comments.Comment{
219 {
220 Type: comments.DisableType,
221 Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces"})`},
222 },
223 },
224 },
225 {
226 input: `# pint disable promql/series(http_errors_total{label="this has spaces and a # symbol"})`,
227 output: []comments.Comment{
228 {
229 Type: comments.DisableType,
230 Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces and a # symbol"})`},
231 },
232 },
233 },
234 {
235 input: "# pint file/snooze",
236 output: []comments.Comment{
237 {
238 Type: comments.InvalidComment,
239 Value: comments.Invalid{Err: comments.CommentError{
240 Line: 1,
241 Err: fmt.Errorf("missing file/snooze value"),
242 }},
243 },
244 },
245 },
246 {
247 input: "# pint file/snooze 2023-12-31",
248 output: []comments.Comment{
249 {
250 Type: comments.InvalidComment,
251 Value: comments.Invalid{Err: comments.CommentError{
252 Line: 1,
253 Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`),
254 }},
255 },
256 },
257 },
258 {
259 input: "# pint file/snooze abc",
260 output: []comments.Comment{
261 {
262 Type: comments.InvalidComment,
263 Value: comments.Invalid{Err: comments.CommentError{
264 Line: 1,
265 Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`),
266 }},
267 },
268 },
269 },
270 {
271 input: `# pint file/snooze 2023-1231 promql/series(http_errors_total{label="this has spaces"})`,
272 output: []comments.Comment{
273 {
274 Type: comments.InvalidComment,
275 Value: comments.Invalid{Err: comments.CommentError{
276 Line: 1,
277 Err: fmt.Errorf("invalid snooze timestamp: %w", errUntil("2023-1231")),
278 }},
279 },
280 },
281 },
282 {
283 input: `# pint file/snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`,
284 output: []comments.Comment{
285 {
286 Type: comments.FileSnoozeType,
287 Value: comments.Snooze{
288 Until: parseUntil("2023-12-31T00:00:00Z"),
289 Match: `promql/series(http_errors_total{label="this has spaces"})`,
290 },
291 },
292 },
293 },
294 {
295 input: "# pint snooze",
296 output: []comments.Comment{
297 {
298 Type: comments.InvalidComment,
299 Value: comments.Invalid{Err: comments.CommentError{
300 Line: 1,
301 Err: fmt.Errorf("missing snooze value"),
302 }},
303 },
304 },
305 },
306 {
307 input: "# pint snooze 2023-12-31",
308 output: []comments.Comment{
309 {
310 Type: comments.InvalidComment,
311 Value: comments.Invalid{Err: comments.CommentError{
312 Line: 1,
313 Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`),
314 }},
315 },
316 },
317 },
318 {
319 input: "# pint snooze abc",
320 output: []comments.Comment{
321 {
322 Type: comments.InvalidComment,
323 Value: comments.Invalid{Err: comments.CommentError{
324 Line: 1,
325 Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`),
326 }},
327 },
328 },
329 },
330 {
331 input: `# pint snooze 2023-1231 promql/series(http_errors_total{label="this has spaces"})`,
332 output: []comments.Comment{
333 {
334 Type: comments.InvalidComment,
335 Value: comments.Invalid{Err: comments.CommentError{
336 Line: 1,
337 Err: fmt.Errorf("invalid snooze timestamp: %w", errUntil("2023-1231")),
338 }},
339 },
340 },
341 },
342 {
343 input: `# pint snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`,
344 output: []comments.Comment{
345 {
346 Type: comments.SnoozeType,
347 Value: comments.Snooze{
348 Until: parseUntil("2023-12-31T00:00:00Z"),
349 Match: `promql/series(http_errors_total{label="this has spaces"})`,
350 },
351 },
352 },
353 },
354 {
355 input: `# pint snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`,
356 output: []comments.Comment{
357 {
358 Type: comments.SnoozeType,
359 Value: comments.Snooze{
360 Until: parseUntil("2023-12-31T00:00:00Z"),
361 Match: `promql/series(http_errors_total{label="this has spaces"})`,
362 },
363 },
364 },
365 },
366 {
367 input: "# pint rule/set",
368 output: []comments.Comment{
369 {
370 Type: comments.InvalidComment,
371 Value: comments.Invalid{Err: comments.CommentError{
372 Line: 1,
373 Err: fmt.Errorf("missing rule/set value"),
374 }},
375 },
376 },
377 },
378 {
379 input: "# pint rule/set bob and alice",
380 output: []comments.Comment{
381 {
382 Type: comments.RuleSetType,
383 Value: comments.RuleSet{Value: "bob and alice"},
384 },
385 },
386 },
387 {
388 input: "code # pint disable xxx \ncode # alice\n",
389 output: []comments.Comment{
390 {
391 Type: comments.DisableType,
392 Value: comments.Disable{Match: "xxx"},
393 },
394 },
395 },
396 {
397 input: "code # pint disable xxx yyy \n # pint\tfile/owner bob",
398 output: []comments.Comment{
399 {
400 Type: comments.DisableType,
401 Value: comments.Disable{Match: "xxx yyy"},
402 },
403 {
404 Type: comments.FileOwnerType,
405 Value: comments.Owner{Name: "bob"},
406 },
407 },
408 },
409 {
410 input: "# pint rule/set promql/series(found) min-age foo",
411 output: []comments.Comment{
412 {
413 Type: comments.RuleSetType,
414 Value: comments.RuleSet{Value: "promql/series(found) min-age foo"},
415 },
416 },
417 },
418 {
419 input: "{#- comment #} # pint ignore/line",
420 output: []comments.Comment{
421 {
422 Type: comments.IgnoreLineType,
423 },
424 },
425 },
426 {
427 input: "{# comment #} # pint ignore/line",
428 output: []comments.Comment{
429 {
430 Type: comments.IgnoreLineType,
431 },
432 },
433 },
434 {
435 input: "#pint # pint # pint boo # pint ignore/line",
436 output: []comments.Comment{
437 {
438 Type: comments.IgnoreLineType,
439 },
440 },
441 },
442 {
443 input: "{# comment #} # pint ignore/line # pint ignore/file",
444 output: []comments.Comment{
445 {
446 Type: comments.InvalidComment,
447 Value: comments.Invalid{Err: comments.CommentError{
448 Line: 1,
449 Err: fmt.Errorf(`unexpected comment suffix: "# pint ignore/file"`),
450 }},
451 },
452 },
453 },
454 }
455
456 for _, tc := range testCases {
457 t.Run(tc.input, func(t *testing.T) {
458 output := comments.Parse(1, tc.input)
459 require.Equal(t, tc.output, output)
460 })
461 }
462}
463
464func TestCommentValueString(t *testing.T) {
465 type testCaseT struct {
466 comment comments.CommentValue
467 expected string
468 }
469
470 parseUntil := func(s string) time.Time {
471 until, err := time.Parse(time.RFC3339, s)
472 require.NoError(t, err)
473 return until
474 }
475
476 testCases := []testCaseT{
477 {
478 comment: comments.Invalid{Err: comments.CommentError{
479 Line: 1,
480 Err: errors.New("foo bar"),
481 }},
482 expected: "foo bar",
483 },
484 {
485 comment: comments.Owner{Name: "bob & alice"},
486 expected: "bob & alice",
487 },
488 {
489 comment: comments.Disable{Match: `promql/series({code="500"})`},
490 expected: `promql/series({code="500"})`,
491 },
492 {
493 comment: comments.RuleSet{Value: "bob & alice"},
494 expected: "bob & alice",
495 },
496 {
497 comment: comments.Snooze{Match: `promql/series({code="500"})`, Until: parseUntil("2023-11-28T00:00:00Z")},
498 expected: `2023-11-28T00:00:00Z promql/series({code="500"})`,
499 },
500 }
501
502 for _, tc := range testCases {
503 t.Run(tc.expected, func(t *testing.T) {
504 require.Equal(t, tc.expected, tc.comment.String())
505 })
506 }
507}
508