cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.66.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/comments/comments_test.go

522lines · 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: errors.New(`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: errors.New(`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: errors.New(`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: errors.New(`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: errors.New(`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: errors.New("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: errors.New("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: errors.New("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: errors.New("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: errors.New("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: errors.New(`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: errors.New(`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: errors.New("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: errors.New(`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: errors.New(`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: errors.New("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 Offset: len("code "),
394 },
395 },
396 },
397 {
398 input: "code # pint disable xxx yyy \n # pint\tfile/owner bob",
399 output: []comments.Comment{
400 {
401 Type: comments.DisableType,
402 Value: comments.Disable{Match: "xxx yyy"},
403 Offset: len("code "),
404 },
405 {
406 Type: comments.FileOwnerType,
407 Value: comments.Owner{Name: "bob"},
408 Offset: 1,
409 },
410 },
411 },
412 {
413 input: "# pint rule/set promql/series(found) min-age foo",
414 output: []comments.Comment{
415 {
416 Type: comments.RuleSetType,
417 Value: comments.RuleSet{Value: "promql/series(found) min-age foo"},
418 },
419 },
420 },
421 {
422 input: "{#- comment #} # pint ignore/line",
423 output: []comments.Comment{
424 {
425 Type: comments.IgnoreLineType,
426 Offset: len("{#- comment #} "),
427 },
428 },
429 },
430 {
431 input: "{# comment #} # pint ignore/line",
432 output: []comments.Comment{
433 {
434 Type: comments.IgnoreLineType,
435 Offset: len("{# comment #} "),
436 },
437 },
438 },
439 {
440 input: "#pint # pint # pint boo # pint ignore/line",
441 output: []comments.Comment{
442 {
443 Type: comments.IgnoreLineType,
444 Offset: len("#pint # pint # pint boo "),
445 },
446 },
447 },
448 {
449 input: "{# comment #} # pint ignore/line # pint ignore/file",
450 output: []comments.Comment{
451 {
452 Type: comments.InvalidComment,
453 Value: comments.Invalid{Err: comments.CommentError{
454 Line: 1,
455 Err: errors.New(`unexpected comment suffix: "# pint ignore/file"`),
456 }},
457 },
458 },
459 },
460 {
461 input: "{#- JIRA-12345: foo<->bar example comment ' -#} # pint ignore/line",
462 output: []comments.Comment{
463 {
464 Type: comments.IgnoreLineType,
465 Offset: len("{#- JIRA-12345: foo<->bar example comment ' -#} "),
466 },
467 },
468 },
469 }
470
471 for _, tc := range testCases {
472 t.Run(tc.input, func(t *testing.T) {
473 output := comments.Parse(1, tc.input)
474 require.Equal(t, tc.output, output)
475 })
476 }
477}
478
479func TestCommentValueString(t *testing.T) {
480 type testCaseT struct {
481 comment comments.CommentValue
482 expected string
483 }
484
485 parseUntil := func(s string) time.Time {
486 until, err := time.Parse(time.RFC3339, s)
487 require.NoError(t, err)
488 return until
489 }
490
491 testCases := []testCaseT{
492 {
493 comment: comments.Invalid{Err: comments.CommentError{
494 Line: 1,
495 Err: errors.New("foo bar"),
496 }},
497 expected: "foo bar",
498 },
499 {
500 comment: comments.Owner{Name: "bob & alice"},
501 expected: "bob & alice",
502 },
503 {
504 comment: comments.Disable{Match: `promql/series({code="500"})`},
505 expected: `promql/series({code="500"})`,
506 },
507 {
508 comment: comments.RuleSet{Value: "bob & alice"},
509 expected: "bob & alice",
510 },
511 {
512 comment: comments.Snooze{Match: `promql/series({code="500"})`, Until: parseUntil("2023-11-28T00:00:00Z")},
513 expected: `2023-11-28T00:00:00Z promql/series({code="500"})`,
514 },
515 }
516
517 for _, tc := range testCases {
518 t.Run(tc.expected, func(t *testing.T) {
519 require.Equal(t, tc.expected, tc.comment.String())
520 })
521 }
522}
523