test_m6_r2_real_dto_shape resolved the companion repo as a sibling directory, which is only true of the development checkout. On the VM the skipif was always true, so the seven cases that exercise the companion's real DTO builder -- the only genuine check of the cross-plugin envelope shape -- never ran in the environment that most needed them, and a skip shows up as a missing dot rather than a failure. test_ops_migration had the harder version of the same bug: the import simply failed, and working around it with PYTHONPATH let the memory side's own core/ops_migration shadow the companion module, producing two failures that looked like product defects and were not. Resolution now lives in tests/_companion_repo.py and tries, in order, the COMPANION_PLUGIN_ROOT override, the sibling development directory, the sibling deployed directory (_ops suffix), and one level up. The path helper inserts at the front of sys.path deliberately: getting that order wrong fails as "green but testing the wrong module", which is worse than an ImportError. Memory suite 330 passed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
62 行
2.3 KiB
Python
62 行
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""定位陪伴插件仓库。
|
||
|
||
跨仓测试原先各写各的相对路径(`parents[1].parent / "astrbot_plugin_private_companion-main"`),
|
||
只在「两个仓库是同级目录」这一种布局下成立。VM 上不是这个布局,于是:
|
||
|
||
- `test_m6_r2_real_dto_shape.py` 的 7 个用例 skipif 恒成立,真机口径下从没跑过;
|
||
- `test_ops_migration.py` 直接 import 失败,只能靠手工挂 PYTHONPATH 绕过,
|
||
而那次绕过又让 `core/ops_migration.py` 遮蔽了陪伴侧同名模块,产生了两个假失败。
|
||
|
||
这里把定位逻辑收成一处,按优先级依次尝试,并允许用环境变量显式指定。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
_MEMORY_ROOT = Path(__file__).resolve().parents[1]
|
||
_DIR_NAME = "astrbot_plugin_private_companion-main"
|
||
# VM 上插件目录用的是 _ops 后缀名
|
||
_DEPLOYED_NAME = "astrbot_plugin_private_companion_ops"
|
||
|
||
|
||
def _candidates():
|
||
override = os.environ.get("COMPANION_PLUGIN_ROOT", "").strip()
|
||
if override:
|
||
yield Path(override)
|
||
# 开发布局:两个仓库是项目根下的同级目录
|
||
yield _MEMORY_ROOT.parent / _DIR_NAME
|
||
# 部署布局:两个插件目录都在 AstrBot 的 plugins/ 下
|
||
yield _MEMORY_ROOT.parent / _DEPLOYED_NAME
|
||
# 再往上一层找一次,容忍多包一层目录的情况
|
||
yield _MEMORY_ROOT.parent.parent / _DIR_NAME
|
||
|
||
|
||
def find_companion_root() -> Path | None:
|
||
"""返回陪伴插件根目录;找不到返回 None(调用方据此 skip)。"""
|
||
for candidate in _candidates():
|
||
try:
|
||
if (candidate / "bot_personal_dto.py").is_file():
|
||
return candidate.resolve()
|
||
except OSError:
|
||
continue
|
||
return None
|
||
|
||
|
||
def ensure_companion_on_path() -> Path | None:
|
||
"""把陪伴插件根目录放到 sys.path 最前面,返回该目录。
|
||
|
||
放最前面是必要的:记忆侧也有 `core/ops_migration.py`,若陪伴目录排在后面,
|
||
`import ops_migration` 会命中记忆侧那个同名模块,测试会以「跑绿了但测错了对象」
|
||
的形态失败,比直接 ImportError 更难查。
|
||
"""
|
||
root = find_companion_root()
|
||
if root is None:
|
||
return None
|
||
text = str(root)
|
||
if text in sys.path:
|
||
sys.path.remove(text)
|
||
sys.path.insert(0, text)
|
||
return root
|