32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
"""在导入 app 之前把数据和下载目录指到临时目录。"""
|
|
|
|
import os
|
|
import shutil
|
|
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("导入时不应创建下载目录")
|
|
|
|
|
|
def pytest_unconfigure() -> None:
|
|
shutil.rmtree(_ROOT, ignore_errors=True)
|