|
| 1 | +"""The openspec early-warning hook: block release actions, warn on commits. |
| 2 | +
|
| 3 | +Drives ``scripts/openspec-gate.py`` in a subprocess with the same stdin JSON |
| 4 | +shape every harness sends (``tool_input.command``), against fixture git repos |
| 5 | +where the release guard genuinely fails and genuinely passes — so both the |
| 6 | +exit-2 branch and the fail-open contract are proven, not assumed. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import json |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +GATE = Path(__file__).resolve().parents[3] / "scripts" / "openspec-gate.py" |
| 19 | +CONSISTENCY = GATE.parent / "check-release-consistency.py" |
| 20 | + |
| 21 | + |
| 22 | +def _git(cwd: Path, *args: str) -> None: |
| 23 | + subprocess.run( |
| 24 | + ["git", *args], |
| 25 | + cwd=cwd, |
| 26 | + check=True, |
| 27 | + capture_output=True, |
| 28 | + env={ |
| 29 | + "PATH": "/usr/bin:/bin", |
| 30 | + "GIT_AUTHOR_NAME": "t", |
| 31 | + "GIT_AUTHOR_EMAIL": "t@t", |
| 32 | + "GIT_COMMITTER_NAME": "t", |
| 33 | + "GIT_COMMITTER_EMAIL": "t@t", |
| 34 | + "HOME": str(cwd), |
| 35 | + }, |
| 36 | + timeout=60, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture() |
| 41 | +def repo(tmp_path: Path) -> Path: |
| 42 | + """A repo with an UNARCHIVED, UNDEFERRED change committed after the tag — |
| 43 | + the exact state the 0.2.0 cut shipped in.""" |
| 44 | + root = tmp_path / "repo" |
| 45 | + (root / "scripts").mkdir(parents=True) |
| 46 | + # The gate imports the guard from the repo under test, so the fixture |
| 47 | + # carries the REAL script — a stub here would test the stub. |
| 48 | + (root / "scripts" / "check-release-consistency.py").write_text( |
| 49 | + CONSISTENCY.read_text(encoding="utf-8"), encoding="utf-8" |
| 50 | + ) |
| 51 | + (root / "seed.txt").write_text("seed") |
| 52 | + _git(root, "init", "-q") |
| 53 | + _git(root, "add", ".") |
| 54 | + _git(root, "commit", "-qm", "seed") |
| 55 | + _git(root, "tag", "v0.0.1") |
| 56 | + change = root / "openspec" / "changes" / "unshipped-thing" |
| 57 | + change.mkdir(parents=True) |
| 58 | + (change / "proposal.md").write_text("## Why\n") |
| 59 | + _git(root, "add", "openspec") |
| 60 | + _git(root, "commit", "-qm", "work on unshipped-thing") |
| 61 | + return root |
| 62 | + |
| 63 | + |
| 64 | +def _run_gate(repo_root: Path, command: str, mode: str = "pre-tool-use"): |
| 65 | + payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": command}}) |
| 66 | + return subprocess.run( |
| 67 | + [sys.executable, str(GATE), mode, "--repo-root", str(repo_root)], |
| 68 | + input=payload, |
| 69 | + capture_output=True, |
| 70 | + text=True, |
| 71 | + timeout=60, |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def test_release_actions_are_blocked_when_the_guard_fails(repo: Path) -> None: |
| 76 | + for command in ("git tag v0.0.2", "uv run python scripts/prepare-release.py 0.0.2"): |
| 77 | + result = _run_gate(repo, command) |
| 78 | + assert result.returncode == 2, f"{command!r} was not blocked: {result.stdout}" |
| 79 | + assert "unshipped-thing" in result.stderr |
| 80 | + assert "release-check" in result.stderr, "the block must name the hard gate" |
| 81 | + |
| 82 | + |
| 83 | +def test_commits_are_warned_never_blocked(repo: Path) -> None: |
| 84 | + result = _run_gate(repo, "git commit -m 'normal cycle work'") |
| 85 | + assert result.returncode == 0, "a commit was blocked — open changes are legal in a cycle" |
| 86 | + assert "unshipped-thing" in result.stdout |
| 87 | + |
| 88 | + |
| 89 | +def test_unrelated_commands_pass_silently(repo: Path) -> None: |
| 90 | + result = _run_gate(repo, "ls -la && pytest -q") |
| 91 | + assert result.returncode == 0 |
| 92 | + assert result.stdout == "" |
| 93 | + |
| 94 | + |
| 95 | +def test_deferred_change_unblocks_the_release_action(repo: Path) -> None: |
| 96 | + meta = repo / "openspec" / "changes" / "unshipped-thing" / ".openspec.yaml" |
| 97 | + meta.write_text("deferred: waiting on the transport decision\n") |
| 98 | + _git(repo, "add", str(meta.relative_to(repo))) |
| 99 | + _git(repo, "commit", "-qm", "defer it") |
| 100 | + |
| 101 | + result = _run_gate(repo, "git tag v0.0.2") |
| 102 | + assert result.returncode == 0, result.stderr |
| 103 | + |
| 104 | + |
| 105 | +def test_remind_mode_is_terse_and_never_blocks(repo: Path) -> None: |
| 106 | + result = _run_gate(repo, "", mode="remind") |
| 107 | + assert result.returncode == 0 |
| 108 | + assert "unshipped-thing" in result.stdout |
| 109 | + assert len(result.stdout.splitlines()) == 1 |
| 110 | + |
| 111 | + |
| 112 | +def test_garbage_stdin_fails_open(repo: Path) -> None: |
| 113 | + """A broken early warning must never block work the hard gate allows.""" |
| 114 | + result = subprocess.run( |
| 115 | + [sys.executable, str(GATE), "pre-tool-use", "--repo-root", str(repo)], |
| 116 | + input="not json at all {", |
| 117 | + capture_output=True, |
| 118 | + text=True, |
| 119 | + timeout=60, |
| 120 | + ) |
| 121 | + assert result.returncode == 0 |
| 122 | + |
| 123 | + |
| 124 | +def test_the_wrappers_call_the_same_one_body() -> None: |
| 125 | + """Three harness wrappers, one script — the xTiles skill's hub pattern. |
| 126 | + A wrapper drifting to its own logic is exactly what this pins against.""" |
| 127 | + repo_root = GATE.parents[1] |
| 128 | + wrappers = [ |
| 129 | + repo_root / ".kiro" / "hooks" / "openspec-gate.json", |
| 130 | + repo_root / ".claude" / "settings.json", |
| 131 | + repo_root / ".codex" / "hooks.json", |
| 132 | + ] |
| 133 | + for wrapper in wrappers: |
| 134 | + assert wrapper.is_file(), f"missing hook wrapper: {wrapper}" |
| 135 | + text = wrapper.read_text(encoding="utf-8") |
| 136 | + assert "scripts/openspec-gate.py" in text, f"{wrapper} does not call the one body" |
| 137 | + # And the one body says what it is: early warning, with per-harness |
| 138 | + # verification state — never "enforcement". |
| 139 | + body = GATE.read_text(encoding="utf-8") |
| 140 | + assert "NOT ENFORCEMENT" in body |
| 141 | + assert "release-check" in body |
0 commit comments