import contextlib import fcntl import json import os import pathlib import shutil import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from chunk_pr.main import cleaned, merged, QUEUE, step, turn, USAGE from failure import Failure from test_env import isolated_env from project_config.settings import ProjectConfig from forge.commands import Run from forge.reviews import THREADS TOOL = pathlib.Path(__file__).resolve().parents[1] / "chunk-pr" GITHUB = "git@github.com:me/project.git" GITEA = "ssh://git@gitea.example.org:22/me/project.git" HEAD = "c0ffee" PUSH = ["git", "push", "--quiet", "--set-upstream", "origin", "item-7"] GH_LIST = ["gh", "pr", "list", "--head", "item-7", "--json", "number,headRefOid,isCrossRepository", "--template", "{{range .}}{{.number}}\t{{.headRefOid}}\t{{.isCrossRepository}}\n{{end}}"] TEA_LIST = ["tea", "api", "repos/{owner}/{repo}/pulls?state=open&limit=50"] GH_SETTINGS = ["gh", "repo", "view", "--json", "mergeCommitAllowed,rebaseMergeAllowed,squashMergeAllowed"] TEA_SETTINGS = ["tea", "api", "repos/{owner}/{repo}"] GH_CHECKS = ["gh", "pr", "checks", "12", "--watch", "--fail-fast"] TEA_BRANCH = ["tea", "api", "repos/{owner}/{repo}/branches/main"] TEA_STATUS = ["tea", "api", f"repos/{{owner}}/{{repo}}/commits/{HEAD}/status"] OWED = [str(pathlib.Path(__file__).resolve().parents[1] / "larv-review"), "scoring-run-owed"] TITLE = [str(pathlib.Path(__file__).resolve().parents[1] / "project-config"), "pull-request-title"] BRANCH = [str(pathlib.Path(__file__).resolve().parents[1] / "project-config"), "branch"] WAITS = [str(pathlib.Path(__file__).resolve().parents[1] / "project-config"), "wait-for-approval"] # The settings a fake checkout's answers hold, each under `project-config ` as the tool once read it. PROJECT_CONFIG = str(pathlib.Path(__file__).resolve().parents[1] / "project-config") GH_DECISION = ["gh", "pr", "view", "12", "--json", "reviewDecision"] GH_THREADS = ["gh", "api", "graphql", "-f", f"query={THREADS}", "-F", "owner={owner}", "-F", "repo={repo}", "-F", "number=12"] BEHIND = ["git", "rev-list", "--count", "HEAD..origin/main"] COMMON = ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"] # The folder the fake checkouts name as their shared git folder, where merge keeps its queue's file. GIT_FOLDER = tempfile.TemporaryDirectory() class Checkout: """A fake run in a checkout of branch item-7 at commit HEAD, cloned from the GitHub or Gitea origin it is given, with main as origin's default branch. Pull request #12 from item-7 is open at HEAD from the start where it is given open=True, and otherwise once a create has run. The repository allows the merge methods in allowed, by the forge's setting names: a merge commit unless given others. It records every command run, and fails the other forge's commands.""" def __init__(self, origin: str, open: bool, allowed: tuple[str, ...] = ("mergeCommitAllowed", "allow_merge_commits")): self.github = origin == GITHUB self.open = open self.allowed = allowed self.statuses: list[str] = [] # What GitHub reports as the pull request's review decision on each read; the last one repeats. self.decisions = ["APPROVED"] # Set, what GitHub prints instead of a decision's JSON. self.unreadable: str | Failure | None = None # The pull request's review threads, as GitHub's GraphQL API prints them; its author is `worker`. self.threads: list[dict[str, object]] = [] self.ran: list[list[str]] = [] self.answers: dict[tuple[str, ...], str | Failure] = { ("git", "remote", "get-url", "origin"): f"{origin}\n", ("git", "symbolic-ref", "--short", "HEAD"): "item-7\n", ("git", "rev-parse", "HEAD"): f"{HEAD}\n", ("git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD"): "origin/main\n", tuple(WAITS): "false\n", tuple(TITLE): "{summary}\n", tuple(BRANCH): "item-{id}\n", tuple(BEHIND): "0\n", tuple(COMMON): f"{GIT_FOLDER.name}\n", ("git", "rev-parse", "--show-toplevel"): "/checkout\n", } def config(self) -> ProjectConfig | Failure: """The project's config, from the settings the answers hold under PROJECT_CONFIG; a Failure where one of them is, as where no config names the checkout.""" named = {key[1]: answer for key, answer in self.answers.items() if key[0] == PROJECT_CONFIG} if failed := next((answer for answer in named.values() if isinstance(answer, Failure)), None): return failed texts = {name: str(answer).strip() for name, answer in named.items()} paths = {name: pathlib.Path(texts[name]) for name in ("plan", "changelog", "comprehension-history") if texts.get(name, "").startswith("/")} return ProjectConfig("proj", pathlib.Path("/config"), pathlib.Path("/checkout"), paths, wait_for_approval=texts.get("wait-for-approval") == "true", commit_title=texts.get("commit-title", "{summary}"), pull_request_title=texts.get("pull-request-title", "{summary}"), branch=texts.get("branch", "item-{id}")) def __call__(self, command: list[str]) -> str | Failure: self.ran.append(command) if command[0] == ("tea" if self.github else "gh"): return Failure(f"{command[0]} on the wrong forge") if tuple(command) in self.answers: return self.answers[tuple(command)] if command[2:3] == ["create"]: self.open = True if command == GH_LIST: return f"12\t{HEAD}\tfalse\n" if self.open else "" if command == TEA_LIST: return json.dumps([{"number": 12, "head": {"ref": "item-7", "label": "item-7", "sha": HEAD, "repo": {"full_name": "me/project"}}, "base": {"repo": {"full_name": "me/project"}}}] if self.open else []) if command == TEA_STATUS: return self.statuses.pop(0) if self.statuses else json.dumps({"total_count": 1, "state": "success", "statuses": [{"context": "CI / tests (pull_request)", "status": "success"}]}) if command == GH_DECISION and self.unreadable is not None: return self.unreadable if command == GH_DECISION: return json.dumps({"reviewDecision": self.decisions.pop(0) if len(self.decisions) > 1 else self.decisions[0]}) if command == GH_THREADS: return json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "worker"}, "reviewThreads": {"nodes": self.threads}}}}}) if command == TEA_BRANCH: return json.dumps({"enable_status_check": False}) if command in (GH_SETTINGS, TEA_SETTINGS): return json.dumps({setting: True for setting in self.allowed}) return "" def acted(self) -> list[list[str]]: """What it ran beyond reading the checkout, the pull requests and the repository's settings.""" reads = [GH_LIST, GH_DECISION, TEA_LIST, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS, GH_THREADS] return [command for command in self.ran if tuple(command) not in self.answers and command not in reads] class Open(unittest.TestCase): def test_a_title_must_fit_the_template_only_on_a_branch_that_fits_the_configs_branch_template(self): for argv in (["open", "Release review of 0.3.0"], ["ready", "Release review of 0.3.0"]): with self.subTest(argv=argv): checkout = Checkout(GITHUB, open=argv[0] == "ready") checkout.answers[tuple(TITLE)] = "PAY-{id} - {summary}\n" checkout.answers[tuple(BRANCH)] = "feature/PAY-{id}\n" self.assertNotIsInstance(step(argv, checkout, checkout.config), Failure) checkout.answers[("git", "symbolic-ref", "--short", "HEAD")] = "feature/PAY-7\n" refused = step(argv, checkout, checkout.config) assert isinstance(refused, Failure), refused self.assertIn("'Release review of 0.3.0' doesn't fit", refused.reason) def test_github_pushes_and_opens_a_draft_into_the_default_branch(self): checkout = Checkout(GITHUB, open=False) opened = step(["open", "ABC-1 - Do it", "The body."], checkout, checkout.config) self.assertEqual(checkout.acted(), [PUSH, ["gh", "pr", "create", "--draft", "--head", "item-7", "--base", "main", "--title", "ABC-1 - Do it", "--body", "The body."]]) self.assertEqual(opened, "Opened draft pull request #12.") def test_gitea_opens_a_draft_with_tea(self): checkout = Checkout(GITEA, open=False) self.assertEqual(step(["open", "Do it"], checkout, checkout.config), "Opened draft pull request #12.") self.assertEqual(checkout.acted(), [PUSH, ["tea", "pulls", "create", "--draft", "--head", "item-7", "--base", "main", "--title", "Do it", "--description", ""]]) def test_an_open_pull_request_is_kept(self): checkout = Checkout(GITEA, open=True) self.assertEqual(step(["open", "Do it"], checkout, checkout.config), "Pull request #12 is open already.") self.assertEqual(checkout.acted(), [PUSH]) def test_on_the_default_branch_it_refuses_and_pushes_nothing(self): checkout = Checkout(GITEA, open=False) checkout.answers[("git", "symbolic-ref", "--short", "HEAD")] = "main\n" self.assertIsInstance(step(["open", "Do it"], checkout, checkout.config), Failure) self.assertEqual(checkout.acted(), []) def test_a_title_that_does_not_fit_the_configs_template_is_refused_before_anything_runs(self): for argv in (["open", "Do it"], ["ready", "Do it"]): with self.subTest(argv=argv): checkout = Checkout(GITHUB, open=argv[0] == "ready") checkout.answers[tuple(TITLE)] = "PAY-{id} - {summary}\n" refused = step(argv, checkout, checkout.config) assert isinstance(refused, Failure), refused self.assertIn("`PAY-{id} - {summary}`", refused.reason) self.assertEqual(checkout.acted(), []) self.assertNotIsInstance(step([argv[0], "PAY-7 - Do it"], checkout, checkout.config), Failure) checkout.answers[tuple(TITLE)] = Failure("no config names the checkout") self.assertEqual(step(argv, checkout, checkout.config), Failure("no config names the checkout")) checkout.answers[tuple(TITLE)] = "PAY-{id} - {summary}\n" checkout.answers[tuple(BRANCH)] = Failure("`branch` is 'x', which holds no {id}") self.assertEqual(step(argv, checkout, checkout.config), Failure("`branch` is 'x', which holds no {id}")) untitled = Checkout(GITHUB, open=True) untitled.answers[tuple(TITLE)] = Failure("no config names the checkout") self.assertNotIsInstance(step(["ready"], untitled, untitled.config), Failure) def test_a_title_of_more_than_one_line_or_a_second_body_is_refused_before_anything_runs(self): for argv, refusal in ((["open", "Do it\nand more"], "give a title of one line"), (["open", " "], "give a title of one line"), (["open", "Do it", "Body", "More"], USAGE)): with self.subTest(argv): checkout = Checkout(GITHUB, open=False) self.assertEqual(step(argv, checkout, checkout.config), Failure(refusal)) self.assertEqual(checkout.ran, []) def test_a_failed_listing_after_the_create_is_returned(self): checkout = Checkout(GITHUB, open=False) checkout.answers[("gh", "pr", "create", "--draft", "--head", "item-7", "--base", "main", "--title", "Do it", "--body", "")] = Failure("gh pr create: done") self.assertEqual(step(["open", "Do it"], checkout, checkout.config), Failure("gh pr create: done")) checkout = Checkout(GITHUB, open=False) listings = iter(["", Failure("gh pr list: offline")]) original = checkout.__call__ checkout_call = lambda command: next(listings) if command == GH_LIST else original(command) self.assertEqual(step(["open", "Do it"], checkout_call, checkout.config), Failure("gh pr list: offline")) class Ready(unittest.TestCase): def test_github_marks_it_ready(self): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(["ready"], checkout, checkout.config), "Marked pull request #12 ready.") self.assertEqual(checkout.acted(), [["gh", "pr", "ready", "12"]]) def test_gitea_marks_it_ready_with_tea(self): checkout = Checkout(GITEA, open=True) self.assertEqual(step(["ready"], checkout, checkout.config), "Marked pull request #12 ready.") self.assertEqual(checkout.acted(), [["tea", "pulls", "edit", "12", "--ready"]]) def test_github_sets_a_given_title_then_marks_it_ready(self): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(["ready", "Ship it"], checkout, checkout.config), "Marked pull request #12 ready, titled “Ship it”.") self.assertEqual(checkout.acted(), [["gh", "pr", "edit", "12", "--title", "Ship it"], ["gh", "pr", "ready", "12"]]) def test_gitea_sets_a_given_title_in_the_call_that_marks_it_ready(self): checkout = Checkout(GITEA, open=True) self.assertEqual(step(["ready", "Ship it"], checkout, checkout.config), "Marked pull request #12 ready, titled “Ship it”.") self.assertEqual(checkout.acted(), [["tea", "pulls", "edit", "12", "--title", "Ship it", "--ready"]]) def test_a_title_of_more_than_one_line_or_none_is_refused_before_anything_runs(self): for argv, refusal in ((["ready", "Ship\nit"], "give a title of one line"), (["ready", " "], "give a title of one line"), (["ready", "Ship it", "twice"], USAGE)): with self.subTest(argv=argv): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(argv, checkout, checkout.config), Failure(refusal)) self.assertEqual(checkout.ran, []) def test_a_failed_title_edit_on_github_leaves_it_a_draft(self): checkout = Checkout(GITHUB, open=True) checkout.answers[("gh", "pr", "edit", "12", "--title", "Ship it")] = Failure("gh pr edit: denied") self.assertEqual(step(["ready", "Ship it"], checkout, checkout.config), Failure("gh pr edit: denied")) self.assertNotIn(["gh", "pr", "ready", "12"], checkout.ran) def test_without_an_open_pull_request_at_the_head_it_fails_naming_the_branch(self): checkout = Checkout(GITHUB, open=False) ready = step(["ready"], checkout, checkout.config) self.assertIsInstance(ready, Failure) self.assertIn("item-7", ready.reason) self.assertEqual(checkout.acted(), []) def test_a_failed_ready_or_git_call_is_returned(self): checkout = Checkout(GITEA, open=True) checkout.answers[("tea", "pulls", "edit", "12", "--ready")] = Failure("tea pulls edit: denied") self.assertEqual(step(["ready"], checkout, checkout.config), Failure("tea pulls edit: denied")) checkout = Checkout(GITEA, open=True) checkout.answers[("git", "symbolic-ref", "--short", "HEAD")] = Failure("git symbolic-ref: HEAD is detached") self.assertEqual(step(["ready"], checkout, checkout.config), Failure("git symbolic-ref: HEAD is detached")) self.assertEqual(checkout.acted(), []) FETCH = ["git", "fetch", "--quiet", "origin"] BEHIND = ("git", "rev-list", "--count", "HEAD..origin/main") MERGE = ["git", "merge", "--quiet", "--no-edit", "origin/main"] CONFLICTED = ("git", "diff", "--name-only", "--diff-filter=U") RANK = str(pathlib.Path(__file__).resolve().parents[1] / "rank_todo.py") PLAN = (str(pathlib.Path(__file__).resolve().parents[1] / "project-config"), "plan") class Sync(unittest.TestCase): def checkout(self, behind: int, todo: bool = False) -> Checkout: """A Checkout behind origin/main by behind commits, at a worktree root holding a todo.md where todo is True.""" checkout = Checkout(GITEA, open=True) root = self.enterContext(tempfile.TemporaryDirectory()) if todo: pathlib.Path(root, "todo.md").write_text("") checkout.answers[("git", "rev-parse", "--show-toplevel")] = f"{root}\n" checkout.answers[BEHIND] = f"{behind}\n" self.todo = str(pathlib.Path(root, "todo.md")) checkout.answers[PLAN] = f"{self.todo}\n" return checkout def test_a_branch_behind_the_default_branch_merges_it(self): checkout = self.checkout(behind=3) self.assertEqual(step(["sync"], checkout, checkout.config), "Merged origin/main into item-7.") self.assertEqual(checkout.acted(), [FETCH, MERGE]) def test_a_branch_that_holds_the_default_branch_merges_nothing(self): checkout = self.checkout(behind=0) self.assertEqual(step(["sync"], checkout, checkout.config), "item-7 already holds origin/main.") self.assertEqual(checkout.acted(), [FETCH]) def test_a_worktree_with_a_todo_md_runs_the_rank_check_after_the_merge(self): checkout = self.checkout(behind=1, todo=True) self.assertEqual(step(["sync"], checkout, checkout.config), f"Merged origin/main into item-7.\nrank_todo.py --check todo.md passed.") self.assertEqual(checkout.acted(), [FETCH, MERGE, [RANK, "--check", self.todo]]) def test_a_checkout_no_config_names_fails_naming_what_it_did(self): checkout = self.checkout(behind=0, todo=True) checkout.answers[PLAN] = Failure("no config names the checkout") self.assertEqual(step(["sync"], checkout, checkout.config), Failure("item-7 already holds origin/main, but the plan to check cannot be found: no config names the checkout")) def test_a_failing_rank_check_fails_naming_what_it_did(self): checkout = self.checkout(behind=0, todo=True) checkout.answers[(RANK, "--check", self.todo)] = Failure("rank_todo.py --check: item 3 is out of order") self.assertEqual(step(["sync"], checkout, checkout.config), Failure("item-7 already holds origin/main, but rank_todo.py --check todo.md failed: rank_todo.py --check: item 3 is out of order")) def test_a_conflict_leaves_the_merge_in_progress_and_names_the_conflicted_files(self): checkout = self.checkout(behind=2, todo=True) checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: CONFLICT") checkout.answers[CONFLICTED] = "todo.md\nREADME.md\n" self.assertEqual(step(["sync"], checkout, checkout.config), Failure("merging origin/main into item-7 conflicts in todo.md, README.md; the merge stays in progress: resolve them and commit")) self.assertNotIn([RANK, "--check", self.todo], checkout.ran) self.assertNotIn(["git", "merge", "--abort"], checkout.ran) def test_a_merge_failing_without_a_conflict_is_returned(self): checkout = self.checkout(behind=2) checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: untracked files would be overwritten") checkout.answers[CONFLICTED] = "" self.assertEqual(step(["sync"], checkout, checkout.config), Failure("git merge --quiet: untracked files would be overwritten")) def test_on_the_default_branch_it_refuses_and_fetches_nothing(self): checkout = self.checkout(behind=0) checkout.answers[("git", "symbolic-ref", "--short", "HEAD")] = "main\n" self.assertEqual(step(["sync"], checkout, checkout.config), Failure("main is origin's default branch; sync the chunk's branch")) self.assertNotIn(FETCH, checkout.ran) def test_a_failed_fetch_is_returned(self): checkout = self.checkout(behind=0) checkout.answers[tuple(FETCH)] = Failure("git fetch --quiet: could not read from remote") self.assertEqual(step(["sync"], checkout, checkout.config), Failure("git fetch --quiet: could not read from remote")) def changes(checkout: Checkout) -> list[list[str]]: """What a merge ran, leaving out what it read, the project's config included, and the fetch each turn opens with.""" return [command for command in checkout.acted() if command != FETCH and not command[0].endswith("/project-config")] class Merge(unittest.TestCase): def test_the_gate_passed_flag_it_no_longer_takes_is_refused_before_anything_runs(self): checkout = Checkout(GITHUB, open=True) self.assertIsInstance(step(["merge", "--gate-passed"], checkout, checkout.config), Failure) self.assertEqual(checkout.ran, []) def test_an_option_given_as_a_title_runs_nothing(self): for argv in (["ready", "--help"], ["open", "-h"]): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(argv, checkout, checkout.config), Failure(USAGE)) self.assertEqual(checkout.ran, []) def test_github_waits_for_the_checks_before_merging(self): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (merge).") self.assertEqual(checkout.ran[-2:], [GH_CHECKS, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_a_failed_check_on_github_leaves_it_unmerged(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(GH_CHECKS)] = Failure("gh pr checks 12: CI / tests\tfail") self.assertEqual(step(["merge"], checkout, checkout.config), Failure("gh pr checks 12: CI / tests\tfail")) self.assertEqual(changes(checkout), []) def test_github_waits_a_minute_for_checks_to_report_before_taking_none_as_passed(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(GH_CHECKS)] = Failure("no checks reported on the 'item-7' branch") slept: list[float] = [] self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).") self.assertEqual(sum(slept), 60) self.assertEqual(checkout.ran.count(GH_CHECKS), 7) def test_gitea_waits_a_minute_for_a_status_before_taking_none_as_passed_where_main_requires_none(self): checkout = Checkout(GITEA, open=True) checkout.statuses = [json.dumps({"total_count": 0, "state": "pending", "statuses": []})] * 7 slept: list[float] = [] self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).") self.assertEqual(sum(slept), 60) def requiring_approval(self, origin: str = GITHUB, *decisions: str) -> Checkout: """A Checkout whose project config sets wait-for-approval, and which reports each of decisions in turn as the review decision.""" checkout = Checkout(origin, open=True) checkout.answers[tuple(WAITS)] = "true\n" checkout.decisions = list(decisions) or ["APPROVED"] return checkout def test_a_project_without_wait_for_approval_never_reads_the_review_decision(self): checkout = Checkout(GITHUB, open=True) checkout.decisions = ["REVIEW_REQUIRED"] self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (merge).") self.assertNotIn(GH_DECISION, checkout.ran) def test_an_approved_pull_request_merges_after_the_checks_and_the_approval(self): checkout = self.requiring_approval() self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (merge).") self.assertEqual(checkout.ran[-4:], [GH_CHECKS, GH_DECISION, GH_THREADS, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_it_waits_a_minute_at_a_time_while_the_branch_protection_still_wants_a_review(self): checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED") slept: list[float] = [] self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).") self.assertEqual(slept, [60]) def test_a_merge_that_parks_returns_at_once_while_the_branch_protection_still_wants_a_review(self): checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED") failed = merged(checkout, checkout.config, self.fail, parks=True) self.assertEqual(failed.reason, "pull request 12 passed its checks and awaits an approval; end your run now, since do-epic parks the chunk and resumes this session once it is approved or a reviewer comments") self.assertEqual(checkout.ran.count(GH_DECISION), 1) self.assertEqual(step(["merge"], self.requiring_approval(GITHUB, "REVIEW_REQUIRED"), checkout.config, parks=True), failed) def test_a_merge_that_parks_reports_a_reviewers_unanswered_comment_before_the_approval_it_awaits(self): checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED") checkout.threads = [MergeQueue.thread("bin/asked.py", "reviewer")] failed = merged(checkout, checkout.config, self.fail, parks=True) self.assertEqual(failed.reason, "a reviewer's comment on pull request 12 waits for an answer, in bin/asked.py; answer it in its thread, push any fix, and run chunk-pr merge again") checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED") checkout.answers[tuple(GH_THREADS)] = Failure("gh api graphql failed") self.assertEqual(merged(checkout, checkout.config, self.fail, parks=True), Failure("gh api graphql failed")) def test_a_merge_that_parks_merges_an_approved_pull_request(self): checkout = self.requiring_approval() self.assertEqual(merged(checkout, checkout.config, self.fail, parks=True), "Merged pull request #12 (merge).") def test_an_approval_that_never_comes_leaves_it_unmerged_after_90_minutes(self): checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED") slept: list[float] = [] failed = merged(checkout, checkout.config, slept.append) self.assertIsInstance(failed, Failure) self.assertIn("90 minutes", failed.reason) self.assertEqual(sum(slept), 90 * 60) self.assertEqual(changes(checkout), []) def test_an_approval_that_comes_in_the_last_minute_still_merges(self): checkout = self.requiring_approval(GITHUB, *["REVIEW_REQUIRED"] * 91, "APPROVED") slept: list[float] = [] self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).") self.assertEqual(sum(slept), 90 * 60) def test_a_review_decision_it_cannot_read_or_does_not_know_leaves_it_unmerged(self): for answer in (Failure("gh pr view failed"), "not json", "[]", "{}", json.dumps({"reviewDecision": "DISMISSED"})): with self.subTest(answer): checkout = self.requiring_approval() checkout.unreadable = answer self.assertIsInstance(step(["merge"], checkout, checkout.config), Failure) self.assertEqual(changes(checkout), []) def test_requested_changes_leave_it_unmerged_without_waiting(self): checkout = self.requiring_approval(GITHUB, "CHANGES_REQUESTED") slept: list[float] = [] failed = merged(checkout, checkout.config, slept.append) self.assertIsInstance(failed, Failure) self.assertIn("requested changes", failed.reason) self.assertEqual((slept, changes(checkout)), ([], [])) def test_a_branch_protection_that_asks_for_no_review_leaves_it_unmerged_naming_the_setting(self): for decision in ("", None): with self.subTest(decision): checkout = self.requiring_approval(GITHUB, decision) failed = step(["merge"], checkout, checkout.config) self.assertIsInstance(failed, Failure) self.assertIn("requires no review", failed.reason) self.assertEqual(changes(checkout), []) def test_gitea_with_wait_for_approval_leaves_it_unmerged_since_chunk_pr_reads_approvals_on_github_only(self): checkout = self.requiring_approval(GITEA) failed = step(["merge"], checkout, checkout.config) self.assertIsInstance(failed, Failure) self.assertIn("GitHub", failed.reason) self.assertEqual(changes(checkout), []) def test_a_config_it_cannot_read_leaves_it_unmerged(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(WAITS)] = Failure("no config names the checkout") self.assertEqual(step(["merge"], checkout, checkout.config), Failure("no config names the checkout")) self.assertEqual(changes(checkout), []) def protected(self, *reads: list[tuple[str, str]]) -> Checkout: """A Gitea Checkout whose main requires a check matching CI / tests*, its status at HEAD holding each of reads in turn.""" checkout = Checkout(GITEA, open=True) checkout.answers[tuple(TEA_BRANCH)] = json.dumps({"enable_status_check": True, "status_check_contexts": ["CI / tests*"]}) def combined(read: list[tuple[str, str]]) -> str: states = {state for _, state in read} state = "failure" if "failure" in states else "pending" if "pending" in states or not read else "success" return json.dumps({"total_count": len(read), "state": state, "statuses": [{"context": context, "status": status} for context, status in read]}) checkout.statuses = [combined(read) for read in reads] return checkout def test_gitea_waits_until_the_required_check_reports_and_passes_then_merges(self): checkout = self.protected([], [("CI / tests (pull_request)", "pending")], [("CI / tests (pull_request)", "success")]) slept: list[float] = [] self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).") self.assertEqual(len(slept), 2) self.assertEqual(checkout.ran.count(TEA_STATUS), 3) def test_a_failed_check_on_gitea_leaves_it_unmerged(self): checkout = self.protected([("CI / tests (pull_request)", "pending")], [("CI / tests (pull_request)", "failure")]) self.assertIsInstance(merged(checkout, checkout.config, lambda _: None), Failure) self.assertEqual(changes(checkout), []) def test_a_branch_protection_it_cannot_read_leaves_it_unmerged(self): checkout = Checkout(GITEA, open=True) checkout.answers[tuple(TEA_BRANCH)] = Failure("tea api: 500") self.assertEqual(merged(checkout, checkout.config, self.fail), Failure("tea api: 500")) self.assertNotIn(TEA_STATUS, checkout.ran) self.assertEqual(changes(checkout), []) def test_an_unknown_default_branch_leaves_it_unmerged_reading_no_checks(self): checkout = Checkout(GITEA, open=True) checkout.answers[("git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD")] = Failure("no such ref") self.assertEqual(merged(checkout, checkout.config, self.fail), Failure("origin records no default branch; set it with `git remote set-head origin --auto`: no such ref")) self.assertNotIn(TEA_STATUS, checkout.ran) self.assertEqual(changes(checkout), []) def test_gitea_checks_that_never_pass_leave_it_unmerged(self): checkout = self.protected(*[[("CI / tests (pull_request)", "pending")]] * 1000) failed = merged(checkout, checkout.config, lambda _: None) self.assertIsInstance(failed, Failure) self.assertIn("gave up on pull request 12", failed.reason) self.assertEqual(changes(checkout), []) def test_a_scoring_run_the_chunk_owes_and_has_not_recorded_refuses_the_merge(self): for origin in (GITHUB, GITEA): with self.subTest(origin=origin): checkout = Checkout(origin, open=True) checkout.answers[tuple(OWED)] = Failure("larv-review scoring-run-owed: the chunk owes 2 scoring runs") self.assertEqual(step(["merge"], checkout, checkout.config), Failure("larv-review scoring-run-owed: the chunk owes 2 scoring runs")) self.assertEqual(changes(checkout), []) def test_github_merges_with_a_merge_commit_pinned_to_the_head(self): checkout = Checkout(GITHUB, open=True, allowed=("squashMergeAllowed", "mergeCommitAllowed")) self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout), [["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_github_without_merge_commits_rebases(self): checkout = Checkout(GITHUB, open=True, allowed=("squashMergeAllowed", "rebaseMergeAllowed")) step(["merge"], checkout, checkout.config) self.assertEqual(changes(checkout), [["gh", "pr", "merge", "12", "--rebase", "--match-head-commit", HEAD]]) def test_gitea_merges_fast_forward_only_where_only_that_and_squash_are_allowed(self): checkout = Checkout(GITEA, open=True, allowed=("allow_squash_merge", "allow_fast_forward_only_merge")) self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (fast-forward-only).") self.assertEqual(changes(checkout), [["tea", "api", "--method", "POST", "--field", "Do=fast-forward-only", "--field", f"head_commit_id={HEAD}", "repos/{owner}/{repo}/pulls/12/merge"]]) def test_a_repository_allowing_no_known_method_is_refused_unmerged(self): checkout = Checkout(GITEA, open=True, allowed=("allow_manual_merge",)) self.assertIsInstance(step(["merge"], checkout, checkout.config), Failure) self.assertEqual(changes(checkout), []) def test_a_forge_failure_is_returned(self): checkout = Checkout(GITHUB, open=True) checkout.answers[("gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD)] = Failure("gh pr merge: head moved") self.assertEqual(step(["merge"], checkout, checkout.config), Failure("gh pr merge: head moved")) checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(GH_SETTINGS)] = "not json" self.assertIsInstance(step(["merge"], checkout, checkout.config), Failure) def test_a_failed_settings_read_is_returned_unmerged(self): for origin, settings in ((GITHUB, GH_SETTINGS), (GITEA, TEA_SETTINGS)): with self.subTest(origin): checkout = Checkout(origin, open=True) checkout.answers[tuple(settings)] = Failure("settings: denied") self.assertEqual(step(["merge"], checkout, checkout.config), Failure("settings: denied")) self.assertEqual(changes(checkout), []) def queue_is_free() -> bool: """Whether another merge could take its turn now.""" fd = os.open(pathlib.Path(GIT_FOLDER.name) / QUEUE, os.O_RDWR | os.O_CREAT) try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: return False finally: os.close(fd) return True class MergeQueue(unittest.TestCase): PUSHED = ["git", "push", "--quiet", "origin", "item-7"] TODO_ITEM = str(pathlib.Path(__file__).resolve().parents[1] / "todo-item") CONFIG = str(pathlib.Path(__file__).resolve().parents[1] / "project-config") def moved(self, checkout: Checkout) -> Checkout: """checkout with origin's main two commits ahead of the branch, whose head is `decaf` once main is merged in.""" checkout.answers[tuple(BEHIND)] = "2\n" run = checkout.__call__ def merging(command: list[str]) -> str | Failure: answer = run(command) if command == MERGE and not isinstance(answer, Failure): checkout.answers[("git", "rev-parse", "HEAD")] = "decaf\n" return answer checkout.__call__ = merging # type: ignore[method-assign] return checkout def with_plan(self, checkout: Checkout, held: bool = True) -> tuple[Checkout, str]: """checkout in a project whose config names a todo.md and a CHANGELOG.md in one folder, which is returned, and titles commits `[{id}] {summary}`. The plan holds item 7 where held. A commit moves the head to `f00d`.""" folder = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, folder) for name in ("todo.md", "CHANGELOG.md"): (pathlib.Path(folder) / name).write_text("") checkout.answers.update({ (self.TODO_ITEM, "show", "7"): "From todo.md:\n" if held else Failure("todo-item show 7: todo.md holds no item 7"), (self.CONFIG, "plan"): f"{folder}/todo.md\n", (self.CONFIG, "changelog"): f"{folder}/CHANGELOG.md\n", (self.CONFIG, "commit-title"): "[{id}] {summary}\n", ("git", "-C", folder, "rev-parse", "--show-toplevel"): f"{folder}\n", ("git", "-C", folder, "status", "--porcelain", "--", f"{folder}/todo.md", f"{folder}/CHANGELOG.md"): " M todo.md\n", }) run = checkout.__call__ def committing(command: list[str]) -> str | Failure: if command[3:4] == ["commit"]: checkout.answers[("git", "rev-parse", "HEAD")] = "f00d\n" return run(command) checkout.__call__ = committing # type: ignore[method-assign] return checkout, folder @staticmethod def thread(path: str, last: str, resolved: bool = False) -> dict[str, object]: return {"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}} def test_malformed_finish_flags_dont_run_anything(self): for flags in (["--finish"], ["--finish", "abc"], ["--changelog", "Bullet."], ["12"], ["--finish", "--no-changelog"], ["--finish", "7", "--changelog"], ["--finish", "7", "--no-changelog", "--changelog", "Bullet."]): with self.subTest(flags): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(["merge", *flags], checkout, checkout.config), Failure(USAGE)) self.assertEqual(checkout.ran, []) def test_a_branch_behind_the_default_branch_merges_it_in_and_pushes_before_the_checks(self): checkout = self.moved(Checkout(GITHUB, open=True)) self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout), [MERGE, self.PUSHED, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "decaf"]]) self.assertLess(checkout.ran.index(self.PUSHED), checkout.ran.index(GH_CHECKS)) def test_a_conflict_with_the_default_branch_leaves_it_unmerged_naming_the_files_and_frees_the_queue(self): checkout = self.moved(Checkout(GITHUB, open=True)) checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: CONFLICT") checkout.answers[("git", "diff", "--name-only", "--diff-filter=U")] = "bin/tool.py\n" failed = merged(checkout.__call__, checkout.config, lambda seconds: None) self.assertIsInstance(failed, Failure) self.assertIn("conflicts in bin/tool.py; the merge stays in progress: resolve them, commit, push, and run chunk-pr merge again", failed.reason) self.assertEqual((changes(checkout), MERGE in checkout.ran), ([], True)) self.assertTrue(queue_is_free()) def test_a_finished_item_is_committed_under_the_configs_title_and_pushed_before_the_merge(self): checkout, folder = self.with_plan(Checkout(GITHUB, open=True)) files = [f"{folder}/todo.md", f"{folder}/CHANGELOG.md"] self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7", "--changelog", "Exports keep their order."]]), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout)[:1], [[self.TODO_ITEM, "finish", "7", "--changelog", "Exports keep their order."]]) self.assertEqual(changes(checkout)[-4:], [ ["git", "-C", folder, "add", "--", *files], ["git", "-C", folder, "commit", "--quiet", "-m", "[7] Finish item 7 in the plan", "--", *files], self.PUSHED, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "f00d"], ]) def test_the_flags_reach_todo_item_as_given(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers.update({(self.TODO_ITEM, "show", "8"): "From todo.md:\n", (self.TODO_ITEM, "show", "9"): "From todo.md:\n"}) self.assertEqual(step(["merge", "--finish", "7", "--no-changelog", "--finish", "8", "--finish", "9", "--changelog", "Exports keep their order."], checkout.__call__, checkout.config), "Merged pull request #12 (merge).") self.assertEqual([command[1:] for command in checkout.ran if command[:2] == [self.TODO_ITEM, "finish"]], [["finish", "7", "--no-changelog"], ["finish", "8"], ["finish", "9", "--changelog", "Exports keep their order."]]) def test_an_item_the_plan_no_longer_holds_is_named_on_the_last_line_and_what_an_earlier_run_left_uncommitted_is_committed(self): checkout, folder = self.with_plan(Checkout(GITHUB, open=True), held=False) self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), "Merged pull request #12 (merge). The plan held no item 7 when merge began, so merge left the plan as it was there.") self.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran) self.assertEqual([command[3] for command in changes(checkout)[:-1]], ["add", "commit", "origin"]) self.assertEqual(changes(checkout)[-1], ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "f00d"]) def test_each_item_the_plan_no_longer_holds_is_named(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True), held=False) checkout.answers[(self.TODO_ITEM, "show", "8")] = Failure("todo-item show 8: todo.md holds no item 8") self.assertIn("The plan held no item 7, 8 when merge began", str(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"], ["8"]]))) def test_a_plan_that_turns_unreadable_inside_the_turn_leaves_it_unmerged(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) run, shows = checkout.__call__, [] def run_whose_second_show_fails(command: list[str]) -> str | Failure: shows.extend([command] if command[1:2] == ["show"] else []) return Failure("todo-item show 7: unreadable") if command[1:2] == ["show"] and len(shows) == 2 else run(command) self.assertEqual(merged(run_whose_second_show_fails, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("todo-item show 7: unreadable")) self.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran) def test_a_finish_that_changes_no_file_commits_and_pushes_nothing(self): for clean in ("status", "records"): with self.subTest(clean): checkout, folder = self.with_plan(Checkout(GITHUB, open=True)) if clean == "status": checkout.answers[("git", "-C", folder, "status", "--porcelain", "--", f"{folder}/todo.md", f"{folder}/CHANGELOG.md")] = "\n" else: checkout.answers.update({(self.CONFIG, "plan"): "PAY-1\n", (self.CONFIG, "changelog"): f"{folder}/none.md\n"}) self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7", "--no-changelog"]]), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout), [[self.TODO_ITEM, "finish", "7", "--no-changelog"], ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_a_branch_outside_the_branch_template_titles_the_finish_with_the_summary_alone(self): checkout, folder = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers[tuple(BRANCH)] = "feature/PAY-{id}\n" merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]) self.assertIn("Finish item 7 in the plan", [command[6] for command in checkout.ran if command[3:4] == ["commit"]]) def test_a_plan_it_cannot_read_or_an_item_it_cannot_finish_leaves_it_unmerged(self): for failing in ("show", "finish"): with self.subTest(failing): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers[(self.TODO_ITEM, failing, "7")] = Failure(f"todo-item {failing} 7: no config names the checkout") self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure(f"todo-item {failing} 7: no config names the checkout")) self.assertNotIn(self.PUSHED, checkout.ran) def test_a_step_of_the_turn_that_fails_leaves_it_unmerged_with_that_failure(self): _, folder = self.with_plan(Checkout(GITHUB, open=True)) for failing in (FETCH, BEHIND, COMMON, self.PUSHED, ["git", "-C", "", "rev-parse", "--show-toplevel"], ["git", "-C", "", "add"], ["git", "-C", "", "commit"]): with self.subTest(failing): checkout, folder = self.with_plan(Checkout(GITHUB, open=True)) run = checkout.__call__ def failing_run(command: list[str]) -> str | Failure: asked = ["" if part == folder else part for part in command] return Failure("that step failed") if tuple(asked[:len(failing)]) == tuple(failing) else run(command) self.assertEqual(merged(failing_run, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("that step failed")) self.assertNotIn("merge", [command[2] for command in checkout.ran if command[0] == "gh"]) def test_a_finish_that_fails_after_the_default_branch_was_merged_in_still_pushes_the_branch(self): checkout, _ = self.with_plan(self.moved(Checkout(GITHUB, open=True))) checkout.answers[(self.TODO_ITEM, "finish", "7")] = Failure("todo-item finish 7: a decision cites item 7") self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("todo-item finish 7: a decision cites item 7")) self.assertEqual(changes(checkout), [MERGE, self.PUSHED]) def test_the_scoring_run_is_asked_for_once_the_finish_is_committed_and_one_owed_leaves_it_unmerged(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) commit = lambda: next(index for index, command in enumerate(checkout.ran) if command[3:4] == ["commit"]) self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), "Merged pull request #12 (merge).") self.assertLess(commit(), checkout.ran.index(OWED)) checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers[tuple(OWED)] = Failure("the chunk owes 1 scoring run") self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("the chunk owes 1 scoring run")) self.assertEqual(changes(checkout)[-1], self.PUSHED) def test_a_merge_holds_the_queue_through_the_checks_and_frees_it_while_it_waits_for_an_approval(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(WAITS)] = "true\n" checkout.decisions = ["REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED"] free: dict[str, bool] = {} def run(command: list[str]) -> str | Failure: if command == GH_CHECKS: free.setdefault("during the checks", queue_is_free()) return checkout(command) self.assertEqual(merged(run, checkout.config, lambda seconds: free.setdefault("waiting for the approval", queue_is_free())), "Merged pull request #12 (merge).") self.assertEqual(free, {"during the checks": False, "waiting for the approval": True}) self.assertEqual(checkout.ran.count(FETCH), 2) def test_a_reviewers_comment_ends_the_wait_naming_its_file_until_the_author_answers_or_it_is_resolved(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(WAITS)] = "true\n" checkout.decisions = ["REVIEW_REQUIRED"] checkout.threads = [self.thread("bin/asked.py", "reviewer"), self.thread("bin/answered.py", "worker"), self.thread("bin/resolved.py", "reviewer", resolved=True)] slept: list[float] = [] failed = merged(checkout, checkout.config, slept.append) self.assertIsInstance(failed, Failure) self.assertIn("waits for an answer, in bin/asked.py;", failed.reason) self.assertEqual((slept, changes(checkout)), ([], [])) checkout.threads = checkout.threads[1:] checkout.decisions = ["REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED"] self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).") def test_an_approved_pull_request_with_a_comment_that_waits_for_an_answer_stays_unmerged(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(WAITS)] = "true\n" checkout.threads = [self.thread("bin/asked.py", "reviewer")] failed = merged(checkout, checkout.config, lambda seconds: None) self.assertIsInstance(failed, Failure) self.assertIn("waits for an answer, in bin/asked.py;", failed.reason) self.assertEqual(changes(checkout), []) def test_review_threads_it_cannot_read_leave_it_unmerged(self): for answer, reason in ((Failure("gh api graphql failed"), "gh api graphql failed"), ("not json", "gh api graphql printed no review threads: not json"), ("{}", "gh api graphql printed no review threads: {}")): with self.subTest(answer): checkout = Checkout(GITHUB, open=True) checkout.answers.update({tuple(WAITS): "true\n", tuple(GH_THREADS): answer}) checkout.decisions = ["REVIEW_REQUIRED"] self.assertEqual(merged(checkout, checkout.config, lambda seconds: None), Failure(reason)) self.assertEqual(changes(checkout), []) def test_a_default_branch_that_moved_after_the_branchs_last_paired_ruling_stops_for_a_new_one(self): history = "/checkout/docs/comprehension-history.md" ruled = ["git", "log", "-1", "--format=%H", "-G", "^Ruling: ", "origin/main..HEAD", "--", history] since = ["git", "rev-list", "--count", "bead..origin/main"] for name, answers, stops in ( ("moved since the ruling", {tuple(ruled): "bead\n", tuple(since): "2\n"}, True), ("ruled at this head", {tuple(ruled): "bead\n", tuple(since): "0\n"}, False), ("no ruling", {tuple(ruled): "\n"}, False), ): with self.subTest(name): checkout = Checkout(GITHUB, open=True) checkout.answers.update({(self.CONFIG, "comprehension-history"): f"{history}\n", **answers}) done = merged(checkout.__call__, checkout.config, lambda seconds: None) self.assertEqual(isinstance(done, Failure) and "run the paired ruling again" in done.reason, stops, done) self.assertEqual(changes(checkout), [] if stops else [["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_a_history_outside_the_checkout_or_one_git_cannot_read_never_stops_or_fails_the_merge_wrongly(self): ruled = ("git", "log", "-1", "--format=%H", "-G", "^Ruling: ", "origin/main..HEAD", "--", "/checkout/history.md") checkout = Checkout(GITHUB, open=True) checkout.answers[(self.CONFIG, "comprehension-history")] = "/elsewhere/history.md\n" self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None), "Merged pull request #12 (merge).") self.assertNotIn("log", [command[1] for command in checkout.ran]) for failing in (ruled, ("git", "rev-list", "--count", "bead..origin/main"), ("git", "rev-parse", "--show-toplevel")): with self.subTest(failing): checkout = Checkout(GITHUB, open=True) checkout.answers.update({(self.CONFIG, "comprehension-history"): "/checkout/history.md\n", ruled: "bead\n"}) checkout.answers[failing] = Failure("git failed") self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None), Failure("git failed")) def test_a_turn_keeps_every_other_merge_out_until_it_ends_even_by_an_error(self): with self.assertRaises(ValueError): with turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "12", lambda seconds: None): self.assertFalse(queue_is_free()) raise ValueError self.assertTrue(queue_is_free()) def test_a_queue_another_pull_request_holds_for_half_an_hour_leaves_it_unmerged_naming_the_holder(self): checkout = Checkout(GITHUB, open=True) slept: list[float] = [] with turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "1234", lambda seconds: None): pass with turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "31", lambda seconds: None): failed = merged(checkout, checkout.config, slept.append) self.assertEqual(failed, Failure("the merge queue stayed held for 30 minutes, last by pull request #31, so this one took no turn; run chunk-pr merge again")) self.assertEqual((sum(slept), set(slept), changes(checkout)), (30 * 60, {10}, [])) def test_a_queue_freed_while_it_waits_gives_it_the_turn(self): checkout = Checkout(GITHUB, open=True) holder = contextlib.ExitStack() holder.enter_context(turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "31", lambda seconds: None)) self.assertEqual(merged(checkout, checkout.config, lambda seconds: holder.close()), "Merged pull request #12 (merge).") def test_a_queue_it_cannot_open_leaves_it_unmerged(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(COMMON)] = "/nonexistent/folder\n" self.assertEqual(merged(checkout, checkout.config, lambda seconds: None), Failure("cannot take a turn in the merge queue, /nonexistent/folder/chunk-pr-merge-queue: No such file or directory")) self.assertEqual(checkout.acted(), []) class Clean(unittest.TestCase): """A real bare repository origin whose default branch is main; the clone project of it; and the worktree project-item-7 on branch item-7, one commit ahead of main and pushed. cleaned runs git for real, but the forge is faked: origin's URL reads as GitHub's, or Gitea's where self.url says so, and pull request #12 from item-7 is merged at self.merged_at where that is set.""" def setUp(self): folder = tempfile.TemporaryDirectory() self.addCleanup(folder.cleanup) root = pathlib.Path(folder.name) self.origin, self.project, self.worktree, self.forge_clone = root / "origin.git", root / "project", root / "project-item-7", root / "forge" self.git(root, "init", "--quiet", "--bare", "--initial-branch=main", str(self.origin)) self.git(root, "clone", "--quiet", str(self.origin), str(self.project)) self.commit(self.project, "first") self.git(self.project, "push", "--quiet", "origin", "main") self.git(self.project, "remote", "set-head", "origin", "--auto") self.git(self.project, "worktree", "add", "--quiet", "-b", "item-7", str(self.worktree), "origin/main") (self.worktree / "chunk.txt").write_text("the chunk\n") self.git(self.worktree, "add", "chunk.txt") self.commit(self.worktree, "the chunk") self.git(self.worktree, "push", "--quiet", "--set-upstream", "origin", "item-7") self.head = self.git(self.worktree, "rev-parse", "HEAD") self.before = self.git(self.project, "rev-parse", "HEAD") self.url = GITHUB self.merged_at: str | None = None # The forge merges in a clone of its own, so the project's origin/main stays stale until clean fetches. self.git(root, "clone", "--quiet", str(self.origin), str(self.forge_clone)) def git(self, folder: pathlib.Path, *args: str) -> str: return subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.org", *args], cwd=folder, check=True, capture_output=True, encoding="utf-8", env=isolated_env()).stdout.strip() def commit(self, folder: pathlib.Path, message: str): self.git(folder, "commit", "--quiet", "--allow-empty", "-m", message) def merge_commit_on_origin(self) -> str: self.git(self.forge_clone, "fetch", "--quiet", "origin", "item-7") self.git(self.forge_clone, "merge", "--quiet", "--no-ff", "-m", "Merge item-7", "FETCH_HEAD") return self.publish() def rebase_on_origin(self) -> str: """Put the chunk's change on origin's main as another commit, as a rebase or a squash merge does.""" (self.forge_clone / "chunk.txt").write_text("the chunk\n") self.git(self.forge_clone, "add", "chunk.txt") self.commit(self.forge_clone, "the chunk, rebased") return self.publish() def publish(self) -> str: self.git(self.forge_clone, "push", "--quiet", "origin", "main") self.merged_at = self.head return self.git(self.forge_clone, "rev-parse", "HEAD") def at(self, folder: str) -> Run: def run(command: list[str]) -> str | Failure: if command == ["git", "remote", "get-url", "origin"]: return f"{self.url}\n" if command == ["gh", "pr", "list", "--head", "item-7", "--state", "merged", *GH_LIST[5:]] and self.url == GITHUB: return f"12\t{self.merged_at}\tfalse\n" if self.merged_at else "" if command == ["tea", "api", "repos/{owner}/{repo}/pulls?state=closed&limit=50"] and self.url == GITEA: return json.dumps([{"number": 12, "merged": self.merged_at is not None, "head": {"ref": "refs/pull/12/head" if self.merged_at else "item-7", "label": "item-7", "sha": self.merged_at or self.head, "repo": {"full_name": "me/project"}}, "base": {"repo": {"full_name": "me/project"}}}]) if command[0] != "git": return Failure(f"the fake forge doesn't answer {command}") done = subprocess.run(command, cwd=folder, capture_output=True, encoding="utf-8", env=isolated_env()) return Failure(f"{' '.join(command[:3])}: {done.stderr.strip()}") if done.returncode else done.stdout return run def clean(self, folder: pathlib.Path | None = None) -> str | Failure: return cleaned(self.at(str(folder or self.worktree)), self.at) def branches(self) -> list[str]: return sorted(self.git(self.project, "branch", "--format=%(refname:short)").splitlines()) def assert_kept(self): """Worktree, branch and main checkout are as setUp left them.""" self.assertTrue(self.worktree.exists()) self.assertEqual(self.branches(), ["item-7", "main"]) self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), self.before) def test_a_chunk_merged_with_a_merge_commit_loses_its_worktree_and_branch_and_the_checkout_fast_forwards(self): for url in (GITHUB, GITEA): with self.subTest(url): if url == GITEA: self.setUp() self.url = url merged = self.merge_commit_on_origin() self.assertEqual(self.clean(), f"Removed worktree {self.worktree} and branch item-7, and fast-forwarded {self.project} to origin/main.") self.assertFalse(self.worktree.exists()) self.assertEqual(self.branches(), ["main"]) self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), merged) def test_a_rebased_chunk_is_cleaned_though_main_lacks_its_commit(self): # Without an upstream, git branch -d would compare item-7 with the main checkout's HEAD, which lacks its commit. self.git(self.project, "branch", "--unset-upstream", "item-7") merged = self.rebase_on_origin() self.assertEqual(self.clean(), f"Removed worktree {self.worktree} and branch item-7, and fast-forwarded {self.project} to origin/main.") self.assertEqual(self.branches(), ["main"]) self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), merged) def test_an_unmerged_chunk_keeps_everything(self): for url in (GITHUB, GITEA): with self.subTest(url): self.url = url self.assertEqual(self.clean(), Failure(f"no merged pull request from item-7 at {self.head[:12]}, so its worktree and branch stay")) self.assert_kept() def test_a_pull_request_merged_at_another_head_keeps_everything(self): self.merge_commit_on_origin() self.merged_at = "0" * 40 self.assertIsInstance(self.clean(), Failure) self.assert_kept() def test_a_main_checkout_that_cannot_fast_forward_is_left_as_it_was_and_named(self): cases = { "dirty": ("it has uncommitted or untracked files:\n?? notes.txt", lambda: (self.project / "notes.txt").write_text("mine\n")), "on another branch": ("it has elsewhere checked out, not main", lambda: self.git(self.project, "switch", "--quiet", "-c", "elsewhere")), "ahead of origin": ("it holds commits origin/main lacks", lambda: self.commit(self.project, "mine")), } for case, (why, spoil) in cases.items(): with self.subTest(case): self.setUp() self.git(self.project, "branch", "--unset-upstream", "item-7") self.merge_commit_on_origin() spoil() before = self.git(self.project, "rev-parse", "HEAD") self.assertEqual(self.clean(), Failure(f"Removed worktree {self.worktree} and branch item-7, but left {self.project} as it was: {why}")) self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), before) self.assertFalse(self.worktree.exists()) self.assertEqual(self.branches(), ["elsewhere", "main"] if case == "on another branch" else ["main"]) def test_a_worktree_with_uncommitted_or_untracked_files_keeps_everything(self): self.merge_commit_on_origin() (self.worktree / "chunk.txt").write_text("changed\n") (self.worktree / "draft.txt").write_text("unsaved\n") self.assertEqual(self.clean(), Failure(f"left everything as it was: {self.worktree} has uncommitted or untracked files:\n M chunk.txt\n?? draft.txt")) self.assert_kept() self.assertEqual((self.worktree / "draft.txt").read_text(), "unsaved\n") def test_a_dirty_worktree_also_reports_a_main_checkout_it_would_have_left(self): self.merge_commit_on_origin() (self.worktree / "draft.txt").write_text("unsaved\n") (self.project / "notes.txt").write_text("mine\n") self.assertEqual(self.clean(), Failure(f"left everything as it was: {self.worktree} has uncommitted or untracked files:\n?? draft.txt\nIt would also have left {self.project} as it was: it has uncommitted or untracked files:\n?? notes.txt")) self.assert_kept() def test_the_main_checkout_itself_is_refused(self): self.merge_commit_on_origin() self.assertEqual(self.clean(self.project), Failure(f"{self.project} is the main checkout; run clean in the chunk's worktree")) self.assert_kept() def test_without_a_default_branch_on_record_it_says_how_to_set_one(self): self.merge_commit_on_origin() self.git(self.project, "remote", "set-head", "origin", "--delete") cleaned_up = self.clean() self.assertIsInstance(cleaned_up, Failure) self.assertTrue(cleaned_up.reason.startswith("origin records no default branch; set it with `git remote set-head origin --auto`: "), cleaned_up) self.assert_kept() class Launcher(unittest.TestCase): def test_help_exits_0_and_a_wrong_step_exits_1(self): self.assertEqual(subprocess.run([str(TOOL), "--help"], capture_output=True, env=isolated_env()).returncode, 0) wrong = subprocess.run([str(TOOL), "close"], capture_output=True, encoding="utf-8", env=isolated_env()) self.assertEqual((wrong.returncode, wrong.stdout), (1, "")) self.assertIn("usage", wrong.stderr) def test_clean_runs_in_the_folder_it_is_started_in(self): with tempfile.TemporaryDirectory() as folder: for args in (["init", "--quiet"], ["remote", "add", "origin", GITHUB], ["-c", "user.name=Test", "-c", "user.email=test@example.org", "commit", "--quiet", "--allow-empty", "-m", "first"]): subprocess.run(["git", *args], cwd=folder, check=True, env=isolated_env()) done = subprocess.run([str(TOOL), "clean"], cwd=folder, capture_output=True, encoding="utf-8", env=isolated_env()) self.assertEqual((done.returncode, done.stdout), (1, "")) self.assertTrue(done.stderr.startswith("chunk-pr: origin records no default branch"), done.stderr) if __name__ == "__main__": unittest.main()