From b709e030238dfcece7eac2b74cac8aded9f33b40 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 03:53:14 +0100 Subject: [PATCH] Kill the surviving mutants of the e2e command's settings, sandbox and entry point --- bin/do_epic/e2e/sandbox.py | 13 +++----- bin/do_epic/e2e/settings.py | 5 +-- bin/do_epic/e2e/test_main.py | 54 ++++++++++++++++++++++---------- bin/do_epic/e2e/test_sandbox.py | 19 ++++++++++- bin/do_epic/e2e/test_scenario.py | 46 +++++++++++++++++++++++++++ bin/do_epic/e2e/test_settings.py | 8 +++-- 6 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 bin/do_epic/e2e/test_scenario.py diff --git a/bin/do_epic/e2e/sandbox.py b/bin/do_epic/e2e/sandbox.py index 392d0638..b290514c 100644 --- a/bin/do_epic/e2e/sandbox.py +++ b/bin/do_epic/e2e/sandbox.py @@ -62,13 +62,10 @@ def epic_made(sandbox: Sandbox, summary: str, http: Http = jira_json, to_adf: To def deleted_with_children(sandbox: Sandbox, key: str, authorization: str, http: Http) -> None | Failure: """Deletes the epic's child issues, with their sub-tasks, and then the epic: Jira deletes an epic's sub-tasks along with it, but not its child issues.""" children = http("POST", api(sandbox, "search/jql"), authorization, {"jql": f"parent = {key}", "fields": ["summary"], "maxResults": 100}) - if isinstance(children, Failure): - return children found = children.get("issues") if isinstance(children, dict) else None - if not isinstance(found, list): - return Failure(f"Jira answered the search for {key}'s issues with {children!r}, which holds no issues") - for each in [item.get("key") for item in found if isinstance(item, dict)] + [key]: + # The first failure is the one to report; the epic is deleted all the same. + problem = children if isinstance(children, Failure) else None if isinstance(found, list) else Failure(f"Jira answered the search for {key}'s issues with {children!r}, which holds no issues") + for each in [*(item.get("key") for item in found if isinstance(item, dict)), key] if isinstance(found, list) else [key]: deleted = http("DELETE", api(sandbox, f"issue/{each}?deleteSubtasks=true"), authorization, None) - if isinstance(deleted, Failure): - return deleted - return None + problem = problem or (deleted if isinstance(deleted, Failure) else None) + return problem diff --git a/bin/do_epic/e2e/settings.py b/bin/do_epic/e2e/settings.py index 7a85082d..a4f730ab 100644 --- a/bin/do_epic/e2e/settings.py +++ b/bin/do_epic/e2e/settings.py @@ -31,13 +31,14 @@ def read(env: typing.Mapping[str, str]) -> Sandbox | Failure: values = {name: env.get(name, "").strip() for name in MEANING} url, project = values[JIRA_URL], values[PROJECT] env_file = pathlib.Path(values[ENV_FILE]) if values[ENV_FILE] else None + absolute = env_file is not None and env_file.is_absolute() faults = [ *(f"{name} is missing: set it to {meaning}" for name, meaning in MEANING.items() if not values[name]), *([f"{JIRA_URL} is {url!r}, not an https address without a path"] if url and not re.fullmatch(SITE, url) else []), *([f"{PROJECT} is {project!r}, not a project key such as SBX"] if project and not re.fullmatch(PROJECT_KEY, project) else []), - *([f"{ENV_FILE} is {values[ENV_FILE]!r}, not an absolute path"] if values[ENV_FILE] and env_file is not None and not env_file.is_absolute() else []), + *([f"{ENV_FILE} is {values[ENV_FILE]!r}, not an absolute path"] if env_file is not None and not absolute else []), ] - if not faults and env_file is not None: + if env_file is not None and absolute: login = basic_auth(env_file) if isinstance(login, Failure): faults.append(login.reason) diff --git a/bin/do_epic/e2e/test_main.py b/bin/do_epic/e2e/test_main.py index d7b95781..b7899b3d 100644 --- a/bin/do_epic/e2e/test_main.py +++ b/bin/do_epic/e2e/test_main.py @@ -10,7 +10,8 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) from do_epic.e2e.settings import ENV_FILE, JIRA_URL, PROJECT -from do_epic.e2e import main, sandbox, scenario +from failure import Failure +from do_epic.e2e import main, sandbox, scenario, settings class MainTest(unittest.TestCase): @@ -30,9 +31,22 @@ class MainTest(unittest.TestCase): code, out, err = self.run_main(["--help"]) self.assertEqual((code, err), (0, "")) self.assertIn(JIRA_URL, out) + self.assertEqual(self.run_main(["-h"])[:2], (0, out)) code, out, err = self.run_main(["--status"]) self.assertEqual((code, out), (1, "")) - self.assertIn("usage: do-epic-e2e", err) + self.assertIn(main.HELP, err) + + +class MachineTest(unittest.TestCase): + def test_stops_a_do_epic_that_runs_over_its_time_and_says_so(self) -> None: + with tempfile.TemporaryDirectory() as folder: + slow = pathlib.Path(folder) / "slow" + slow.write_text("#!/bin/sh\nsleep 30\n", encoding="utf-8") + slow.chmod(0o755) + with mock.patch.object(scenario, "TIMEOUT", 1): + done = scenario.Machine(pathlib.Path(folder), slow).do_epic("ABC-1") + self.assertEqual(done.returncode, 124) + self.assertIn("ran over 1 seconds", done.stderr) class ReportedTest(unittest.TestCase): @@ -44,18 +58,20 @@ class ReportedTest(unittest.TestCase): env_file.chmod(0o600) self.env = {JIRA_URL: "https://acme.atlassian.net", ENV_FILE: str(env_file), PROJECT: "SBX"} - def run_scenario(self, deleted: bool) -> tuple[int, str, str]: + def run_scenario(self, deleted: bool, report: scenario.Report | None = None, made: str | Failure = "SBX-1") -> tuple[int, str, str]: @contextlib.contextmanager - def epic_made(*args: object) -> typing.Iterator[str]: - yield "SBX-1" + def epic_made(*args: object) -> typing.Iterator[str | Failure]: + self.made_with = args + yield made if not deleted: raise sandbox.Leftover("SBX-1 is still there") out, err = io.StringIO(), io.StringIO() - report = scenario.Report(["a check held"], ["a check broke"]) - with mock.patch.object(sandbox, "epic_made", epic_made), mock.patch.object(scenario, "run", return_value=report): + report = report or scenario.Report(["a check held"], ["a check broke"]) + with mock.patch.object(sandbox, "epic_made", epic_made), mock.patch.object(scenario, "run", return_value=report) as ran: with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): code = main.main([], self.env, pathlib.Path("do-epic")) + self.ran_with = ran.call_args.args if ran.called else None return code, out.getvalue(), err.getvalue() def test_prints_each_check_and_fails_where_one_broke(self) -> None: @@ -69,14 +85,18 @@ class ReportedTest(unittest.TestCase): self.assertIn("ok a check held", out) self.assertIn("SBX-1 is still there", err) + def test_makes_the_epic_in_the_sandbox_and_runs_the_scenario_on_it(self) -> None: + code, out, err = self.run_scenario(True, scenario.Report(["a check held"])) + sandbox_set = settings.read(self.env) + self.assertEqual((code, err), (0, "")) + self.assertEqual(self.made_with, (sandbox_set, "do-epic e2e sandbox epic")) + self.assertEqual(self.ran_with, (sandbox_set, "SBX-1", pathlib.Path("do-epic"))) + self.assertEqual(out, "Sandbox epic SBX-1 in https://acme.atlassian.net\nok a check held\n1 held, 0 broke\n") -class MachineTest(unittest.TestCase): - def test_stops_a_do_epic_that_runs_over_its_time_and_says_so(self) -> None: - with tempfile.TemporaryDirectory() as folder: - slow = pathlib.Path(folder) / "slow" - slow.write_text("#!/bin/sh\nsleep 30\n", encoding="utf-8") - slow.chmod(0o755) - with mock.patch.object(scenario, "TIMEOUT", 1): - done = scenario.Machine(pathlib.Path(folder), slow).do_epic("ABC-1") - self.assertEqual(done.returncode, 124) - self.assertIn("ran over 1 seconds", done.stderr) + def test_fails_without_a_scenario_where_the_epic_cannot_be_made(self) -> None: + code, out, err = self.run_scenario(True, made=Failure("Jira said no")) + self.assertEqual((code, out, self.ran_with), (1, "", None)) + self.assertEqual(err, "do-epic-e2e: Jira said no\n") + + def test_fails_where_every_check_held_but_the_epic_stays(self) -> None: + self.assertEqual(self.run_scenario(False, scenario.Report(["a check held"]))[0], 1) diff --git a/bin/do_epic/e2e/test_sandbox.py b/bin/do_epic/e2e/test_sandbox.py index cd032d68..0616b5ec 100644 --- a/bin/do_epic/e2e/test_sandbox.py +++ b/bin/do_epic/e2e/test_sandbox.py @@ -1,3 +1,4 @@ +import base64 import pathlib import sys import tempfile @@ -15,16 +16,21 @@ class JiraStub: def __init__(self, refuses: str | None = None) -> None: self.requests: list[tuple[str, str]] = [] + self.authorizations: set[str] = set() + self.bodies: list[object] = [] self.refuses = refuses + self.search_answer: object = {"issues": [{"key": "SBX-2"}, {"key": "SBX-3"}]} def __call__(self, method: str, url: str, authorization: str, body: object) -> object: self.requests.append((method, url.split("/rest/api/3/")[1])) + self.authorizations.add(authorization) + self.bodies.append(body) if self.refuses and url.endswith(self.refuses): return Failure(f"{method} {url} answered 403") if method == "POST" and url.endswith("/issue"): return {"key": "SBX-1"} if method == "POST": - return {"issues": [{"key": "SBX-2"}, {"key": "SBX-3"}]} + return self.search_answer return None @@ -41,6 +47,8 @@ class EpicMadeTest(unittest.TestCase): jira = JiraStub() with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}) as key: self.assertEqual(key, "SBX-1") + self.assertEqual(jira.authorizations, {"Basic " + base64.b64encode(b"a@example.com:secret").decode()}) + self.assertEqual(jira.bodies[1], {"jql": "parent = SBX-1", "fields": ["summary"], "maxResults": 100}) self.assertEqual(jira.requests[1:], [ ("POST", "search/jql"), ("DELETE", "issue/SBX-2?deleteSubtasks=true"), @@ -61,6 +69,15 @@ class EpicMadeTest(unittest.TestCase): pass self.assertIn("SBX-1", str(left.exception)) + def test_deletes_the_epic_all_the_same_and_says_so_when_the_search_for_its_issues_answers_nothing_usable(self) -> None: + jira = JiraStub() + jira.search_answer = {"errorMessages": ["no"]} + with self.assertRaises(Leftover) as left: + with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}): + pass + self.assertIn("holds no issues", str(left.exception)) + self.assertEqual(jira.requests[1:], [("POST", "search/jql"), ("DELETE", "issue/SBX-1?deleteSubtasks=true")]) + def test_yields_a_failure_and_deletes_nothing_when_the_epic_cannot_be_made(self) -> None: jira = JiraStub(refuses="/issue") with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}) as key: diff --git a/bin/do_epic/e2e/test_scenario.py b/bin/do_epic/e2e/test_scenario.py new file mode 100644 index 00000000..3a48f9a9 --- /dev/null +++ b/bin/do_epic/e2e/test_scenario.py @@ -0,0 +1,46 @@ +import pathlib +import sys +import tempfile +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from do_epic.config import load +from do_epic.e2e import scenario +from do_epic.e2e.settings import Sandbox +from failure import Failure + + +class ReportTest(unittest.TestCase): + def test_sorts_each_check_into_what_held_and_what_broke(self) -> None: + report = scenario.Report() + self.assertTrue(report.check(True, "one")) + self.assertFalse(report.check(False, "two")) + self.assertEqual((report.held, report.broke), (["one"], ["two"])) + + +class MachineTest(unittest.TestCase): + def setUp(self) -> None: + made = tempfile.TemporaryDirectory() + self.addCleanup(made.cleanup) + self.folder = pathlib.Path(made.name).resolve() + self.env_file = self.folder / "jira.env" + self.env_file.write_text("ATLASSIAN_EMAIL=a@example.com\nATLASSIAN_API_TOKEN=secret\n", encoding="utf-8") + self.env_file.chmod(0o600) + self.sandbox = Sandbox("https://acme.atlassian.net", self.env_file, "SBX") + + def test_writes_settings_that_do_epic_loads_and_that_point_at_the_sandbox(self) -> None: + home = self.folder / "home" + machine = scenario.machine_made(home, self.sandbox, pathlib.Path("do-epic")) + assert not isinstance(machine, Failure), machine + config = load(home) + assert not isinstance(config, Failure), config + self.assertEqual((config.jira_url, config.env_file, config.repo_rule), ("https://acme.atlassian.net", self.env_file, scenario.RULE)) + self.assertEqual((config.finish.transition, config.finish.status) if config.finish else None, ("Done", "Done")) + + def test_runs_do_epic_with_the_machines_home_and_the_words_and_stdin_given(self) -> None: + launcher = self.folder / "echo-home" + launcher.write_text('#!/bin/sh\necho "$HOME $*"; cat\n', encoding="utf-8") + launcher.chmod(0o755) + done = scenario.Machine(self.folder, launcher).do_epic("ABC-1", "--file", stdin="detail") + self.assertEqual((done.returncode, done.stdout), (0, f"{self.folder} ABC-1 --file\ndetail")) diff --git a/bin/do_epic/e2e/test_settings.py b/bin/do_epic/e2e/test_settings.py index e7464022..0318fe3f 100644 --- a/bin/do_epic/e2e/test_settings.py +++ b/bin/do_epic/e2e/test_settings.py @@ -5,7 +5,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) -from do_epic.e2e.settings import ENV_FILE, JIRA_URL, PROJECT, read, Sandbox +from do_epic.e2e.settings import ENV_FILE, JIRA_URL, MEANING, PROJECT, read, Sandbox from failure import Failure @@ -33,7 +33,11 @@ class SettingsTest(unittest.TestCase): self.assertNotIn(PROJECT, self.failure({k: v for k, v in self.env.items() if k != JIRA_URL})) def test_treats_a_blank_setting_as_missing(self) -> None: - self.assertIn(ENV_FILE, self.failure({**self.env, ENV_FILE: " "})) + self.assertEqual(self.failure({**self.env, ENV_FILE: " "}), f"{ENV_FILE} is missing: set it to {MEANING[ENV_FILE]}") + + def test_names_every_fault_at_once_separated_by_semicolons(self) -> None: + self.env_file.chmod(0o644) + self.assertEqual(self.failure({**self.env, PROJECT: "sbx-1"}), f"{PROJECT} is 'sbx-1', not a project key such as SBX; {self.env_file} holds an API token, so only its owner may read it; run chmod 600 {self.env_file}") def test_refuses_a_site_that_is_not_an_https_address_without_a_path(self) -> None: self.assertIn(JIRA_URL, self.failure({**self.env, JIRA_URL: "http://acme.atlassian.net/jira"}))