"""원장에 적힌 관문이 **그 원장의 대상**에 돌았는지 (R5). 원장은 명령 문자열과 종료 코드를 적는다. 그런데 그 명령이 어느 대상에 돌았는지는 아무도 안 봤다 — 다른 프로젝트에 돌려 받은 exit 0 을 적어도 통과로 셌다. **정상 원장이 그대로 통과하는 대조를 함께 둔다.** 무조건 거절로 성공률을 올리는 것이 R-003 이 든 실패다. """ from __future__ import annotations import copy import glob import importlib.util import json import os import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def _module(): path = os.path.join(ROOT, "scripts", "verify-pipeline-run.py") spec = importlib.util.spec_from_file_location("verify_pipeline_run", path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def _a_ledger() -> dict | None: for p in sorted(glob.glob(os.path.join(ROOT, "runs", "*", "*", "run.json"))): d = json.load(open(p, encoding="utf-8")) gates = [g for st in d.get("stages", []) for g in (st.get("gates") or [])] if any("docs/" in str(g.get("cmd", "")) for g in gates): return d return None def _verify(doc: dict) -> object: with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False, dir=os.path.join(ROOT, "runs"), encoding="utf-8") as f: json.dump(doc, f, ensure_ascii=False) path = f.name try: return _module().verify(path) finally: os.unlink(path) class GateRanAgainstTheLedgersTarget(unittest.TestCase): def setUp(self) -> None: self.doc = _a_ledger() if self.doc is None: self.skipTest("대상 경로가 있는 원장이 이 저장소에 없다") def test_untouched_ledger_still_passes(self) -> None: """대조군 — 손대지 않은 원장은 이 규칙으로 새 error 를 얻지 않는다.""" rep = _verify(self.doc) self.assertNotIn("관문이 다른 대상에 돌았다", rep.errors) def test_gate_on_another_project_is_an_error(self) -> None: other = "n+1liner" if self.doc["project"] != "n+1liner" else "keycloak" if not os.path.isdir(os.path.join(ROOT, "docs", other)): self.skipTest("대조에 쓸 다른 프로젝트가 없다") bad = copy.deepcopy(self.doc) swapped = False for st in bad["stages"]: for g in st.get("gates") or []: cmd = str(g.get("cmd") or "") if not swapped and f"docs/{bad['project']}/" in cmd: g["cmd"] = cmd.replace(f"docs/{bad['project']}/", f"docs/{other}/") swapped = True self.assertTrue(swapped, "바꿀 관문을 못 찾았다") rep = _verify(bad) self.assertIn("관문이 다른 대상에 돌았다", rep.errors) if __name__ == "__main__": unittest.main()