scripts-test-template.sh 1.56 KB
#!/usr/bin/env bash
# scripts/test.sh —— 合并到默认分支(main / master)前的测试闸门。
# 顺序:detect → setup-db → build → lint → unit+integration → e2e → reset-db
# 由 .githooks/pre-push 和 test-gate skill(通过子会话)调用。

set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$PROJECT_ROOT"

# Stack detection (runtime, mode-agnostic)
HAS_BACKEND=0; [ -d backend ] && HAS_BACKEND=1
HAS_FRONTEND=0; [ -d frontend ] && HAS_FRONTEND=1
if [ $HAS_BACKEND -eq 0 ] && [ $HAS_FRONTEND -eq 0 ]; then
  echo "[test.sh] FATAL: neither backend/ nor frontend/ exists" >&2
  exit 1
fi

echo "[test.sh] 1/6 setup test db"
./scripts/setup-test-db.sh

echo "[test.sh] 2/6 build"
if [ $HAS_BACKEND -eq 1 ]; then (cd backend && {{backend_build}}); else echo "[test.sh] skip backend build"; fi
if [ $HAS_FRONTEND -eq 1 ]; then (cd frontend && {{frontend_build}}); else echo "[test.sh] skip frontend build"; fi

echo "[test.sh] 3/6 lint"
if [ $HAS_BACKEND -eq 1 ]; then (cd backend && {{backend_lint}}); else echo "[test.sh] skip backend lint"; fi
if [ $HAS_FRONTEND -eq 1 ]; then (cd frontend && {{frontend_lint}}); else echo "[test.sh] skip frontend lint"; fi

echo "[test.sh] 4/6 unit + integration"
if [ $HAS_BACKEND -eq 1 ]; then (cd backend && {{backend_test}}); else echo "[test.sh] skip backend test"; fi
if [ $HAS_FRONTEND -eq 1 ]; then (cd frontend && {{frontend_test}}); else echo "[test.sh] skip frontend test"; fi

echo "[test.sh] 5/6 E2E"
{{e2e_cmd}}

echo "[test.sh] 6/6 reset test db"
./scripts/setup-test-db.sh

echo "[test.sh] GREEN"