From 379185caed87a80a40cf234d882be8f9e2b35ae3 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sat, 24 Oct 2026 08:06:12 +0000 Subject: [PATCH] =?UTF-8?q?=E7=9B=AF=E4=BD=8F=E8=BF=9E=E6=8E=A5=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E9=87=8C=E9=82=A3=E6=AC=A1=E6=8F=92=E4=BB=B6=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原来两条用例自己调自动导入,连接模块里那行被删掉它们照样绿。加一条查源码的用例,盯住模块加载时那次调用本身还在。 --- .../tests/plugins_func/test_loadplugins.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/main/xiaozhi-server/tests/plugins_func/test_loadplugins.py b/main/xiaozhi-server/tests/plugins_func/test_loadplugins.py index 2e9e5bdb9..260167c44 100644 --- a/main/xiaozhi-server/tests/plugins_func/test_loadplugins.py +++ b/main/xiaozhi-server/tests/plugins_func/test_loadplugins.py @@ -5,10 +5,14 @@ ``@register_function`` 把自己登记进注册表。这段自动导入万一被删掉, 插件一个都不会被加载,工具链直接空掉,而且不报错,很难发现。这里守住这条链路。 +最后一条用例查的是 ``core/connection.py`` 的源码本身:自动导入这个动作做得对 +不对,跟连接模块有没有真的调它,是两回事,两条都要守。 + 早先这个文件整份跳过,理由是自动导入会拉起一串重量级依赖。CI 里 ``data/.config.yaml`` 是现造的,那个理由已经不成立了。 """ import sys +from pathlib import Path from plugins_func.loadplugins import auto_import_modules from plugins_func.register import all_function_registry @@ -28,3 +32,34 @@ def test_auto_import_registers_plugin_functions(): assert "get_lunar" in all_function_registry assert "get_weather" in all_function_registry + + +def test_connection_module_triggers_auto_import(): + """core/connection.py 模块加载时那次自动导入得在 + +
+ 这行被删掉或者改成注释,插件一个都不会被加载:启动不报错,工具链空掉, + 上面两条用例照样绿。这里查源码,盯住那一次调用。 + """ + import ast + + source_path = Path(__file__).resolve().parents[2] / "core" / "connection.py" + tree = ast.parse(source_path.read_text(encoding="utf-8")) + + imported_packages = [] + for node in tree.body: + if not isinstance(node, ast.Expr) or not isinstance(node.value, ast.Call): + continue + call = node.value + func = call.func + name = func.id if isinstance(func, ast.Name) else getattr(func, "attr", "") + if name != "auto_import_modules": + continue + for arg in call.args: + if isinstance(arg, ast.Constant) and isinstance(arg.value, str): + imported_packages.append(arg.value) + + assert "plugins_func.functions" in imported_packages, ( + "core/connection.py 模块加载时的那次自动导入不见了," + "插件不会被加载,工具链会空掉" + )