"""测试路径解析功能 - 验证从任意路径调用脚本。""" 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()