"""scripts/verify.sh의 시험. 단계를 계약 파일에서 읽는지, 잘못된 인자를 원격에 보내기 전에 거절하는지, 지금 트리를 원격 worktree로 옮겨 같은 트리에서 돌리고 지우는지를 표준 라이브러리와 git·bash·flock으로 본다. 원격은 가짜 ssh(호스트를 무시하고 이 기계에서 실행)다. pnpm은 돌리지 않는다. python3 -m unittest discover -s scripts/tests """ from __future__ import annotations import json import os import shutil import subprocess import tempfile import unittest from pathlib import Path REPO = Path(__file__).resolve().parent.parent.parent VERIFY = REPO / "scripts/verify.sh" GIT_ENV = {"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.com", "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@example.com", "GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "core.hooksPath", "GIT_CONFIG_VALUE_0": "/dev/null"} def sh(args: list[str], cwd: Path, env: dict | None = None, check: bool = True) -> subprocess.CompletedProcess: result = subprocess.run(args, cwd=cwd, env={**os.environ, **GIT_ENV, **(env or {})}, capture_output=True, text=True, timeout=60) if check and result.returncode != 0: raise AssertionError(f"{args} → {result.returncode}\n{result.stdout}\n{result.stderr}") return result def gates() -> dict: return json.loads((REPO / "config/ci/gates.json").read_text(encoding="utf-8")) class StagePlanTest(unittest.TestCase): def test_a_stage_runs_every_gate_the_contract_lists(self) -> None: for stage in gates()["stages"]: with self.subTest(stage=stage["id"]): result = sh([str(VERIFY), stage["id"], "--dry-run"], cwd=REPO) planned = [line.split(":")[0] for line in result.stdout.splitlines()] self.assertEqual(planned, stage["gateIds"]) self.assertIn(f"ci:gate {stage['gateIds'][0]}", result.stdout) def test_parts_runs_each_command_of_the_gate(self) -> None: contract = gates() gate = next(g for g in contract["gates"] if g["id"] == "FE-GATE-005") scripts = {c["id"]: c["script"] for c in contract["commands"]} result = sh([str(VERIFY), "parts", "FE-GATE-005", "--dry-run"], cwd=REPO) self.assertEqual([line.split(": ", 1)[1] for line in result.stdout.splitlines()], [f"corepack pnpm -s {scripts[c]}" for c in gate["commandIds"]]) def test_a_gate_with_a_host_environment_runs_in_it_as_in_the_workflow(self) -> None: # FE-GATE-013 needs systemd and bubblewrap; the workflow and verify.sh both run it through # scripts/run-in-system-container.sh (config/ci/gates.json hostEnvironment). contract = gates() hosted = [g for g in contract["gates"] if g.get("hostEnvironment") == "system-container"] self.assertEqual([g["id"] for g in hosted], ["FE-GATE-013"]) scripts = {c["id"]: c["script"] for c in contract["commands"]} wrapper = "scripts/run-in-system-container.sh" result = sh([str(VERIFY), "gate", "FE-GATE-013", "FE-GATE-005", "--dry-run"], cwd=REPO) self.assertEqual(result.stdout.splitlines(), [f"FE-GATE-013: {wrapper} corepack pnpm -s ci:gate FE-GATE-013", "FE-GATE-005: corepack pnpm -s ci:gate FE-GATE-005"]) result = sh([str(VERIFY), "parts", "FE-GATE-013", "--dry-run"], cwd=REPO) self.assertEqual([line.split(": ", 1)[1] for line in result.stdout.splitlines()], [f"{wrapper} corepack pnpm -s {scripts[c]}" for c in hosted[0]["commandIds"]]) workflow = (REPO / ".gitea/workflows/quality-gates.yml").read_text(encoding="utf-8") self.assertIn(f"run: {wrapper} corepack pnpm ci:gate -- FE-GATE-013", workflow) def test_parts_passes_each_command_its_arguments_as_ci_gate_does(self) -> None: # run-ci-gate.ts runs `pnpm