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

此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
2026-03-05 13:27:12 +08:00

117 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Check 命令端到端测试
测试 yaml2pptx.py check 命令的验证功能
"""
import pytest
import subprocess
import sys
from pathlib import Path
class TestCheckCmd:
"""check 命令测试类"""
def run_check(self, *args):
"""辅助函数:运行 check 命令"""
cmd = ["uv", "run", "python", "yaml2pptx.py", "check"]
cmd.extend(args)
result = subprocess.run(
cmd, capture_output=True, text=True, cwd=Path(__file__).parent.parent.parent
)
return result
def test_check_valid_yaml(self, sample_yaml):
"""测试检查有效的 YAML"""
result = self.run_check(str(sample_yaml))
assert result.returncode == 0
assert "验证" in result.stdout or "通过" in result.stdout
def test_check_with_warnings_only(self, temp_dir):
"""测试只有警告的 YAML验证通过但有警告"""
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [8, 1, 3, 1] # 边界超出
content: "Test"
font:
size: 24
"""
yaml_path = temp_dir / "warning.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
# 应该显示警告但返回 0
assert "警告" in result.stdout
def test_check_with_template(self, temp_dir, sample_template):
"""测试检查使用模板的 YAML"""
yaml_content = f"""
metadata:
size: "16:9"
slides:
- template: title-slide
vars:
title: "Test Title"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template", str(sample_template))
assert result.returncode == 0
def test_check_nonexistent_template(self, temp_dir):
"""测试检查使用不存在模板的 YAML"""
yaml_content = """
metadata:
size: "16:9"
slides:
- template: nonexistent
vars:
title: "Test"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template", str(temp_dir))
# 应该有错误(模板不存在)
assert result.returncode != 0
def test_check_with_missing_required_variable(self, temp_dir, sample_template):
"""测试检查缺少必需变量的模板"""
yaml_content = f"""
metadata:
size: "16:9"
slides:
- template: title-slide
vars: {{}}
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template", str(sample_template))
# 应该有错误
assert result.returncode != 0
def test_check_nonexistent_file(self, temp_dir):
"""测试检查不存在的文件"""
result = self.run_check(str(temp_dir / "nonexistent.yaml"))
assert result.returncode != 0