"""Tests for scripts/check_adrs.py — ADR immutability gate.""" from __future__ import annotations import subprocess import sys from pathlib import Path import pytest SCRIPTS = Path(__file__).resolve().parents[2] / "scripts" sys.path.insert(0, str(SCRIPTS)) from check_adrs import find_modified_adrs, run_check # noqa: E402 def _git(cwd: Path, *args: str) -> None: subprocess.check_call(["git", *args], cwd=cwd, stdout=subprocess.DEVNULL) def test_no_modified_adrs_on_current_branch() -> None: assert run_check(base="origin/main") == 0 def test_detects_adr_edit_in_sandbox(tmp_path: Path) -> None: _git(tmp_path, "init") _git(tmp_path, "config", "user.email", "ci@test") _git(tmp_path, "config", "user.name", "ci") adr = tmp_path / "docs/adr" adr.mkdir(parents=True) (adr / "ADR-0001-test.md").write_text("v1\n") _git(tmp_path, "add", ".") _git(tmp_path, "commit", "-m", "add adr") _git(tmp_path, "branch", "-M", "main") (adr / "ADR-0001-test.md").write_text("v2\n") _git(tmp_path, "checkout", "-b", "feature") _git(tmp_path, "add", ".") _git(tmp_path, "commit", "-m", "edit adr") monkeypatch = pytest.MonkeyPatch() monkeypatch.setattr("check_adrs.ROOT", tmp_path) try: assert find_modified_adrs("main") == ["docs/adr/ADR-0001-test.md"] assert run_check(base="main") == 1 finally: monkeypatch.undo() def test_new_adr_allowed(tmp_path: Path) -> None: _git(tmp_path, "init") _git(tmp_path, "config", "user.email", "ci@test") _git(tmp_path, "config", "user.name", "ci") adr = tmp_path / "docs/adr" adr.mkdir(parents=True) (adr / "ADR-0001-test.md").write_text("v1\n") _git(tmp_path, "add", ".") _git(tmp_path, "commit", "-m", "add adr") _git(tmp_path, "branch", "-M", "main") (adr / "ADR-0002-new.md").write_text("new\n") _git(tmp_path, "checkout", "-b", "feature") _git(tmp_path, "add", ".") _git(tmp_path, "commit", "-m", "add adr 2") monkeypatch = pytest.MonkeyPatch() monkeypatch.setattr("check_adrs.ROOT", tmp_path) try: assert find_modified_adrs("main") == [] assert run_check(base="main") == 0 finally: monkeypatch.undo()