Files
xlxumu/scripts/test.sh

464 lines
12 KiB
Bash
Raw Normal View History

#!/bin/bash
# 智慧畜牧业小程序矩阵测试脚本
# 用于运行各种测试和代码质量检查
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查环境
check_environment() {
log_info "检查测试环境..."
# 检查 Node.js
if ! command -v node &> /dev/null; then
log_error "Node.js 未安装"
exit 1
fi
# 检查 npm
if ! command -v npm &> /dev/null; then
log_error "npm 未安装"
exit 1
fi
# 检查项目目录
if [ ! -d "mini_program" ]; then
log_error "mini_program 目录不存在"
exit 1
fi
log_success "环境检查通过"
}
# 安装测试依赖
install_test_dependencies() {
log_info "安装测试依赖..."
cd mini_program
# 检查是否已安装测试相关依赖
if ! npm list jest &> /dev/null; then
npm install --save-dev jest @vue/test-utils
fi
if ! npm list eslint &> /dev/null; then
npm install --save-dev eslint @vue/eslint-config-typescript
fi
if ! npm list prettier &> /dev/null; then
npm install --save-dev prettier
fi
log_success "测试依赖安装完成"
cd ..
}
# 代码格式检查
check_code_format() {
log_info "检查代码格式..."
cd mini_program
# 检查 Prettier 格式
if command -v npx &> /dev/null; then
if npx prettier --check "**/*.{js,vue,ts,json,md}" 2>/dev/null; then
log_success "代码格式检查通过"
else
log_warning "代码格式不符合规范,建议运行 npm run format 修复"
fi
else
log_warning "Prettier 未安装,跳过格式检查"
fi
cd ..
}
# 代码质量检查
check_code_quality() {
log_info "检查代码质量..."
cd mini_program
# ESLint 检查
if command -v npx &> /dev/null; then
if npx eslint "**/*.{js,vue,ts}" --quiet 2>/dev/null; then
log_success "代码质量检查通过"
else
log_warning "发现代码质量问题,建议运行 npm run lint 查看详情"
fi
else
log_warning "ESLint 未安装,跳过质量检查"
fi
cd ..
}
# 运行单元测试
run_unit_tests() {
log_info "运行单元测试..."
cd mini_program
# 检查是否有测试文件
if find . -name "*.test.js" -o -name "*.spec.js" | grep -q .; then
if npm test 2>/dev/null; then
log_success "单元测试通过"
else
log_error "单元测试失败"
cd ..
return 1
fi
else
log_warning "未找到测试文件,跳过单元测试"
fi
cd ..
}
# 检查依赖安全性
check_security() {
log_info "检查依赖安全性..."
cd mini_program
# npm audit 检查
if npm audit --audit-level moderate 2>/dev/null; then
log_success "依赖安全检查通过"
else
log_warning "发现安全漏洞,建议运行 npm audit fix 修复"
fi
cd ..
}
# 检查包大小
check_bundle_size() {
log_info "检查包大小..."
cd mini_program
# 构建项目并检查大小
if npm run build:h5 &> /dev/null; then
if [ -d "dist" ]; then
local size=$(du -sh dist 2>/dev/null | cut -f1)
log_info "构建包大小: $size"
# 检查是否超过限制 (例如 10MB)
local size_bytes=$(du -s dist 2>/dev/null | cut -f1)
if [ "$size_bytes" -gt 10240 ]; then # 10MB = 10240KB
log_warning "构建包较大,建议优化"
else
log_success "构建包大小合理"
fi
fi
else
log_warning "构建失败,无法检查包大小"
fi
cd ..
}
# 性能测试
run_performance_tests() {
log_info "运行性能测试..."
# 这里可以添加性能测试逻辑
# 例如Lighthouse、WebPageTest 等
log_info "性能测试功能待实现"
}
# 兼容性测试
run_compatibility_tests() {
log_info "运行兼容性测试..."
cd mini_program
# 检查各平台构建是否成功
local platforms=("mp-weixin" "mp-alipay" "h5")
local success_count=0
for platform in "${platforms[@]}"; do
log_info "测试 $platform 平台兼容性..."
if npm run "build:$platform" &> /dev/null; then
log_success "$platform 平台兼容性测试通过"
success_count=$((success_count + 1))
else
log_error "$platform 平台兼容性测试失败"
fi
done
log_info "兼容性测试完成: $success_count/${#platforms[@]} 平台通过"
cd ..
}
# 生成测试报告
generate_test_report() {
log_info "生成测试报告..."
local report_file="test-report.html"
local test_time=$(date "+%Y-%m-%d %H:%M:%S")
cat > "$report_file" << EOF
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试报告 - 智慧畜牧业小程序矩阵</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.header { background: #2E8B57; color: white; padding: 20px; border-radius: 8px; }
.section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 8px; }
.success { border-left: 4px solid #28a745; }
.warning { border-left: 4px solid #ffc107; }
.error { border-left: 4px solid #dc3545; }
.test-item { margin: 10px 0; padding: 10px; background: #f8f9fa; border-radius: 4px; }
.footer { margin-top: 30px; text-align: center; color: #666; }
</style>
</head>
<body>
<div class="header">
<h1>智慧畜牧业小程序矩阵 - 测试报告</h1>
<p>测试时间: $test_time</p>
</div>
<div class="section success">
<h2>✓ 测试概要</h2>
<p>本次测试包含代码格式检查、质量检查、单元测试、安全检查等多个方面</p>
</div>
<div class="section">
<h2>测试项目</h2>
<div class="test-item">代码格式检查 - Prettier</div>
<div class="test-item">代码质量检查 - ESLint</div>
<div class="test-item">单元测试 - Jest</div>
<div class="test-item">依赖安全检查 - npm audit</div>
<div class="test-item">包大小检查</div>
<div class="test-item">兼容性测试</div>
</div>
<div class="footer">
<p>© 2024 智慧畜牧业小程序矩阵</p>
</div>
</body>
</html>
EOF
log_success "测试报告已生成: $report_file"
}
# 清理测试文件
cleanup_test_files() {
log_info "清理测试文件..."
cd mini_program
# 清理构建文件
rm -rf dist/
rm -rf unpackage/
rm -rf coverage/
log_success "测试文件清理完成"
cd ..
}
# 显示帮助信息
show_help() {
echo "智慧畜牧业小程序矩阵测试脚本"
echo ""
echo "用法:"
echo " $0 [选项]"
echo ""
echo "选项:"
echo " -h, --help 显示帮助信息"
echo " -f, --format 只运行代码格式检查"
echo " -q, --quality 只运行代码质量检查"
echo " -u, --unit 只运行单元测试"
echo " -s, --security 只运行安全检查"
echo " -p, --performance 只运行性能测试"
echo " -c, --compatibility 只运行兼容性测试"
echo " -r, --report 生成测试报告"
echo " --clean 清理测试文件"
echo ""
echo "示例:"
echo " $0 # 运行所有测试"
echo " $0 --format # 只检查代码格式"
echo " $0 --unit # 只运行单元测试"
echo " $0 --report # 生成测试报告"
}
# 主函数
main() {
local format_only=false
local quality_only=false
local unit_only=false
local security_only=false
local performance_only=false
local compatibility_only=false
local report_only=false
local clean_only=false
local run_all=true
# 解析参数
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-f|--format)
format_only=true
run_all=false
shift
;;
-q|--quality)
quality_only=true
run_all=false
shift
;;
-u|--unit)
unit_only=true
run_all=false
shift
;;
-s|--security)
security_only=true
run_all=false
shift
;;
-p|--performance)
performance_only=true
run_all=false
shift
;;
-c|--compatibility)
compatibility_only=true
run_all=false
shift
;;
-r|--report)
report_only=true
run_all=false
shift
;;
--clean)
clean_only=true
run_all=false
shift
;;
*)
log_error "未知参数: $1"
show_help
exit 1
;;
esac
done
# 检查环境
check_environment
# 执行相应操作
if [ "$clean_only" = true ]; then
cleanup_test_files
exit 0
fi
if [ "$report_only" = true ]; then
generate_test_report
exit 0
fi
# 安装测试依赖
install_test_dependencies
log_info "开始运行测试..."
local test_passed=true
# 运行指定的测试或所有测试
if [ "$format_only" = true ] || [ "$run_all" = true ]; then
check_code_format || test_passed=false
fi
if [ "$quality_only" = true ] || [ "$run_all" = true ]; then
check_code_quality || test_passed=false
fi
if [ "$unit_only" = true ] || [ "$run_all" = true ]; then
run_unit_tests || test_passed=false
fi
if [ "$security_only" = true ] || [ "$run_all" = true ]; then
check_security || test_passed=false
fi
if [ "$performance_only" = true ] || [ "$run_all" = true ]; then
run_performance_tests || test_passed=false
fi
if [ "$compatibility_only" = true ] || [ "$run_all" = true ]; then
run_compatibility_tests || test_passed=false
fi
# 检查包大小
if [ "$run_all" = true ]; then
check_bundle_size || test_passed=false
fi
# 生成测试报告
if [ "$run_all" = true ]; then
generate_test_report
fi
# 清理测试文件
cleanup_test_files
# 输出测试结果
if [ "$test_passed" = true ]; then
log_success "所有测试通过!"
exit 0
else
log_error "部分测试失败!"
exit 1
fi
}
# 错误处理
error_handler() {
local line_number=$1
log_error "脚本在第 $line_number 行出错"
exit 1
}
# 设置错误处理
trap 'error_handler $LINENO' ERR
# 执行主函数
main "$@"