astrbot_plugin_imagestudio/tests/test_astrbot_entrypoint_import.py

47 行
1.5 KiB
Python

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()