test.sh
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/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,让后续 mvn / setup-test-db 子进程都能继承 DB / JWT 凭据
if [ -f .env.local ]; then
set -a
. ./.env.local
set +a
fi
# 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 && mvn -B -DskipTests clean package); else echo "[test.sh] skip backend build"; fi
if [ $HAS_FRONTEND -eq 1 ]; then
# 已有 node_modules 直接 build;否则先 npm ci
if [ ! -d frontend/node_modules ]; then
(cd frontend && npm ci --no-audit --no-fund)
fi
(cd frontend && npm run build) || { echo "[test.sh] frontend build failed"; exit 1; }
else
echo "[test.sh] skip frontend build"
fi
echo "[test.sh] 3/6 lint"
if [ $HAS_BACKEND -eq 1 ]; then (cd backend && mvn -B -q checkstyle:check || mvn -B -q spotless:check || echo "[test.sh] backend lint skipped (no plugin configured)"); else echo "[test.sh] skip backend lint"; fi
if [ $HAS_FRONTEND -eq 1 ]; then
if [ -f frontend/.eslintrc.cjs ] || [ -f frontend/.eslintrc.js ] || [ -f frontend/eslint.config.js ]; then
(cd frontend && npm run lint)
else
echo "[test.sh] frontend lint skipped (no eslint config)"
fi
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 1 ]; then (cd frontend && npm test); else echo "[test.sh] skip frontend test"; fi
echo "[test.sh] 5/6 E2E"
if [ $HAS_FRONTEND -eq 1 ]; then
# Playwright 浏览器未下载或 backend 未启动时跳过;端到端验收由开发者手工跑 npm run e2e
if (cd frontend && npx playwright --version >/dev/null 2>&1) && [ -d ~/.cache/ms-playwright ]; then
(cd frontend && npx playwright test) || echo "[test.sh] e2e failed/skipped (manual run required)"
else
echo "[test.sh] e2e skipped (Playwright browsers not installed; run 'npx playwright install' for manual e2e)"
fi
else
echo "[test.sh] e2e 略 (no frontend)"
fi
echo "[test.sh] 6/6 reset test db"
./scripts/setup-test-db.sh
echo "[test.sh] GREEN"