test: 隔离单测中的站点原生资源 (#6452)

* test: isolate native site resources

* refactor(test): retain sites stub helper contract

* fix(test): preserve sites stub probe imports

---------

Co-authored-by: jxxghp <jxxghp@gmail.com>
This commit is contained in:
InfinityPacer
2026-08-25 16:26:46 +08:00
committed by GitHub
co-authored by jxxghp
parent f3622de82e
commit c727d9c9f1
10 changed files with 31 additions and 49 deletions
-1
View File
@@ -6471,7 +6471,6 @@
"app.testing -> app.testing.stub",
"app.testing.bootstrap -> app.application",
"app.testing.bootstrap -> app.application.service",
"app.testing.bootstrap -> app.application.site",
"app.testing.bootstrap -> app.db",
"app.testing.bootstrap -> app.db.adapters",
"app.testing.bootstrap -> app.db.adapters.transaction",
+1 -2
View File
@@ -302,8 +302,7 @@ import app.monitor
assert not any(name.startswith('app.doctor.') for name in sys.modules)
assert not any(name.startswith('app.monitor.') for name in sys.modules)
# CI 无 app.application.site.sites 资源模块,触发实现加载前先补 conftest 同源垫片;
# 独立子进程不经过 pytest 引导,必须在此显式安装,否则链式 import 会因缺模块失败。
# 独立子进程不经过 pytest 引导,触发实现加载前必须隔离站点原生制品。
from app.testing.bootstrap import ensure_sites_stub
ensure_sites_stub()
+1 -1
View File
@@ -71,7 +71,7 @@ IDENTITY_INDEX_SIGNATURES = {
CURRENT_SCHEMA_CHAIN_SCRIPT = """
from app.testing.bootstrap import ensure_sites_stub
# Alembic 会导入引用业务链的旧 revision;全新 CI 环境没有动态下发的 sites 模块
# Alembic 会导入引用业务链的旧 revision,迁移验证不应加载本机站点原生制品
ensure_sites_stub()
from alembic.config import Config
+1 -1
View File
@@ -167,7 +167,7 @@ def test_manifest_aliases_reuse_real_canonical_modules():
code = """
import importlib
from app.runtime.compat.manifest import MODULE_ALIASES
# CI 无 app.application.site.sites 二进制模块,先补垫片再校验全部映射(与 conftest 同源)
# 独立探针不经过 pytest 引导,先隔离站点原生制品再校验兼容映射
from app.testing.bootstrap import ensure_sites_stub
ensure_sites_stub()
+1 -2
View File
@@ -14,8 +14,7 @@ def test_main_script_does_not_shadow_stdlib_platform(tmp_path):
(
"import runpy",
"import sys",
# CI 无 app.application.site.sites 二进制模块,先补垫片再执行 main.py(与 conftest 同源);
# 必须在篡改 sys.path / 摘除 platform 之前调用,避免真实依赖链受探针环境影响。
# 独立探针不经过 pytest 引导,先隔离站点原生制品,再改变模块搜索路径。
"from app.testing.bootstrap import ensure_sites_stub",
"ensure_sites_stub()",
f"sys.path.insert(0, {str(MAIN_PATH.parent)!r})",
+17 -15
View File
@@ -9,28 +9,30 @@ from pathlib import Path
from app.testing import bootstrap
def test_install_sites_stub_replaces_loaded_dynamic_resource(monkeypatch):
def test_ensure_sites_stub_replaces_loaded_dynamic_resource(monkeypatch):
"""隔离探针必须覆盖本机资源模块,保证 source-only CI 与开发机前提一致。"""
real_module = types.ModuleType("app.application.site.sites")
real_module.SitesHelper = object
monkeypatch.setitem(sys.modules, "app.application.site.sites", real_module)
bootstrap.install_sites_stub()
installed = sys.modules["app.application.site.sites"]
assert installed is not real_module
assert installed.SitesHelper is bootstrap._SitesHelperStub
def test_ensure_sites_stub_preserves_loaded_dynamic_resource(monkeypatch):
"""常规测试引导仍应优先复用已经加载的真实站点资源。"""
real_module = types.ModuleType("app.application.site.sites")
real_module.SitesHelper = object
monkeypatch.setitem(sys.modules, "app.application.site.sites", real_module)
bootstrap.ensure_sites_stub()
assert sys.modules["app.application.site.sites"] is real_module
installed = sys.modules["app.application.site.sites"]
assert installed is not real_module
assert installed.SitesHelper is bootstrap._SitesHelperStub
def test_prepare_backend_replaces_loaded_dynamic_resource(monkeypatch):
"""普通测试引导必须隔离源码目录中已存在的站点原生制品。"""
real_module = types.ModuleType("app.application.site.sites")
real_module.SitesHelper = object
monkeypatch.setitem(sys.modules, "app.application.site.sites", real_module)
bootstrap.prepare_backend()
installed = sys.modules["app.application.site.sites"]
assert installed is not real_module
assert installed.SitesHelper is bootstrap._SitesHelperStub
def test_isolate_config_cleanup_uses_loaded_db_module_without_late_import(monkeypatch):