mutate.py¶
scripts/mutate.py
Single-mechanism mutation control.
main
int mutate.main()
CLI.
Source · 22 lines
def main() -> int:
"""@brief Entry point.
@return Exit code.
@version 1
"""
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("path")
ap.add_argument("old")
ap.add_argument("new")
ap.add_argument("selector")
args = ap.parse_args()
require_clean()
## The BASELINE half: the test must pass before the mutation, or a failure afterwards proves
## only that the suite was already broken.
if not run_test(args.selector):
sys.exit("REFUSED: the test fails BEFORE the mutation; there is nothing to control for.")
caught = mutate(args.path, args.old, args.new, args.selector)
print(f"{'CAUGHT' if caught else 'MISS '} {args.path} <- {args.selector}")
return 0 if caught else 2
Activity · 12 steps
Flows
mutate
bool mutate.mutate(str path, str old, str new, str selector)
Apply one substitution, run one test, restore the file.
Source · 19 lines
def mutate(path: str, old: str, new: str, selector: str) -> bool:
"""@brief One mutation, one verdict.
@return Caught state.
@version 1
"""
target = ROOT / path
source = target.read_text()
hits = source.count(old)
if hits != 1:
sys.exit(f"REFUSED: pattern appears {hits} times in {path}; a mutation must be singular.")
target.write_text(source.replace(old, new))
try:
caught = not run_test(selector)
finally:
subprocess.run(["git", "checkout", "HEAD", "--", path], cwd=ROOT, check=True)
purge_cache()
return caught
Activity · 9 steps
Flows
purge_cache
None mutate.purge_cache()
Delete every pycache under the repository.
Source · 9 lines
def purge_cache() -> None:
"""@brief Purge bytecode caches.
@return None.
@version 1
"""
for cache in ROOT.rglob("__pycache__"):
shutil.rmtree(cache, ignore_errors=True)
Activity · 2 steps
Flows
require_clean
None mutate.require_clean()
Refuse to run on a dirty tree.
Source · 15 lines
def require_clean() -> None:
"""A mutation is restored FROM A COMMIT. If uncommitted work is present, the restore would
discard it — which is exactly how ten files of finished work were lost here once.
@brief Dirty-tree refusal.
@return None.
@version 1
"""
dirty = subprocess.run(
["git", "status", "--porcelain"], cwd=ROOT, capture_output=True, text=True, check=True
).stdout.strip()
if dirty:
sys.exit(f"REFUSED: working tree is dirty; commit first.\n{dirty}")
Activity · 3 steps
Flows
run_test
bool mutate.run_test(str selector)
Run one test selector and report whether it passed.
Source · 19 lines
def run_test(selector: str) -> bool:
"""@brief Run pytest.
@return Pass state.
@version 1
"""
purge_cache()
done = subprocess.run(
[str(ROOT / ".venv/bin/python"), "-m", "pytest", selector, "-q", "-p", "no:cacheprovider"],
cwd=ROOT,
capture_output=True,
text=True,
check=False,
)
purge_cache()
if done.returncode not in (0, 1):
print(done.stdout[-2000:], file=sys.stderr)
return done.returncode == 0