mutate.py

scripts/mutate.py

Single-mechanism mutation control.

main

int mutate.main()

CLI.

scripts/mutate.py:113

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
lines 118–118ap = argparse.ArgumentParser(description=__doc__)lines 119–119ap.add_argument("path")lines 120–120ap.add_argument("old")lines 121–121ap.add_argument("new")lines 122–122ap.add_argument("selector")lines 123–123args = ap.parse_args()lines 125–125require_clean()lines 128–128◇ if not run_test(args.selector)lines 129–129sys.exit("REFUSED: the test fails BEFORE the mutation; there is nothing to control for.")lines 130–130caught = mutate(args.path, args.old, args.new, args.selector)lines 131–131print(f"{'CAUGHT' if caught else 'MISS '} {args.path} <- {args.selector}")lines 132–132return 0 if caught else 2

Flows

mutate

bool mutate.mutate(str path, str old, str new, str selector)

Apply one substitution, run one test, restore the file.

scripts/mutate.py:91

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
lines 96–96target = ROOT / pathlines 97–97source = target.read_text()lines 98–98hits = source.count(old)lines 99–99◇ if hits != 1lines 100–100sys.exit(f"REFUSED: pattern appears {hits} times in {path}; a mutation must be singular.")lines 101–101target.write_text(source.replace(old, new))lines 102–102trylines 103–103caught = not run_test(selector)lines 107–107return caught

Flows

purge_cache

None mutate.purge_cache()

Delete every pycache under the repository.

scripts/mutate.py:34

Show on the map →

Called by mutate, run_test

Calls none

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
lines 39–39◇ for cache in ROOT.rglob("__pycache__")lines 40–40shutil.rmtree(cache, ignore_errors=True)

Flows

require_clean

None mutate.require_clean()

Refuse to run on a dirty tree.

scripts/mutate.py:69

Show on the map →

Called by main

Calls none

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
lines 77–79dirty = subprocess.run( ["git", "status", "--porcelain"], cwd=ROOT, capture_output=True, text=True, check=True ).stdout.strip()lines 80–80◇ if dirtylines 81–81sys.exit(f"REFUSED: working tree is dirty; commit first.\n{dirty}")

Flows

run_test

bool mutate.run_test(str selector)

Run one test selector and report whether it passed.

scripts/mutate.py:47

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

Activity · 6 steps
lines 52–52purge_cache()lines 53–59done = subprocess.run( [str(ROOT / ".venv/bin/python"), "-m", "pytest", selector, "-q", "-p", "no:cacheprovider"], cwd=ROOT, capture_output=True, text=True, check=False, )lines 60–60purge_cache()lines 61–61◇ if done.returncode not in (0, 1)lines 62–62print(done.stdout[-2000:], file=sys.stderr)lines 63–63return done.returncode == 0

Flows