diff --git a/scripts/tests/test_verify.py b/scripts/tests/test_verify.py new file mode 100644 index 00000000..49e017a1 --- /dev/null +++ b/scripts/tests/test_verify.py @@ -0,0 +1,119 @@ +"""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-013") + scripts = {c["id"]: c["script"] for c in contract["commands"]} + 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"corepack pnpm -s {scripts[c]}" for c in gate["commandIds"]]) + + def test_wrong_arguments_are_refused_here_before_anything_is_sent(self) -> None: + with tempfile.TemporaryDirectory() as folder: + calls = Path(folder) / "calls" + fake_ssh = Path(folder) / "ssh" + fake_ssh.write_text(f'#!/bin/sh\necho "$@" >> {calls}\n', encoding="utf-8") + fake_ssh.chmod(0o755) + for args in (["nope"], ["gate", "FE-GATE-999"], ["parts"], ["gate"], ["merge", "extra"], ["docs", "x"]): + with self.subTest(args=args): + result = sh([str(VERIFY), *args], cwd=REPO, env={"VERIFY_SSH": str(fake_ssh)}, check=False) + self.assertEqual(result.returncode, 2, result.stderr) + self.assertFalse(calls.exists(), "a refused command must not reach the remote host") + + +class RemoteTransportTest(unittest.TestCase): + """--on <호스트>: the patch lands in a fresh worktree, the tree matches, the run is logged, the worktree goes.""" + + def test_the_working_tree_is_run_remotely_on_the_same_tree_and_cleaned_up(self) -> None: + with tempfile.TemporaryDirectory() as folder: + base = Path(folder) + project, bare, remote, logs = (base / name for name in ("project", "origin.git", "remote", "logs")) + (project / "scripts").mkdir(parents=True) + (project / "config/ci").mkdir(parents=True) + shutil.copy(VERIFY, project / "scripts/verify.sh") + shutil.copy(REPO / "config/ci/gates.json", project / "config/ci/gates.json") + shutil.copy(REPO / ".nvmrc", project / ".nvmrc") + (project / "scripts/check-work-docs.py").write_text("print('docs checked')\n", encoding="utf-8") + sh(["git", "init", "-q", "-b", "main"], cwd=project) + sh(["git", "add", "-A"], cwd=project) + sh(["git", "commit", "-q", "-m", "init"], cwd=project) + sh(["git", "clone", "-q", "--bare", str(project), str(bare)], cwd=base) + sh(["git", "remote", "add", "origin", str(bare)], cwd=project) + sh(["git", "fetch", "-q", "origin"], cwd=project) + sh(["git", "clone", "-q", str(bare), str(remote)], cwd=base) + (project / "notes.md").write_text("커밋 안 한 새 파일\n", encoding="utf-8") + with (project / "scripts/check-work-docs.py").open("a", encoding="utf-8") as handle: + handle.write("print('changed here')\n") + fake_ssh = base / "fake-ssh" + fake_ssh.write_text('#!/bin/sh\nshift\nexec bash -c "$*"\n', encoding="utf-8") + fake_ssh.chmod(0o755) + env = {"VERIFY_SSH": str(fake_ssh), "VERIFY_REMOTE_REPO": str(remote), "VERIFY_LOCK": str(base / "lock"), + "VERIFY_LOG_ROOT": str(logs), "VERIFY_NODE_BIN": "/nonexistent"} + temp_index = base / "index" + shutil.copy(project / ".git/index", temp_index) + sh(["git", "add", "-A"], cwd=project, env={"GIT_INDEX_FILE": str(temp_index)}) + here = sh(["git", "write-tree"], cwd=project, env={"GIT_INDEX_FILE": str(temp_index)}).stdout.strip() + + result = sh(["scripts/verify.sh", "docs", "--on", "build-host", "--log", "t"], cwd=project, env=env, + check=False) + + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + summary = (logs / "t/summary.txt").read_text(encoding="utf-8") + self.assertTrue(summary.startswith(f"tree {here} commit "), summary) + self.assertIn("rc=0", summary) + self.assertIn("changed here", (logs / "t/run.log").read_text(encoding="utf-8")) + self.assertFalse((logs / "t/install.log").exists(), "a laptop stage installs nothing") + worktrees = sh(["git", "worktree", "list"], cwd=remote).stdout.strip().splitlines() + self.assertEqual(len(worktrees), 1, worktrees) + self.assertEqual(sh(["git", "status", "--porcelain"], cwd=remote).stdout, "") + + missing = sh(["scripts/verify.sh", "docs", "--on", "build-host"], cwd=project, + env={**env, "VERIFY_REMOTE_REPO": str(base / "missing")}, check=False) + self.assertEqual(missing.returncode, 4) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/verify.sh b/scripts/verify.sh new file mode 100755 index 00000000..ceccbef4 --- /dev/null +++ b/scripts/verify.sh @@ -0,0 +1,272 @@ +#!/usr/bin/env bash +# 검증 진입점. 사람·CI·에이전트가 같은 명령을 쓴다(docs/work/reference/gates.md). +# +# scripts/verify.sh <단계> [대상…] [--on here|<호스트>] [--log <이름>] [--dry-run] +# +# 단계 무엇 기본 위치 +# docs 작업 매뉴얼 검사(check-work-docs.py)와 장부 형식 여기 +# hooks repo 훅·검사기·장부 시험(python3 unittest) 여기 +# gate … 게이트마다 `corepack pnpm ci:gate `, CI env로 desktop +# parts 그 게이트의 명령을 하나씩 끝까지(첫 실패에서 멈추지 않음) desktop +# script <이름>… package script를 하나씩(예: verify:documentation) desktop +# config/ci/gates.json stages의 gateIds 전부(merge 등) desktop +# +# 게이트·stage는 실패가 있어도 끝까지 돌고, 끝에 게이트마다 rc와 초를 적는다. 실패가 하나라도 있으면 rc=1. +# --on <호스트>: 지금 작업 트리(커밋 안 된 변경 포함)를 패치로 그 호스트에 옮긴다. 기준 커밋에서 새 worktree를 +# 만들어 패치를 적용하고 트리 해시가 여기와 같은지 본 뒤, 그 트리를 커밋 하나로 만들어(깨끗한 HEAD) 의존성을 +# 오프라인으로 설치하고 잠금 아래에서 같은 단계를 돌린다. 로그를 남기고 worktree를 지운다. push하지 않는다. +# 환경 변수: VERIFY_HOST(desktop 단계의 --on 기본값, 기본 desktop), VERIFY_REMOTE_REPO(원격 checkout, 기본 +# workspace/, 홈 기준), VERIFY_BASE_REF(기본 origin/main), VERIFY_LOCK(기본 /tmp/claude-gates/heavy.lock), +# VERIFY_LOG_ROOT(원격 로그 폴더, 기본 /tmp/claude-gates), VERIFY_SSH(기본 ssh), VERIFY_NODE_BIN(원격 node 폴더, +# 기본 ~/.nvm/versions/node/v<.nvmrc>/bin). +# 종료 코드: 0 통과, 1 실패, 2 사용법, 4 원격 준비 실패. +set -euo pipefail + +ROOT=$(git rev-parse --show-toplevel) +cd "$ROOT" +TMP_FILES=() +trap '[ ${#TMP_FILES[@]} -eq 0 ] || rm -f "${TMP_FILES[@]}"' EXIT # 빈 배열 + set -u는 옛 bash에서 오류다 +DRY_RUN=0 + +usage() { + sed -n '2,22p' "$0" | sed 's/^# \{0,1\}//' >&2 + exit 2 +} + +# ---------------------------------------------------------------- config/ci/gates.json 읽기 + +gates_json() { + python3 - "$@" <<'PY' +import json, sys +contract = json.load(open("config/ci/gates.json", encoding="utf-8")) +kind, *names = sys.argv[1:] +if kind == "stages": + print("\n".join(stage["id"] for stage in contract["stages"])) +elif kind == "stage": + found = [stage for stage in contract["stages"] if stage["id"] == names[0]] + if not found: + sys.exit(1) + print("\n".join(found[0]["gateIds"])) +elif kind == "gate": + if not any(gate["id"] == names[0] for gate in contract["gates"]): + sys.exit(1) +elif kind == "scripts": + gate = next((gate for gate in contract["gates"] if gate["id"] == names[0]), None) + if gate is None: + sys.exit(1) + commands = {command["id"]: command for command in contract["commands"]} + for command_id in gate["commandIds"]: + command = commands[command_id] + print(f'{command["script"]}\t{command.get("expect", "pass")}\t{command.get("expectedExitCode", "")}') +PY +} + +is_stage() { gates_json stages | grep -qx -- "$1"; } + +# ---------------------------------------------------------------- 단계 본문 + +run_docs() { + local rc=0 + python3 scripts/check-work-docs.py || rc=1 + if [ -s docs/ledger/estimates.jsonl ] && ! python3 -c ' +import json, sys +for number, line in enumerate(open(sys.argv[1], encoding="utf-8"), 1): + if line.strip(): + json.loads(line) +' docs/ledger/estimates.jsonl; then + echo "docs/ledger/estimates.jsonl에 JSON이 아닌 줄이 있다." >&2 + rc=1 + fi + return "$rc" +} + +run_hooks() { python3 -m unittest discover -s scripts/tests; } + +# CI workflow의 전역 env(.gitea/workflows/quality-gates.yml)와 같은 꼴. CI=true가 있어야 빌드 식별값을 엄격하게 본다. +ci_env() { + local sha + sha=$(git rev-parse HEAD) + export CI=true VITE_COMMIT_SHA="$sha" VITE_BUILD_ID="gitea-${VERIFY_RUN:-1}-1" \ + RELEASE_ID="refs/heads/${VERIFY_REF:-verify}-${VERIFY_RUN:-1}-1" \ + CI_RUNNER_IMAGE="local/runner@sha256:$(printf 'a%.0s' $(seq 64))" +} + +# 한 줄 결과: "<이름> rc= <초>s". 로그는 VERIFY_STEP_LOGS가 있으면 그 폴더에 이름별로. +step() { + local name=$1 rc start log + shift + if [ "$DRY_RUN" = 1 ]; then + echo "$name: $*" + return 0 + fi + start=$(date +%s) + log=${VERIFY_STEP_LOGS:+$VERIFY_STEP_LOGS/$(printf '%s' "$name" | tr ' /:' '___').log} + set +e + if [ -n "$log" ]; then "$@" > "$log" 2>&1; else "$@"; fi + rc=$? + set -e + echo "$name rc=$rc $(( $(date +%s) - start ))s" | tee -a "${VERIFY_SUMMARY:-/dev/null}" + return "$rc" +} + +run_gates() { + local failed=0 gate + [ "$DRY_RUN" = 1 ] || ci_env + for gate in "$@"; do + gates_json gate "$gate" || { echo "verify: config/ci/gates.json에 없는 게이트: $gate" >&2; return 2; } + step "$gate" corepack pnpm -s ci:gate "$gate" || failed=1 + done + return "$failed" +} + +run_parts() { + local gate=$1 failed=0 script expect code rc + [ "$DRY_RUN" = 1 ] || ci_env + gates_json scripts "$gate" > /dev/null || { echo "verify: config/ci/gates.json에 없는 게이트: $gate" >&2; return 2; } + while IFS=$'\t' read -r script expect code; do + rc=0 + step "$gate $script" corepack pnpm -s "$script" || rc=$? + # expect=fail인 명령(음성 fixture)은 정해진 종료 코드로 실패해야 통과다. + if [ "$expect" = fail ]; then + [ "$DRY_RUN" = 1 ] || [ "$rc" = "${code:-1}" ] || failed=1 + elif [ "$rc" != 0 ]; then + failed=1 + fi + done < <(gates_json scripts "$gate") + return "$failed" +} + +run_scripts() { + local failed=0 script + for script in "$@"; do + step "$script" corepack pnpm -s "$script" || failed=1 + done + return "$failed" +} + +# ---------------------------------------------------------------- 실행 위치 + +laptop_stage() { [ "$1" = docs ] || [ "$1" = hooks ]; } + +# 원격으로 보내기 전에 여기서 사용법을 본다. 잘못된 단계 때문에 desktop에 worktree를 만들지 않는다. +check_args() { + local stage=$1 gate + shift + case "$stage" in + docs | hooks) [ $# -eq 0 ] || usage ;; + gate) [ $# -gt 0 ] || usage + for gate in "$@"; do gates_json gate "$gate" || { echo "verify: config/ci/gates.json에 없는 게이트: $gate" >&2; exit 2; }; done ;; + parts) [ $# -eq 1 ] || usage + gates_json gate "$1" || { echo "verify: config/ci/gates.json에 없는 게이트: $1" >&2; exit 2; } ;; + script) [ $# -gt 0 ] || usage ;; + *) is_stage "$stage" || usage; [ $# -eq 0 ] || usage ;; + esac +} + +run_here() { + local stage=$1 + shift + case "$stage" in + docs | hooks) [ $# -eq 0 ] || usage; [ "$DRY_RUN" = 1 ] && { echo "$stage"; return 0; }; "run_$stage" ;; + gate) [ $# -gt 0 ] || usage; run_gates "$@" ;; + parts) [ $# -eq 1 ] || usage; run_parts "$1" ;; + script) [ $# -gt 0 ] || usage; run_scripts "$@" ;; + *) + is_stage "$stage" || usage + [ $# -eq 0 ] || usage + # shellcheck disable=SC2046 # 게이트 ID는 공백 없는 낱말이다 + run_gates $(gates_json stage "$stage") + ;; + esac +} + +run_remote() { + local host=$1 log=$2 ssh=${VERIFY_SSH:-ssh} + shift 2 + local name base tree tmp_index patch id remote_repo lock log_root node_bin args ref install=1 + name=$(basename "$(dirname "$(git rev-parse --path-format=absolute --git-common-dir)")") + remote_repo=${VERIFY_REMOTE_REPO:-"workspace/$name"} + lock=${VERIFY_LOCK:-/tmp/claude-gates/heavy.lock} + log_root=${VERIFY_LOG_ROOT:-/tmp/claude-gates} + node_bin=${VERIFY_NODE_BIN:-"\$HOME/.nvm/versions/node/v$(tr -d '[:space:]' < .nvmrc)/bin"} + base=$(git merge-base HEAD "${VERIFY_BASE_REF:-origin/main}") + ref=$(git branch --show-current | tr '/' '-') + laptop_stage "$1" && install=0 + tmp_index=$(mktemp) + patch=$(mktemp) + TMP_FILES+=("$tmp_index" "$patch") + cp "$(git rev-parse --git-path index)" "$tmp_index" + GIT_INDEX_FILE=$tmp_index git add -A + tree=$(GIT_INDEX_FILE=$tmp_index git write-tree) + git diff --binary "$base" "$tree" > "$patch" + id="verify-$(date +%s)-$$" + args=$(printf '%q ' "$@") + echo "verify: $host에서 '$*' (기준 ${base:0:12}, 트리 ${tree:0:12}, 로그 $log_root/$log/)" >&2 + $ssh "$host" "cat > /tmp/$id.patch" < "$patch" + $ssh "$host" bash -s </dev/null || git fetch -q origin || exit 4 +git cat-file -e $base^{commit} || { echo "기준 커밋 ${base:0:12}이 원격에 없다. 그 커밋을 push한다." >&2; exit 4; } +wt=\$(mktemp -d /tmp/$id.XXXXXX) +logs=$log_root/$log +git worktree add -q --detach "\$wt" $base || exit 4 +cleanup() { + cp -r "\$wt/artifacts/quality/gates" "\$logs/gate-artifacts" 2>/dev/null + cd "\$repo" + git worktree remove --force "\$wt" + rm -f /tmp/$id.patch +} +trap cleanup EXIT +cd "\$wt" +if [ -s /tmp/$id.patch ]; then git apply --binary --index /tmp/$id.patch || exit 4; fi +got=\$(git write-tree) +if [ "\$got" != "$tree" ]; then echo "트리 해시가 다르다: 원격 \$got, 여기 $tree" >&2; exit 4; fi +# 게이트는 깨끗한 HEAD(커밋 식별값 = 트리)를 본다. 훅을 거치지 않는 commit-tree로 이 worktree에만 만든다. +commit=\$(git -c user.name=verify -c user.email=verify@localhost commit-tree "\$got" -p HEAD -m "verify $id") +git checkout -q --detach "\$commit" +mkdir -p "\$(dirname $lock)" "\$logs" +: > "\$logs/summary.txt" +echo "tree $tree commit \$commit \$(date '+%F %T') load \$(cut -d' ' -f1 /proc/loadavg)" >> "\$logs/summary.txt" +if [ $install = 1 ]; then + CI=true corepack pnpm install --frozen-lockfile --offline > "\$logs/install.log" 2>&1 \ + || CI=true corepack pnpm install --frozen-lockfile >> "\$logs/install.log" 2>&1 \ + || { echo "의존성 설치 실패: \$logs/install.log" >&2; exit 4; } +fi +export VERIFY_SUMMARY="\$logs/summary.txt" VERIFY_STEP_LOGS="\$logs" VERIFY_REF="$ref" +flock $lock ./scripts/verify.sh $args --on here 2>&1 | tee "\$logs/run.log" +rc=\${PIPESTATUS[0]} +git status --porcelain > "\$logs/status-after.txt" +echo "rc=\$rc" >> "\$logs/summary.txt" +exit \$rc +EOF +} + +main() { + local stage=${1:-} on="" log="" rest=() + [ -n "$stage" ] || usage + shift + while [ $# -gt 0 ]; do + case "$1" in + --on) on=${2:?--on 뒤에 호스트}; shift 2 ;; + --log) log=${2:?--log 뒤에 이름}; shift 2 ;; + --dry-run) DRY_RUN=1; shift ;; + -*) usage ;; + *) rest+=("$1"); shift ;; + esac + done + check_args "$stage" ${rest[@]+"${rest[@]}"} + if [ -z "$on" ]; then + if laptop_stage "$stage"; then on=here; else on=${VERIFY_HOST:-desktop}; fi + fi + if [ "$on" = here ] || [ "$DRY_RUN" = 1 ]; then + run_here "$stage" ${rest[@]+"${rest[@]}"} + else + log=${log:-"$(git branch --show-current | tr '/' '-')-$stage"} + run_remote "$on" "${log:-detached}" "$stage" ${rest[@]+"${rest[@]}"} + fi +} + +main "$@"