Skip to content

Commit 7d5da66

Browse files
committed
feat(ci): 添加本地 CI 模拟脚本
- 新增 scripts/ci-local.sh * 模拟 CI 的所有检查步骤 * 提供友好的彩色输出 * 在本地验证代码是否能通过 CI - 更新 docs/ci_workflow.md * 添加本地测试指南 * 说明快速检查和完整 CI 模拟的区别
1 parent e6d48f7 commit 7d5da66

2 files changed

Lines changed: 148 additions & 1 deletion

File tree

docs/ci_workflow.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,30 @@ graph TB
118118

119119
在提交 PR 前,建议先在本地运行:
120120

121+
### 快速检查(推荐)
122+
121123
```bash
122-
# 完整检查(包括 format, lint, type-check, test)
124+
# 运行所有代码质量检查(format, lint, type-check, test)
123125
bash scripts/check.sh
126+
```
127+
128+
### 完整 CI 模拟
124129

130+
```bash
131+
# 模拟 CI 的所有步骤(包括构建检查)
132+
bash scripts/ci-local.sh
133+
```
134+
135+
这个脚本会:
136+
1. ✅ 检查代码格式(Ruff format)
137+
2. ✅ 运行 Linter(Ruff check)
138+
3. ✅ 类型检查(MyPy)
139+
4. ✅ 运行测试并生成报告
140+
5. ✅ 构建包并验证
141+
142+
### 仅运行测试
143+
144+
```bash
125145
# 仅运行测试并生成报告
126146
pytest --junitxml=pytest-report.xml --cov=deep_solutions --cov-report=xml --cov-report=term-missing
127147
```

scripts/ci-local.sh

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/bin/bash
2+
# =============================================================================
3+
# CI 本地模拟脚本
4+
# 用途: 在提交 PR 前模拟 CI 的所有检查步骤
5+
# =============================================================================
6+
7+
set -e # 遇到错误立即退出
8+
9+
# 颜色定义
10+
RED='\033[0;31m'
11+
GREEN='\033[0;32m'
12+
YELLOW='\033[1;33m'
13+
BLUE='\033[0;34m'
14+
NC='\033[0m' # No Color
15+
16+
print_header() {
17+
echo ""
18+
echo -e "${BLUE}========================================${NC}"
19+
echo -e "${BLUE} $1${NC}"
20+
echo -e "${BLUE}========================================${NC}"
21+
echo ""
22+
}
23+
24+
print_success() {
25+
echo -e "${GREEN}$1${NC}"
26+
}
27+
28+
print_error() {
29+
echo -e "${RED}$1${NC}"
30+
}
31+
32+
print_warning() {
33+
echo -e "${YELLOW}$1${NC}"
34+
}
35+
36+
# =============================================================================
37+
# Step 1: Lint & Format Check
38+
# =============================================================================
39+
print_header "Step 1/4: Lint & Format Check"
40+
41+
echo "检查代码格式..."
42+
if ruff format --check src/ tests/; then
43+
print_success "代码格式检查通过"
44+
else
45+
print_error "代码格式检查失败"
46+
echo ""
47+
echo "运行以下命令修复:"
48+
echo " ruff format src/ tests/"
49+
exit 1
50+
fi
51+
52+
echo ""
53+
echo "运行 Linter..."
54+
if ruff check src/ tests/; then
55+
print_success "Lint 检查通过"
56+
else
57+
print_error "Lint 检查失败"
58+
echo ""
59+
echo "尝试自动修复:"
60+
echo " ruff check --fix src/ tests/"
61+
exit 1
62+
fi
63+
64+
# =============================================================================
65+
# Step 2: Type Check
66+
# =============================================================================
67+
print_header "Step 2/4: Type Check"
68+
69+
echo "运行 MyPy 类型检查..."
70+
if mypy src/; then
71+
print_success "类型检查通过"
72+
else
73+
print_error "类型检查失败"
74+
exit 1
75+
fi
76+
77+
# =============================================================================
78+
# Step 3: Run Tests
79+
# =============================================================================
80+
print_header "Step 3/4: Run Tests"
81+
82+
echo "运行测试并生成报告..."
83+
if pytest --junitxml=pytest-report.xml --cov=deep_solutions --cov-report=xml --cov-report=term-missing; then
84+
print_success "所有测试通过"
85+
else
86+
print_error "测试失败"
87+
exit 1
88+
fi
89+
90+
# =============================================================================
91+
# Step 4: Build Check
92+
# =============================================================================
93+
print_header "Step 4/4: Build Check"
94+
95+
echo "清理旧的构建产物..."
96+
rm -rf dist/ build/ *.egg-info
97+
98+
echo "构建包..."
99+
if python -m build; then
100+
print_success "包构建成功"
101+
else
102+
print_error "包构建失败"
103+
exit 1
104+
fi
105+
106+
echo ""
107+
echo "检查包..."
108+
if twine check dist/*; then
109+
print_success "包检查通过"
110+
else
111+
print_error "包检查失败"
112+
exit 1
113+
fi
114+
115+
# =============================================================================
116+
# Summary
117+
# =============================================================================
118+
print_header "总结"
119+
120+
echo -e "${GREEN}✓ 所有 CI 检查通过!${NC}"
121+
echo ""
122+
echo "生成的文件:"
123+
echo " - pytest-report.xml (测试报告)"
124+
echo " - coverage.xml (覆盖率报告)"
125+
echo " - dist/ (构建产物)"
126+
echo ""
127+
print_success "代码已准备好创建 Pull Request"

0 commit comments

Comments
 (0)