Files
claude-config/bin/do_epic/test_dry_run.py
T
Lilleman 12ace75f15
CI / tests (pull_request) Successful in 2m29s
Sort the dry run's imports by their first binding
2026-11-20 10:43:14 +01:00

132 lines
6.7 KiB
Python

import pathlib
import subprocess
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from do_epic import dry_run
from failure import Failure
from do_epic.test_scheduler import issue
from do_epic.backend.fake import MemoryEpic
LISTED = "-t", "=epic-ABC-12", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"
START = ["new-window", "-d", "-t", "=epic-ABC-12:", "-n", "ABC-21", "-c", "/code/api", "-P", "-F", "#{window_id}", "--", "do-epic"]
def tmux(calls: list[list[str]], shown: str = "ABC-20\t0\t@1\n"):
def run(args: list[str]) -> subprocess.CompletedProcess[str]:
calls.append(args)
return subprocess.CompletedProcess(args, 0, shown, "")
return run
class TheRecordingTmux(unittest.TestCase):
def test_starts_no_window_and_lists_the_one_it_would_start_beside_the_real_ones(self) -> None:
calls: list[list[str]] = []
run = dry_run.recording(tmux(calls))
self.assertEqual(run(START).stdout, "@dry1\n")
self.assertEqual(run(["list-windows", *LISTED]).stdout, "ABC-20\t0\t@1\nABC-21\t0\t@dry1\n")
everywhere = run(["list-windows", "-a", "-F", "x"]).stdout
self.assertEqual(everywhere, "ABC-20\t0\t@1\nepic-ABC-12\tABC-21\t0\t@dry1\t/code/api\n")
self.assertEqual([call[0] for call in calls], ["list-windows", "list-windows"])
def test_lists_the_window_it_would_start_where_tmux_finds_no_session(self) -> None:
def none(args: list[str]) -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess(args, 1, "", "no server running")
run = dry_run.recording(none)
run(["new-session", "-d", "-s", "epic-ABC-12", "-n", "ABC-21", "-c", "/code/api"])
self.assertEqual(run(["list-windows", *LISTED]).stdout, "ABC-21\t0\t@dry1\n")
self.assertEqual(run(["list-windows", "-t", "=epic-ABC-99", "-F", "x"]).stdout, "")
def test_answers_each_command_with_its_own_words_and_no_error(self) -> None:
def refused(args: list[str]) -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess(args, 1, "junk", "no server")
def fields(done: subprocess.CompletedProcess[str]) -> tuple[object, int, str, str]:
return done.args, done.returncode, done.stdout, done.stderr
run = dry_run.recording(refused)
self.assertEqual(fields(run(START)), (START, 0, "@dry1\n", ""))
self.assertEqual(fields(run(["kill-window", "-t", "@1"])), (["kill-window", "-t", "@1"], 0, "", ""))
self.assertEqual(fields(run(["list-windows", *LISTED])), (["list-windows", *LISTED], 0, "ABC-21\t0\t@dry1\n", ""))
self.assertEqual(fields(dry_run.recording(refused)(["list-windows", *LISTED])), (["list-windows", *LISTED], 0, "", ""))
def test_lists_every_window_it_would_start(self) -> None:
run = dry_run.recording(tmux([], ""))
run(START)
run(["new-window", "-d", "-t", "=epic-ABC-12:", "-n", "ABC-22", "-c", "/code/web", "-P"])
run(["new-window", "-d", "-t", "=epic-ABC-99:", "-n", "ABC-23", "-c", "/code/web", "-P"])
self.assertEqual(run(["list-windows", *LISTED]).stdout, "ABC-21\t0\t@dry1\nABC-22\t0\t@dry2\n")
self.assertEqual(run(["list-windows", "-a", "-F", "x"]).stdout, "epic-ABC-12\tABC-21\t0\t@dry1\t/code/api\nepic-ABC-12\tABC-22\t0\t@dry2\t/code/web\nepic-ABC-99\tABC-23\t0\t@dry3\t/code/web\n")
def test_answers_a_change_as_done_and_reads_the_real_tmux_for_a_read(self) -> None:
calls: list[list[str]] = []
run = dry_run.recording(tmux(calls))
self.assertEqual(run(["kill-window", "-t", "@1"]).returncode, 0)
self.assertEqual(run(["show-options", "-qv", "-t", "=s:", "@loop-root"]).stdout, "ABC-20\t0\t@1\n")
self.assertEqual(calls, [["show-options", "-qv", "-t", "=s:", "@loop-root"]])
class TheGitThatClonesNothing(unittest.TestCase):
def test_fails_a_clone_and_runs_any_other_command(self) -> None:
ran: list[tuple[str, ...]] = []
def real(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure:
ran.append(args)
return "ok"
git = dry_run.without_clones(real)
self.assertEqual(git("-c", "a=b", "clone", "--", "url", "dir"), Failure("a dry run doesn't clone a repo, but a real run would"))
self.assertEqual(git("remote", cwd="/c"), "ok")
self.assertEqual(ran, [("remote",)])
def test_passes_every_option_on(self) -> None:
seen: list[tuple[tuple[str, ...], dict[str, object]]] = []
def real(*args: str, **options: object) -> str | Failure:
seen.append((args, options))
return "ok"
git = dry_run.without_clones(real) # type: ignore[arg-type]
git("log", cwd="/c", timeout=5.0, guarded=True, errors="replace")
git("log")
self.assertEqual(seen, [(("log",), {"cwd": "/c", "timeout": 5.0, "guarded": True, "errors": "replace"}), (("log",), {"cwd": None, "timeout": None, "guarded": False, "errors": "strict"})])
class TheEpicItCopies(unittest.TestCase):
def test_copies_the_issues_and_the_closed_state_and_files_under_keys_jira_never_gives(self) -> None:
real = MemoryEpic("ABC-12", [issue("ABC-20"), issue("ABC-21")], done=True)
copy = dry_run.seeded(real)
assert not isinstance(copy, Failure)
self.assertEqual((copy.closed(), [each.key for each in copy.read()]), (True, ["ABC-20", "ABC-21"])) # type: ignore[union-attr]
self.assertEqual([copy.file("ABC-20", "A", "d", ()), copy.file("ABC-20", "B", "d", ())], ["ABC-new1", "ABC-new2"])
self.assertEqual((real.writes, [each.key for each in real.issues]), ([], ["ABC-20", "ABC-21"]))
self.assertEqual(copy.writes, ["file ABC-new1 under ABC-20: A", "file ABC-new2 under ABC-20: B"])
def test_notes_each_kind_of_write_in_order(self) -> None:
epic = MemoryEpic("ABC-12", [issue("ABC-20"), issue("ABC-21")])
epic.file("ABC-20", "Sub", "d", ("repo:api", "do-epic-claude"), "ABC-21")
epic.add_label("ABC-21", "do-epic-claude")
epic.rank("ABC-21", "ABC-20")
epic.comment("ABC-20", " Why. ")
epic.finish_issue("ABC-21")
epic.close("All done")
self.assertEqual(epic.writes, ["file ABC-102 under ABC-20: Sub, labelled do-epic-claude, repo:api, blocked by ABC-21", "label ABC-21 with do-epic-claude", "rank ABC-21 before ABC-20", "comment on ABC-20: Why.", "finish ABC-21", "close the epic: All done"])
def test_notes_no_write_that_fails(self) -> None:
epic = MemoryEpic("ABC-12", [issue("ABC-20")])
epic.add_label("ABC-99", "x")
epic.comment("ABC-20", " ")
epic.finish_issue("ABC-99")
self.assertEqual(epic.writes, [])
def test_fails_where_the_real_epic_cannot_be_read(self) -> None:
self.assertEqual(dry_run.seeded(MemoryEpic("ABC-12", [], fails="Jira answered 503")), Failure("Jira answered 503"))
def test_fails_where_only_the_closed_state_cannot_be_read(self) -> None:
class Unclosed(MemoryEpic):
def closed(self) -> bool | Failure:
return Failure("no state")
self.assertEqual(dry_run.seeded(Unclosed("ABC-12", [issue("ABC-20")])), Failure("no state"))
if __name__ == "__main__":
unittest.main()