|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""plan_completion_guard.py 단위 테스트.""" |
| 3 | +import json |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | + |
| 9 | +SCRIPT = os.path.join(os.path.dirname(__file__), "plan_completion_guard.py") |
| 10 | + |
| 11 | + |
| 12 | +def run_hook(tool_name, tool_input, plan_base=None): |
| 13 | + """훅 스크립트를 실행하고 (exit_code, stderr)를 반환.""" |
| 14 | + data = json.dumps({"tool_name": tool_name, "tool_input": tool_input}) |
| 15 | + env = os.environ.copy() |
| 16 | + if plan_base is not None: |
| 17 | + env["CLAUDE_PROJECT_DIR"] = plan_base |
| 18 | + result = subprocess.run( |
| 19 | + [sys.executable, SCRIPT], |
| 20 | + input=data, |
| 21 | + capture_output=True, |
| 22 | + text=True, |
| 23 | + env=env, |
| 24 | + ) |
| 25 | + return result.returncode, result.stderr |
| 26 | + |
| 27 | + |
| 28 | +def create_plan_dir(base, name, complete=True): |
| 29 | + """테스트용 plan 디렉토리 생성.""" |
| 30 | + plan_dir = os.path.join(base, "docs", "plan", name) |
| 31 | + os.makedirs(plan_dir, exist_ok=True) |
| 32 | + check = "[x]" if complete else "[ ]" |
| 33 | + with open(os.path.join(plan_dir, "plan.md"), "w") as f: |
| 34 | + f.write(f"# Test\n- {check} step 1\n") |
| 35 | + with open(os.path.join(plan_dir, "context.md"), "w") as f: |
| 36 | + f.write("# Context\n") |
| 37 | + with open(os.path.join(plan_dir, "checklist.md"), "w") as f: |
| 38 | + f.write("# Checklist\n") |
| 39 | + |
| 40 | + |
| 41 | +def test_non_bash_allowed(): |
| 42 | + code, _ = run_hook("Edit", {"file_path": "src/main/Test.kt"}) |
| 43 | + assert code == 0, f"Non-Bash should be allowed, got {code}" |
| 44 | + |
| 45 | + |
| 46 | +def test_non_pr_push_allowed(): |
| 47 | + code, _ = run_hook("Bash", {"command": "git status"}) |
| 48 | + assert code == 0, f"Non PR/push should be allowed, got {code}" |
| 49 | + |
| 50 | + |
| 51 | +def test_pr_create_blocked_with_incomplete_plan(): |
| 52 | + with tempfile.TemporaryDirectory() as tmp: |
| 53 | + create_plan_dir(tmp, "test-task", complete=False) |
| 54 | + code, stderr = run_hook( |
| 55 | + "Bash", {"command": 'gh pr create --title "test"'}, plan_base=tmp |
| 56 | + ) |
| 57 | + assert code == 2, f"Should block, got {code}" |
| 58 | + assert "plan-guard" in stderr |
| 59 | + |
| 60 | + |
| 61 | +def test_git_push_blocked_with_incomplete_plan(): |
| 62 | + with tempfile.TemporaryDirectory() as tmp: |
| 63 | + create_plan_dir(tmp, "test-task", complete=False) |
| 64 | + code, stderr = run_hook( |
| 65 | + "Bash", {"command": "git push -u origin main"}, plan_base=tmp |
| 66 | + ) |
| 67 | + assert code == 2, f"Should block, got {code}" |
| 68 | + assert "plan-guard" in stderr |
| 69 | + |
| 70 | + |
| 71 | +def test_pr_create_allowed_with_complete_plan(): |
| 72 | + with tempfile.TemporaryDirectory() as tmp: |
| 73 | + create_plan_dir(tmp, "test-task", complete=True) |
| 74 | + code, _ = run_hook( |
| 75 | + "Bash", {"command": 'gh pr create --title "test"'}, plan_base=tmp |
| 76 | + ) |
| 77 | + assert code == 0, f"Should allow, got {code}" |
| 78 | + |
| 79 | + |
| 80 | +def test_pr_create_allowed_with_no_plans(): |
| 81 | + with tempfile.TemporaryDirectory() as tmp: |
| 82 | + os.makedirs(os.path.join(tmp, "docs", "plan"), exist_ok=True) |
| 83 | + code, _ = run_hook( |
| 84 | + "Bash", {"command": 'gh pr create --title "test"'}, plan_base=tmp |
| 85 | + ) |
| 86 | + assert code == 0, f"Should allow with no plans, got {code}" |
| 87 | + |
| 88 | + |
| 89 | +def test_mixed_plans_blocked(): |
| 90 | + with tempfile.TemporaryDirectory() as tmp: |
| 91 | + create_plan_dir(tmp, "done-task", complete=True) |
| 92 | + create_plan_dir(tmp, "wip-task", complete=False) |
| 93 | + code, stderr = run_hook( |
| 94 | + "Bash", {"command": 'gh pr create --title "test"'}, plan_base=tmp |
| 95 | + ) |
| 96 | + assert code == 2, f"Should block with any incomplete, got {code}" |
| 97 | + assert "wip-task" in stderr |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + tests = [ |
| 102 | + test_non_bash_allowed, |
| 103 | + test_non_pr_push_allowed, |
| 104 | + test_pr_create_blocked_with_incomplete_plan, |
| 105 | + test_git_push_blocked_with_incomplete_plan, |
| 106 | + test_pr_create_allowed_with_complete_plan, |
| 107 | + test_pr_create_allowed_with_no_plans, |
| 108 | + test_mixed_plans_blocked, |
| 109 | + ] |
| 110 | + failed = 0 |
| 111 | + for test in tests: |
| 112 | + try: |
| 113 | + test() |
| 114 | + print(f" PASS: {test.__name__}") |
| 115 | + except AssertionError as e: |
| 116 | + print(f" FAIL: {test.__name__}: {e}") |
| 117 | + failed += 1 |
| 118 | + print(f"\n{len(tests) - failed}/{len(tests)} passed") |
| 119 | + sys.exit(1 if failed else 0) |
0 commit comments