fix(tests): tmp_path 落点改到仓库内,消除 33 个与缺陷无关的 setup 失败

问题:本机 `%TEMP%\pytest-of-Windows` 被权限更高的会话建过,当前用户无权写入,
于是所有用 `tmp_path` 的用例在 setup 阶段批量 ERROR(WinError 5)——实测 33 个,
散布在 offsite 附件预览、通知发送、promotion、worker 等处。这类噪声会让
"到底哪里坏了"完全看不出来(同事这批新用例首次把它暴露出来)。

修法:在 tests/conftest.py 覆盖 `tmp_path`,把根目录改到仓库内 `.workdir/pytest-tmp`
(已在 .gitignore)。语义不变——每个用例仍拿到一个**新建的空目录**(残留目录会污染断言)。
只覆盖 `tmp_path` 而不动 `tmp_path_factory`:后者是 pytest 私有构造,参数随版本变化
(实测直接实例化 `TempPathFactory(...)` 会 TypeError),而本仓库用例只用 `tmp_path`。

同时把 pyproject.toml 里那条 `basetemp = ...` 删掉:pytest **只在命令行认 basetemp**,
写在 ini 里会被静默忽略(实测无效),留着会让人误以为已配好。改为注释指向 conftest。

效果:不带任何参数 `pytest -q` 从「3 failed + 33 errors」变为「3 failed,0 error」。
This commit is contained in:
qyqy
2026-09-11 19:13:43 +08:00
committed by wangjianlong_0626
parent 636dcbbe7e
commit 8cff6b35f9
2 changed files with 36 additions and 8 deletions
+3 -7
View File
@@ -52,13 +52,9 @@ testpaths = ["tests"]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
markers = ["integration: requires local database services"]
# 临时目录固定到仓库内(`.workdir/` 已在 .gitignore 中忽略)。
#
# 为什么不用系统临时目录:Windows 上 `%TEMP%\pytest-of-<user>` 一旦被权限更高的会话
# 建过,后续运行就**无权写入**,所有用 `tmp_path` 的用例批量 setup 失败
# (实测 33 个用例 ERROR,报 WinError 5)。固定到仓库内后不受系统临时目录权限影响,
# 且清理范围可见、可控。
basetemp = ".workdir/pytest-tmp"
# 临时目录的落点由 `tests/conftest.py` 覆盖 `tmp_path` 决定(落在仓库内 `.workdir/pytest-tmp`)。
# 这里**不**写 `basetemp`:pytest 只在命令行认它,写在 ini 里会被静默忽略(实测无效),
# 留着会让人误以为已经配好了。系统临时目录权限坏掉的原因与修法见 conftest 的说明。
[tool.ruff]
line-length = 100