S121 AstrBot安装导入兼容

这个提交存在于:
YokiiroBW 2026-07-06 09:19:54 +08:00
父节点 4bff71d80b
当前提交 d0c4a8fe0e
共有 2 个文件被更改,包括 55 次插入 和 0 次删除

查看文件

@ -6,8 +6,16 @@ dispatch helpers for the runtime layer without starting a standalone server.
from __future__ import annotations from __future__ import annotations
import sys
from pathlib import Path
from typing import Any from typing import Any
PLUGIN_ROOT = Path(__file__).resolve().parent
if str(PLUGIN_ROOT) not in sys.path:
# AstrBot may import the plugin entrypoint without adding the plugin root.
sys.path.insert(0, str(PLUGIN_ROOT))
from adapters.astrbot_adapter import ( from adapters.astrbot_adapter import (
dispatch_extension_request, dispatch_extension_request,
register_astrbot_runtime, register_astrbot_runtime,

查看文件

@ -0,0 +1,47 @@
import os
import subprocess
import sys
import tempfile
import textwrap
import unittest
from pathlib import Path
class AstrBotEntrypointImportTest(unittest.TestCase):
def test_main_imports_when_plugin_root_is_not_on_sys_path(self):
repo_root = Path(__file__).resolve().parents[1]
code = textwrap.dedent(
f"""
import importlib.util
import json
import sys
from pathlib import Path
repo_root = Path({str(repo_root)!r})
sys.path = [p for p in sys.path if Path(p or ".").resolve() != repo_root]
spec = importlib.util.spec_from_file_location(
"imagestudio_runtime_entry",
repo_root / "main.py",
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
manifest = module.get_plugin_manifest()
print(json.dumps({{"plugin_name": manifest["plugin_name"]}}))
"""
)
with tempfile.TemporaryDirectory() as tmp:
env = dict(os.environ)
env.pop("PYTHONPATH", None)
result = subprocess.run(
[sys.executable, "-c", code],
cwd=tmp,
env=env,
check=True,
capture_output=True,
text=True,
)
self.assertIn("astrbot_plugin_imagestudio", result.stdout)
if __name__ == "__main__":
unittest.main()