🗑️ remove(legacy): drop old scripts and tests

This commit is contained in:
csh
2026-01-23 14:58:31 +08:00
parent 0c4cd0e037
commit b4f712acb4
23 changed files with 158 additions and 5516 deletions
+20 -438
View File
@@ -1,18 +1,14 @@
# 🧪 Playbook 测试套件
本目录包含 Playbook 项目的完整测试框架,用于验证脚本、模板文档的正确性
本目录包含 Playbook 项目的测试,用于验证 CLI、模板文档链接
## 📋 目录结构
```txt
tests/
├── README.md # 本文件:测试文档
├── scripts/ # Shell 脚本测试(bats
── test_sync_standards.bats # sync_standards.sh 测试
│ ├── test_sync_templates.bats # sync_templates.sh 测试
│ ├── test_vendor_playbook.bats # vendor_playbook.sh 测试
│ ├── test_install_codex_skills.bats # install_codex_skills.sh 测试
│ └── test_windows_script_lints.bats # Windows 脚本 lint 测试
├── cli/ # Python CLI 测试(unittest
── test_playbook_cli.py # playbook.py 基础功能测试
├── templates/ # 模板验证测试
│ ├── validate_python_templates.sh # Python 模板验证
│ ├── validate_cpp_templates.sh # C++ 模板验证
@@ -24,33 +20,24 @@ tests/
## 🚀 快速开始
### 本地运行所有测试
```bash
# 进入 playbook 根目录
cd /path/to/playbook
# 1. 运行 Shell 脚本测试(需要 bats
sudo apt-get install bats # Ubuntu/Debian
cd tests/scripts
bats test_sync_standards.bats
bats test_sync_templates.bats
bats test_vendor_playbook.bats
bats test_install_codex_skills.bats
# 1. 运行 Python CLI 测试
python -m unittest discover -s tests/cli -v
# 2. 运行模板验证测试
cd tests/templates
sh validate_python_templates.sh
sh validate_cpp_templates.sh
sh validate_ci_templates.sh
sh validate_project_templates.sh
sh tests/templates/validate_python_templates.sh
sh tests/templates/validate_cpp_templates.sh
sh tests/templates/validate_ci_templates.sh
sh tests/templates/validate_project_templates.sh
# 3. 运行集成测试
cd tests/integration
sh check_doc_links.sh
# 3. 运行文档链接检查
sh tests/integration/check_doc_links.sh
```
### CI 自动化测试
## 🧭 CI 自动化测试
测试套件通过 Gitea Actions 自动运行(见 `.gitea/workflows/test.yml`):
@@ -59,426 +46,21 @@ sh check_doc_links.sh
- Pull Request 到 `main` 分支
- 手动触发(workflow_dispatch
- **运行平台**ubuntu-22.04
- **并行策略**:使用 matrix 策略并行运行多个测试组
## 📚 测试详解
### 1. Shell 脚本测试 (scripts/)
### 1. Python CLI 测试 (cli/)
使用 [bats-core](https://github.com/bats-core/bats-core) 框架测试 shell 脚本。
使用 `unittest` 运行,覆盖 `scripts/playbook.py` 的核心行为:
#### test_sync_standards.bats
测试 `scripts/sync_standards.sh` 脚本的功能:
- **基础功能**
- 脚本存在且可执行
- 单语言同步(`-langs tsl`/`-langs cpp`
- 多语言同步(`-langs tsl,cpp`
- **.gitattributes 同步**
- 默认模式追加缺失规则
- 保留现有内容
- 更新已存在的 managed block
- **AGENTS.md 处理**
- 不存在时自动创建
- 已存在时不覆盖
- **备份功能**
- 更新前创建备份
- **错误处理**
- 未找到 playbook 快照时报错
- 无效语言参数时报错
- **环境变量**
- `SYNC_GITATTR_MODE` 配置
- **幂等性**
- 多次执行结果一致
#### test_sync_templates.bats
测试 `scripts/sync_templates.sh` 脚本的功能:
- **基础同步**
- 同步 memory-bank/ 与 docs/prompts/
- 创建 AGENTS.md / AGENT_RULES.md
- **占位符替换**
- `{{PROJECT_NAME}}``{{DATE}}`
- **目录覆盖策略**
-`-force` 时不覆盖已有目录
- `-force` 时覆盖并备份
- **AGENTS.md 更新**
- `-full` 更新 framework 区块
#### test_vendor_playbook.bats
测试 `scripts/vendor_playbook.sh` 脚本的功能:
- **基础功能**
- 单语言 vendoring`-langs tsl`
- 多语言 vendoring`-langs tsl,cpp`
- **自动同步**
- 自动执行 sync_standards
- **SOURCE.md 生成**
- 包含来源信息
- 包含 commit hash
- 包含时间戳
- **裁剪功能**
- 仅包含指定语言
- 始终包含 common 目录
- 包含对应模板文件
- **目标目录处理**
- 已存在时覆盖更新
- 创建必要的父目录
- **错误处理**
- 目标目录不存在时报错
- 无效语言参数时报错
- **完整性验证**
- 所有必要文件已复制
- 脚本可执行
- **幂等性**
- 多次 vendor 结果一致
#### test_install_codex_skills.bats
测试 `scripts/install_codex_skills.sh` 脚本的功能:
- **基础功能**
- 脚本存在且可执行
- **安装功能**
- 创建 skills 目录
- 复制 skill 目录(包含 `SKILL.md`
- 支持 `-skills` 指定单个 skill 安装
- 支持 `-all` 安装全部 skills
- 同名目录安装前创建备份
- **错误处理**
- 指定不存在的 skill 报错
- 未传 `-all/-skills` 报错
- **幂等性**
- 多次安装结果一致
#### test_windows_script_lints.bats
测试 Windows 脚本的基础 lint 规则:
- **PowerShell**
- here-string 终止符不与管道同一行
- Help 参数别名不与参数名冲突
- **Batch**
- `:show_help` 标签不应阻断参数解析
- CLI 参数解析与帮助信息
- TOML 配置解析与动作顺序
- vendor/sync/install 等基础动作落地
### 2. 模板验证测试 (templates/)
验证项目模板文件的正确性和完整性
通过脚本检查各类模板文件结构与关键字段(如占位符、关键配置项)
#### validate_python_templates.sh
### 3. 文档链接检查 (integration/)
验证 `templates/python/` 目录下的 Python 模板:
- **pyproject.toml**
- 文件存在
- TOML 语法正确
- 包含必要配置节(tool.black, tool.isort, tool.pytest.ini_options
- black line-length 配置正确
- isort profile 配置正确
- **.flake8**
- 文件存在
- 包含 [flake8] 配置
- 配置了 max-line-length
- 配置了错误忽略规则
- **.pylintrc**
- 文件存在
- 包含必要配置节(MASTER, MESSAGES CONTROL, FORMAT
- 配置了 max-line-length
- **.pre-commit-config.yaml**
- 文件存在
- YAML 语法正确
- 包含 repos 配置
- 配置了常用 hooksblack, isort, flake8
- **.editorconfig**
- 文件存在
- 包含 root = true
- 包含 Python 文件配置
- **.vscode/settings.json**
- 文件存在
- JSON 语法正确
- 包含 Python 配置
#### validate_cpp_templates.sh
验证 `templates/cpp/` 目录下的 C++ 模板:
- **CMakeLists.txt**
- 文件存在
- CMake 基础语法正确
- 配置了 C++23 标准
- 启用了 C++ Modules 扫描(可选)
- 启用了 import std; 支持(可选)
- 启用了编译命令导出(用于 clangd)
- **.clang-format**
- 文件存在
- YAML 语法正确
- 配置了 Language: Cpp
- 基于某个风格(BasedOnStyle
- 配置了 C++ 标准
- 配置了缩进宽度
- **.clangd**
- 文件存在
- YAML 语法正确
- 包含 CompileFlags 配置
- 配置了 -std=c++23
- 配置了 CompilationDatabase 路径
- 配置了 Index 选项
- **conanfile.txt**
- 文件存在
- 包含 [requires] 配置(可选)
- 包含 [generators] 配置
- 配置了 CMakeDeps 生成器
- 配置了 CMakeToolchain 生成器
- 包含 [options] 配置
- **CMakeUserPresets.json**
- 文件存在
- JSON 语法正确
- 包含 version 字段
- 包含 configurePresets
- 包含 buildPresets
#### validate_ci_templates.sh
验证 `templates/ci/` 目录下的 CI 模板:
- **Gitea/GitHub workflow 文件**
- 文件存在
- YAML 语法正确
- 包含必要字段(name, on, jobs, runs-on, steps
- 包含中文注释(符合项目风格)
- 包含配置区域标记
- 包含环境变量配置
- **特定 workflow 模板**
- standards-check.yml:包含格式化检查和 lint 检查
- test.yml:包含测试步骤和测试矩阵
- **文档**
- 包含 README 说明文档
- 包含使用说明
#### validate_project_templates.sh
验证项目通用模板:
- **核心模板**
- `templates/AGENTS.template.md`
- `templates/AGENT_RULES.template.md`
- `templates/README.md`
- **memory-bank 模板**
- 项目定位/技术栈/架构/进度/决策/实施计划
- **prompts 模板**
- `prompts/README.md`
- `prompts/system/agent-behavior.template.md`
- `prompts/coding/clarify.template.md`
- `prompts/coding/verify.template.md`
### 3. 集成测试 (integration/)
端到端测试,验证整体功能。
#### check_doc_links.sh
检查所有 Markdown 文档中的链接有效性:
- **扫描范围**
- 所有 `*.md` 文件(排除 `*.template.md`
- 排除 node_modules, .git, build, dist 等目录
- **链接类型**
- Markdown 链接:`[text](link)`
- 引用链接:`[text]: link`
- **验证逻辑**
- 检查相对路径链接是否指向存在的文件
- 跳过外部链接(http/https
- 跳过 mailto 链接
- 跳过代码块与行内代码中的链接样式文本
- 支持锚点(但只验证文件存在性)
- **报告内容**
- 总链接数
- 有效链接数
- 跳过链接数(外部/mailto
- 断开链接详情(文件、行号、链接、目标路径)
## 🔧 本地开发
### 安装测试依赖
**Ubuntu/Debian**
```bash
# bats-core (Shell 脚本测试)
sudo apt-get update
sudo apt-get install bats
# Python 工具(模板验证)
pip install toml tomli jsonschema yamllint
# C++ 工具(模板验证,可选)
sudo apt-get install cmake clang-format
# YAML 验证
pip install yamllint
```
**macOS**
```bash
# bats-core
brew install bats-core
# Python 工具
pip3 install toml tomli jsonschema yamllint
# C++ 工具(可选)
brew install cmake clang-format
# YAML 验证
pip3 install yamllint
```
### 运行单个测试
```bash
# Shell 脚本测试
cd tests/scripts
bats test_sync_standards.bats --tap # TAP 格式输出
bats test_sync_templates.bats
bats test_vendor_playbook.bats --formatter junit # JUnit 格式
# 模板验证测试
cd tests/templates
sh validate_python_templates.sh
sh validate_cpp_templates.sh
sh validate_ci_templates.sh
sh validate_project_templates.sh
# 集成测试
cd tests/integration
sh check_doc_links.sh
```
### 添加新测试
#### 添加新的 bats 测试
`tests/scripts/` 创建新文件 `test_<script_name>.bats`
```bash
#!/usr/bin/env bats
setup() {
# 测试前准备
export TEST_DIR="$(mktemp -d)"
}
teardown() {
# 测试后清理
rm -rf "$TEST_DIR"
}
@test "描述测试内容" {
# 测试代码
[ -f "some_file" ]
}
```
#### 添加新的模板验证
`tests/templates/` 创建新文件 `validate_<template_type>_templates.sh`,参考现有脚本结构。
#### 添加新的集成测试
`tests/integration/` 创建新脚本,确保:
1. 使用 `set -eu` 启用错误检测
2. 输出清晰的测试进度
3. 生成详细的报告文件
4. 返回正确的退出码(0 = 成功,非 0 = 失败)
## 📊 测试覆盖率目标
- **Shell 脚本测试**:目标覆盖率 ≥ 80%
- **模板验证测试**:目标通过率 = 100%
- **集成测试**:目标通过率 = 100%
- **文档链接有效性**:目标有效率 = 100%
## 🐛 故障排查
### bats 测试失败
```bash
# 使用 --verbose 查看详细输出
bats --verbose test_sync_standards.bats
# 使用 --trace 查看执行跟踪
bats --trace test_sync_standards.bats
```
### 模板验证失败
验证脚本会生成详细报告文件:
- `tests/templates/python_validation_report.txt`
- `tests/templates/cpp_validation_report.txt`
- `tests/templates/ci_validation_report.txt`
### 文档链接检查失败
查看详细报告:
```bash
cat /tmp/doc_links_report.txt
```
## 🤝 贡献指南
添加新功能时,请同步更新相应的测试:
1. **修改脚本**`scripts/`)→ 更新对应的 `.bats` 测试
2. **修改模板**`templates/`)→ 更新对应的验证脚本
3. **修改文档**`docs/`, `.agents/`)→ 运行文档链接检查
4. **修改 CI workflow**`.gitea/workflows/`)→ 验证 YAML 语法
## 📖 相关文档
- [开发规范索引](../docs/index.md)
- [提交信息规范](../docs/common/commit_message.md)
- [Gitea Actions 文档](https://docs.gitea.com/usage/actions/overview)
- [bats-core 文档](https://bats-core.readthedocs.io/)
## ❓ 常见问题
### Q: 为什么测试在 CI 通过,但本地失败?
A: 可能原因:
- 环境差异(工具版本、路径)
- 权限问题
- Git 配置差异
建议使用 Docker 容器复现 CI 环境。
### Q: 如何跳过某些测试?
A: bats 支持 `skip` 命令:
```bash
@test "某个测试" {
skip "原因说明"
# 测试代码
}
```
### Q: 测试运行很慢,如何加速?
A: 建议:
1. 使用 bats 的 `--jobs` 参数并行运行
2. 只运行变更相关的测试
3. 使用 CI 的缓存机制
---
**测试套件维护者**: Playbook 团队
**最后更新**: 2026-01-07
扫描 `docs/` 与模板文件中的本地链接,确保引用路径有效。
@@ -1,116 +0,0 @@
#!/usr/bin/env bats
# install_codex_skills.sh 测试套件
setup() {
export TEST_DIR="$(mktemp -d)"
export PLAYBOOK_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)"
export SCRIPT_PATH="$PLAYBOOK_ROOT/scripts/install_codex_skills.sh"
# 创建临时 HOME 避免污染真实环境
export ORIGINAL_HOME="$HOME"
export HOME="$TEST_DIR/home"
mkdir -p "$HOME"
export CODEX_HOME="$HOME/.codex"
export SKILLS_DST_ROOT="$CODEX_HOME/skills"
cd "$TEST_DIR"
}
teardown() {
export HOME="$ORIGINAL_HOME"
if [ -n "$TEST_DIR" ] && [ -d "$TEST_DIR" ]; then
chmod -R u+w "$TEST_DIR" 2>/dev/null || true
rm -rf "$TEST_DIR"
fi
}
skip_if_no_skills() {
if [ ! -d "$PLAYBOOK_ROOT/codex/skills" ]; then
skip "No codex/skills directory found"
fi
}
first_skill_name() {
ls -1 "$PLAYBOOK_ROOT/codex/skills" 2>/dev/null | head -n 1
}
# ==============================================
# 基础功能测试
# ==============================================
@test "install_codex_skills.sh 脚本存在且可执行" {
[ -f "$SCRIPT_PATH" ]
}
@test "安装 - 创建 skills 目录" {
skip_if_no_skills
[ ! -d "$SKILLS_DST_ROOT" ]
sh "$SCRIPT_PATH" -all
[ -d "$SKILLS_DST_ROOT" ]
}
@test "安装 - 复制 skill 目录" {
skip_if_no_skills
sh "$SCRIPT_PATH" -all
SKILL_DIRS=$(find "$SKILLS_DST_ROOT" -mindepth 1 -maxdepth 1 -type d)
[ -n "$SKILL_DIRS" ]
for dir in $SKILL_DIRS; do
[ -f "$dir/SKILL.md" ]
done
}
@test "安装 - 指定单个 skill" {
skip_if_no_skills
SKILL_NAME="$(first_skill_name)"
[ -n "$SKILL_NAME" ]
sh "$SCRIPT_PATH" -skills "$SKILL_NAME"
[ -d "$SKILLS_DST_ROOT/$SKILL_NAME" ]
COUNT=$(find "$SKILLS_DST_ROOT" -mindepth 1 -maxdepth 1 -type d | wc -l)
[ "$COUNT" -eq 1 ]
}
@test "备份 - 同名 skill 目录会创建备份" {
skip_if_no_skills
SKILL_NAME="$(first_skill_name)"
[ -n "$SKILL_NAME" ]
mkdir -p "$SKILLS_DST_ROOT/$SKILL_NAME"
echo "marker" > "$SKILLS_DST_ROOT/$SKILL_NAME/marker.txt"
sh "$SCRIPT_PATH" -skills "$SKILL_NAME"
BACKUP_DIRS=$(find "$SKILLS_DST_ROOT" -maxdepth 1 -type d -name "$SKILL_NAME.bak.*")
[ -n "$BACKUP_DIRS" ]
}
@test "错误处理 - 指定不存在 skill 报错" {
skip_if_no_skills
run sh "$SCRIPT_PATH" -skills nonexistent-skill
[ "$status" -ne 0 ]
}
@test "幂等性 - 多次安装结果一致" {
skip_if_no_skills
sh "$SCRIPT_PATH" -all
CHECKSUM1=$(find "$SKILLS_DST_ROOT" -type f -name "SKILL.md" ! -path "$SKILLS_DST_ROOT"'/*.bak.*/*' -exec md5sum {} \; | sort | md5sum)
sh "$SCRIPT_PATH" -all
CHECKSUM2=$(find "$SKILLS_DST_ROOT" -type f -name "SKILL.md" ! -path "$SKILLS_DST_ROOT"'/*.bak.*/*' -exec md5sum {} \; | sort | md5sum)
[ "$CHECKSUM1" = "$CHECKSUM2" ]
}
-274
View File
@@ -1,274 +0,0 @@
#!/usr/bin/env bats
# sync_standards.sh 测试套件
# 测试辅助函数
setup() {
# 创建临时测试目录
export TEST_DIR="$(mktemp -d)"
export PLAYBOOK_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)"
# 初始化测试项目
cd "$TEST_DIR"
git init -b main
git config user.name "Test User"
git config user.email "test@example.com"
# 模拟 playbook 快照目录
mkdir -p docs/standards/playbook
cp -r "$PLAYBOOK_ROOT"/{rulesets,.gitattributes,docs,scripts} docs/standards/playbook/ 2>/dev/null || true
export SCRIPT_PATH="$TEST_DIR/docs/standards/playbook/scripts/sync_standards.sh"
}
teardown() {
# 清理测试目录
if [ -n "$TEST_DIR" ] && [ -d "$TEST_DIR" ]; then
chmod -R u+rwX "$TEST_DIR" 2>/dev/null || true
rm -rf "$TEST_DIR"
fi
}
# ==============================================
# 基础功能测试
# ==============================================
@test "sync_standards.sh 脚本存在且可执行" {
[ -f "$SCRIPT_PATH" ]
}
@test "sync_standards.sh - 未传 -langs 报错" {
cd "$TEST_DIR"
run sh "$SCRIPT_PATH"
[ "$status" -ne 0 ]
}
@test "sync_standards.sh tsl - 同步 TSL 规则集" {
cd "$TEST_DIR"
sh "$SCRIPT_PATH" -langs tsl
# 验证必须文件
[ -d ".agents/tsl" ]
[ -f ".agents/tsl/index.md" ]
[ ! -f ".agents/tsl/auth.md" ]
[ ! -f ".agents/tsl/code_quality.md" ]
[ ! -f ".agents/tsl/performance.md" ]
[ ! -f ".agents/tsl/testing.md" ]
}
@test "sync_standards.sh cpp - 同步 C++ 规则集" {
cd "$TEST_DIR"
sh "$SCRIPT_PATH" -langs cpp
# 验证必须文件
[ -d ".agents/cpp" ]
[ -f ".agents/cpp/index.md" ]
[ ! -f ".agents/cpp/auth.md" ]
[ ! -f ".agents/cpp/code_quality.md" ]
[ ! -f ".agents/cpp/performance.md" ]
[ ! -f ".agents/cpp/testing.md" ]
}
@test "sync_standards.sh tsl cpp - 同步多个规则集" {
cd "$TEST_DIR"
sh "$SCRIPT_PATH" -langs tsl,cpp
# 验证两个规则集都存在
[ -d ".agents/tsl" ]
[ -d ".agents/cpp" ]
[ -f ".agents/index.md" ]
}
# ==============================================
# .gitattributes 同步测试
# ==============================================
@test ".gitattributes - 默认模式追加缺失规则" {
cd "$TEST_DIR"
[ ! -f ".gitattributes" ]
sh "$SCRIPT_PATH" -langs tsl
[ -f ".gitattributes" ]
grep -q "Added from playbook .gitattributes" .gitattributes
grep -q "\\*.tsl" .gitattributes
}
@test ".gitattributes - 保留现有内容" {
cd "$TEST_DIR"
echo "# My custom rules" > .gitattributes
echo "*.custom binary" >> .gitattributes
sh "$SCRIPT_PATH" -langs tsl
grep -q "# My custom rules" .gitattributes
grep -q "*.custom binary" .gitattributes
}
@test ".gitattributes - 更新已存在的 managed block" {
cd "$TEST_DIR"
cat > .gitattributes << 'EOF'
# BEGIN playbook .gitattributes
# Old content
# END playbook .gitattributes
EOF
export SYNC_GITATTR_MODE=block
sh "$SCRIPT_PATH" -langs tsl
# 验证 block 已更新(不再包含 "Old content"
! grep -q "Old content" .gitattributes
}
# ==============================================
# AGENTS.md 自动生成测试
# ==============================================
@test "AGENTS.md - 不存在时自动创建" {
cd "$TEST_DIR"
[ ! -f "AGENTS.md" ]
sh "$SCRIPT_PATH" -langs tsl
[ -f "AGENTS.md" ]
grep -q ".agents/" AGENTS.md
}
@test "AGENTS.md - 已存在时不覆盖" {
cd "$TEST_DIR"
echo "# My custom AGENTS.md" > AGENTS.md
sh "$SCRIPT_PATH" -langs tsl
grep -q "# My custom AGENTS.md" AGENTS.md
}
# ==============================================
# 备份功能测试
# ==============================================
@test "备份 - .gitattributes 更新前创建备份" {
cd "$TEST_DIR"
echo "# Original content" > .gitattributes
sh "$SCRIPT_PATH" -langs tsl
# 验证备份文件存在
[ -f ".gitattributes.bak."* ] || [ -f ".gitattributes.bak" ]
}
@test "备份 - rulesets/ 更新前创建备份" {
cd "$TEST_DIR"
mkdir -p .agents/tsl
echo "# Old index" > .agents/tsl/index.md
sh "$SCRIPT_PATH" -langs tsl
# 验证备份目录存在
[ -d ".agents/tsl.bak."* ] || [ -d ".agents/tsl.bak" ]
}
# ==============================================
# 多语言项目测试
# ==============================================
@test "多语言 - TSL + C++ + Python 规则集共存" {
cd "$TEST_DIR"
# 复制 Python 规则集(如果存在)
if [ -d "$PLAYBOOK_ROOT/rulesets/python" ]; then
cp -r "$PLAYBOOK_ROOT/rulesets/python" docs/standards/playbook/rulesets/
fi
sh "$SCRIPT_PATH" -langs tsl,cpp
# 验证规则集不互相覆盖
[ -d ".agents/tsl" ]
[ -d ".agents/cpp" ]
# 验证索引文件正确引用
[ -f ".agents/index.md" ]
}
# ==============================================
# 错误处理测试
# ==============================================
@test "错误处理 - 未找到 playbook 快照时报错" {
cd "$TEST_DIR"
rm -rf docs/standards/playbook/rulesets
run sh "$SCRIPT_PATH" -langs tsl
[ "$status" -ne 0 ]
}
@test "错误处理 - 无效语言参数时报错" {
cd "$TEST_DIR"
run sh "$SCRIPT_PATH" -langs invalid_lang
[ "$status" -ne 0 ]
}
# ==============================================
# 环境变量配置测试
# ==============================================
@test "环境变量 - SYNC_GITATTR_MODE=skip 跳过 .gitattributes" {
cd "$TEST_DIR"
export SYNC_GITATTR_MODE=skip
sh "$SCRIPT_PATH" -langs tsl
[ ! -f ".gitattributes" ]
}
@test "环境变量 - SYNC_GITATTR_MODE=overwrite 覆盖 .gitattributes" {
cd "$TEST_DIR"
echo "# Custom content" > .gitattributes
export SYNC_GITATTR_MODE=overwrite
sh "$SCRIPT_PATH" -langs tsl
# 验证自定义内容被覆盖
! grep -q "# Custom content" .gitattributes
}
@test "环境变量 - SYNC_GITATTR_MODE=append 追加缺失规则" {
cd "$TEST_DIR"
echo "# Custom rules only" > .gitattributes
export SYNC_GITATTR_MODE=append
sh "$SCRIPT_PATH" -langs tsl
grep -q "Added from playbook .gitattributes" .gitattributes
grep -q "\\*.tsl" .gitattributes
}
@test "环境变量 - SYNC_GITATTR_MODE=append 无缺失规则不追加" {
cd "$TEST_DIR"
cp "$PLAYBOOK_ROOT/.gitattributes" .gitattributes
export SYNC_GITATTR_MODE=append
sh "$SCRIPT_PATH" -langs tsl
! grep -q "Added from playbook .gitattributes" .gitattributes
}
# ==============================================
# 幂等性测试
# ==============================================
@test "幂等性 - 多次执行结果一致" {
cd "$TEST_DIR"
# 第一次同步
sh "$SCRIPT_PATH" -langs tsl
CHECKSUM1=$(find rulesets/tsl -type f -exec md5sum {} \; | sort | md5sum)
# 第二次同步
sh "$SCRIPT_PATH" -langs tsl
CHECKSUM2=$(find rulesets/tsl -type f -exec md5sum {} \; | sort | md5sum)
[ "$CHECKSUM1" = "$CHECKSUM2" ]
}
-84
View File
@@ -1,84 +0,0 @@
#!/usr/bin/env bats
# sync_templates.sh 测试套件
setup() {
export PLAYBOOK_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)"
export SCRIPT_PATH="$PLAYBOOK_ROOT/scripts/sync_templates.sh"
export TARGET_DIR="$(mktemp -d)"
}
teardown() {
if [ -n "$TARGET_DIR" ] && [ -d "$TARGET_DIR" ]; then
chmod -R u+w "$TARGET_DIR" 2>/dev/null || true
rm -rf "$TARGET_DIR"
fi
}
# ==============================================
# 基础功能测试
# ==============================================
@test "sync_templates.sh 脚本存在且可执行" {
[ -f "$SCRIPT_PATH" ]
}
@test "sync_templates.sh - 基础同步与占位符替换" {
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -project-name "DemoProject" -date "2026-02-03"
[ -d "$TARGET_DIR/memory-bank" ]
[ -f "$TARGET_DIR/memory-bank/project-brief.md" ]
[ -f "$TARGET_DIR/docs/prompts/coding/clarify.md" ]
[ -f "$TARGET_DIR/AGENTS.md" ]
[ -f "$TARGET_DIR/AGENT_RULES.md" ]
grep -q "DemoProject" "$TARGET_DIR/memory-bank/project-brief.md"
! grep -q "{{DATE}}" "$TARGET_DIR/AGENT_RULES.md"
[ -z "$(find "$TARGET_DIR" -name '*.template.md' -print -quit)" ]
}
@test "sync_templates.sh - 已存在目录不覆盖 (无 -force)" {
mkdir -p "$TARGET_DIR/memory-bank"
mkdir -p "$TARGET_DIR/docs/prompts"
echo "keep" > "$TARGET_DIR/memory-bank/keep.md"
echo "keep" > "$TARGET_DIR/docs/prompts/keep.md"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR"
[ -f "$TARGET_DIR/memory-bank/keep.md" ]
[ ! -f "$TARGET_DIR/memory-bank/project-brief.md" ]
[ -f "$TARGET_DIR/docs/prompts/keep.md" ]
[ ! -f "$TARGET_DIR/docs/prompts/README.md" ]
}
@test "sync_templates.sh - -force 覆盖并备份" {
mkdir -p "$TARGET_DIR/memory-bank"
echo "marker" > "$TARGET_DIR/memory-bank/marker.txt"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -force
[ -f "$TARGET_DIR/memory-bank/project-brief.md" ]
[ ! -f "$TARGET_DIR/memory-bank/marker.txt" ]
backup_dir="$(ls -d "$TARGET_DIR"/memory-bank.bak.* 2>/dev/null | head -n 1)"
[ -n "$backup_dir" ]
[ -f "$backup_dir/marker.txt" ]
}
@test "sync_templates.sh - -full 更新 framework 区块" {
cat > "$TARGET_DIR/AGENTS.md" << 'EOF'
# Agent Instructions
<!-- playbook:framework:start -->
OLD_FRAMEWORK
<!-- playbook:framework:end -->
Footer
EOF
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -full
! grep -q "OLD_FRAMEWORK" "$TARGET_DIR/AGENTS.md"
grep -q "<!-- playbook:framework:start -->" "$TARGET_DIR/AGENTS.md"
grep -q "Footer" "$TARGET_DIR/AGENTS.md"
}
-301
View File
@@ -1,301 +0,0 @@
#!/usr/bin/env bats
# vendor_playbook.sh 测试套件
# 测试辅助函数
setup() {
# 创建临时测试目录
export TEST_DIR="$(mktemp -d)"
export PLAYBOOK_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)"
export SCRIPT_PATH="$PLAYBOOK_ROOT/scripts/vendor_playbook.sh"
# 创建目标项目目录
export TARGET_DIR="$(mktemp -d)"
cd "$TARGET_DIR"
git init -b main
git config user.name "Test User"
git config user.email "test@example.com"
}
teardown() {
# 清理测试目录
if [ -n "$TEST_DIR" ] && [ -d "$TEST_DIR" ]; then
chmod -R u+w "$TEST_DIR" 2>/dev/null || true
rm -rf "$TEST_DIR"
fi
if [ -n "$TARGET_DIR" ] && [ -d "$TARGET_DIR" ]; then
chmod -R u+w "$TARGET_DIR" 2>/dev/null || true
rm -rf "$TARGET_DIR"
fi
}
# ==============================================
# 基础功能测试
# ==============================================
@test "vendor_playbook.sh 脚本存在且可执行" {
[ -f "$SCRIPT_PATH" ]
}
@test "vendor_playbook.sh - 无参数时显示用法" {
cd "$TARGET_DIR"
run sh "$SCRIPT_PATH"
[ "$status" -ne 0 ]
}
@test "vendor_playbook.sh - 单语言 vendoring (tsl)" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证快照目录结构
[ -d "docs/standards/playbook" ]
[ -d "docs/standards/playbook/docs/common" ]
[ -d "docs/standards/playbook/docs/tsl" ]
[ -d "docs/standards/playbook/rulesets/tsl" ]
[ -f "docs/standards/playbook/scripts/sync_standards.sh" ]
}
@test "vendor_playbook.sh - 多语言 vendoring (tsl cpp)" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl,cpp
# 验证包含两种语言的文档
[ -d "docs/standards/playbook/docs/tsl" ]
[ -d "docs/standards/playbook/docs/cpp" ]
[ -d "docs/standards/playbook/rulesets/tsl" ]
[ -d "docs/standards/playbook/rulesets/cpp" ]
}
# ==============================================
# 自动同步测试
# ==============================================
@test "vendor_playbook.sh - 自动执行 sync_standards" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证根目录已同步规则集
[ -d ".agents/tsl" ]
[ -f ".agents/tsl/index.md" ]
[ -f ".gitattributes" ]
}
@test "vendor_playbook.sh - 多语言自动同步" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl,cpp
# 验证两个规则集都已同步
[ -d ".agents/tsl" ]
[ -d ".agents/cpp" ]
[ -f ".agents/index.md" ]
}
# ==============================================
# SOURCE.md 生成测试
# ==============================================
@test "vendor_playbook.sh - 生成 SOURCE.md" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
[ -f "docs/standards/playbook/SOURCE.md" ]
grep -q "Source:" docs/standards/playbook/SOURCE.md
grep -q "Commit:" docs/standards/playbook/SOURCE.md
}
@test "vendor_playbook.sh - SOURCE.md 包含 commit hash" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证包含 commit hash40个十六进制字符)
grep -E "[0-9a-f]{40}" docs/standards/playbook/SOURCE.md
}
@test "vendor_playbook.sh - SOURCE.md 包含时间戳" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证包含日期格式 YYYY-MM-DD
grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2}" docs/standards/playbook/SOURCE.md
}
# ==============================================
# 裁剪功能测试
# ==============================================
@test "裁剪 - 仅包含指定语言的文档" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证包含 TSL 文档
[ -d "docs/standards/playbook/docs/tsl" ]
# 验证不包含其他语言(如果原本有 Python)
if [ -d "$PLAYBOOK_ROOT/docs/python" ]; then
[ ! -d "docs/standards/playbook/docs/python" ]
fi
}
@test "裁剪 - 始终包含 common 目录" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
[ -d "docs/standards/playbook/docs/common" ]
[ -f "docs/standards/playbook/docs/common/commit_message.md" ]
}
@test "裁剪 - 包含对应的模板文件" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs cpp
# 验证包含 C++ 模板
[ -d "docs/standards/playbook/templates/cpp" ]
# 验证不包含 Python 模板(如果指定了 cpp
if [ -d "$PLAYBOOK_ROOT/templates/python" ]; then
[ ! -d "docs/standards/playbook/templates/python" ]
fi
}
@test "裁剪 - 包含通用 CI 模板" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
[ -d "docs/standards/playbook/templates/ci" ]
}
@test "vendor_playbook.sh - -apply-templates 应用模板到项目根目录" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs cpp -apply-templates
if [ -f "$PLAYBOOK_ROOT/templates/cpp/.clang-format" ]; then
[ -f ".clang-format" ]
fi
if [ -f "$PLAYBOOK_ROOT/templates/ci/gitea/.gitea/workflows/standards-check.yml" ]; then
[ -f ".gitea/workflows/standards-check.yml" ]
fi
}
# ==============================================
# 目标目录处理测试
# ==============================================
@test "目标目录 - 已存在时覆盖更新" {
cd "$TARGET_DIR"
# 首次 vendor
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 在快照中添加标记文件
touch docs/standards/playbook/OLD_MARKER
# 再次 vendor
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证旧标记不存在(已被覆盖)
[ ! -f "docs/standards/playbook/OLD_MARKER" ]
}
@test "目标目录 - 创建必要的父目录" {
cd "$TARGET_DIR"
# 确保 docs/standards 不存在
[ ! -d "docs/standards" ]
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
[ -d "docs/standards/playbook" ]
}
# ==============================================
# 错误处理测试
# ==============================================
@test "错误处理 - 目标目录不存在时报错" {
cd "$TARGET_DIR"
missing_dir="$TEST_DIR/missing-project"
rm -rf "$missing_dir"
run sh "$SCRIPT_PATH" -project-root "$missing_dir" -langs tsl
[ "$status" -ne 0 ]
}
@test "错误处理 - 无效语言参数时报错" {
cd "$TARGET_DIR"
run sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs invalid_lang
[ "$status" -ne 0 ]
}
@test "错误处理 - 目标目录不是 git 仓库时警告" {
cd "$TARGET_DIR"
rm -rf .git
run sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 应该给出警告但不失败
[ "$status" -eq 0 ]
}
# ==============================================
# 完整性测试
# ==============================================
@test "完整性 - 验证所有必要文件已复制" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证关键文件
[ -f "docs/standards/playbook/README.md" ]
[ -f "docs/standards/playbook/docs/index.md" ]
[ -f "docs/standards/playbook/.gitattributes" ]
[ -f "docs/standards/playbook/scripts/sync_standards.sh" ]
[ -f "docs/standards/playbook/SOURCE.md" ]
}
@test "完整性 - 验证脚本可执行性" {
cd "$TARGET_DIR"
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
# 验证同步脚本可执行
run sh docs/standards/playbook/scripts/sync_standards.sh -langs tsl
[ "$status" -eq 0 ]
}
# ==============================================
# 多次执行测试
# ==============================================
@test "幂等性 - 多次 vendor 结果一致" {
cd "$TARGET_DIR"
# 第一次 vendor
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
CHECKSUM1=$(find docs/standards/playbook -type f -name "*.md" ! -name "SOURCE.md" -exec md5sum {} \; | sort | md5sum)
# 第二次 vendor
sh "$SCRIPT_PATH" -project-root "$TARGET_DIR" -langs tsl
CHECKSUM2=$(find docs/standards/playbook -type f -name "*.md" ! -name "SOURCE.md" -exec md5sum {} \; | sort | md5sum)
[ "$CHECKSUM1" = "$CHECKSUM2" ]
}
@@ -1,44 +0,0 @@
#!/usr/bin/env bats
# Windows script lint tests (PowerShell/Batch)
setup() {
export PLAYBOOK_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)"
}
@test "sync_standards.ps1 - here-string terminator not piped" {
run grep -nE "^[[:space:]]*['\\\"]@\\s*\\|" "$PLAYBOOK_ROOT/scripts/sync_standards.ps1"
[ "$status" -ne 0 ]
}
@test "sync_standards.ps1 - Help alias does not shadow parameter name" {
run grep -niE "Alias\\([^)]*['\"]help['\"]" "$PLAYBOOK_ROOT/scripts/sync_standards.ps1"
[ "$status" -ne 0 ]
}
@test "install_codex_skills.ps1 - Help alias does not shadow parameter name" {
run grep -niE "Alias\\([^)]*['\"]help['\"]" "$PLAYBOOK_ROOT/scripts/install_codex_skills.ps1"
[ "$status" -ne 0 ]
}
@test "sync_templates.ps1 - Help alias does not shadow parameter name" {
run grep -niE "Alias\\([^)]*['\"]help['\"]" "$PLAYBOOK_ROOT/scripts/sync_templates.ps1"
[ "$status" -ne 0 ]
}
@test "vendor_playbook.ps1 - Help alias does not shadow parameter name" {
run grep -niE "Alias\\([^)]*['\"]help['\"]" "$PLAYBOOK_ROOT/scripts/vendor_playbook.ps1"
[ "$status" -ne 0 ]
}
@test "sync_standards.bat - show_help label follows parse_args" {
local file="$PLAYBOOK_ROOT/scripts/sync_standards.bat"
local show_line
local parse_line
show_line=$(grep -n "^:show_help" "$file" | head -n 1 | cut -d: -f1)
parse_line=$(grep -n "^:parse_args" "$file" | head -n 1 | cut -d: -f1)
[ -n "$show_line" ]
[ -n "$parse_line" ]
[ "$show_line" -gt "$parse_line" ]
}