fix: 支持从任意路径调用 lyxy_document_reader.py

- 从 __file__ 动态计算项目根目录
- 使用绝对路径引用 bootstrap.py
- 设置正确的 PYTHONPATH 和 cwd
- 添加路径解析测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 12:06:44 +08:00
parent a5c0b67360
commit edbdeec90d
2 changed files with 62 additions and 4 deletions

View File

@@ -8,8 +8,13 @@ import subprocess
import sys import sys
from pathlib import Path from pathlib import Path
# 确定项目根目录和脚本路径
script_file = Path(__file__).resolve()
scripts_dir = script_file.parent
project_root = scripts_dir.parent
bootstrap_path = str(scripts_dir / "bootstrap.py")
# 将 scripts/ 目录添加到 sys.path # 将 scripts/ 目录添加到 sys.path
scripts_dir = Path(__file__).resolve().parent
if str(scripts_dir) not in sys.path: if str(scripts_dir) not in sys.path:
sys.path.append(str(scripts_dir)) sys.path.append(str(scripts_dir))
@@ -96,7 +101,7 @@ def main():
# 生成 uv 命令参数列表 # 生成 uv 命令参数列表
uv_args = generate_uv_args( uv_args = generate_uv_args(
dependencies=dependencies, dependencies=dependencies,
script_path="scripts/bootstrap.py", script_path=bootstrap_path,
python_version=python_version, python_version=python_version,
include_pyarmor=True include_pyarmor=True
) )
@@ -106,10 +111,10 @@ def main():
# 设置环境变量 # 设置环境变量
env = os.environ.copy() env = os.environ.copy()
env["PYTHONPATH"] = "." env["PYTHONPATH"] = str(project_root)
# 自启动:使用 subprocess 替代 execvpeWindows 兼容) # 自启动:使用 subprocess 替代 execvpeWindows 兼容)
result = subprocess.run(uv_args, env=env) result = subprocess.run(uv_args, env=env, cwd=str(project_root))
sys.exit(result.returncode) sys.exit(result.returncode)

View File

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