cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/comments/comments_test.go
537lines · modecode
| 1 | package comments_test |
| 2 | |
| 3 | import ( |
| 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 | |
| 14 | func 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{ |
| 159 | Name: "bob and alice", |
| 160 | Line: 1, |
| 161 | }, |
| 162 | }, |
| 163 | }, |
| 164 | }, |
| 165 | { |
| 166 | input: "# pint rule/owner", |
| 167 | output: []comments.Comment{ |
| 168 | { |
| 169 | Type: comments.InvalidComment, |
| 170 | Value: comments.Invalid{Err: comments.CommentError{ |
| 171 | Line: 1, |
| 172 | Err: errors.New("missing rule/owner value"), |
| 173 | }}, |
| 174 | }, |
| 175 | }, |
| 176 | }, |
| 177 | { |
| 178 | input: "# pint rule/owner bob and alice", |
| 179 | output: []comments.Comment{ |
| 180 | { |
| 181 | Type: comments.RuleOwnerType, |
| 182 | Value: comments.Owner{ |
| 183 | Name: "bob and alice", |
| 184 | }, |
| 185 | }, |
| 186 | }, |
| 187 | }, |
| 188 | { |
| 189 | input: "# pint file/disable", |
| 190 | output: []comments.Comment{ |
| 191 | { |
| 192 | Type: comments.InvalidComment, |
| 193 | Value: comments.Invalid{Err: comments.CommentError{ |
| 194 | Line: 1, |
| 195 | Err: errors.New("missing file/disable value"), |
| 196 | }}, |
| 197 | }, |
| 198 | }, |
| 199 | }, |
| 200 | { |
| 201 | input: `# pint file/disable promql/series(http_errors_total{label="this has spaces"})`, |
| 202 | output: []comments.Comment{ |
| 203 | { |
| 204 | Type: comments.FileDisableType, |
| 205 | Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces"})`}, |
| 206 | }, |
| 207 | }, |
| 208 | }, |
| 209 | { |
| 210 | input: "# pint disable", |
| 211 | output: []comments.Comment{ |
| 212 | { |
| 213 | Type: comments.InvalidComment, |
| 214 | Value: comments.Invalid{Err: comments.CommentError{ |
| 215 | Line: 1, |
| 216 | Err: errors.New("missing disable value"), |
| 217 | }}, |
| 218 | }, |
| 219 | }, |
| 220 | }, |
| 221 | { |
| 222 | input: `# pint disable promql/series(http_errors_total{label="this has spaces"})`, |
| 223 | output: []comments.Comment{ |
| 224 | { |
| 225 | Type: comments.DisableType, |
| 226 | Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces"})`}, |
| 227 | }, |
| 228 | }, |
| 229 | }, |
| 230 | { |
| 231 | input: `# pint disable promql/series(http_errors_total{label="this has spaces and a # symbol"})`, |
| 232 | output: []comments.Comment{ |
| 233 | { |
| 234 | Type: comments.DisableType, |
| 235 | Value: comments.Disable{Match: `promql/series(http_errors_total{label="this has spaces and a # symbol"})`}, |
| 236 | }, |
| 237 | }, |
| 238 | }, |
| 239 | { |
| 240 | input: "# pint file/snooze", |
| 241 | output: []comments.Comment{ |
| 242 | { |
| 243 | Type: comments.InvalidComment, |
| 244 | Value: comments.Invalid{Err: comments.CommentError{ |
| 245 | Line: 1, |
| 246 | Err: errors.New("missing file/snooze value"), |
| 247 | }}, |
| 248 | }, |
| 249 | }, |
| 250 | }, |
| 251 | { |
| 252 | input: "# pint file/snooze 2023-12-31", |
| 253 | output: []comments.Comment{ |
| 254 | { |
| 255 | Type: comments.InvalidComment, |
| 256 | Value: comments.Invalid{Err: comments.CommentError{ |
| 257 | Line: 1, |
| 258 | Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`), |
| 259 | }}, |
| 260 | }, |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | input: "# pint file/snooze abc", |
| 265 | output: []comments.Comment{ |
| 266 | { |
| 267 | Type: comments.InvalidComment, |
| 268 | Value: comments.Invalid{Err: comments.CommentError{ |
| 269 | Line: 1, |
| 270 | Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`), |
| 271 | }}, |
| 272 | }, |
| 273 | }, |
| 274 | }, |
| 275 | { |
| 276 | input: `# pint file/snooze 2023-1231 promql/series(http_errors_total{label="this has spaces"})`, |
| 277 | output: []comments.Comment{ |
| 278 | { |
| 279 | Type: comments.InvalidComment, |
| 280 | Value: comments.Invalid{Err: comments.CommentError{ |
| 281 | Line: 1, |
| 282 | Err: fmt.Errorf("invalid snooze timestamp: %w", errUntil("2023-1231")), |
| 283 | }}, |
| 284 | }, |
| 285 | }, |
| 286 | }, |
| 287 | { |
| 288 | input: `# pint file/snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`, |
| 289 | output: []comments.Comment{ |
| 290 | { |
| 291 | Type: comments.FileSnoozeType, |
| 292 | Value: comments.Snooze{ |
| 293 | Until: parseUntil("2023-12-31T00:00:00Z"), |
| 294 | Match: `promql/series(http_errors_total{label="this has spaces"})`, |
| 295 | }, |
| 296 | }, |
| 297 | }, |
| 298 | }, |
| 299 | { |
| 300 | input: "# pint snooze", |
| 301 | output: []comments.Comment{ |
| 302 | { |
| 303 | Type: comments.InvalidComment, |
| 304 | Value: comments.Invalid{Err: comments.CommentError{ |
| 305 | Line: 1, |
| 306 | Err: errors.New("missing snooze value"), |
| 307 | }}, |
| 308 | }, |
| 309 | }, |
| 310 | }, |
| 311 | { |
| 312 | input: "# pint snooze 2023-12-31", |
| 313 | output: []comments.Comment{ |
| 314 | { |
| 315 | Type: comments.InvalidComment, |
| 316 | Value: comments.Invalid{Err: comments.CommentError{ |
| 317 | Line: 1, |
| 318 | Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`), |
| 319 | }}, |
| 320 | }, |
| 321 | }, |
| 322 | }, |
| 323 | { |
| 324 | input: "# pint snooze abc", |
| 325 | output: []comments.Comment{ |
| 326 | { |
| 327 | Type: comments.InvalidComment, |
| 328 | Value: comments.Invalid{Err: comments.CommentError{ |
| 329 | Line: 1, |
| 330 | Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`), |
| 331 | }}, |
| 332 | }, |
| 333 | }, |
| 334 | }, |
| 335 | { |
| 336 | input: `# pint snooze 2023-1231 promql/series(http_errors_total{label="this has spaces"})`, |
| 337 | output: []comments.Comment{ |
| 338 | { |
| 339 | Type: comments.InvalidComment, |
| 340 | Value: comments.Invalid{Err: comments.CommentError{ |
| 341 | Line: 1, |
| 342 | Err: fmt.Errorf("invalid snooze timestamp: %w", errUntil("2023-1231")), |
| 343 | }}, |
| 344 | }, |
| 345 | }, |
| 346 | }, |
| 347 | { |
| 348 | input: `# pint snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`, |
| 349 | output: []comments.Comment{ |
| 350 | { |
| 351 | Type: comments.SnoozeType, |
| 352 | Value: comments.Snooze{ |
| 353 | Until: parseUntil("2023-12-31T00:00:00Z"), |
| 354 | Match: `promql/series(http_errors_total{label="this has spaces"})`, |
| 355 | }, |
| 356 | }, |
| 357 | }, |
| 358 | }, |
| 359 | { |
| 360 | input: `# pint snooze 2023-12-31 promql/series(http_errors_total{label="this has spaces"})`, |
| 361 | output: []comments.Comment{ |
| 362 | { |
| 363 | Type: comments.SnoozeType, |
| 364 | Value: comments.Snooze{ |
| 365 | Until: parseUntil("2023-12-31T00:00:00Z"), |
| 366 | Match: `promql/series(http_errors_total{label="this has spaces"})`, |
| 367 | }, |
| 368 | }, |
| 369 | }, |
| 370 | }, |
| 371 | { |
| 372 | input: "# pint rule/set", |
| 373 | output: []comments.Comment{ |
| 374 | { |
| 375 | Type: comments.InvalidComment, |
| 376 | Value: comments.Invalid{Err: comments.CommentError{ |
| 377 | Line: 1, |
| 378 | Err: errors.New("missing rule/set value"), |
| 379 | }}, |
| 380 | }, |
| 381 | }, |
| 382 | }, |
| 383 | { |
| 384 | input: "# pint rule/set bob and alice", |
| 385 | output: []comments.Comment{ |
| 386 | { |
| 387 | Type: comments.RuleSetType, |
| 388 | Value: comments.RuleSet{Value: "bob and alice"}, |
| 389 | }, |
| 390 | }, |
| 391 | }, |
| 392 | { |
| 393 | input: "code # pint disable xxx \ncode # alice\n", |
| 394 | output: []comments.Comment{ |
| 395 | { |
| 396 | Type: comments.DisableType, |
| 397 | Value: comments.Disable{Match: "xxx"}, |
| 398 | Offset: len("code "), |
| 399 | }, |
| 400 | }, |
| 401 | }, |
| 402 | { |
| 403 | input: "code # pint disable xxx yyy \n # pint\tfile/owner bob", |
| 404 | output: []comments.Comment{ |
| 405 | { |
| 406 | Type: comments.DisableType, |
| 407 | Value: comments.Disable{Match: "xxx yyy"}, |
| 408 | Offset: len("code "), |
| 409 | }, |
| 410 | { |
| 411 | Type: comments.FileOwnerType, |
| 412 | Value: comments.Owner{ |
| 413 | Name: "bob", |
| 414 | Line: 2, |
| 415 | }, |
| 416 | Offset: 1, |
| 417 | }, |
| 418 | }, |
| 419 | }, |
| 420 | { |
| 421 | input: "# pint rule/set promql/series(found) min-age foo", |
| 422 | output: []comments.Comment{ |
| 423 | { |
| 424 | Type: comments.RuleSetType, |
| 425 | Value: comments.RuleSet{Value: "promql/series(found) min-age foo"}, |
| 426 | }, |
| 427 | }, |
| 428 | }, |
| 429 | { |
| 430 | input: "{#- comment #} # pint ignore/line", |
| 431 | output: []comments.Comment{ |
| 432 | { |
| 433 | Type: comments.IgnoreLineType, |
| 434 | Offset: len("{#- comment #} "), |
| 435 | }, |
| 436 | }, |
| 437 | }, |
| 438 | { |
| 439 | input: "{# comment #} # pint ignore/line", |
| 440 | output: []comments.Comment{ |
| 441 | { |
| 442 | Type: comments.IgnoreLineType, |
| 443 | Offset: len("{# comment #} "), |
| 444 | }, |
| 445 | }, |
| 446 | }, |
| 447 | { |
| 448 | input: "#pint # pint # pint boo # pint ignore/line", |
| 449 | output: []comments.Comment{ |
| 450 | { |
| 451 | Type: comments.IgnoreLineType, |
| 452 | Offset: len("#pint # pint # pint boo "), |
| 453 | }, |
| 454 | }, |
| 455 | }, |
| 456 | { |
| 457 | input: "{# comment #} # pint ignore/line # pint ignore/file", |
| 458 | output: []comments.Comment{ |
| 459 | { |
| 460 | Type: comments.InvalidComment, |
| 461 | Value: comments.Invalid{Err: comments.CommentError{ |
| 462 | Line: 1, |
| 463 | Err: errors.New(`unexpected comment suffix: "# pint ignore/file"`), |
| 464 | }}, |
| 465 | }, |
| 466 | }, |
| 467 | }, |
| 468 | { |
| 469 | input: "{#- JIRA-12345: foo<->bar example comment ' -#} # pint ignore/line", |
| 470 | output: []comments.Comment{ |
| 471 | { |
| 472 | Type: comments.IgnoreLineType, |
| 473 | Offset: len("{#- JIRA-12345: foo<->bar example comment ' -#} "), |
| 474 | }, |
| 475 | }, |
| 476 | }, |
| 477 | } |
| 478 | |
| 479 | for _, tc := range testCases { |
| 480 | t.Run(tc.input, func(t *testing.T) { |
| 481 | output := comments.Parse(1, tc.input) |
| 482 | require.Equal(t, tc.output, output) |
| 483 | }) |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | func TestCommentValueString(t *testing.T) { |
| 488 | type testCaseT struct { |
| 489 | comment comments.CommentValue |
| 490 | expected string |
| 491 | } |
| 492 | |
| 493 | parseUntil := func(s string) time.Time { |
| 494 | until, err := time.Parse(time.RFC3339, s) |
| 495 | require.NoError(t, err) |
| 496 | return until |
| 497 | } |
| 498 | |
| 499 | testCases := []testCaseT{ |
| 500 | { |
| 501 | comment: comments.Invalid{Err: comments.CommentError{ |
| 502 | Line: 1, |
| 503 | Err: errors.New("foo bar"), |
| 504 | }}, |
| 505 | expected: "foo bar", |
| 506 | }, |
| 507 | { |
| 508 | comment: comments.Invalid{Err: comments.CommentError{ |
| 509 | Line: 1, |
| 510 | Err: comments.OwnerError{Name: "foo bar"}, |
| 511 | }}, |
| 512 | expected: "foo bar", |
| 513 | }, |
| 514 | { |
| 515 | comment: comments.Owner{Name: "bob & alice"}, |
| 516 | expected: "bob & alice", |
| 517 | }, |
| 518 | { |
| 519 | comment: comments.Disable{Match: `promql/series({code="500"})`}, |
| 520 | expected: `promql/series({code="500"})`, |
| 521 | }, |
| 522 | { |
| 523 | comment: comments.RuleSet{Value: "bob & alice"}, |
| 524 | expected: "bob & alice", |
| 525 | }, |
| 526 | { |
| 527 | comment: comments.Snooze{Match: `promql/series({code="500"})`, Until: parseUntil("2023-11-28T00:00:00Z")}, |
| 528 | expected: `2023-11-28T00:00:00Z promql/series({code="500"})`, |
| 529 | }, |
| 530 | } |
| 531 | |
| 532 | for _, tc := range testCases { |
| 533 | t.Run(tc.expected, func(t *testing.T) { |
| 534 | require.Equal(t, tc.expected, tc.comment.String()) |
| 535 | }) |
| 536 | } |
| 537 | } |