cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.51.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/comments/comments_test.go

447lines · 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: fmt.Errorf(`unexpected comment suffix: "this file"`)},
57 },
58 },
59 },
60 {
61 input: "# pint ignore/file",
62 output: []comments.Comment{
63 {Type: comments.IgnoreFileType},
64 },
65 },
66 {
67 input: "# pint ignore/line this line",
68 output: []comments.Comment{
69 {
70 Type: comments.InvalidComment,
71 Value: comments.Invalid{Err: fmt.Errorf(`unexpected comment suffix: "this line"`)},
72 },
73 },
74 },
75 {
76 input: "# pint ignore/line",
77 output: []comments.Comment{
78 {Type: comments.IgnoreLineType},
79 },
80 },
81 {
82 input: "# pint ignore/begin here",
83 output: []comments.Comment{
84 {
85 Type: comments.InvalidComment,
86 Value: comments.Invalid{Err: fmt.Errorf(`unexpected comment suffix: "here"`)},
87 },
88 },
89 },
90 {
91 input: "# pint ignore/begin",
92 output: []comments.Comment{
93 {Type: comments.IgnoreBeginType},
94 },
95 },
96 {
97 input: "# pint ignore/end here",
98 output: []comments.Comment{
99 {
100 Type: comments.InvalidComment,
101 Value: comments.Invalid{Err: fmt.Errorf(`unexpected comment suffix: "here"`)},
102 },
103 },
104 },
105 {
106 input: "# pint ignore/end",
107 output: []comments.Comment{
108 {Type: comments.IgnoreEndType},
109 },
110 },
111 {
112 input: "# pint ignore/next-line\there",
113 output: []comments.Comment{
114 {
115 Type: comments.InvalidComment,
116 Value: comments.Invalid{Err: fmt.Errorf(`unexpected comment suffix: "here"`)},
117 },
118 },
119 },
120 {
121 input: "# pint ignore/next-line",
122 output: []comments.Comment{
123 {Type: comments.IgnoreNextLineType},
124 },
125 },
126 {
127 input: "# pint file/owner",
128 output: []comments.Comment{
129 {
130 Type: comments.InvalidComment,
131 Value: comments.Invalid{Err: fmt.Errorf("missing file/owner value")},
132 },
133 },
134 },
135 {
136 input: "# pint file/owner bob and alice",
137 output: []comments.Comment{
138 {
139 Type: comments.FileOwnerType,
140 Value: comments.Owner{Name: "bob and alice"},
141 },
142 },
143 },
144 {
145 input: "# pint rule/owner",
146 output: []comments.Comment{
147 {
148 Type: comments.InvalidComment,
149 Value: comments.Invalid{Err: fmt.Errorf("missing rule/owner value")},
150 },
151 },
152 },
153 {
154 input: "# pint rule/owner bob and alice",
155 output: []comments.Comment{
156 {
157 Type: comments.RuleOwnerType,
158 Value: comments.Owner{Name: "bob and alice"},
159 },
160 },
161 },
162 {
163 input: "# pint file/disable",
164 output: []comments.Comment{
165 {
166 Type: comments.InvalidComment,
167 Value: comments.Invalid{Err: fmt.Errorf("missing file/disable value")},
168 },
169 },
170 },
171 {
172 input: `# pint file/disable promql/series(http_errors_total{label="this has spaces"})`,
173 output: []comments.Comment{
174 {
175 Type: comments.FileDisableType,
176 Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces"})`},
177 },
178 },
179 },
180 {
181 input: "# pint disable",
182 output: []comments.Comment{
183 {
184 Type: comments.InvalidComment,
185 Value: comments.Invalid{Err: fmt.Errorf("missing disable value")},
186 },
187 },
188 },
189 {
190 input: `# pint disable promql/series(http_errors_total{label="this has spaces"})`,
191 output: []comments.Comment{
192 {
193 Type: comments.DisableType,
194 Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces"})`},
195 },
196 },
197 },
198 {
199 input: `# pint disable promql/series(http_errors_total{label="this has spaces and a # symbol"})`,
200 output: []comments.Comment{
201 {
202 Type: comments.DisableType,
203 Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces and a # symbol"})`},
204 },
205 },
206 },
207 {
208 input: "# pint file/snooze",
209 output: []comments.Comment{
210 {
211 Type: comments.InvalidComment,
212 Value: comments.Invalid{Err: fmt.Errorf("missing file/snooze value")},
213 },
214 },
215 },
216 {
217 input: "# pint file/snooze 2023-12-31",
218 output: []comments.Comment{
219 {
220 Type: comments.InvalidComment,
221 Value: comments.Invalid{Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`)},
222 },
223 },
224 },
225 {
226 input: "# pint file/snooze abc",
227 output: []comments.Comment{
228 {
229 Type: comments.InvalidComment,
230 Value: comments.Invalid{Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`)},
231 },
232 },
233 },
234 {
235 input: `# pint file/snooze 2023-1231 promql/series(http_errors_total{label="this has spaces"})`,
236 output: []comments.Comment{
237 {
238 Type: comments.InvalidComment,
239 Value: comments.Invalid{Err: fmt.Errorf("invalid snooze timestamp: %w", errUntil("2023-1231"))},
240 },
241 },
242 },
243 {
244 input: `# pint file/snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`,
245 output: []comments.Comment{
246 {
247 Type: comments.FileSnoozeType,
248 Value: comments.Snooze{
249 Until: parseUntil("2023-12-31T00:00:00Z"),
250 Match: `promql/series(http_errors_total{label="this has spaces"})`,
251 },
252 },
253 },
254 },
255 {
256 input: "# pint snooze",
257 output: []comments.Comment{
258 {
259 Type: comments.InvalidComment,
260 Value: comments.Invalid{Err: fmt.Errorf("missing snooze value")},
261 },
262 },
263 },
264 {
265 input: "# pint snooze 2023-12-31",
266 output: []comments.Comment{
267 {
268 Type: comments.InvalidComment,
269 Value: comments.Invalid{Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`)},
270 },
271 },
272 },
273 {
274 input: "# pint snooze abc",
275 output: []comments.Comment{
276 {
277 Type: comments.InvalidComment,
278 Value: comments.Invalid{Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`)},
279 },
280 },
281 },
282 {
283 input: `# pint snooze 2023-1231 promql/series(http_errors_total{label="this has spaces"})`,
284 output: []comments.Comment{
285 {
286 Type: comments.InvalidComment,
287 Value: comments.Invalid{Err: fmt.Errorf("invalid snooze timestamp: %w", errUntil("2023-1231"))},
288 },
289 },
290 },
291 {
292 input: `# pint snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`,
293 output: []comments.Comment{
294 {
295 Type: comments.SnoozeType,
296 Value: comments.Snooze{
297 Until: parseUntil("2023-12-31T00:00:00Z"),
298 Match: `promql/series(http_errors_total{label="this has spaces"})`,
299 },
300 },
301 },
302 },
303 {
304 input: `# pint snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`,
305 output: []comments.Comment{
306 {
307 Type: comments.SnoozeType,
308 Value: comments.Snooze{
309 Until: parseUntil("2023-12-31T00:00:00Z"),
310 Match: `promql/series(http_errors_total{label="this has spaces"})`,
311 },
312 },
313 },
314 },
315 {
316 input: "# pint rule/set",
317 output: []comments.Comment{
318 {
319 Type: comments.InvalidComment,
320 Value: comments.Invalid{Err: fmt.Errorf("missing rule/set value")},
321 },
322 },
323 },
324 {
325 input: "# pint rule/set bob and alice",
326 output: []comments.Comment{
327 {
328 Type: comments.RuleSetType,
329 Value: comments.RuleSet{Value: "bob and alice"},
330 },
331 },
332 },
333 {
334 input: "code # pint disable xxx \ncode # alice\n",
335 output: []comments.Comment{
336 {
337 Type: comments.DisableType,
338 Value: comments.Disable{Match: "xxx"},
339 },
340 },
341 },
342 {
343 input: "code # pint disable xxx yyy \n # pint\tfile/owner bob",
344 output: []comments.Comment{
345 {
346 Type: comments.DisableType,
347 Value: comments.Disable{Match: "xxx yyy"},
348 },
349 {
350 Type: comments.FileOwnerType,
351 Value: comments.Owner{Name: "bob"},
352 },
353 },
354 },
355 {
356 input: "# pint rule/set promql/series(found) min-age foo",
357 output: []comments.Comment{
358 {
359 Type: comments.RuleSetType,
360 Value: comments.RuleSet{Value: "promql/series(found) min-age foo"},
361 },
362 },
363 },
364 {
365 input: "{#- comment #} # pint ignore/line",
366 output: []comments.Comment{
367 {
368 Type: comments.IgnoreLineType,
369 },
370 },
371 },
372 {
373 input: "{# comment #} # pint ignore/line",
374 output: []comments.Comment{
375 {
376 Type: comments.IgnoreLineType,
377 },
378 },
379 },
380 {
381 input: "#pint # pint # pint boo # pint ignore/line",
382 output: []comments.Comment{
383 {
384 Type: comments.IgnoreLineType,
385 },
386 },
387 },
388 {
389 input: "{# comment #} # pint ignore/line # pint ignore/file",
390 output: []comments.Comment{
391 {
392 Type: comments.InvalidComment,
393 Value: comments.Invalid{Err: fmt.Errorf(`unexpected comment suffix: "# pint ignore/file"`)},
394 },
395 },
396 },
397 }
398
399 for _, tc := range testCases {
400 t.Run(tc.input, func(t *testing.T) {
401 output := comments.Parse(tc.input)
402 require.Equal(t, tc.output, output)
403 })
404 }
405}
406
407func TestCommentValueString(t *testing.T) {
408 type testCaseT struct {
409 comment comments.CommentValue
410 expected string
411 }
412
413 parseUntil := func(s string) time.Time {
414 until, err := time.Parse(time.RFC3339, s)
415 require.NoError(t, err)
416 return until
417 }
418
419 testCases := []testCaseT{
420 {
421 comment: comments.Invalid{Err: errors.New("foo bar")},
422 expected: "foo bar",
423 },
424 {
425 comment: comments.Owner{Name: "bob & alice"},
426 expected: "bob & alice",
427 },
428 {
429 comment: comments.Disable{Match: `promql/series({code="500"})`},
430 expected: `promql/series({code="500"})`,
431 },
432 {
433 comment: comments.RuleSet{Value: "bob & alice"},
434 expected: "bob & alice",
435 },
436 {
437 comment: comments.Snooze{Match: `promql/series({code="500"})`, Until: parseUntil("2023-11-28T00:00:00Z")},
438 expected: `2023-11-28T00:00:00Z promql/series({code="500"})`,
439 },
440 }
441
442 for _, tc := range testCases {
443 t.Run(tc.expected, func(t *testing.T) {
444 require.Equal(t, tc.expected, tc.comment.String())
445 })
446 }
447}
448