test(785): close two findings from the independent cold review
Both are the same species the milestone is about — a coverage claim resting on source-text presence rather than on the guard actually running or the clause actually being reached. MEDIUM, `test_build_catalog_check_path.py`. `_ci_check_command()` grepped the whole workflow for the command text. So it matched a step that had been COMMENTED OUT, and it was not scoped to the `decisions-guard` job at all — a match anywhere in the file would do. Commenting out a step is the ordinary way a check gets switched off during an incident and never switched back, and the subprocess proof would have stayed green over a guard CI no longer runs. `guard-ships-with-mutation-proof`: the guard being WIRED is not the guard RUNNING. Now the extractor finds the job by key, stops at the next top-level job, strips comment lines before matching, and asserts exactly one active step. `test_the_extractor_rejects_a_ COMMENTED_OUT_step` pins it. Witnessed against the real workflow: commenting the step out reddens 2 of 7, deleting it reddens the same 2. LOW, `test_worktree_ownership_guard.py`. The docstring claimed the three mutations covered every clause the pair hangs on. They did not: the detection clause is `(commit|merge)`, and every payload in the file used `commit`, so `merge` could be dropped and the file stayed green. That is the more damaging half to lose — the mechanism exists for the plumbing-merge path (`process.foreign-worktree-plumbing-merge`). Adds a merge deny case and a fourth mutation, narrow by construction: the mutant must stop denying a merge WHILE STILL denying a commit, so a mutation that merely broke the regex cannot pass for it. Witnessed: dropping `merge` from the real hook reddens exactly those 2. The docstring now also states which clauses are NOT mutated (the `git -C`/`cd` redirection extraction, the marker hook's argument parsing) rather than implying the file is exhaustive. The review's third finding — the inventory calling both husky files thin dispatchers — was raised against ad0bed458..7bd7b4c8 and had already been corrected in fdc91056f. Refs: #785 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -137,23 +137,100 @@ def _copy_tree(src: Path, dst: Path) -> None:
|
||||
WORKFLOW = REPO_ROOT / ".gitea" / "workflows" / "pr-checks.yml"
|
||||
|
||||
|
||||
def _ci_check_command() -> list[str]:
|
||||
def _decisions_guard_run_lines(workflow: Path | None = None) -> list[str]:
|
||||
"""Every ACTIVE `run:` line inside the `decisions-guard` job, scoped to that job.
|
||||
|
||||
Three things this deliberately does, each closing a way the extractor could report coverage
|
||||
that is not there:
|
||||
|
||||
* it finds the job by its key (`decisions-guard:`) and stops at the next top-level job, so a
|
||||
matching command in some OTHER job cannot stand in for this one;
|
||||
* it strips comment lines FIRST, so a step that has been commented out — the ordinary way a
|
||||
CI check gets switched off during an incident and never switched back — does not read as
|
||||
wired. `guard-ships-with-mutation-proof` is explicit that the guard being WIRED is not the
|
||||
guard RUNNING, and a text search over YAML cannot tell those apart unless it excludes
|
||||
comments;
|
||||
* it returns the job's run lines rather than a single grepped match, so the caller asserts
|
||||
the command it needs is among the commands that job actually executes.
|
||||
"""
|
||||
lines = (workflow or WORKFLOW).read_text().splitlines()
|
||||
try:
|
||||
start = next(i for i, ln in enumerate(lines) if ln.strip() == "decisions-guard:")
|
||||
except StopIteration:
|
||||
raise AssertionError(
|
||||
"no `decisions-guard:` job in pr-checks.yml. Either the job was renamed or removed — "
|
||||
"the second is the far more serious finding, and this test is where it surfaces"
|
||||
) from None
|
||||
indent = len(lines[start]) - len(lines[start].lstrip())
|
||||
body = []
|
||||
for ln in lines[start + 1 :]:
|
||||
stripped = ln.strip()
|
||||
if stripped and not stripped.startswith("#"):
|
||||
cur = len(ln) - len(ln.lstrip())
|
||||
if cur <= indent:
|
||||
break
|
||||
if stripped.startswith("#"):
|
||||
continue
|
||||
body.append(ln)
|
||||
return [ln.split("run:", 1)[1].strip() for ln in body if "run:" in ln]
|
||||
|
||||
|
||||
def _ci_check_command(workflow: Path | None = None) -> list[str]:
|
||||
"""The command the `decisions-guard` job runs, DERIVED from the workflow, not restated here.
|
||||
|
||||
`testing.guard-derives-population-from-source`: a hand-copied command is a second copy of the
|
||||
workflow that drifts silently, and this test's entire value is that it runs what CI runs. If the
|
||||
job is reworded, this fails loudly instead of proving the wiring of a command nobody invokes.
|
||||
workflow that drifts silently, and this test's entire value is that it runs what CI runs.
|
||||
"""
|
||||
for line in WORKFLOW.read_text().splitlines():
|
||||
if "build_decisions_catalog.py" in line and "--check" in line:
|
||||
body = line.split("run:", 1)[1].strip() if "run:" in line else line.strip()
|
||||
assert body.startswith("PYTHONPATH=."), (
|
||||
f"the decisions-guard invocation no longer starts with PYTHONPATH=.: {body!r}"
|
||||
)
|
||||
return ["bash", "-c", body.replace("python3", sys.executable, 1)]
|
||||
raise AssertionError(
|
||||
"no `build_decisions_catalog.py --check` line found in pr-checks.yml — either the guard "
|
||||
"stopped running in CI (which is the more serious finding) or this extractor has drifted"
|
||||
runs = _decisions_guard_run_lines(workflow)
|
||||
matches = [r for r in runs if "build_decisions_catalog.py" in r and "--check" in r]
|
||||
assert matches, (
|
||||
"the `decisions-guard` job has no ACTIVE step running "
|
||||
"`build_decisions_catalog.py --check`. The catalog guard has stopped running in CI — that "
|
||||
f"is the finding, not this test's failure. Active run lines in that job: {runs}"
|
||||
)
|
||||
assert len(matches) == 1, f"expected exactly one such step, found {matches}"
|
||||
body = matches[0]
|
||||
assert body.startswith("PYTHONPATH=."), (
|
||||
f"the decisions-guard invocation no longer starts with PYTHONPATH=.: {body!r}"
|
||||
)
|
||||
return ["bash", "-c", body.replace("python3", sys.executable, 1)]
|
||||
|
||||
|
||||
def test_the_extractor_rejects_a_COMMENTED_OUT_step():
|
||||
"""A step commented out is a guard that has stopped running, and must not read as wired.
|
||||
|
||||
Without this the subprocess proof below stays green for as long as the text survives anywhere in
|
||||
the file — including inside the `#` that switched it off. That is the shape
|
||||
`guard-ships-with-mutation-proof` names as "the guard being WIRED is not the guard RUNNING".
|
||||
"""
|
||||
active = _decisions_guard_run_lines()
|
||||
assert any("build_decisions_catalog.py" in r for r in active), "precondition: it is wired today"
|
||||
for r in active:
|
||||
assert not r.lstrip().startswith("#"), f"a comment line survived the strip: {r!r}"
|
||||
|
||||
raw = WORKFLOW.read_text()
|
||||
commented = raw.replace(
|
||||
" - name: Active catalog in sync\n"
|
||||
" run: PYTHONPATH=. python3 scripts/build_decisions_catalog.py --check\n",
|
||||
" # - name: Active catalog in sync\n"
|
||||
" # run: PYTHONPATH=. python3 scripts/build_decisions_catalog.py --check\n",
|
||||
1,
|
||||
)
|
||||
assert commented != raw, "retarget this mutation; the step's text has changed"
|
||||
|
||||
import tempfile
|
||||
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
alt = Path(d) / "pr-checks.yml"
|
||||
alt.write_text(commented)
|
||||
raised = False
|
||||
try:
|
||||
_ci_check_command(alt)
|
||||
except AssertionError:
|
||||
raised = True
|
||||
assert raised, (
|
||||
"the extractor still found the command after the step was commented out, so the subprocess "
|
||||
"proof would stay green over a guard CI no longer runs"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -20,9 +20,14 @@ So this file:
|
||||
marker hook first as the harness would fire it, then the guard hook;
|
||||
* carries a negative control (a non-mutating git command) and a fail-open control (an unmarked
|
||||
worktree), because a guard that denied everything would satisfy the deny assertions;
|
||||
* and performs THREE clause-level mutations, one per clause the mechanism actually hangs on —
|
||||
the guard's marker read, the guard's ownership comparison, and the *other file's* marker write.
|
||||
The third is the one that could not exist while the halves were tested apart.
|
||||
* and performs FOUR clause-level mutations: the guard's marker read, the guard's ownership
|
||||
comparison, the command-detection alternation (`commit|merge`), and the *other file's* marker
|
||||
write. The last is the one that could not exist while the halves were tested apart.
|
||||
|
||||
These four are the clauses whose disarm this file detects. They are not every line in either hook —
|
||||
the `git -C` / `cd` redirection extraction and the marker hook's argument parsing are exercised
|
||||
behaviourally but not mutated, and the grade in `docs/guard-inventory.md` covers the clause its
|
||||
cited case mutates, not the whole file.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -217,6 +222,22 @@ def test_a_NON_MUTATING_git_command_in_a_foreign_worktree_is_ALLOWED(tmp_path):
|
||||
assert out == b"", f"a read-only git command was denied: {out!r}"
|
||||
|
||||
|
||||
def test_a_MERGE_in_a_foreign_worktree_is_DENIED(tmp_path):
|
||||
"""The other half of the guarded alternation.
|
||||
|
||||
Every other deny case here uses `git commit`, so `merge` could be dropped from the detection
|
||||
regex and this file would stay green — the mechanism guards the plumbing-merge path
|
||||
(`process.foreign-worktree-plumbing-merge`) specifically, which makes that the more damaging
|
||||
half to lose.
|
||||
"""
|
||||
repo = _scratch_repo(tmp_path)
|
||||
wt = _add_worktree(repo, "wt-a", MARKER_HOOK, SESSION_A)
|
||||
|
||||
rc, out = _run(GUARD, _commit_payload(SESSION_B, wt, "git merge --no-ff topic"), wt)
|
||||
assert rc == 0
|
||||
assert _denied(out), f"a merge into session A's worktree was not denied: {out!r}"
|
||||
|
||||
|
||||
def test_a_git_C_into_a_foreign_worktree_is_DENIED_from_the_main_tree(tmp_path):
|
||||
"""The redirection that makes the guard non-trivial.
|
||||
|
||||
@@ -344,3 +365,38 @@ def test_MUTATION_a_marker_hook_that_stops_WRITING_makes_the_guard_go_quiet(tmp_
|
||||
"the guard denied a commit in a worktree that carries NO marker, which means the deny in "
|
||||
f"the live case above is not evidence that the two halves are connected: {out!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_MUTATION_dropping_MERGE_from_the_detection_clause_stops_denying_a_merge(tmp_path):
|
||||
"""The alternation is two guarded operations, and losing one of them is silent.
|
||||
|
||||
This mutation is deliberately narrow: it must stop the guard denying a `merge` while leaving it
|
||||
denying a `commit`. Asserting both is what distinguishes "the alternation is load-bearing" from
|
||||
"the mutant broke the regex", which would redden everything and prove nothing about `merge`.
|
||||
"""
|
||||
repo = _scratch_repo(tmp_path)
|
||||
wt = _add_worktree(repo, "wt-a", MARKER_HOOK, SESSION_A)
|
||||
merge_payload = _commit_payload(SESSION_B, wt, "git merge --no-ff topic")
|
||||
commit_payload = _commit_payload(SESSION_B, wt)
|
||||
|
||||
rc_live, out_live = _run(GUARD, merge_payload, wt)
|
||||
assert rc_live == 0 and _denied(out_live), (
|
||||
f"the UNMUTATED guard did not deny a merge, so a silent mutant proves nothing: {out_live!r}"
|
||||
)
|
||||
|
||||
mutant = _mutate(
|
||||
GUARD,
|
||||
tmp_path,
|
||||
"(commit|merge)\\b",
|
||||
"(commit)\\b",
|
||||
"the command-detection alternation",
|
||||
)
|
||||
rc, out = _run(mutant, merge_payload, wt)
|
||||
assert rc == 0
|
||||
assert out == b"", f"dropping `merge` from the alternation did not stop the merge being denied: {out!r}"
|
||||
|
||||
rc_c, out_c = _run(mutant, commit_payload, wt)
|
||||
assert rc_c == 0 and _denied(out_c), (
|
||||
"the mutant stopped denying COMMITS too, so it broke detection wholesale rather than "
|
||||
f"removing the merge alternative — this proves nothing about `merge`: {out_c!r}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user