From fe0b444ceb601c8721c9b47500d35774b12a7de3 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 14 Aug 2026 18:18:06 +0800 Subject: [PATCH] fix(test): stub app.infrastructure.sites in subprocess tests for CI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI 无站点二进制模块,子进程测试绕过 conftest 垫片导致 test_manifest_aliases_reuse_real_canonical_modules 与 test_main_script_does_not_shadow_stdlib_platform 失败。 在子进程中复用 ensure_sites_stub,并给垫片补 __spec__。 --- app/testing/bootstrap.py | 2 ++ tests/test_legacy_import_compat.py | 3 +++ tests/test_main_direct_execution.py | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/app/testing/bootstrap.py b/app/testing/bootstrap.py index 86238f9ac..344b74e56 100644 --- a/app/testing/bootstrap.py +++ b/app/testing/bootstrap.py @@ -124,9 +124,11 @@ def ensure_sites_stub() -> None: try: import app.infrastructure.sites # noqa: F401 本地已拉取时用真实模块 except (ModuleNotFoundError, ImportError): + from importlib.util import spec_from_loader from types import ModuleType stub = ModuleType("app.infrastructure.sites") stub.SitesHelper = _SitesHelperStub + stub.__spec__ = spec_from_loader("app.infrastructure.sites", None) sys.modules["app.infrastructure.sites"] = stub diff --git a/tests/test_legacy_import_compat.py b/tests/test_legacy_import_compat.py index 165f55df9..ea358019b 100644 --- a/tests/test_legacy_import_compat.py +++ b/tests/test_legacy_import_compat.py @@ -165,6 +165,9 @@ def test_manifest_aliases_reuse_real_canonical_modules(): code = """ import importlib from app.compat.manifest import MODULE_ALIASES +# CI 无 app.infrastructure.sites 二进制模块,先补垫片再校验全部映射(与 conftest 同源)。 +from app.testing.bootstrap import ensure_sites_stub +ensure_sites_stub() for legacy_name, alias in MODULE_ALIASES.items(): try: diff --git a/tests/test_main_direct_execution.py b/tests/test_main_direct_execution.py index 6ce3c9b59..341a120b7 100644 --- a/tests/test_main_direct_execution.py +++ b/tests/test_main_direct_execution.py @@ -14,6 +14,10 @@ def test_main_script_does_not_shadow_stdlib_platform(tmp_path): ( "import runpy", "import sys", + # CI 无 app.infrastructure.sites 二进制模块,先补垫片再执行 main.py(与 conftest 同源); + # 必须在篡改 sys.path / 摘除 platform 之前调用,避免真实模块依赖链误引被遮蔽的 app/platform。 + "from app.testing.bootstrap import ensure_sites_stub", + "ensure_sites_stub()", f"sys.path.insert(0, {str(MAIN_PATH.parent)!r})", "sys.modules.pop('platform', None)", f"runpy.run_path({str(MAIN_PATH)!r}, run_name='pycharm_main_probe')",