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 BEHIND, CheckFailed, checks_now, checks_passed, CONFLICTS, open_pull, Pending, POLL_SECONDS, required_checks, unmergeable, WAIT_SECONDS from failure import Failure from forge.commands import forge_of, gitea, url_beside 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") class UrlBeside(unittest.TestCase): def test_names_a_repo_in_the_same_place_of_the_same_host_in_each_spelling_of_an_origin(self): self.assertEqual(url_beside("git@github.com:acme/billing.git", "ledger"), "git@github.com:acme/ledger") self.assertEqual(url_beside("https://gitea.larvit.se/acme/billing", "ledger"), "https://gitea.larvit.se/acme/ledger") self.assertEqual(url_beside("https://gitea.larvit.se/acme/billing.git/", "ledger"), "https://gitea.larvit.se/acme/ledger") self.assertEqual(url_beside("gitea:billing", "ledger"), "gitea:ledger", "a host with no owner") def test_names_none_where_the_origin_has_no_place_to_put_a_repo_in(self): self.assertIsNone(url_beside("billing", "ledger")) self.assertIsNone(url_beside("", "ledger")) self.assertIsNone(url_beside("https://gitea.larvit.se", "ledger")) # 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]}") class Unmergeable(unittest.TestCase): def gh(self, mergeable: str, state: str) -> str | None | Failure: commands: list[list[str]] = [] answer = unmergeable("gh", "7", lambda command: commands.append(command) or f'{{"mergeable": "{mergeable}", "mergeStateStatus": "{state}"}}') self.assertEqual(commands, [["gh", "pr", "view", "7", "--json", "mergeable,mergeStateStatus"]]) return answer def test_a_github_pull_request_that_conflicts_with_its_base_says_so(self): self.assertEqual(self.gh("CONFLICTING", "DIRTY"), CONFLICTS) def test_a_github_pull_request_behind_a_base_that_requires_it_up_to_date_says_so(self): self.assertEqual(self.gh("MERGEABLE", "BEHIND"), BEHIND) def test_a_github_pull_request_merges_whatever_else_holds_it_and_while_github_computes(self): for mergeable, state in (("MERGEABLE", "CLEAN"), ("MERGEABLE", "BLOCKED"), ("MERGEABLE", "UNSTABLE"), ("UNKNOWN", "UNKNOWN")): with self.subTest(mergeable=mergeable, state=state): self.assertIsNone(self.gh(mergeable, state)) def test_a_gitea_pull_request_conflicts_only_where_gitea_says_it_is_not_mergeable(self): commands: list[list[str]] = [] self.assertEqual(unmergeable("tea", "7", lambda command: commands.append(command) or '{"number": 7, "mergeable": false}'), CONFLICTS) self.assertEqual(commands, [["tea", "api", "repos/{owner}/{repo}/pulls/7"]]) self.assertIsNone(unmergeable("tea", "7", lambda command: '{"number": 7, "mergeable": true}')) def test_fails_where_the_forge_cannot_be_read_or_answers_in_another_shape(self): unread = Failure("no network") for forge in ("gh", "tea"): with self.subTest(forge=forge): self.assertIs(unmergeable(forge, "7", lambda command: unread), unread) self.assertEqual(unmergeable("gh", "7", lambda command: LONG).reason, f"gh pr view printed no mergeability: {LONG[:200]}") self.assertEqual(unmergeable("gh", "7", lambda command: "not json").reason, "gh pr view printed no mergeability: not json") self.assertEqual(unmergeable("gh", "7", lambda command: '{"mergeable": "MERGEABLE"}').reason, 'gh pr view printed no mergeability: {"mergeable": "MERGEABLE"}') self.assertEqual(unmergeable("tea", "7", lambda command: LONG).reason, f"tea api answered pull request 7 in a shape that can't be read: {LONG[:200]}") self.assertEqual(unmergeable("tea", "7", lambda command: '{"number": 7}').reason, "tea api answered pull request 7 in a shape that can't be read: {'number': 7}") 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 ChecksNow(unittest.TestCase): def test_reads_on_gh_once_and_says_a_check_is_pending(self): read = [] self.assertEqual(checks_now("gh", "7", "abc", set(), lambda command: read.append(command) or '[{"bucket": "pass"}, {"bucket": "pending"}]'), Pending([])) self.assertEqual(read, [["gh", "pr", "checks", "7", "--json", "bucket"]]) def test_takes_on_gh_passed_and_skipped_checks_and_no_checks_as_passed(self): self.assertIsNone(checks_now("gh", "7", "abc", set(), lambda command: '[{"bucket": "pass"}, {"bucket": "skipping"}]')) self.assertIsNone(checks_now("gh", "7", "abc", set(), lambda command: Failure("gh pr checks: no checks reported on the 'x' branch"))) def test_fails_on_gh_where_a_check_failed_or_was_cancelled_or_gh_fails(self): for bucket in ("fail", "cancel"): self.assertEqual(checks_now("gh", "7", "abc", set(), lambda command: f'[{{"bucket": "pending"}}, {{"bucket": "{bucket}"}}]'), CheckFailed("a check on pull request 7 failed")) self.assertEqual(checks_now("gh", "7", "abc", set(), lambda command: Failure("no network")), Failure("no network")) self.assertEqual(checks_now("tea", "1", "abc", set(), tea(['{"state": "failure", "total_count": 1}'])), CheckFailed("a check on pull request 1 ended in failure")) self.assertEqual(checks_now("gh", "7", "abc", set(), lambda command: "[1]").reason, "gh pr checks printed no checks: [1]") def test_reads_on_tea_once_and_names_each_required_check_that_has_not_passed(self): self.assertEqual(checks_now("tea", "1", "abc", {"lint"}, tea(['{"state": "pending", "total_count": 0, "statuses": []}'])), Pending(["lint"])) self.assertEqual(checks_now("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 1}'])), Pending([])) self.assertIsNone(checks_now("tea", "1", "abc", set(), tea(['{"state": "success", "total_count": 1}']))) def test_takes_a_commit_with_no_status_on_tea_as_pending_until_settled(self): self.assertEqual(checks_now("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 0}']), settled=False), Pending([])) self.assertIsNone(checks_now("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 0}']))) 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()