|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
7 | | -from coverage_comment import github |
| 7 | +from coverage_comment import github, github_client |
8 | 8 |
|
9 | 9 |
|
10 | 10 | @pytest.mark.parametrize( |
@@ -491,3 +491,99 @@ def test_get_branch_diff(gh, session): |
491 | 491 | ) |
492 | 492 |
|
493 | 493 | assert result == "diff --git a/foo.py b/foo.py..." |
| 494 | + |
| 495 | + |
| 496 | +def test_get_pr_diff__too_large(gh, session): |
| 497 | + error_response = { |
| 498 | + "message": "Sorry, the diff exceeded the maximum number of files (300).", |
| 499 | + "errors": [{"resource": "PullRequest", "field": "diff", "code": "too_large"}], |
| 500 | + "documentation_url": "https://docs.github.com/rest/pulls/pulls#list-pull-requests-files", |
| 501 | + "status": "406", |
| 502 | + } |
| 503 | + session.register( |
| 504 | + "GET", |
| 505 | + "/repos/foo/bar/pulls/123", |
| 506 | + headers={"Accept": "application/vnd.github.v3.diff"}, |
| 507 | + )(json=error_response, status_code=406) |
| 508 | + |
| 509 | + with pytest.raises(github.CannotGetDiff) as exc_info: |
| 510 | + github.get_pr_diff(github=gh, repository="foo/bar", pr_number=123) |
| 511 | + |
| 512 | + assert "too large" in str(exc_info.value) |
| 513 | + assert "maximum 300 files" in str(exc_info.value) |
| 514 | + |
| 515 | + |
| 516 | +def test_get_branch_diff__too_large(gh, session): |
| 517 | + error_response = { |
| 518 | + "message": "Sorry, the diff exceeded the maximum number of files (300).", |
| 519 | + "errors": [{"resource": "PullRequest", "field": "diff", "code": "too_large"}], |
| 520 | + "documentation_url": "https://docs.github.com/rest/pulls/pulls#list-pull-requests-files", |
| 521 | + "status": "406", |
| 522 | + } |
| 523 | + session.register( |
| 524 | + "GET", |
| 525 | + "/repos/foo/bar/compare/main...feature", |
| 526 | + headers={"Accept": "application/vnd.github.v3.diff"}, |
| 527 | + )(json=error_response, status_code=406) |
| 528 | + |
| 529 | + with pytest.raises(github.CannotGetDiff) as exc_info: |
| 530 | + github.get_branch_diff( |
| 531 | + github=gh, repository="foo/bar", base_branch="main", head_branch="feature" |
| 532 | + ) |
| 533 | + |
| 534 | + assert "too large" in str(exc_info.value) |
| 535 | + assert "maximum 300 files" in str(exc_info.value) |
| 536 | + |
| 537 | + |
| 538 | +def test_get_pr_diff__other_error(gh, session): |
| 539 | + error_response = {"message": "Some other error", "errors": []} |
| 540 | + session.register( |
| 541 | + "GET", |
| 542 | + "/repos/foo/bar/pulls/123", |
| 543 | + headers={"Accept": "application/vnd.github.v3.diff"}, |
| 544 | + )(json=error_response, status_code=500) |
| 545 | + |
| 546 | + with pytest.raises(github_client.ApiError): |
| 547 | + github.get_pr_diff(github=gh, repository="foo/bar", pr_number=123) |
| 548 | + |
| 549 | + |
| 550 | +def test_get_branch_diff__other_error(gh, session): |
| 551 | + error_response = {"message": "Some other error", "errors": []} |
| 552 | + session.register( |
| 553 | + "GET", |
| 554 | + "/repos/foo/bar/compare/main...feature", |
| 555 | + headers={"Accept": "application/vnd.github.v3.diff"}, |
| 556 | + )(json=error_response, status_code=500) |
| 557 | + |
| 558 | + with pytest.raises(github_client.ApiError): |
| 559 | + github.get_branch_diff( |
| 560 | + github=gh, repository="foo/bar", base_branch="main", head_branch="feature" |
| 561 | + ) |
| 562 | + |
| 563 | + |
| 564 | +@pytest.mark.parametrize( |
| 565 | + "error_str,expected", |
| 566 | + [ |
| 567 | + # Valid JSON with too_large error |
| 568 | + ('{"errors": [{"code": "too_large"}]}', True), |
| 569 | + # Valid JSON with too_large error and extra fields |
| 570 | + ( |
| 571 | + '{"message": "Diff too large", "errors": [{"resource": "PR", "code": "too_large"}]}', |
| 572 | + True, |
| 573 | + ), |
| 574 | + # Valid JSON without too_large error |
| 575 | + ('{"errors": [{"code": "other"}]}', False), |
| 576 | + # Valid JSON with empty errors |
| 577 | + ('{"errors": []}', False), |
| 578 | + # Valid JSON with no errors key |
| 579 | + ('{"message": "error"}', False), |
| 580 | + # Non-JSON string (returns False, not a fallback match) |
| 581 | + ("not valid json", False), |
| 582 | + # Empty string |
| 583 | + ("", False), |
| 584 | + ], |
| 585 | +) |
| 586 | +def test__is_too_large_error(error_str, expected): |
| 587 | + """Test the _is_too_large_error helper function with various inputs.""" |
| 588 | + exc = github_client.ApiError(error_str) |
| 589 | + assert github._is_too_large_error(exc) is expected |
0 commit comments