Do not count an unmutated worker build as a kill.

A worker that fails before any mutation was recording every mutant as
killed. Run the tests once on the unmutated overlay and stop the way a
failed baseline does. The tests also kill the mutants in the functions
this change touches.

Proved by a stand-in xcodebuild that fails only inside a worker, and by
mutator on engine.py and workers.py: 140 killed, 0 survived, 0
uncovered. pytest: 139 passed, 1 skipped.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 04:22:01 +00:00
co-authored by Robert C. Martin
parent 0cfa995431
commit 4fea247660
7 changed files with 332 additions and 36 deletions
+3 -1
View File
@@ -721,8 +721,10 @@ def test_selection_coverage_and_timeouts_keep_their_boundaries(tmp_path, capsys,
mutation_warning=50,
baselines={},
)
# Project baseline, then the unmutated worker, then a mutant at 10x.
assert clock.timeouts[0] is None
assert clock.timeouts[1] == 300
assert clock.timeouts[1] is None
assert clock.timeouts[2] == 300
class Red:
verbose = False
+145 -6
View File
@@ -2,12 +2,14 @@ import os
import shutil
import subprocess
import threading
from pathlib import Path
from mutator.engine import mutate_file, restore_backups
from mutator.edn import loads
from mutator.metrics import snapshot_path
from mutator.engine import _backup, _drop_bytecode, mutate_file, restore_backups, scan_file
from mutator.runner import CommandResult
from mutator.workers import create_workers, delete_tree, new_run_dir, worker_count
from mutator.edn import dumps, loads
from mutator.functions import form_digests
from mutator.metrics import snapshot_path
from mutator.workers import create_workers, delete_tree, importers_of, new_run_dir, worker_count
class FakeRunner:
@@ -85,8 +87,10 @@ def test_mutants_are_killed_or_kept_and_the_snapshot_is_differential(tmp_path):
)
assert second.forms[0].killed == 1
assert second.forms[0].survived == 3
# Baseline plus the three survivors. The killed mutant stays in the snapshot.
assert runner.calls - first_calls == 4
# Baseline, the unmutated worker, and the three survivors.
# The killed mutant stays in the snapshot and is not run again.
assert first_calls == 6
assert runner.calls - first_calls == 5
def test_uncovered_sites_are_not_executed(tmp_path):
@@ -300,3 +304,138 @@ def test_restore_backups_puts_an_interrupted_mutant_back(tmp_path):
assert restored == [source]
assert source.read_text(encoding="utf-8") == "original\n"
assert not backup.exists()
def test_worker_count_is_one_when_the_processor_count_is_unknown(monkeypatch):
monkeypatch.setattr(os, "cpu_count", lambda: None)
assert worker_count(4, None) == 1
assert worker_count(4, 3) == 1
def test_workers_can_be_created_twice_and_under_a_missing_directory(tmp_path):
source = tmp_path / "src" / "demo.py"
source.parent.mkdir()
source.write_text("def place(x):\n return x > 0\n", encoding="utf-8")
original = source.read_bytes()
base = tmp_path / "fresh" / "run"
first = create_workers(base, tmp_path, "src/demo.py", original, 1)
second = create_workers(base, tmp_path, "src/demo.py", original, 1)
assert first[0] == second[0]
assert (second[0] / "src" / "demo.py").read_bytes() == original
assert source.read_bytes() == original
def test_a_module_is_not_listed_as_its_own_importer(tmp_path):
source = tmp_path / "src" / "find.mjs"
source.parent.mkdir()
source.write_text(
'import { value } from "./find.mjs";\nexport const value = 1\n',
encoding="utf-8",
)
assert importers_of(tmp_path, "src/find.mjs") == set()
def test_importers_keep_queries_hashes_bare_specifiers_and_nesting(tmp_path):
source = tmp_path / "src" / "find.mjs"
source.parent.mkdir()
source.write_text('export const value = "real"\n', encoding="utf-8")
plain = tmp_path / "src" / "plain.mjs"
plain.write_text('import { value } from "./find";\n', encoding="utf-8")
typed = tmp_path / "src" / "typed.mjs"
typed.write_text('import { value } from "./find.js";\n', encoding="utf-8")
query = tmp_path / "src" / "query.mjs"
query.write_text('import { value } from "./find.mjs?raw";\n', encoding="utf-8")
hashed = tmp_path / "src" / "hashed.mjs"
hashed.write_text('import { value } from "./find.mjs#part";\n', encoding="utf-8")
nested = tmp_path / "src" / "nested" / "use.mjs"
nested.parent.mkdir()
nested.write_text('import { value } from "../find.mjs";\n', encoding="utf-8")
escape = tmp_path / "src" / "escape.mjs"
escape.write_text('import x from "../../outside.js";\n', encoding="utf-8")
git_hook = tmp_path / ".git" / "hook.mjs"
git_hook.parent.mkdir()
git_hook.write_text('import { value } from "../src/find.mjs";\n', encoding="utf-8")
original = source.read_bytes()
base = new_run_dir(tmp_path)
try:
worker = create_workers(base, tmp_path, "src/find.mjs", original, 1)[0]
nested_dir = worker / "src" / "nested"
assert nested_dir.is_dir() and not nested_dir.is_symlink()
for relative in (
"src/plain.mjs",
"src/typed.mjs",
"src/query.mjs",
"src/hashed.mjs",
"src/nested/use.mjs",
):
copied = worker / relative
assert copied.is_file()
assert not copied.is_symlink()
assert not (worker / ".git").exists()
assert escape.read_text(encoding="utf-8").startswith("import")
finally:
delete_tree(base)
assert source.read_bytes() == original
assert nested.read_text(encoding="utf-8").startswith("import")
def test_the_first_backup_creates_a_nested_directory(tmp_path):
path = tmp_path / "src" / "demo.py"
path.parent.mkdir()
path.write_bytes(b"original")
saved = _backup(tmp_path, path, b"original")
assert saved == tmp_path / "target" / "mutator-backup" / "src" / "demo.py"
assert saved.read_bytes() == b"original"
assert path.read_bytes() == b"original"
def test_dropping_bytecode_ignores_a_pyc_that_vanishes(tmp_path, monkeypatch):
path = tmp_path / "src" / "demo.py"
path.parent.mkdir()
path.write_text("x", encoding="utf-8")
missing = path.parent / "__pycache__" / "demo.cpython-314.pyc"
missing.parent.mkdir()
def glob(self, pattern):
if self == missing.parent and pattern.startswith("demo."):
return [missing]
return []
monkeypatch.setattr(Path, "glob", glob)
_drop_bytecode(path)
assert not missing.exists()
def test_a_scan_without_coverage_does_not_call_a_site_uncovered(tmp_path):
path = tmp_path / "src" / "demo.py"
path.parent.mkdir()
path.write_text("def place(x):\n return x > 0\n", encoding="utf-8")
text = scan_file(path, tmp_path, covered_lines=None, ignore_coverage=False, lines=None)
assert "uncovered" not in text
assert "> -> >=" in text
def test_a_scan_marks_a_function_whose_recorded_text_changed(tmp_path):
path = tmp_path / "src" / "demo.py"
path.parent.mkdir()
source = "def place(x):\n return x > 0\n"
path.write_text(source, encoding="utf-8")
digests = form_digests(source, path, tmp_path)
(namespace, form_id), _digest = next(iter(digests.items()))
snapshot = snapshot_path(tmp_path, namespace)
snapshot.parent.mkdir(parents=True)
snapshot.write_text(
dumps(
{
"namespace": namespace,
"source": "src/demo.py",
"forms": [{"id": form_id, "hash": "stale", "file": "src/demo.py"}],
"outcomes": {},
}
)
+ "\n",
encoding="utf-8",
)
text = scan_file(path, tmp_path, covered_lines=None, ignore_coverage=True, lines=None)
assert "whose text changed" in text
assert any(line.startswith("* ") for line in text.splitlines())
+68
View File
@@ -294,6 +294,7 @@ def test_a_worker_copies_an_asset_catalog_and_keeps_safe_links(tmp_path):
catalog = root / "DungeonDoor" / "Assets.xcassets"
_write(catalog / "AccentColor.colorset" / "Contents.json", '{"color":"accent"}\n')
(catalog / "AppIcon.appiconset").symlink_to(shared)
_write(root / "DungeonDoor" / "Core" / "Other.swift", "func other() -> Int { 2 }\n")
_write(root / "DungeonDoor" / "Notes.xcassets", "not a catalog\n")
_write(root / "DungeonDoor" / "Preview" / "readme.txt", "preview\n")
_write(root / "Docs" / "readme.txt", "notes\n")
@@ -314,6 +315,11 @@ def test_a_worker_copies_an_asset_catalog_and_keeps_safe_links(tmp_path):
assert icon.read_text(encoding="utf-8") == "png-bytes"
assert accent.is_file() and not accent.is_symlink()
assert accent.read_text(encoding="utf-8") == '{"color":"accent"}\n'
core = worker / "DungeonDoor" / "Core"
other = core / "Other.swift"
assert core.is_dir() and not core.is_symlink()
assert other.is_symlink()
assert other.read_text(encoding="utf-8").startswith("func other")
assert (worker / "DungeonDoor" / "Notes.xcassets").is_symlink()
assert (worker / "DungeonDoor" / "Preview").is_symlink()
assert (worker / "Docs").is_symlink()
@@ -647,6 +653,68 @@ def test_a_compiling_mutant_survives_when_the_worker_catalog_is_real(tmp_path, m
assert catalog.is_dir() and not catalog.is_symlink()
def _worker_only_failure(bin_dir: Path, body: str) -> None:
bin_dir.mkdir(parents=True, exist_ok=True)
tool = bin_dir / "xcodebuild"
tool.write_text(body, encoding="utf-8")
tool.chmod(0o755)
def test_an_unmutated_worker_build_failure_is_not_a_kill(tmp_path, monkeypatch, capsys):
bin_dir = tmp_path / "bin"
_worker_only_failure(
bin_dir,
"#!/bin/sh\n"
'case "$PWD" in\n'
"*mutation-workers*)\n"
" echo WORKER-BOOM\n"
" exit 65\n"
" ;;\n"
"esac\n"
"exit 0\n",
)
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
root = tmp_path / "ios"
source = _ios_source(root, "App.xcodeproj")
code = run(["--no-coverage", "--root", str(root), "--max-workers", "1"])
captured = capsys.readouterr()
assert code == 2
assert "Worker build failed for App/ContentView.swift before any mutation:" in captured.err
assert "WORKER-BOOM" in captured.err
assert "KILLED" not in captured.out
assert "SURVIVED" not in captured.out
assert source.read_text(encoding="utf-8") == _IOS_BODY
assert not (root / ".metrics" / "mutate").exists()
def test_an_unmutated_worker_build_failure_without_output_is_not_a_kill(
tmp_path, monkeypatch, capsys
):
bin_dir = tmp_path / "bin"
_worker_only_failure(
bin_dir,
"#!/bin/sh\n"
'case "$PWD" in\n'
"*mutation-workers*) exit 65 ;;\n"
"esac\n"
"exit 0\n",
)
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
root = tmp_path / "ios"
source = _ios_source(root, "App.xcodeproj")
code = run(["--no-coverage", "--root", str(root), "--max-workers", "1"])
captured = capsys.readouterr()
lines = [line for line in captured.err.splitlines() if line.strip()]
assert code == 2
assert len(lines) == 1
assert lines[0].startswith(
"Worker build failed for App/ContentView.swift before any mutation: xcodebuild test "
)
assert "KILLED" not in captured.out
assert source.read_text(encoding="utf-8") == _IOS_BODY
assert not (root / ".metrics" / "mutate").exists()
def test_scan_marks_a_swift_line_uncovered_when_llvm_cov_did_not_hit_it(tmp_path, capsys):
import json
+38
View File
@@ -73,6 +73,44 @@ def test_delete_tree_unlinks_a_symlink_without_following_it(tmp_path):
assert kept.read_text(encoding="utf-8") == "keep"
def test_delete_tree_unlinks_a_symlink_that_vanishes(tmp_path, monkeypatch):
real = tmp_path / "project"
real.mkdir()
kept = real / "important.txt"
kept.write_text("keep", encoding="utf-8")
link = tmp_path / "worker-link"
link.symlink_to(real)
original_unlink = Path.unlink
def unlink(self, *args, **kwargs):
if self == link and link.is_symlink():
original_unlink(self)
return original_unlink(self, *args, **kwargs)
monkeypatch.setattr(Path, "unlink", unlink)
delete_tree(link, attempts=1, sleep=lambda _: None)
assert not link.exists()
assert kept.read_text(encoding="utf-8") == "keep"
def test_symlink_replaces_a_file_and_creates_a_missing_parent(tmp_path):
from mutator.workers import symlink
target = tmp_path / "real.txt"
target.write_text("keep", encoding="utf-8")
link = tmp_path / "nested" / "link.txt"
link.parent.mkdir()
link.write_text("old", encoding="utf-8")
symlink(link, target)
other = tmp_path / "deep" / "child" / "link.txt"
symlink(other, target)
assert link.is_symlink()
assert link.read_text(encoding="utf-8") == "keep"
assert other.is_symlink()
assert other.read_text(encoding="utf-8") == "keep"
assert target.read_text(encoding="utf-8") == "keep"
def test_delete_tree_reraises_after_the_attempts_are_exhausted(tmp_path, monkeypatch):
tree = tmp_path / "worker"
tree.mkdir()