diff --git a/bin/mutate_changes/test_mutate_changes.py b/bin/mutate_changes/test_mutate_changes.py index 09dd8dcf..f2805ba2 100644 --- a/bin/mutate_changes/test_mutate_changes.py +++ b/bin/mutate_changes/test_mutate_changes.py @@ -9,6 +9,7 @@ import textwrap import threading import time import unittest +import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -211,7 +212,28 @@ class ToolProcess(unittest.TestCase): def test_a_scratch_folder_it_cannot_make_is_a_failure(self): self.assertRegex(scratch_folder("/nonexistent/top").reason, "^could not make a scratch folder in /nonexistent/top: ") - def test_an_interrupted_run_ends_each_process_the_tool_started(self): + def test_an_interrupted_run_ends_at_once_with_each_process_the_tool_started(self): + sleeper, took = self.interrupted() + self.assertLess(took, 30) + for _ in range(200): + if ended(sleeper): + break + time.sleep(0.05) + else: + self.fail(f"sleep {sleeper}, which the tool started, still runs") + + def test_an_interrupt_ends_the_run_where_the_tool_s_processes_end_first(self): + killpg = os.killpg + + def then_gone(group: int, number: int) -> None: + killpg(group, number) + raise ProcessLookupError + + with unittest.mock.patch("os.killpg", then_gone): + self.interrupted() + + def interrupted(self) -> tuple[int, float]: + """Interrupt run_tool once its tool has started a sleep of 60 s: the sleep's pid, and how long run_tool took.""" with tempfile.TemporaryDirectory() as temp: started = pathlib.Path(temp, "sleep.pid") @@ -221,15 +243,10 @@ class ToolProcess(unittest.TestCase): os.kill(os.getpid(), signal.SIGINT) threading.Thread(target=interrupt, daemon=True).start() + began = time.monotonic() with self.assertRaises(KeyboardInterrupt): run_tool(["true"], temp, f"sleep 60 & echo $! > {started}; wait;") - sleeper = int(started.read_text()) - for _ in range(200): - if ended(sleeper): - break - time.sleep(0.05) - else: - self.fail(f"sleep {sleeper}, which the tool started, still runs") + return int(started.read_text()), time.monotonic() - began def ended(pid: int) -> bool: diff --git a/bin/mutate_changes/test_mutate_changes_runs.py b/bin/mutate_changes/test_mutate_changes_runs.py index 47d9d4d8..c00ce2b6 100644 --- a/bin/mutate_changes/test_mutate_changes_runs.py +++ b/bin/mutate_changes/test_mutate_changes_runs.py @@ -15,6 +15,7 @@ import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +from mutate_changes.test_mutate_changes import ended from test_env import isolated_env from mutate_changes import main from mutate_changes.in_diff_tools import run_cargo_mutants @@ -276,9 +277,8 @@ class MutateChangesRuns(unittest.TestCase): launched.send_signal(signal.SIGTERM) self.assertEqual(launched.wait(timeout=30), 128 + signal.SIGTERM) self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "pkg"]) - sleeper = pathlib.Path(f"/proc/{started.read_text().strip()}/stat") for _ in range(200): - if not sleeper.exists() or sleeper.read_text().split(") ")[1][0] == "Z": + if ended(int(started.read_text())): break time.sleep(0.05) else: @@ -308,6 +308,14 @@ class MutateChangesRuns(unittest.TestCase): ran = self.run_tool(env={"PATH": str(tools)}) self.assertEqual((ran.returncode, ran.stdout.splitlines()[:2]), (0, PASSED), ran.stderr) + def test_a_mutmut_results_that_cannot_start_fails_the_run(self): + tools = self.path_with_git() + (tools / "mutmut").write_text(f'#!/bin/sh\n[ "$1" = run ] && {shutil.which("rm")} "$0"\nexec {sys.executable} {self.temp / "fake_mutmut.py"} {self.log} passes mutmut "$@"\n') + (tools / "mutmut").chmod(0o755) + ran = self.run_tool(env={"PATH": str(tools)}) + self.assertEqual(ran.returncode, 1) + self.assertRegex(ran.stderr, "^mutmut results --all true could not start: ") + def test_rust_php_and_typescript_go_to_cargo_mutants_and_infection_at_the_top_and_to_stryker_by_package(self): self.change_rust_php_and_typescript() ran = self.run_tool("--base", "HEAD", "--jobs=3", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", cwd=self.checkout / "src") @@ -371,6 +379,10 @@ class MutateChangesRuns(unittest.TestCase): self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "pkg", "src", "web"]) def test_a_branch_that_adds_no_line_a_tool_mutates_says_so_and_names_what_it_changes(self): + # A file the branch deletes is no longer there to mutate, so the report doesn't name it. + (self.checkout / "pkg" / "old.md").write_text("Gone.\n") + self.commit() + (self.checkout / "pkg" / "old.md").unlink() ran = self.run_tool(*self.faked("passes"), "--base", "HEAD") self.assertEqual((ran.returncode, ran.stdout.splitlines()[:-1]), (0, ["No tool ran: the branch adds no line to a file that a tool mutates."])) (self.checkout / "README.md").write_text("Doubles.\n")