|
| 1 | +"""Tests for the --fix helpers (add_frontmatter.py, add_comment_marker.py) |
| 2 | +and the drift-check path that invokes them.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import importlib.util |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from scripts.drift_check import cli |
| 9 | +from scripts.drift_check.semver import parse_version |
| 10 | +from scripts.drift_check.types import Finding, RepoConfig, RepoSnapshot |
| 11 | + |
| 12 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 13 | +SCRIPTS_DIR = REPO_ROOT / "scripts" |
| 14 | + |
| 15 | + |
| 16 | +def _load(name: str): |
| 17 | + spec = importlib.util.spec_from_file_location(name, SCRIPTS_DIR / f"{name}.py") |
| 18 | + module = importlib.util.module_from_spec(spec) |
| 19 | + spec.loader.exec_module(module) |
| 20 | + return module |
| 21 | + |
| 22 | + |
| 23 | +fm = _load("add_frontmatter") |
| 24 | +cm = _load("add_comment_marker") |
| 25 | + |
| 26 | + |
| 27 | +# ------------------------------- frontmatter -------------------------------- |
| 28 | + |
| 29 | + |
| 30 | +def test_frontmatter_replaces_existing_value(): |
| 31 | + text = "---\nname: x\nstandards-version: 1.0.0\n---\nbody\n" |
| 32 | + assert fm._update(text, "1.10.1") == "---\nname: x\nstandards-version: 1.10.1\n---\nbody\n" |
| 33 | + |
| 34 | + |
| 35 | +def test_frontmatter_inserts_key_into_existing_block(): |
| 36 | + text = "---\nname: x\n---\nbody\n" |
| 37 | + assert fm._update(text, "1.10.1") == "---\nstandards-version: 1.10.1\nname: x\n---\nbody\n" |
| 38 | + |
| 39 | + |
| 40 | +def test_frontmatter_prepends_block_when_absent(): |
| 41 | + text = "# Title\n" |
| 42 | + assert fm._update(text, "1.10.1") == "---\nstandards-version: 1.10.1\n---\n# Title\n" |
| 43 | + |
| 44 | + |
| 45 | +def test_frontmatter_ignores_key_outside_block(): |
| 46 | + text = "---\nname: x\n---\nstandards-version: 0.0.1\n" |
| 47 | + out = fm._update(text, "2.0.0") |
| 48 | + assert out.startswith("---\nstandards-version: 2.0.0\nname: x\n---\n") |
| 49 | + assert out.endswith("standards-version: 0.0.1\n") |
| 50 | + |
| 51 | + |
| 52 | +def test_frontmatter_main_writes_file(tmp_path: Path): |
| 53 | + path = tmp_path / "SKILL.md" |
| 54 | + path.write_text("---\nname: x\n---\n", encoding="utf-8") |
| 55 | + assert fm.main(["add_frontmatter.py", str(path), "1.2.3"]) == 0 |
| 56 | + assert "standards-version: 1.2.3" in path.read_text(encoding="utf-8") |
| 57 | + |
| 58 | + |
| 59 | +# ------------------------------- comment marker ----------------------------- |
| 60 | + |
| 61 | + |
| 62 | +def test_marker_replaces_existing(): |
| 63 | + text = "<!-- standards-version: 1.0.0 -->\n# Agents\n" |
| 64 | + assert cm._update(text, "1.10.1") == "<!-- standards-version: 1.10.1 -->\n# Agents\n" |
| 65 | + |
| 66 | + |
| 67 | +def test_marker_inserted_when_missing(): |
| 68 | + assert cm._update("# Agents\n", "1.10.1") == "<!-- standards-version: 1.10.1 -->\n# Agents\n" |
| 69 | + |
| 70 | + |
| 71 | +def test_marker_main_on_missing_file_fails(tmp_path: Path): |
| 72 | + assert cm.main(["add_comment_marker.py", str(tmp_path / "nope.md"), "1.0.0"]) == 1 |
| 73 | + |
| 74 | + |
| 75 | +# ------------------------------- --fix wiring ------------------------------- |
| 76 | + |
| 77 | + |
| 78 | +def test_apply_fixes_live_inserts_missing_signal(tmp_path: Path): |
| 79 | + repo = tmp_path / "repo" |
| 80 | + (repo / "skills" / "a").mkdir(parents=True) |
| 81 | + skill = repo / "skills" / "a" / "SKILL.md" |
| 82 | + skill.write_text("---\nname: a\n---\nbody\n", encoding="utf-8") |
| 83 | + agents = repo / "AGENTS.md" |
| 84 | + agents.write_text("# Agents\n", encoding="utf-8") |
| 85 | + |
| 86 | + meta = parse_version("1.10.1") |
| 87 | + snap = RepoSnapshot( |
| 88 | + slug="repo", |
| 89 | + repo_type="cursor-plugin", |
| 90 | + files={}, |
| 91 | + meta_version=meta, |
| 92 | + meta_commit="sha", |
| 93 | + config=RepoConfig(slug="repo", repo_type="cursor-plugin"), |
| 94 | + ) |
| 95 | + findings = [ |
| 96 | + Finding(repo="repo", file=Path("skills/a/SKILL.md"), check="version-signal", |
| 97 | + severity="error", message="missing standards-version"), |
| 98 | + Finding(repo="repo", file=Path("AGENTS.md"), check="version-signal", |
| 99 | + severity="error", message="missing standards-version"), |
| 100 | + ] |
| 101 | + touched = cli._apply_fixes_live([snap], [repo], findings, SCRIPTS_DIR, meta) |
| 102 | + assert touched == 2 |
| 103 | + assert "standards-version: 1.10.1" in skill.read_text(encoding="utf-8") |
| 104 | + assert agents.read_text(encoding="utf-8").startswith("<!-- standards-version: 1.10.1 -->") |
0 commit comments