1
0

refactor: 重构外部模板系统,改为单文件模板库模式

主要变更:
- 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件
- 添加模板库 YAML 验证功能
- 为模板添加 base_dir 支持,正确解析相对路径资源
- 内联模板与外部模板同名时改为警告(内联优先)
- 移除模板缓存机制,直接使用模板库字典
- 更新所有相关测试以适配新的模板加载方式

此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
2026-03-05 13:26:29 +08:00
parent bd12fce14b
commit f1aae96a04
27 changed files with 2141 additions and 1988 deletions

View File

@@ -29,28 +29,6 @@ class TestCheckCmd:
assert result.returncode == 0
assert "验证" in result.stdout or "通过" in result.stdout
def test_check_invalid_yaml(self, temp_dir):
"""测试检查无效的 YAML"""
# 创建包含错误的 YAML
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [1, 1, 8, 1]
content: "Test"
font:
color: "red" # 无效颜色
"""
yaml_path = temp_dir / "invalid.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
# 应该有错误
assert result.returncode != 0 or "错误" in result.stdout
def test_check_with_warnings_only(self, temp_dir):
"""测试只有警告的 YAML验证通过但有警告"""
@@ -88,7 +66,7 @@ slides:
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template-dir", str(sample_template))
result = self.run_check(str(yaml_path), "--template", str(sample_template))
assert result.returncode == 0
@@ -106,62 +84,12 @@ slides:
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template-dir", str(temp_dir))
result = self.run_check(str(yaml_path), "--template", str(temp_dir))
# 应该有错误(模板不存在)
assert result.returncode != 0
def test_check_reports_multiple_errors(self, temp_dir):
"""测试检查报告多个错误"""
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [1, 1, 8, 1]
content: "Test 1"
font:
color: "red"
- type: text
box: [2, 2, 8, 1]
content: "Test 2"
font:
color: "blue"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
output = result.stdout + result.stderr
# 应该报告多个错误
assert "错误" in output or "2" in output
def test_check_includes_location_info(self, temp_dir):
"""测试检查包含位置信息"""
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [1, 1, 8, 1]
content: "Test"
font:
color: "red"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
output = result.stdout + result.stderr
# 应该包含位置信息
assert "幻灯片" in output or "元素" in output
def test_check_with_missing_required_variable(self, temp_dir, sample_template):
"""测试检查缺少必需变量的模板"""
@@ -176,7 +104,7 @@ slides:
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template-dir", str(sample_template))
result = self.run_check(str(yaml_path), "--template", str(sample_template))
# 应该有错误
assert result.returncode != 0