pbl_blueprint/tests/_m1b_loader.py
2026-09-16 19:36:42 +08:00

40 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""M1b 测试引导器tests 专用,不属于模块运行时代码)。
背景pbl_blueprint/__init__.py 会 eager import .init -> .api_blueprint
后者依赖 pbl_commonM1a 挂载链)。当 pbl_common 契约尚未对齐时,
任何 `from pbl_blueprint.m1b_xxx import ...` 都会在包初始化阶段炸掉,
导致 M1b 自包含代码无法被单测覆盖。
做法:在 sys.modules 里预置一个「空壳包」pbl_blueprint只设 __path__
指向真实包目录,不执行真实 __init__.py随后 import pbl_blueprint.m1b_*
即按子模块直接加载(子模块间的相对导入照常工作)。
M1b 的 m1b_common / m1b_db / m1b_template / m1b_subobject / m1b_ref /
m1b_api / m1b_init 均不 import pbl_common故本引导器不掩盖任何真实缺陷
M1a 挂载链的集成验证由 tests/test_m1b_realdb.py 与部署阶段负责。
"""
import os
import sys
import types
_PKG_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "pbl_blueprint")
_MOD_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(_PKG_DIR)))
def bootstrap():
"""预置空壳包,返回包目录。可重复调用(幂等)。"""
if _MOD_ROOT not in sys.path:
sys.path.insert(0, _MOD_ROOT)
pkg = sys.modules.get("pbl_blueprint")
if pkg is None or not getattr(pkg, "_m1b_stub", False):
stub = types.ModuleType("pbl_blueprint")
stub.__path__ = [_PKG_DIR]
stub._m1b_stub = True
sys.modules["pbl_blueprint"] = stub
return _PKG_DIR
bootstrap()