From 4cc06ce7adaf6a417620376ecd2a16d0b9659b88 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 18 Aug 2026 17:28:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(test):=20=E4=BF=AE=E5=A4=8D=20doctor/monito?= =?UTF-8?q?r=20=E6=83=B0=E6=80=A7=E9=97=A8=E9=9D=A2=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E5=9C=A8=E6=97=A0=E7=AB=99=E7=82=B9=E8=B5=84=E6=BA=90=E7=9A=84?= =?UTF-8?q?=20CI=20=E5=AD=90=E8=BF=9B=E7=A8=8B=E4=B8=AD=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该用例以独立子进程加载 app.monitor 实现,链路最终 import app.application.site.sites;CI 无该动态资源模块且子进程不经过 conftest 引导,导致 ModuleNotFoundError。改为触发实现加载前显式 安装与 conftest 同源的 sites 垫片,并通过 CONFIG_DIR 指向临时 目录隔离子进程配置。 --- tests/test_architecture_contract_baseline.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_architecture_contract_baseline.py b/tests/test_architecture_contract_baseline.py index bd4670242..9915614d3 100644 --- a/tests/test_architecture_contract_baseline.py +++ b/tests/test_architecture_contract_baseline.py @@ -1,4 +1,5 @@ import json +import os import subprocess import sys from pathlib import Path @@ -166,7 +167,7 @@ assert sanitize_for_host({'token': 'secret'}) == {'token': '***'} assert result.returncode == 0, result.stderr -def test_doctor_and_monitor_roots_are_lazy_identity_preserving_facades(): +def test_doctor_and_monitor_roots_are_lazy_identity_preserving_facades(tmp_path): """诊断和监控包根不得预载实现,旧路径仍需返回同一公开对象。""" script = """ import sys @@ -176,6 +177,11 @@ 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 会因缺模块失败。 +from app.testing.bootstrap import ensure_sites_stub +ensure_sites_stub() + from app.doctor import DoctorRunner, run_doctor from app.doctor.runner import DoctorRunner as DirectDoctorRunner from app.monitor import LocalDirectoryWatcher, Monitor @@ -187,9 +193,11 @@ assert Monitor is DirectMonitor assert LocalDirectoryWatcher is DirectWatcher assert callable(run_doctor) """ + env = {**os.environ, "CONFIG_DIR": str(tmp_path / "config")} result = subprocess.run( [sys.executable, "-c", script], cwd=PROJECT_ROOT, + env=env, capture_output=True, text=True, check=False,