|
8 | 8 | import contextlib |
9 | 9 | import io |
10 | 10 | import json |
| 11 | +import os |
11 | 12 | import subprocess |
12 | 13 | import tempfile |
13 | 14 | import unittest |
@@ -138,6 +139,48 @@ def test_selects_all_matching_members_deduplicated(self): |
138 | 139 | {"alice", "bob", "carol"}, |
139 | 140 | ) |
140 | 141 |
|
| 142 | + def test_multiple_area_labels_message(self): |
| 143 | + with self.assertRaises(routing.LabelRoutingError) as caught: |
| 144 | + routing.select_reviewers(CATEGORIES, {"area:runtime", "area:build"}) |
| 145 | + self.assertEqual( |
| 146 | + str(caught.exception), |
| 147 | + "This PR has more than one area label: area:build, area:runtime. " |
| 148 | + "A ready PR needs exactly one; remove the others.", |
| 149 | + ) |
| 150 | + |
| 151 | + def test_missing_area_label_message(self): |
| 152 | + with self.assertRaises(routing.LabelRoutingError) as caught: |
| 153 | + routing.select_reviewers(CATEGORIES, {"topic:policy"}) |
| 154 | + self.assertEqual( |
| 155 | + str(caught.exception), |
| 156 | + "This PR has no area label. A ready PR needs exactly one; add one.", |
| 157 | + ) |
| 158 | + |
| 159 | + def test_unknown_label_message(self): |
| 160 | + with self.assertRaises(routing.LabelRoutingError) as caught: |
| 161 | + routing.select_reviewers( |
| 162 | + CATEGORIES, {"area:runtime", "topic:nope", "topic:zzz"} |
| 163 | + ) |
| 164 | + self.assertEqual( |
| 165 | + str(caught.exception), |
| 166 | + "There are invalid area and topic labels on this PR: topic:nope, " |
| 167 | + "topic:zzz. Remove them.", |
| 168 | + ) |
| 169 | + |
| 170 | + def test_single_unknown_label_message(self): |
| 171 | + with self.assertRaises(routing.LabelRoutingError) as caught: |
| 172 | + routing.select_reviewers(CATEGORIES, {"area:runtime", "topic:nope"}) |
| 173 | + self.assertEqual( |
| 174 | + str(caught.exception), |
| 175 | + "There is an invalid area or topic label on this PR: topic:nope. " |
| 176 | + "Remove it.", |
| 177 | + ) |
| 178 | + |
| 179 | + def test_area_count_is_checked_before_unknown_labels(self): |
| 180 | + with self.assertRaises(routing.LabelRoutingError) as caught: |
| 181 | + routing.select_reviewers(CATEGORIES, {"area:nope", "area:other"}) |
| 182 | + self.assertIn("more than one area label", str(caught.exception)) |
| 183 | + |
141 | 184 | def test_requires_one_known_area_and_known_topics(self): |
142 | 185 | for labels in ( |
143 | 186 | set(), |
@@ -683,7 +726,123 @@ def test_label_write_failure_does_not_request_reviewers(self): |
683 | 726 | load.assert_not_called() |
684 | 727 |
|
685 | 728 |
|
| 729 | +class FailureReportTests(unittest.TestCase): |
| 730 | + GH_COMMAND = ( |
| 731 | + "gh", |
| 732 | + "api", |
| 733 | + "repos/o/r/pulls/42/requested_reviewers", |
| 734 | + "--method", |
| 735 | + "POST", |
| 736 | + "--input", |
| 737 | + "-", |
| 738 | + ) |
| 739 | + |
| 740 | + def test_escape_data_and_property(self): |
| 741 | + self.assertEqual(routing._escape_annotation("a%b\r\nc"), "a%25b%0D%0Ac") |
| 742 | + self.assertEqual(routing._escape_annotation("x:y,z", prop=True), "x%3Ay%2Cz") |
| 743 | + |
| 744 | + def test_api_failure_includes_method_path_status_and_stderr(self): |
| 745 | + error = subprocess.CalledProcessError( |
| 746 | + 1, |
| 747 | + self.GH_COMMAND, |
| 748 | + stderr="gh: Reviews may only be requested from collaborators. (HTTP 422)\n", |
| 749 | + ) |
| 750 | + category, message = routing.describe_failure(error) |
| 751 | + self.assertEqual(category, "GitHub API error") |
| 752 | + self.assertTrue( |
| 753 | + message.startswith("Requesting reviewers failed with a GitHub API error"), |
| 754 | + message, |
| 755 | + ) |
| 756 | + self.assertIn("POST repos/o/r/pulls/42/requested_reviewers", message) |
| 757 | + self.assertIn("exit status 1", message) |
| 758 | + self.assertIn("HTTP 422", message) |
| 759 | + self.assertNotIn("--input", message) |
| 760 | + |
| 761 | + def test_api_failure_with_empty_stderr(self): |
| 762 | + for stderr in (None, "", " \n"): |
| 763 | + error = subprocess.CalledProcessError(1, self.GH_COMMAND, stderr=stderr) |
| 764 | + _, message = routing.describe_failure(error) |
| 765 | + self.assertNotIn("None", message) |
| 766 | + self.assertTrue(message.endswith("(exit status 1)"), message) |
| 767 | + |
| 768 | + def test_get_is_the_default_method(self): |
| 769 | + error = subprocess.CalledProcessError( |
| 770 | + 1, ["gh", "api", "repos/o/r/pulls/42", "--method", "GET"], stderr="x" |
| 771 | + ) |
| 772 | + _, message = routing.describe_failure(error) |
| 773 | + self.assertIn("GET repos/o/r/pulls/42", message) |
| 774 | + |
| 775 | + def test_api_failure_names_the_action(self): |
| 776 | + pull = "repos/o/r/pulls/42" |
| 777 | + cases = ( |
| 778 | + ("POST", f"{pull}/requested_reviewers", "Requesting reviewers"), |
| 779 | + ("DELETE", f"{pull}/requested_reviewers", "Removing reviewer requests"), |
| 780 | + ("POST", "repos/o/r/issues/42/labels", "Adding labels"), |
| 781 | + ("GET", pull, "Reading the PR from GitHub"), |
| 782 | + ) |
| 783 | + for method, path, action in cases: |
| 784 | + error = subprocess.CalledProcessError( |
| 785 | + 1, ["gh", "api", path, "--method", method], stderr="x" |
| 786 | + ) |
| 787 | + with self.subTest(method=method, path=path): |
| 788 | + _, message = routing.describe_failure(error) |
| 789 | + self.assertTrue( |
| 790 | + message.startswith(f"{action} failed with a GitHub API error"), |
| 791 | + message, |
| 792 | + ) |
| 793 | + |
| 794 | + def test_label_error_category(self): |
| 795 | + self.assertEqual( |
| 796 | + routing.describe_failure(routing.LabelRoutingError("x")), |
| 797 | + ("PR labels", "x"), |
| 798 | + ) |
| 799 | + |
| 800 | + def test_other_value_error_is_generic(self): |
| 801 | + text = "PR routing changed; rerun reconciliation" |
| 802 | + self.assertEqual(routing.describe_failure(ValueError(text)), ("error", text)) |
| 803 | + |
| 804 | + def test_annotation_only_in_actions(self): |
| 805 | + error = routing.LabelRoutingError("bad 100%\nlabel") |
| 806 | + actions, local = io.StringIO(), io.StringIO() |
| 807 | + routing.report_failure(error, stream=actions, env={"GITHUB_ACTIONS": "true"}) |
| 808 | + routing.report_failure(error, stream=local, env={}) |
| 809 | + self.assertEqual( |
| 810 | + actions.getvalue(), |
| 811 | + "Review readiness: bad 100%\nlabel\n" |
| 812 | + "::error title=Review Readiness%3A PR labels::bad 100%25%0Alabel\n", |
| 813 | + ) |
| 814 | + self.assertEqual(local.getvalue(), "Review readiness: bad 100%\nlabel\n") |
| 815 | + |
| 816 | + def test_cli_failure_in_actions_prints_annotation(self): |
| 817 | + errors = io.StringIO() |
| 818 | + with ( |
| 819 | + mock.patch.dict(os.environ, {"GITHUB_ACTIONS": "true"}), |
| 820 | + mock.patch.object( |
| 821 | + routing, |
| 822 | + "check_review_readiness", |
| 823 | + side_effect=routing.LabelRoutingError("boom"), |
| 824 | + ), |
| 825 | + tempfile.TemporaryDirectory() as directory, |
| 826 | + contextlib.redirect_stderr(errors), |
| 827 | + ): |
| 828 | + path = Path(directory) / "REVIEWERS.md" |
| 829 | + path.write_text(DOCUMENT, encoding="utf-8") |
| 830 | + result = routing.main( |
| 831 | + ["--reviewers", str(path), "--repo", REPO, "--pr", "42"] |
| 832 | + ) |
| 833 | + self.assertEqual(result, 1) |
| 834 | + self.assertIn( |
| 835 | + "::error title=Review Readiness%3A PR labels::boom", errors.getvalue() |
| 836 | + ) |
| 837 | + |
| 838 | + |
686 | 839 | class CliTests(unittest.TestCase): |
| 840 | + def setUp(self): |
| 841 | + patcher = mock.patch.dict(os.environ) |
| 842 | + patcher.start() |
| 843 | + self.addCleanup(patcher.stop) |
| 844 | + os.environ.pop("GITHUB_ACTIONS", None) |
| 845 | + |
687 | 846 | def test_emits_plan_and_passes_apply_option(self): |
688 | 847 | with tempfile.TemporaryDirectory() as directory: |
689 | 848 | path = Path(directory) / "REVIEWERS.md" |
|
0 commit comments