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')",