github.com/mistwind/reviewdog@v0.0.0-20230322024206-9cfa11856d58/doghouse/server/doghouse_test.go (about) 1 package server 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "strings" 8 "testing" 9 10 "github.com/google/go-cmp/cmp" 11 "github.com/google/go-github/v39/github" 12 13 "github.com/mistwind/reviewdog/doghouse" 14 "github.com/mistwind/reviewdog/filter" 15 "github.com/mistwind/reviewdog/proto/rdf" 16 ) 17 18 type fakeCheckerGitHubCli struct { 19 checkerGitHubClientInterface 20 FakeGetPullRequestDiff func(ctx context.Context, owner, repo string, number int) ([]byte, error) 21 FakeCreateCheckRun func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) 22 FakeUpdateCheckRun func(ctx context.Context, owner, repo string, checkID int64, opt github.UpdateCheckRunOptions) (*github.CheckRun, error) 23 } 24 25 func (f *fakeCheckerGitHubCli) GetPullRequestDiff(ctx context.Context, owner, repo string, number int) ([]byte, error) { 26 return f.FakeGetPullRequestDiff(ctx, owner, repo, number) 27 } 28 29 func (f *fakeCheckerGitHubCli) CreateCheckRun(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 30 return f.FakeCreateCheckRun(ctx, owner, repo, opt) 31 } 32 33 func (f *fakeCheckerGitHubCli) UpdateCheckRun(ctx context.Context, owner, repo string, checkID int64, opt github.UpdateCheckRunOptions) (*github.CheckRun, error) { 34 return f.FakeUpdateCheckRun(ctx, owner, repo, checkID, opt) 35 } 36 37 const sampleDiff = `--- a/sample.old.txt 2016-10-13 05:09:35.820791185 +0900 38 +++ b/sample.new.txt 2016-10-13 05:15:26.839245048 +0900 39 @@ -1,3 +1,4 @@ 40 unchanged, contextual line 41 -deleted line 42 +added line 43 +added line 44 unchanged, contextual line 45 --- a/nonewline.old.txt 2016-10-13 15:34:14.931778318 +0900 46 +++ b/nonewline.new.txt 2016-10-13 15:34:14.868444672 +0900 47 @@ -1,4 +1,4 @@ 48 " vim: nofixeol noendofline 49 No newline at end of both the old and new file 50 -a 51 -a 52 \ No newline at end of file 53 +b 54 +b 55 \ No newline at end of file 56 ` 57 58 func TestCheck_OK(t *testing.T) { 59 const ( 60 name = "haya14busa-linter" 61 owner = "haya14busa" 62 repo = "reviewdog" 63 prNum = 14 64 sha = "1414" 65 reportURL = "http://example.com/report_url" 66 conclusion = "neutral" 67 wantCheckID = 1414 68 ) 69 70 req := &doghouse.CheckRequest{ 71 Name: name, 72 Owner: owner, 73 Repo: repo, 74 PullRequest: prNum, 75 SHA: sha, 76 Annotations: []*doghouse.Annotation{ 77 { 78 Diagnostic: &rdf.Diagnostic{ 79 Message: "test message", 80 Location: &rdf.Location{ 81 Path: "sample.new.txt", 82 Range: &rdf.Range{ 83 Start: &rdf.Position{Line: 2, Column: 1}, 84 }, 85 }, 86 OriginalOutput: "raw test message", 87 }, 88 }, 89 { 90 Diagnostic: &rdf.Diagnostic{ 91 Message: "test message outside diff", 92 Location: &rdf.Location{ 93 Path: "sample.new.txt", 94 Range: &rdf.Range{ 95 Start: &rdf.Position{Line: 14}, 96 }, 97 }, 98 OriginalOutput: "raw test message outside diff", 99 }, 100 }, 101 { 102 Diagnostic: &rdf.Diagnostic{ 103 Message: "test multiline", 104 Location: &rdf.Location{ 105 Path: "sample.new.txt", 106 Range: &rdf.Range{ 107 Start: &rdf.Position{Line: 2}, 108 End: &rdf.Position{Line: 3}, 109 }, 110 }, 111 }, 112 }, 113 { 114 Diagnostic: &rdf.Diagnostic{ 115 Message: "test multiline with column", 116 Location: &rdf.Location{ 117 Path: "sample.new.txt", 118 Range: &rdf.Range{ 119 Start: &rdf.Position{Line: 2, Column: 1}, 120 End: &rdf.Position{Line: 3, Column: 5}, 121 }, 122 }, 123 }, 124 }, 125 { 126 Diagnostic: &rdf.Diagnostic{ 127 Message: "test range comment", 128 Location: &rdf.Location{ 129 Path: "sample.new.txt", 130 Range: &rdf.Range{ 131 Start: &rdf.Position{Line: 2, Column: 1}, 132 End: &rdf.Position{Line: 2, Column: 5}, 133 }, 134 }, 135 }, 136 }, 137 { 138 Diagnostic: &rdf.Diagnostic{ 139 Message: "test severity override", 140 Severity: rdf.Severity_ERROR, 141 Location: &rdf.Location{ 142 Path: "sample.new.txt", 143 Range: &rdf.Range{ 144 Start: &rdf.Position{Line: 2}, 145 }, 146 }, 147 }, 148 }, 149 { 150 Diagnostic: &rdf.Diagnostic{ 151 Message: "source test", 152 Source: &rdf.Source{ 153 Name: "awesome-linter", 154 }, 155 Location: &rdf.Location{ 156 Path: "sample.new.txt", 157 Range: &rdf.Range{ 158 Start: &rdf.Position{Line: 2}, 159 }, 160 }, 161 }, 162 }, 163 { 164 Diagnostic: &rdf.Diagnostic{ 165 Message: "code test w/o URL", 166 Location: &rdf.Location{ 167 Path: "sample.new.txt", 168 Range: &rdf.Range{Start: &rdf.Position{Line: 2}}, 169 }, 170 Code: &rdf.Code{Value: "CODE14"}, 171 }, 172 }, 173 { 174 Diagnostic: &rdf.Diagnostic{ 175 Message: "code test w/ URL", 176 Location: &rdf.Location{ 177 Path: "sample.new.txt", 178 Range: &rdf.Range{Start: &rdf.Position{Line: 2}}, 179 }, 180 Code: &rdf.Code{Value: "CODE14", Url: "https://github.com/reviewdog#CODE14"}, 181 }, 182 }, 183 { 184 Path: "sample.new.txt", 185 Line: 2, 186 Message: "request from old clients", 187 RawMessage: "raw message from old clients", 188 }, 189 }, 190 Level: "warning", 191 } 192 193 cli := &fakeCheckerGitHubCli{} 194 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 195 return []byte(sampleDiff), nil 196 } 197 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 198 if opt.Name != name { 199 t.Errorf("CreateCheckRunOptions.Name = %q, want %q", opt.Name, name) 200 } 201 if opt.HeadSHA != sha { 202 t.Errorf("CreateCheckRunOptions.HeadSHA = %q, want %q", opt.HeadSHA, sha) 203 } 204 return &github.CheckRun{ID: github.Int64(wantCheckID)}, nil 205 } 206 cli.FakeUpdateCheckRun = func(ctx context.Context, owner, repo string, checkID int64, opt github.UpdateCheckRunOptions) (*github.CheckRun, error) { 207 if checkID != wantCheckID { 208 t.Errorf("UpdateCheckRun: checkID = %d, want %d", checkID, wantCheckID) 209 } 210 if opt.Name != name { 211 t.Errorf("UpdateCheckRunOptions.Name = %q, want %q", opt.Name, name) 212 } 213 annotations := opt.Output.Annotations 214 if len(annotations) == 0 { 215 if *opt.Conclusion != conclusion { 216 t.Errorf("UpdateCheckRunOptions.Conclusion = %q, want %q", *opt.Conclusion, conclusion) 217 } 218 } else { 219 wantAnnotations := []*github.CheckRunAnnotation{ 220 { 221 Path: github.String("sample.new.txt"), 222 StartLine: github.Int(2), 223 EndLine: github.Int(2), 224 AnnotationLevel: github.String("warning"), 225 Message: github.String("test message"), 226 Title: github.String("[haya14busa-linter] sample.new.txt#L2"), 227 RawDetails: github.String("raw test message"), 228 }, 229 { 230 Path: github.String("sample.new.txt"), 231 StartLine: github.Int(2), 232 EndLine: github.Int(3), 233 AnnotationLevel: github.String("warning"), 234 Message: github.String("test multiline"), 235 Title: github.String("[haya14busa-linter] sample.new.txt#L2-L3"), 236 }, 237 { 238 Path: github.String("sample.new.txt"), 239 StartLine: github.Int(2), 240 EndLine: github.Int(3), 241 AnnotationLevel: github.String("warning"), 242 Message: github.String("test multiline with column"), 243 Title: github.String("[haya14busa-linter] sample.new.txt#L2-L3"), 244 }, 245 { 246 Path: github.String("sample.new.txt"), 247 StartLine: github.Int(2), 248 EndLine: github.Int(2), 249 StartColumn: github.Int(1), 250 EndColumn: github.Int(5), 251 AnnotationLevel: github.String("warning"), 252 Message: github.String("test range comment"), 253 Title: github.String("[haya14busa-linter] sample.new.txt#L2"), 254 }, 255 { 256 Path: github.String("sample.new.txt"), 257 StartLine: github.Int(2), 258 EndLine: github.Int(2), 259 AnnotationLevel: github.String("failure"), 260 Message: github.String("test severity override"), 261 Title: github.String("[haya14busa-linter] sample.new.txt#L2"), 262 }, 263 { 264 Path: github.String("sample.new.txt"), 265 StartLine: github.Int(2), 266 EndLine: github.Int(2), 267 AnnotationLevel: github.String("warning"), 268 Message: github.String("source test"), 269 Title: github.String("[awesome-linter] sample.new.txt#L2"), 270 }, 271 { 272 Path: github.String("sample.new.txt"), 273 StartLine: github.Int(2), 274 EndLine: github.Int(2), 275 AnnotationLevel: github.String("warning"), 276 Message: github.String("code test w/o URL"), 277 Title: github.String("[haya14busa-linter] sample.new.txt#L2 <CODE14>"), 278 }, 279 { 280 Path: github.String("sample.new.txt"), 281 StartLine: github.Int(2), 282 EndLine: github.Int(2), 283 AnnotationLevel: github.String("warning"), 284 Message: github.String("code test w/ URL"), 285 Title: github.String("[haya14busa-linter] sample.new.txt#L2 <CODE14>(https://github.com/reviewdog#CODE14)"), 286 }, 287 { 288 Path: github.String("sample.new.txt"), 289 StartLine: github.Int(2), 290 EndLine: github.Int(2), 291 AnnotationLevel: github.String("warning"), 292 Message: github.String("request from old clients"), 293 Title: github.String("[haya14busa-linter] sample.new.txt#L2"), 294 RawDetails: github.String("raw message from old clients"), 295 }, 296 } 297 if d := cmp.Diff(annotations, wantAnnotations); d != "" { 298 t.Errorf("Annotation diff found:\n%s", d) 299 } 300 } 301 return &github.CheckRun{HTMLURL: github.String(reportURL)}, nil 302 } 303 checker := &Checker{req: req, gh: cli} 304 res, err := checker.Check(context.Background()) 305 if err != nil { 306 t.Fatal(err) 307 } 308 309 if res.ReportURL != reportURL { 310 t.Errorf("res.reportURL = %q, want %q", res.ReportURL, reportURL) 311 } 312 } 313 314 func testOutsideDiff(t *testing.T, outsideDiff bool, filterMode filter.Mode) { 315 const ( 316 name = "haya14busa-linter" 317 owner = "haya14busa" 318 repo = "reviewdog" 319 prNum = 14 320 sha = "1414" 321 reportURL = "http://example.com/report_url" 322 conclusion = "neutral" 323 wantCheckID = 1414 324 ) 325 326 req := &doghouse.CheckRequest{ 327 Name: name, 328 Owner: owner, 329 Repo: repo, 330 PullRequest: prNum, 331 SHA: sha, 332 Annotations: []*doghouse.Annotation{ 333 { 334 Diagnostic: &rdf.Diagnostic{ 335 Message: "test message", 336 Location: &rdf.Location{ 337 Path: "sample.new.txt", 338 Range: &rdf.Range{ 339 Start: &rdf.Position{Line: 2}, 340 }, 341 }, 342 OriginalOutput: "raw test message", 343 }, 344 }, 345 { 346 Diagnostic: &rdf.Diagnostic{ 347 Message: "test message outside diff", 348 Location: &rdf.Location{ 349 Path: "sample.new.txt", 350 Range: &rdf.Range{ 351 Start: &rdf.Position{Line: 14}, 352 }, 353 }, 354 OriginalOutput: "raw test message outside diff", 355 }, 356 }, 357 }, 358 Level: "warning", 359 OutsideDiff: outsideDiff, 360 FilterMode: filterMode, 361 } 362 363 cli := &fakeCheckerGitHubCli{} 364 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 365 return []byte(sampleDiff), nil 366 } 367 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 368 return &github.CheckRun{ID: github.Int64(wantCheckID)}, nil 369 } 370 cli.FakeUpdateCheckRun = func(ctx context.Context, owner, repo string, checkID int64, opt github.UpdateCheckRunOptions) (*github.CheckRun, error) { 371 annotations := opt.Output.Annotations 372 if len(annotations) == 0 { 373 if *opt.Conclusion != conclusion { 374 t.Errorf("UpdateCheckRunOptions.Conclusion = %q, want %q", *opt.Conclusion, conclusion) 375 } 376 } else { 377 wantAnnotations := []*github.CheckRunAnnotation{ 378 { 379 Path: github.String("sample.new.txt"), 380 StartLine: github.Int(2), 381 EndLine: github.Int(2), 382 AnnotationLevel: github.String("warning"), 383 Message: github.String("test message"), 384 Title: github.String("[haya14busa-linter] sample.new.txt#L2"), 385 RawDetails: github.String("raw test message"), 386 }, 387 { 388 Path: github.String("sample.new.txt"), 389 StartLine: github.Int(14), 390 EndLine: github.Int(14), 391 AnnotationLevel: github.String("warning"), 392 Message: github.String("test message outside diff"), 393 Title: github.String("[haya14busa-linter] sample.new.txt#L14"), 394 RawDetails: github.String("raw test message outside diff"), 395 }, 396 } 397 if d := cmp.Diff(annotations, wantAnnotations); d != "" { 398 t.Errorf("Annotation diff found:\n%s", d) 399 } 400 } 401 return &github.CheckRun{HTMLURL: github.String(reportURL)}, nil 402 } 403 checker := &Checker{req: req, gh: cli} 404 if _, err := checker.Check(context.Background()); err != nil { 405 t.Fatal(err) 406 } 407 } 408 409 func TestCheck_OK_deprecated_outsidediff(t *testing.T) { 410 t.Run("deprecated: outside_diff", func(t *testing.T) { 411 testOutsideDiff(t, true, filter.ModeDefault) 412 }) 413 t.Run("filter-mode=NoFilter", func(t *testing.T) { 414 testOutsideDiff(t, false, filter.ModeNoFilter) 415 }) 416 } 417 418 func TestCheck_OK_multiple_update_runs(t *testing.T) { 419 const ( 420 name = "haya14busa-linter" 421 owner = "haya14busa" 422 repo = "reviewdog" 423 prNum = 14 424 sha = "1414" 425 reportURL = "http://example.com/report_url" 426 conclusion = "neutral" 427 wantCheckID = 1414 428 ) 429 430 req := &doghouse.CheckRequest{ 431 Name: name, 432 Owner: owner, 433 Repo: repo, 434 PullRequest: prNum, 435 SHA: sha, 436 Level: "warning", 437 } 438 for i := 0; i < 101; i++ { 439 req.Annotations = append(req.Annotations, &doghouse.Annotation{ 440 Diagnostic: &rdf.Diagnostic{ 441 Message: "test message", 442 Location: &rdf.Location{ 443 Path: "sample.new.txt", 444 Range: &rdf.Range{ 445 Start: &rdf.Position{Line: 2}, 446 }, 447 }, 448 OriginalOutput: "raw test message", 449 }, 450 }) 451 } 452 453 cli := &fakeCheckerGitHubCli{} 454 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 455 return []byte(sampleDiff), nil 456 } 457 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 458 if opt.Name != name { 459 t.Errorf("CreateCheckRunOptions.Name = %q, want %q", opt.Name, name) 460 } 461 if opt.HeadSHA != sha { 462 t.Errorf("CreateCheckRunOptions.HeadSHA = %q, want %q", opt.HeadSHA, sha) 463 } 464 return &github.CheckRun{ID: github.Int64(wantCheckID)}, nil 465 } 466 cli.FakeUpdateCheckRun = func(ctx context.Context, owner, repo string, checkID int64, opt github.UpdateCheckRunOptions) (*github.CheckRun, error) { 467 if checkID != wantCheckID { 468 t.Errorf("UpdateCheckRun: checkID = %d, want %d", checkID, wantCheckID) 469 } 470 annotations := opt.Output.Annotations 471 switch len(annotations) { 472 case 0: 473 if *opt.Conclusion != conclusion { 474 t.Errorf("UpdateCheckRunOptions.Conclusion = %q, want %q", *opt.Conclusion, conclusion) 475 } 476 case maxAnnotationsPerRequest, 1: // Expected 477 default: 478 t.Errorf("UpdateCheckRun: len(annotations) = %d, but it's unexpected", len(annotations)) 479 } 480 return &github.CheckRun{HTMLURL: github.String(reportURL)}, nil 481 } 482 checker := &Checker{req: req, gh: cli} 483 if _, err := checker.Check(context.Background()); err != nil { 484 t.Fatal(err) 485 } 486 } 487 488 func TestCheck_OK_nonPullRequests(t *testing.T) { 489 const ( 490 name = "haya14busa-linter" 491 owner = "haya14busa" 492 repo = "reviewdog" 493 sha = "1414" 494 reportURL = "http://example.com/report_url" 495 conclusion = "neutral" 496 wantCheckID = 1414 497 ) 498 499 req := &doghouse.CheckRequest{ 500 // Do not set PullRequest 501 Name: name, 502 Owner: owner, 503 Repo: repo, 504 SHA: sha, 505 Annotations: []*doghouse.Annotation{ 506 { 507 Diagnostic: &rdf.Diagnostic{ 508 Message: "test message", 509 Location: &rdf.Location{ 510 Path: "sample.new.txt", 511 Range: &rdf.Range{ 512 Start: &rdf.Position{Line: 2}, 513 }, 514 }, 515 OriginalOutput: "raw test message", 516 }, 517 }, 518 { 519 Diagnostic: &rdf.Diagnostic{ 520 Message: "test message2", 521 Location: &rdf.Location{ 522 Path: "sample.new.txt", 523 Range: &rdf.Range{ 524 Start: &rdf.Position{Line: 14}, 525 }, 526 }, 527 OriginalOutput: "raw test message2", 528 }, 529 }, 530 }, 531 Level: "warning", 532 } 533 534 cli := &fakeCheckerGitHubCli{} 535 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 536 t.Errorf("GetPullRequestDiff should not be called") 537 return nil, nil 538 } 539 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 540 return &github.CheckRun{ID: github.Int64(wantCheckID)}, nil 541 } 542 cli.FakeUpdateCheckRun = func(ctx context.Context, owner, repo string, checkID int64, opt github.UpdateCheckRunOptions) (*github.CheckRun, error) { 543 if checkID != wantCheckID { 544 t.Errorf("UpdateCheckRun: checkID = %d, want %d", checkID, wantCheckID) 545 } 546 annotations := opt.Output.Annotations 547 if len(annotations) == 0 { 548 if *opt.Conclusion != conclusion { 549 t.Errorf("UpdateCheckRunOptions.Conclusion = %q, want %q", *opt.Conclusion, conclusion) 550 } 551 } else { 552 wantAnnotations := []*github.CheckRunAnnotation{ 553 { 554 Path: github.String("sample.new.txt"), 555 StartLine: github.Int(2), 556 EndLine: github.Int(2), 557 AnnotationLevel: github.String("warning"), 558 Message: github.String("test message"), 559 Title: github.String("[haya14busa-linter] sample.new.txt#L2"), 560 RawDetails: github.String("raw test message"), 561 }, 562 { 563 Path: github.String("sample.new.txt"), 564 StartLine: github.Int(14), 565 EndLine: github.Int(14), 566 AnnotationLevel: github.String("warning"), 567 Message: github.String("test message2"), 568 Title: github.String("[haya14busa-linter] sample.new.txt#L14"), 569 RawDetails: github.String("raw test message2"), 570 }, 571 } 572 if d := cmp.Diff(annotations, wantAnnotations); d != "" { 573 t.Errorf("Annotation diff found:\n%s", d) 574 } 575 } 576 return &github.CheckRun{HTMLURL: github.String(reportURL)}, nil 577 } 578 checker := &Checker{req: req, gh: cli} 579 res, err := checker.Check(context.Background()) 580 if err != nil { 581 t.Fatal(err) 582 } 583 584 if res.ReportURL != reportURL { 585 t.Errorf("res.reportURL = %q, want %q", res.ReportURL, reportURL) 586 } 587 } 588 589 func TestCheck_fail_diff(t *testing.T) { 590 req := &doghouse.CheckRequest{PullRequest: 1} 591 cli := &fakeCheckerGitHubCli{} 592 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 593 return nil, errors.New("test diff failure") 594 } 595 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 596 return &github.CheckRun{}, nil 597 } 598 checker := &Checker{req: req, gh: cli} 599 600 if _, err := checker.Check(context.Background()); err == nil { 601 t.Fatalf("got no error, want some error") 602 } else { 603 t.Log(err) 604 } 605 } 606 607 func TestCheck_fail_invalid_diff(t *testing.T) { 608 t.Skip("Parse invalid diff function somehow doesn't return error") 609 req := &doghouse.CheckRequest{PullRequest: 1} 610 cli := &fakeCheckerGitHubCli{} 611 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 612 return []byte("invalid diff"), nil 613 } 614 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 615 return &github.CheckRun{}, nil 616 } 617 checker := &Checker{req: req, gh: cli} 618 619 if _, err := checker.Check(context.Background()); err == nil { 620 t.Fatalf("got no error, want some error") 621 } else { 622 t.Log(err) 623 } 624 } 625 626 func TestCheck_fail_check(t *testing.T) { 627 req := &doghouse.CheckRequest{PullRequest: 1} 628 cli := &fakeCheckerGitHubCli{} 629 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 630 return []byte(sampleDiff), nil 631 } 632 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 633 return nil, errors.New("test check failure") 634 } 635 checker := &Checker{req: req, gh: cli} 636 637 if _, err := checker.Check(context.Background()); err == nil { 638 t.Fatalf("got no error, want some error") 639 } else { 640 t.Log(err) 641 } 642 } 643 644 func TestCheck_fail_check_with_403(t *testing.T) { 645 req := &doghouse.CheckRequest{PullRequest: 1} 646 cli := &fakeCheckerGitHubCli{} 647 cli.FakeGetPullRequestDiff = func(ctx context.Context, owner, repo string, number int) ([]byte, error) { 648 return []byte(sampleDiff), nil 649 } 650 cli.FakeCreateCheckRun = func(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, error) { 651 return nil, &github.ErrorResponse{ 652 Response: &http.Response{ 653 StatusCode: http.StatusForbidden, 654 }, 655 } 656 } 657 checker := &Checker{req: req, gh: cli} 658 659 resp, err := checker.Check(context.Background()) 660 if err != nil { 661 t.Fatalf("got unexpected error: %v", err) 662 } 663 if resp.CheckedResults == nil { 664 t.Error("resp.CheckedResults should not be nil") 665 } 666 } 667 func TestCheck_too_many_findings_cut_off_correctly(t *testing.T) { 668 checker := &Checker{req: &doghouse.CheckRequest{}} 669 670 var diagnostics []*filter.FilteredDiagnostic 671 for i := 0; i < 1000; i++ { 672 diagnostics = append(diagnostics, &filter.FilteredDiagnostic{ 673 ShouldReport: true, 674 Diagnostic: &rdf.Diagnostic{ 675 Message: "this is a pretty long test message that will lead to overshooting the maximum allowed size", 676 }, 677 }) 678 } 679 summaryText := checker.summary(diagnostics) 680 if len(summaryText) > maxAllowedSize { 681 t.Errorf("summary text is %d bytes long, but the maximum allowed size is %d", len(summaryText), maxAllowedSize) 682 } 683 if !strings.Contains(summaryText, "... (Too many findings. Dropped some findings)\n</details>") { 684 t.Error("summary text was not cut off correctly") 685 } 686 }