#!/bin/bash # verify_api_compat.sh — 验证 API 拆分前后路由清单完全一致 # # 用法: # bash tests/scripts/verify_api_compat.sh # # 前置: # - 当前在开发目录(sanguo_moziplus_v2/) # - git working tree 有拆分改动 # - main 分支是拆分前的基准 # # 输出: # ✅ 路由完全一致(exit 0) # ❌ 路由有差异(exit 1,打印 diff) set -euo pipefail BEFORE_FILE="/tmp/routes_before_$$.txt" AFTER_FILE="/tmp/routes_after_$$.txt" echo "=== 提取拆分前路由清单(main 分支)===" # stash 当前改动(如果有 untracked 新文件,--include-untracked) STASHED=0 if ! git diff --quiet || ! git diff --cached --quiet; then git stash --include-untracked STASHED=1 fi python3 -c " from src.main import app for route in app.routes: if hasattr(route, 'methods') and hasattr(route, 'path'): for m in sorted(route.methods): if m in ('GET','POST','PATCH','DELETE','PUT'): print(f'{m} {route.path}') " | sort > "$BEFORE_FILE" echo "Routes before: $(wc -l < "$BEFORE_FILE")" # 恢复改动 if [ "$STASHED" = "1" ]; then git stash pop fi echo "=== 提取拆分后路由清单(当前 working tree)===" python3 -c " from src.main import app for route in app.routes: if hasattr(route, 'methods') and hasattr(route, 'path'): for m in sorted(route.methods): if m in ('GET','POST','PATCH','DELETE','PUT'): print(f'{m} {route.path}') " | sort > "$AFTER_FILE" echo "Routes after: $(wc -l < "$AFTER_FILE")" echo "" echo "=== Diff ===" if diff "$BEFORE_FILE" "$AFTER_FILE"; then echo "✅ 路由完全一致" rm -f "$BEFORE_FILE" "$AFTER_FILE" exit 0 else echo "❌ 路由有差异" rm -f "$BEFORE_FILE" "$AFTER_FILE" exit 1 fi