From 9f4ccbe1113dae29c65707cf8e8770db9b409725 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Mon, 16 Nov 2026 23:52:55 +0100 Subject: [PATCH] Pin the forge helpers' failure reasons and the give-up's read count in tests --- bin/forge/test_forge.py | 45 ++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/bin/forge/test_forge.py b/bin/forge/test_forge.py index 9b72aafe..b19a0dd0 100644 --- a/bin/forge/test_forge.py +++ b/bin/forge/test_forge.py @@ -6,8 +6,8 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from forge.pulls import checks_passed, POLL_SECONDS, required_checks, WAIT_SECONDS -from failure import Failure +from forge.pulls import checks_passed, open_pull, POLL_SECONDS, required_checks, WAIT_SECONDS +from forge.commands import forge_of, gitea from forge.commands import forge_of @@ -17,6 +17,23 @@ class ForgeOf(unittest.TestCase): self.assertEqual(forge_of("https://gitea.larvit.se/o/r.git"), "tea") +# An answer longer than the 200 characters a failure quotes of it, in a shape no call reads. +LONG = "[" + ", ".join(["1"] * 100) + "]" + + +class Gitea(unittest.TestCase): + def test_fails_with_giteas_message_where_it_answers_an_error(self): + self.assertEqual(gitea(lambda command: '{"message": "not found"}', "repos/o/r").reason, "tea api repos/o/r: not found") + + def test_fails_quoting_the_first_200_characters_where_tea_prints_no_json(self): + self.assertEqual(gitea(lambda command: "x" * 300, "repos/o/r").reason, f"tea api repos/o/r printed no JSON: {'x' * 200}") + + +class OpenPull(unittest.TestCase): + def test_fails_quoting_the_first_200_characters_where_tea_lists_pull_requests_in_another_shape(self): + self.assertEqual(open_pull("tea", "item-3", "abc", lambda command: LONG).reason, f"tea api listed pull requests in a shape that can't be read: {LONG[:200]}") + + def tea(statuses: typing.Iterable[str], branch: str = '{"name": "main"}') -> typing.Callable[[list[str]], str]: """A tea answering each read of the head's status with the next of statuses, and of branch main with branch.""" answers = iter(statuses) @@ -33,8 +50,8 @@ class RequiredChecks(unittest.TestCase): def test_reads_none_on_tea_where_the_protection_turns_status_checks_off(self): self.assertEqual(required_checks("tea", "main", tea([], '{"name": "main", "enable_status_check": false, "status_check_contexts": ["CI / tests (pull_request)"]}')), set()) - def test_fails_on_tea_where_the_branch_reads_in_another_shape(self): - self.assertIsInstance(required_checks("tea", "main", tea([], "[]")), Failure) + def test_fails_on_tea_quoting_the_first_200_characters_where_the_branch_reads_in_another_shape(self): + self.assertEqual(required_checks("tea", "main", tea([], LONG)).reason, f"tea api answered branch main in a shape that can't be read: {LONG[:200]}") def test_reads_none_on_github_whose_watch_covers_them(self): self.assertEqual(required_checks("gh", "main", self.fail), set()) @@ -59,15 +76,23 @@ class ChecksPassed(unittest.TestCase): passed = '{"state": "success", "total_count": 1, "statuses": [{"context": "CI / tests (pull_request)", "status": "success"}]}' self.assertIsNone(checks_passed("tea", "1", "abc", {"CI / *"}, tea([passed]), self.fail)) - def test_gives_up_on_tea_after_the_wait_naming_a_required_check_that_never_reported(self): - slept = [] - waited = checks_passed("tea", "1", "abc", {"CI / tests (pull_request)"}, tea(itertools.repeat('{"state": "pending", "total_count": 0, "statuses": []}')), slept.append) - self.assertIsInstance(waited, Failure) - self.assertIn("CI / tests (pull_request)", waited.reason) + def test_gives_up_on_tea_after_the_wait_naming_each_required_check_that_never_reported(self): + slept, read = [], [] + pending = tea(itertools.repeat('{"state": "pending", "total_count": 0, "statuses": []}')) + waited = checks_passed("tea", "1", "abc", {"lint", "CI / tests (pull_request)"}, lambda command: read.append(command) or pending(command), slept.append) + self.assertEqual(waited.reason, "gave up on pull request 1 after 30 minutes: CI / tests (pull_request), lint, which the protection requires, never passed") self.assertEqual(sum(slept), WAIT_SECONDS) + self.assertEqual(len(read), WAIT_SECONDS // POLL_SECONDS + 1) + + def test_gives_up_on_tea_after_the_wait_saying_a_check_is_still_pending(self): + waited = checks_passed("tea", "1", "abc", set(), tea(itertools.repeat('{"state": "pending", "total_count": 1, "statuses": []}')), lambda seconds: None) + self.assertEqual(waited.reason, "gave up on pull request 1 after 30 minutes: a check is still pending") def test_fails_on_tea_where_a_check_failed(self): - self.assertIsInstance(checks_passed("tea", "1", "abc", set(), tea(['{"state": "failure", "total_count": 1}']), self.fail), Failure) + self.assertEqual(checks_passed("tea", "1", "abc", set(), tea(['{"state": "failure", "total_count": 1}']), self.fail).reason, "a check on pull request 1 ended in failure") + + def test_fails_on_tea_quoting_the_first_200_characters_where_the_status_reads_in_another_shape(self): + self.assertEqual(checks_passed("tea", "1", "abc", set(), tea([LONG]), self.fail).reason, f"tea api printed no commit status for abc: {LONG[:200]}")