79 lines
3.6 KiB
Python
79 lines
3.6 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 do_epic.backend.fake import MemoryEpic
|
|
from do_epic.test_scheduler import issue
|
|
from failure import Failure
|
|
|
|
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_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 clones nothing"))
|
|
self.assertEqual(git("remote", cwd="/c"), "ok")
|
|
self.assertEqual(ran, [("remote",)])
|
|
|
|
|
|
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_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"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|