#!/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" # 让 .env.local 注入 JAVA_HOME / DB_* / JWT_SECRET 等运行期变量 # 由 .githooks/pre-push 触发本脚本时也能拿到 ENV_FILE="${PROJECT_ROOT}/.env.local" if [ -f "$ENV_FILE" ]; then set -a; . "$ENV_FILE"; set +a fi if [ -n "${JAVA_HOME:-}" ] && [ -d "$JAVA_HOME" ]; then export PATH="$JAVA_HOME/bin:$PATH" fi if [ -n "${EXTRA_PATH:-}" ]; then export PATH="$EXTRA_PATH:$PATH" fi # Stack detection: tracked content only — untracked WIP must not gate pushes HAS_BACKEND=0; [ -d backend ] && [ -n "$(git ls-files -- backend 2>/dev/null)" ] && HAS_BACKEND=1 HAS_FRONTEND=0; [ -d frontend ] && [ -n "$(git ls-files -- frontend 2>/dev/null)" ] && 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 && mvn -B clean package -DskipTests); else echo "[test.sh] skip backend build"; fi if [ $HAS_FRONTEND -eq 1 ]; then (cd frontend && npm run build); else echo "[test.sh] skip frontend build"; fi echo "[test.sh] 3/6 lint" if [ $HAS_BACKEND -eq 1 ]; then (cd backend && mvn -B compile); else echo "[test.sh] skip backend lint"; fi if [ $HAS_FRONTEND -eq 1 ]; then (cd frontend && npm run lint); else echo "[test.sh] skip frontend lint"; fi echo "[test.sh] 4/6 unit + integration" if [ $HAS_BACKEND -eq 1 ]; then (cd backend && mvn -B test); else echo "[test.sh] skip backend test"; fi if [ $HAS_FRONTEND -eq 0 ]; then echo "[test.sh] skip frontend test" elif node -e "process.exit(require('./frontend/package.json').scripts?.test?0:1)" >/dev/null 2>&1; then (cd frontend && npm test -- --run) else echo "[test.sh] skip frontend test (no 'test' script in frontend/package.json)" fi echo "[test.sh] 5/6 E2E" echo "[test.sh] e2e 略" echo "[test.sh] 6/6 reset test db" ./scripts/setup-test-db.sh echo "[test.sh] GREEN"