Files
zsp-project/scripts/run-tests.sh
jxw 2ff3002b19 feat: 添加项目基础设施、测试框架和路由对接文档
- 新增 .gitignore、ARCHITECTURE.md 项目基础设施文件
- 新增前后端路由对接文档,完整映射前端页面到后端 API 端点
- 配置前端 Vitest 测试框架,添加 API/Store/Utils/Components 单元测试
- 添加后端 UserService 单元测试
- 新增统一测试运行脚本 scripts/run-tests.sh
- 清理旧文档和过期覆盖率报告文件

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-04 08:53:36 +08:00

141 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# =============================================================================
# Unified Test Runner for ZSP Project
# Usage:
# ./scripts/run-tests.sh # Run all tests
# ./scripts/run-tests.sh backend # Only backend tests
# ./scripts/run-tests.sh frontend # Only frontend tests
# ./scripts/run-tests.sh coverage # All tests with coverage
# ./scripts/run-tests.sh ci # CI mode (all tests, fail-fast)
# =============================================================================
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
BACKEND_DIR="$PROJECT_ROOT/backend"
FRONTEND_DIR="$PROJECT_ROOT/frontend"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_section() { echo -e "\n${BLUE}══════════════════════════════════════════════════════${NC}"; echo -e "${BLUE} $1${NC}"; echo -e "${BLUE}══════════════════════════════════════════════════════${NC}"; }
log_pass() { echo -e "${GREEN}${NC} $1"; }
log_fail() { echo -e "${RED}${NC} $1"; }
log_info() { echo -e "${YELLOW}${NC} $1"; }
PASSED=0
FAILED=0
# ─── Backend tests ────────────────────────────────────────────────
run_backend_tests() {
log_section "Backend Tests"
cd "$BACKEND_DIR"
# Run handler tests
log_info "Running handler tests (test/)..."
if go test ./test/ -v -count=1 2>&1 | tail -5; then
log_pass "Handler tests passed"
PASSED=$((PASSED + 1))
else
log_fail "Handler tests failed"
FAILED=$((FAILED + 1))
if [ "${CI:-}" = "true" ]; then exit 1; fi
fi
# Run service tests
log_info "Running service tests (internal/service/)..."
if go test ./internal/service/ -v -count=1 2>&1 | tail -5; then
log_pass "Service tests passed"
PASSED=$((PASSED + 1))
else
log_fail "Service tests failed"
FAILED=$((FAILED + 1))
if [ "${CI:-}" = "true" ]; then exit 1; fi
fi
cd "$PROJECT_ROOT"
}
# ─── Frontend tests ───────────────────────────────────────────────
run_frontend_tests() {
log_section "Frontend Tests"
cd "$FRONTEND_DIR"
log_info "Running Vitest tests..."
if npx vitest run --reporter=verbose 2>&1 | tail -10; then
log_pass "Frontend tests passed"
PASSED=$((PASSED + 1))
else
log_fail "Frontend tests failed"
FAILED=$((FAILED + 1))
if [ "${CI:-}" = "true" ]; then exit 1; fi
fi
cd "$PROJECT_ROOT"
}
# ─── Coverage ─────────────────────────────────────────────────────
run_coverage() {
log_section "Test Coverage"
# Backend coverage
cd "$BACKEND_DIR"
log_info "Backend coverage..."
go test ./... -coverprofile=coverage.out -covermode=atomic 2>&1 || true
go tool cover -func=coverage.out 2>/dev/null | tail -1 || true
cd "$PROJECT_ROOT"
# Frontend coverage
cd "$FRONTEND_DIR"
log_info "Frontend coverage..."
npx vitest run --coverage 2>&1 | tail -20 || true
cd "$PROJECT_ROOT"
}
# ─── CI mode ──────────────────────────────────────────────────────
run_ci() {
export CI=true
log_section "CI Test Suite"
# Backend
cd "$BACKEND_DIR"
log_info "Running all Go tests..."
go test ./... -v -count=1 2>&1
log_pass "Go tests passed"
# Frontend
cd "$FRONTEND_DIR"
log_info "Running all frontend tests..."
npx vitest run --reporter=verbose 2>&1
log_pass "Frontend tests passed"
cd "$PROJECT_ROOT"
echo ""
log_section "All tests passed ✓"
}
# ─── Main ─────────────────────────────────────────────────────────
case "${1:-all}" in
backend) run_backend_tests ;;
frontend) run_frontend_tests ;;
coverage) run_coverage ;;
ci) run_ci ;;
all|"")
run_backend_tests
run_frontend_tests
echo ""
log_section "Summary: $PASSED passed, $FAILED failed"
if [ "$FAILED" -gt 0 ]; then
exit 1
fi
;;
*)
echo "Usage: $0 [backend|frontend|coverage|ci|all]"
exit 1
;;
esac