refactor: 预留并行修复所需的共享接口

This commit is contained in:
2026-10-25 06:48:26 +08:00
parent d96f430a7d
commit 6b84a5e406
10 changed files with 179 additions and 42 deletions
+26
View File
@@ -0,0 +1,26 @@
"""在导入 app 之前把数据和下载目录指到临时目录。"""
import os
import sys
import tempfile
from pathlib import Path
_TEMP = Path(__file__).resolve().parents[2] / "Temp"
_TEMP.mkdir(parents=True, exist_ok=True)
_ROOT = Path(tempfile.mkdtemp(prefix="pytest-", dir=_TEMP))
os.environ["NQI_DATA_DIR"] = str(_ROOT / "data")
os.environ["NQI_DOWNLOAD_DIR"] = str(_ROOT / "downloads")
if "app" in sys.modules or "app.shared.paths" in sys.modules:
raise RuntimeError("测试必须在导入 app 之前设置 NQI_DATA_DIR 和 NQI_DOWNLOAD_DIR")
def pytest_configure() -> None:
from app.shared.paths import DATA_ROOT, DOWNLOAD_ROOT
if DATA_ROOT != Path(os.environ["NQI_DATA_DIR"]):
raise RuntimeError("DATA_ROOT 没有使用 NQI_DATA_DIR")
if DOWNLOAD_ROOT != Path(os.environ["NQI_DOWNLOAD_DIR"]):
raise RuntimeError("DOWNLOAD_ROOT 没有使用 NQI_DOWNLOAD_DIR")
if DOWNLOAD_ROOT.exists():
raise RuntimeError("导入时不应创建下载目录")