merge: test_a11 m3/m4 模块级 game_dir/dedup 替换收进可恢复 fixture——根除 5 个跨模块污染失败(证据修正:基线12≠全污染,余7=环境前置) (尾波小单)
Some checks failed
contract-gates / contract-gates (push) Has been cancelled
docs-gate / docs-gate (push) Has been cancelled

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
lili 2026-07-03 21:13:06 -07:00
commit 42eed68675
2 changed files with 47 additions and 12 deletions

View File

@ -20,13 +20,30 @@ import cheap_run # noqa: E402
import cheap_modify as M # noqa: E402
import worker_service as W # noqa: E402
import dedup # noqa: E402
import pytest # noqa: E402
# 把 game_dir 重定向到临时目录:materialize / worker 接线测试落盘免污染真 games/(与 cheap_modify、worker_service
# 同引用一个 cheap_run 模块对象 → patch 模块属性即对二者生效)。
_TMP_GAMES = Path(tempfile.mkdtemp(prefix="a11-m3-games-"))
cheap_run.game_dir = lambda gid: _TMP_GAMES / f"amgen-{gid}" # noqa: E731
# create 路会调 D9 dedup.check_similarity(写 FS 登记表)→ 重定向防污染真登记表(与 test_worker_service 同范式)。
dedup.DEDUP_REGISTRY = Path(tempfile.mkdtemp(prefix="a11m3-dedup-")) / "_dedup_registry.jsonl"
# game_dir 与 dedup 登记表在本模块测试期间重定向到临时目录:materialize、worker 接线、create 路(D9 dedup)
# 的落盘都进临时目录,不碰真 games/ 与真登记表(cheap_modify、worker_service 与本模块同引用一个 cheap_run
# 模块对象,改模块属性即对三者同时生效)。重定向收进 autouse fixture:只在本模块测试期间生效、跑完即恢复
# 原值,不再像早先那样在 import 时无恢复地替换 cheap_run.game_dir——那样会污染同会话后续的 test_run /
# test_studio_smoke / test_toolkit(它们按真 games/ 根校验 write 边界,game_dir 被换后一写就判越界)。
def _install_tmp_redirect():
"""把 cheap_run.game_dir、dedup.DEDUP_REGISTRY 指向新建临时目录,返回二者原值供恢复。"""
orig = (cheap_run.game_dir, dedup.DEDUP_REGISTRY)
tmp_games = Path(tempfile.mkdtemp(prefix="a11-m3-games-"))
cheap_run.game_dir = lambda gid: tmp_games / f"amgen-{gid}" # noqa: E731
dedup.DEDUP_REGISTRY = Path(tempfile.mkdtemp(prefix="a11m3-dedup-")) / "_dedup_registry.jsonl"
return orig
@pytest.fixture(autouse=True, scope="module")
def _tmp_redirect():
orig_game_dir, orig_registry = _install_tmp_redirect()
try:
yield
finally:
cheap_run.game_dir = orig_game_dir
dedup.DEDUP_REGISTRY = orig_registry
_GOOD_BUNDLE = "var __GameBundle=(function(){return{bootGameHost(){}}})();"
@ -287,6 +304,7 @@ def test_create_path_unaffected_by_modify_wiring():
if __name__ == "__main__":
_install_tmp_redirect() # 独立运行(非 pytest,autouse fixture 不触发):单进程一次性装重定向,退出即释放、无需恢复
_fns = [v for k, v in sorted(globals().items()) if k.startswith("test_") and callable(v)]
_failed = 0
for _fn in _fns:

View File

@ -22,13 +22,29 @@ import cheap_run # noqa: E402
import cheap_modify as M # noqa: E402
import worker_service as W # noqa: E402
import dedup # noqa: E402
import pytest # noqa: E402
# game_dir 重定向到临时目录:materialize / 接线落盘免污染真 games/(cheap_modify、worker_service 同引用一个
# cheap_run 模块对象 → patch 模块属性即对二者生效;与 test_a11_m3 同范式)。
_TMP_GAMES = Path(tempfile.mkdtemp(prefix="a11-m4-games-"))
cheap_run.game_dir = lambda gid: _TMP_GAMES / f"amgen-{gid}" # noqa: E731
# 防 D9 dedup 写真登记表(本测不走 create 路、稳妥起见仍重定向)。
dedup.DEDUP_REGISTRY = Path(tempfile.mkdtemp(prefix="a11m4-dedup-")) / "_dedup_registry.jsonl"
# game_dir 与 dedup 登记表在本模块测试期间重定向到临时目录:materialize、worker 接线的落盘都进临时目录,
# 不碰真 games/(cheap_modify、worker_service 与本模块同引用一个 cheap_run 模块对象,改模块属性即对三者同时
# 生效);本测虽不走 create 路,dedup 登记表仍一并重定向以防万一。重定向收进 autouse fixture:只在本模块测试
# 期间生效、跑完即恢复原值,不再在 import 时无恢复地替换 cheap_run.game_dir 而污染同会话后续测试模块。
def _install_tmp_redirect():
"""把 cheap_run.game_dir、dedup.DEDUP_REGISTRY 指向新建临时目录,返回二者原值供恢复。"""
orig = (cheap_run.game_dir, dedup.DEDUP_REGISTRY)
tmp_games = Path(tempfile.mkdtemp(prefix="a11-m4-games-"))
cheap_run.game_dir = lambda gid: tmp_games / f"amgen-{gid}" # noqa: E731
dedup.DEDUP_REGISTRY = Path(tempfile.mkdtemp(prefix="a11m4-dedup-")) / "_dedup_registry.jsonl"
return orig
@pytest.fixture(autouse=True, scope="module")
def _tmp_redirect():
orig_game_dir, orig_registry = _install_tmp_redirect()
try:
yield
finally:
cheap_run.game_dir = orig_game_dir
dedup.DEDUP_REGISTRY = orig_registry
_GOOD_BUNDLE = "var __GameBundle=(function(){return{bootGameHost(){}}})();"
_BASE_LOGIC = "// base game-logic\nexport function createGame({ plugins, bundle, viewport }) { return {}; }\n"
@ -262,6 +278,7 @@ def test_worker_unknown_mode_still_failed():
if __name__ == "__main__":
_install_tmp_redirect() # 独立运行(非 pytest,autouse fixture 不触发):单进程一次性装重定向,退出即释放、无需恢复
_fns = [v for k, v in sorted(globals().items()) if k.startswith("test_") and callable(v)]
_failed = 0
for _fn in _fns: