Files
lyxy-document/tests/test_cli/test_path_resolution.py
lanyuanxiaoyao edbdeec90d fix: 支持从任意路径调用 lyxy_document_reader.py
- 从 __file__ 动态计算项目根目录
- 使用绝对路径引用 bootstrap.py
- 设置正确的 PYTHONPATH 和 cwd
- 添加路径解析测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 12:06:44 +08:00

54 lines
2.2 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.
"""测试路径解析功能 - 验证从任意路径调用脚本。"""
import sys
from pathlib import Path
class TestPathResolution:
"""测试路径解析逻辑。"""
def test_project_root_detection(self):
"""测试项目根目录检测逻辑。"""
# 模拟 lyxy_document_reader.py 中的路径计算逻辑
# 获取当前测试文件的路径,然后向上找到项目根
test_file = Path(__file__).resolve()
tests_dir = test_file.parent.parent # tests/
project_root = tests_dir.parent # 项目根
# 验证我们能正确找到项目根
assert (project_root / "scripts").exists()
assert (project_root / "scripts" / "lyxy_document_reader.py").exists()
assert (project_root / "scripts" / "bootstrap.py").exists()
def test_bootstrap_path_absolute(self):
"""测试 bootstrap.py 路径是绝对路径。"""
# 模拟 lyxy_document_reader.py 中的路径计算
test_file = Path(__file__).resolve()
project_root = test_file.parent.parent.parent # 从 tests/test_cli/ 向上两级
scripts_dir = project_root / "scripts"
bootstrap_path = scripts_dir / "bootstrap.py"
# 验证路径是绝对路径
assert bootstrap_path.is_absolute()
assert bootstrap_path.exists()
def test_path_independent_from_cwd(self, monkeypatch, tmp_path):
"""测试路径计算不依赖当前工作目录。"""
# 保存原始路径
test_file = Path(__file__).resolve()
project_root = test_file.parent.parent.parent
scripts_dir = project_root / "scripts"
# 切换到临时目录
monkeypatch.chdir(tmp_path)
# 即使在临时目录,我们仍然能通过 __file__ 找到正确的路径
# 这里我们模拟 lyxy_document_reader.py 中的逻辑
# 注意:实际中 __file__ 是脚本本身的路径,不是测试文件的路径
# 这里我们验证原理__file__ 给出的是脚本位置,与 cwd 无关
# 验证 scripts_dir 和 bootstrap_path 的计算只依赖 __file__
# 这个测试验证的是概念,不是实际的脚本导入
assert scripts_dir.is_absolute()
assert (scripts_dir / "bootstrap.py").exists()