refactor: 重构外部模板系统,改为单文件模板库模式
主要变更: - 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件 - 添加模板库 YAML 验证功能 - 为模板添加 base_dir 支持,正确解析相对路径资源 - 内联模板与外部模板同名时改为警告(内联优先) - 移除模板缓存机制,直接使用模板库字典 - 更新所有相关测试以适配新的模板加载方式 此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -74,7 +74,7 @@ slides:
|
||||
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(
|
||||
str(yaml_path), str(output), "--template-dir", str(sample_template)
|
||||
str(yaml_path), str(output), "--template", str(sample_template)
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
@@ -205,3 +205,68 @@ slides:
|
||||
else: # 4:3
|
||||
assert abs(prs.slide_width.inches - 10.0) < 0.01
|
||||
assert abs(prs.slide_height.inches - 7.5) < 0.01
|
||||
|
||||
def test_subdirectory_path_resolution(self, temp_dir):
|
||||
"""测试子目录中的文件路径解析"""
|
||||
# 创建子目录结构
|
||||
doc_dir = temp_dir / "docs"
|
||||
template_dir = temp_dir / "templates"
|
||||
doc_dir.mkdir()
|
||||
template_dir.mkdir()
|
||||
|
||||
# 创建模板库文件
|
||||
template_content = """
|
||||
templates:
|
||||
test-template:
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "{title}"
|
||||
"""
|
||||
template_path = template_dir / "templates.yaml"
|
||||
template_path.write_text(template_content)
|
||||
|
||||
# 创建文档文件
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- template: test-template
|
||||
vars:
|
||||
title: "Test Title"
|
||||
"""
|
||||
yaml_path = doc_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
|
||||
# 使用相对路径运行转换
|
||||
import os
|
||||
original_cwd = os.getcwd()
|
||||
try:
|
||||
os.chdir(temp_dir)
|
||||
result = subprocess.run(
|
||||
["uv", "run", "python",
|
||||
str(Path(original_cwd) / "yaml2pptx.py"),
|
||||
"convert",
|
||||
"docs/test.yaml",
|
||||
"output.pptx",
|
||||
"--template", "templates/templates.yaml"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
assert result.returncode == 0, f"转换失败: {result.stderr}"
|
||||
assert output_path.exists()
|
||||
|
||||
# 验证生成的 PPTX
|
||||
prs = Presentation(str(output_path))
|
||||
assert len(prs.slides) == 1
|
||||
text_content = prs.slides[0].shapes[0].text_frame.text
|
||||
assert "Test Title" in text_content
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
@@ -22,12 +22,6 @@ class TestGeneratePreviewHtml:
|
||||
assert "<html>" in html
|
||||
assert "</html>" in html
|
||||
|
||||
def test_html_contains_slide_content(self, sample_yaml):
|
||||
"""测试 HTML 包含幻灯片内容"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
# 应该包含文本内容
|
||||
assert "Hello, World!" in html
|
||||
|
||||
def test_html_contains_css_styles(self, sample_yaml):
|
||||
"""测试 HTML 包含 CSS 样式"""
|
||||
@@ -37,24 +31,6 @@ class TestGeneratePreviewHtml:
|
||||
assert ".slide" in html
|
||||
assert "position: absolute" in html
|
||||
|
||||
def test_html_with_template(self, temp_dir, sample_template):
|
||||
"""测试使用模板生成 HTML"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Template Title"
|
||||
subtitle: "Template Subtitle"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
html = generate_preview_html(str(yaml_path), str(sample_template))
|
||||
|
||||
assert "Template Title" in html
|
||||
|
||||
def test_html_with_invalid_yaml(self, temp_dir):
|
||||
"""测试无效 YAML 返回错误页面"""
|
||||
@@ -67,35 +43,6 @@ slides:
|
||||
assert "<!DOCTYPE html>" in html
|
||||
assert "错误" in html or "error" in html.lower()
|
||||
|
||||
def test_html_with_multiple_slides(self, temp_dir):
|
||||
"""测试多张幻灯片的 HTML"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Slide 1"
|
||||
font:
|
||||
size: 24
|
||||
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Slide 2"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
html = generate_preview_html(str(yaml_path), None)
|
||||
|
||||
# 应该包含两张幻灯片的内容
|
||||
assert "Slide 1" in html
|
||||
assert "Slide 2" in html
|
||||
|
||||
def test_html_contains_slide_number(self, sample_yaml):
|
||||
"""测试 HTML 包含幻灯片编号"""
|
||||
@@ -111,91 +58,4 @@ slides:
|
||||
assert "/events" in html
|
||||
|
||||
|
||||
class TestCreateFlaskApp:
|
||||
"""create_flask_app 函数测试类"""
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_creates_flask_app(self, mock_queue):
|
||||
"""测试创建 Flask 应用"""
|
||||
app = create_flask_app()
|
||||
|
||||
assert app is not None
|
||||
assert hasattr(app, 'url_map')
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_has_index_route(self, mock_queue):
|
||||
"""测试有 / 路由"""
|
||||
app = create_flask_app()
|
||||
|
||||
# 检查路由
|
||||
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
||||
assert '/' in rules
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_has_events_route(self, mock_queue):
|
||||
"""测试有 /events 路由"""
|
||||
app = create_flask_app()
|
||||
|
||||
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
||||
assert '/events' in rules
|
||||
|
||||
|
||||
class TestYAMLChangeHandler:
|
||||
"""YAMLChangeHandler 测试类"""
|
||||
|
||||
def test_on_modified_with_yaml_file(self):
|
||||
"""测试处理 YAML 文件修改"""
|
||||
from preview.server import YAMLChangeHandler
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
handler = YAMLChangeHandler()
|
||||
mock_queue = MagicMock()
|
||||
import preview.server
|
||||
preview.server.change_queue = mock_queue
|
||||
|
||||
event = MagicMock()
|
||||
event.src_path = "test.yaml"
|
||||
|
||||
handler.on_modified(event)
|
||||
|
||||
mock_queue.put.assert_called_once_with('reload')
|
||||
|
||||
def test_on_modified_with_non_yaml_file(self):
|
||||
"""测试忽略非 YAML 文件修改"""
|
||||
from preview.server import YAMLChangeHandler
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
handler = YAMLChangeHandler()
|
||||
mock_queue = MagicMock()
|
||||
import preview.server
|
||||
preview.server.change_queue = mock_queue
|
||||
|
||||
event = MagicMock()
|
||||
event.src_path = "test.txt"
|
||||
|
||||
handler.on_modified(event)
|
||||
|
||||
mock_queue.put.assert_not_called()
|
||||
|
||||
|
||||
class TestPreviewHTMLTemplate:
|
||||
"""HTML 模板常量测试"""
|
||||
|
||||
def test_html_template_is_defined(self):
|
||||
"""测试 HTML_TEMPLATE 已定义"""
|
||||
from preview.server import HTML_TEMPLATE
|
||||
assert isinstance(HTML_TEMPLATE, str)
|
||||
assert "<!DOCTYPE html>" in HTML_TEMPLATE
|
||||
|
||||
def test_error_template_is_defined(self):
|
||||
"""测试 ERROR_TEMPLATE 已定义"""
|
||||
from preview.server import ERROR_TEMPLATE
|
||||
assert isinstance(ERROR_TEMPLATE, str)
|
||||
assert "<!DOCTYPE html>" in ERROR_TEMPLATE
|
||||
assert "错误" in ERROR_TEMPLATE or "error" in ERROR_TEMPLATE.lower()
|
||||
|
||||
Reference in New Issue
Block a user