|
| 1 | +"""Guard sync manifest — single source of truth for attribution guard distribution.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[1] |
| 11 | +GIT = ROOT / "scripts" / "git" |
| 12 | +MANIFEST = GIT / "guard-sync-manifest.sh" |
| 13 | +SYNC = GIT / "sync-attribution-guard-scripts.sh" |
| 14 | +VERIFY = GIT / "verify-guard-parity.sh" |
| 15 | + |
| 16 | + |
| 17 | +def _extract_sync_binding_pattern() -> str: |
| 18 | + """Pull the actual grep -Eq pattern out of verify-guard-parity.sh's own |
| 19 | + SYNC BINDING check, rather than hardcoding a copy of it here. A |
| 20 | + hardcoded literal doesn't change when the production pattern does -- |
| 21 | + this test would keep passing (or failing) against a stale definition |
| 22 | + while a real regression in the production check goes undetected. |
| 23 | + Reading it from the source means drift between the two is impossible |
| 24 | + by construction: there is only one pattern, extracted at test time.""" |
| 25 | + text = VERIFY.read_text(encoding="utf-8") |
| 26 | + match = re.search( |
| 27 | + r"grep -Eq '(\^\[\[:space:\]\].*guard-sync-manifest\\\.sh)'", |
| 28 | + text, |
| 29 | + ) |
| 30 | + assert match, ( |
| 31 | + "could not find the SYNC BINDING grep pattern in " |
| 32 | + f"{VERIFY} -- verify-guard-parity.sh's check may have been " |
| 33 | + "rewritten in a way this extraction no longer matches" |
| 34 | + ) |
| 35 | + return match.group(1) |
| 36 | + |
| 37 | + |
| 38 | +_SYNC_BINDING_PATTERN = _extract_sync_binding_pattern() |
| 39 | + |
| 40 | + |
| 41 | +def _run_sync_binding_check(sync_script: Path) -> subprocess.CompletedProcess[str]: |
| 42 | + """Same grep as verify-guard-parity.sh SYNC BINDING check.""" |
| 43 | + return subprocess.run( |
| 44 | + ["grep", "-Eq", _SYNC_BINDING_PATTERN, str(sync_script)], |
| 45 | + capture_output=True, |
| 46 | + text=True, |
| 47 | + encoding="utf-8", |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def _bash_array(name: str) -> list[str]: |
| 52 | + result = subprocess.run( |
| 53 | + [ |
| 54 | + "bash", |
| 55 | + "-c", |
| 56 | + f'source "$1" && printf "%s\\n" "${{{name}[@]}}"', |
| 57 | + "_", |
| 58 | + str(MANIFEST), |
| 59 | + ], |
| 60 | + cwd=ROOT, |
| 61 | + capture_output=True, |
| 62 | + text=True, |
| 63 | + encoding="utf-8", |
| 64 | + check=True, |
| 65 | + ) |
| 66 | + return [line for line in result.stdout.splitlines() if line] |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.unit |
| 70 | +def test_manifest_files_exist_on_disk() -> None: |
| 71 | + all_paths = _bash_array("GUARD_SYNC_EXECUTABLES") + _bash_array("GUARD_SYNC_DATA_FILES") |
| 72 | + missing = [rel for rel in all_paths if not (GIT / rel).is_file()] |
| 73 | + assert not missing, f"manifest lists missing files: {missing}" |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.unit |
| 77 | +def test_parity_required_expands_in_bash() -> None: |
| 78 | + """GUARD_PARITY_REQUIRED is assembled at source time from the two sync arrays.""" |
| 79 | + parity = _bash_array("GUARD_PARITY_REQUIRED") |
| 80 | + expected = set(_bash_array("GUARD_SYNC_EXECUTABLES")) | set( |
| 81 | + _bash_array("GUARD_SYNC_DATA_FILES") |
| 82 | + ) |
| 83 | + assert set(parity) == expected |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.unit |
| 87 | +def test_sync_and_verify_source_manifest() -> None: |
| 88 | + pattern = r'^\s*(source|\.)\s+.*guard-sync-manifest\.sh' |
| 89 | + for script in (SYNC, VERIFY): |
| 90 | + body = script.read_text(encoding="utf-8") |
| 91 | + assert re.search(pattern, body, re.MULTILINE), ( |
| 92 | + f"{script.name} must source guard-sync-manifest.sh via an uncommented command" |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.unit |
| 97 | +def test_verify_rejects_comment_only_manifest_reference(tmp_path: Path) -> None: |
| 98 | + """Comment-only 'source guard-sync-manifest.sh' must not satisfy the binding check.""" |
| 99 | + fake_sync = tmp_path / "sync-attribution-guard-scripts.sh" |
| 100 | + fake_sync.write_text( |
| 101 | + "# source guard-sync-manifest.sh\n", |
| 102 | + encoding="utf-8", |
| 103 | + ) |
| 104 | + fake_result = _run_sync_binding_check(fake_sync) |
| 105 | + assert fake_result.returncode != 0, ( |
| 106 | + "production binding grep must reject comment-only manifest reference" |
| 107 | + ) |
| 108 | + real_result = _run_sync_binding_check(SYNC) |
| 109 | + assert real_result.returncode == 0, ( |
| 110 | + "canonical sync-attribution-guard-scripts.sh must satisfy binding check" |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.unit |
| 115 | +def test_verify_guard_parity_passes_in_canonical_repo() -> None: |
| 116 | + result = subprocess.run( |
| 117 | + ["bash", str(VERIFY)], |
| 118 | + cwd=ROOT, |
| 119 | + capture_output=True, |
| 120 | + text=True, |
| 121 | + encoding="utf-8", |
| 122 | + ) |
| 123 | + assert result.returncode == 0, result.stdout + result.stderr |
0 commit comments