import itertools import pathlib import sys import typing import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from forge.pulls import checks_passed, open_pull, POLL_SECONDS, required_checks, WAIT_SECONDS from forge.commands import forge_of, gitea class ForgeOf(unittest.TestCase): def test_takes_a_github_origin_as_gh_and_any_other_as_tea(self): self.assertEqual(forge_of("git@github.com:o/r.git"), "gh") self.assertEqual(forge_of("https://gitea.larvit.se/o/r.git"), "tea") # An answer no call can read, longer than the 200 characters of it that a failure quotes. 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) return lambda command: branch if command[-1] == "repos/{owner}/{repo}/branches/main" else next(answers) REQUIRED = '{"name": "main", "enable_status_check": true, "status_check_contexts": ["CI / tests (pull_request)"]}' class RequiredChecks(unittest.TestCase): def test_reads_on_tea_the_checks_the_protection_of_the_base_requires(self): self.assertEqual(required_checks("tea", "main", tea([], REQUIRED)), {"CI / tests (pull_request)"}) 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_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()) class ChecksPassed(unittest.TestCase): def test_waits_on_tea_while_a_check_is_pending(self): slept = [] self.assertIsNone(checks_passed("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 1}', '{"state": "success", "total_count": 1}']), slept.append)) self.assertEqual(slept, [POLL_SECONDS]) def test_takes_a_commit_with_no_status_on_tea_as_passed_where_the_base_requires_none(self): self.assertIsNone(checks_passed("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 0}']), self.fail)) def test_waits_on_tea_for_a_check_the_base_requires_until_it_reports_and_passes(self): slept = [] statuses = ['{"state": "pending", "total_count": 0, "statuses": []}', '{"state": "success", "total_count": 1, "statuses": [{"context": "lint", "status": "success"}]}', '{"state": "success", "total_count": 2, "statuses": [{"context": "lint", "status": "success"}, {"context": "CI / tests (pull_request)", "status": "success"}]}'] self.assertIsNone(checks_passed("tea", "1", "abc", {"CI / tests (pull_request)"}, tea(statuses), slept.append)) self.assertEqual(slept, [POLL_SECONDS, POLL_SECONDS]) def test_takes_on_tea_a_required_glob_as_met_by_a_check_it_matches(self): 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_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.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]}") if __name__ == "__main__": unittest.main()