2022-01-26 15:37:35 +08:00
|
|
|
import sys
|
2022-06-20 15:49:53 +08:00
|
|
|
from dataclasses import asdict
|
2022-01-26 15:37:35 +08:00
|
|
|
from typing import TYPE_CHECKING, Set
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from nonebug import App
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from nonebot.plugin import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2022-05-26 16:35:47 +08:00
|
|
|
async def test_load_plugin(app: App, load_plugin: Set["Plugin"]):
|
2022-01-26 15:37:35 +08:00
|
|
|
import nonebot
|
2022-05-26 16:35:47 +08:00
|
|
|
from nonebot.plugin import PluginManager
|
2022-01-26 15:37:35 +08:00
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
loaded_plugins = {
|
2022-01-26 15:37:35 +08:00
|
|
|
plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin
|
2022-05-26 16:35:47 +08:00
|
|
|
}
|
2022-01-26 15:37:35 +08:00
|
|
|
assert loaded_plugins == load_plugin
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
# check simple plugin
|
2022-01-26 15:37:35 +08:00
|
|
|
assert "plugins.export" in sys.modules
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
# check sub plugin
|
2022-05-27 11:25:51 +08:00
|
|
|
plugin = nonebot.get_plugin("nested_subplugin")
|
|
|
|
assert plugin
|
2022-05-26 16:35:47 +08:00
|
|
|
assert "plugins.nested.plugins.nested_subplugin" in sys.modules
|
2022-05-27 11:25:51 +08:00
|
|
|
assert plugin.parent_plugin == nonebot.get_plugin("nested")
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
# check load again
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
PluginManager(plugins=["plugins.export"]).load_all_plugins()
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
PluginManager(search_path=["plugins"]).load_all_plugins()
|
2022-01-26 15:37:35 +08:00
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
# check not found
|
2022-01-26 15:37:35 +08:00
|
|
|
assert nonebot.load_plugin("some_plugin_not_exist") is None
|
|
|
|
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_bad_plugin(app: App):
|
|
|
|
import nonebot
|
|
|
|
|
|
|
|
nonebot.load_plugins("bad_plugins")
|
|
|
|
|
|
|
|
assert nonebot.get_plugin("bad_plugins") is None
|
|
|
|
|
|
|
|
|
2022-01-26 15:37:35 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_require_loaded(app: App, monkeypatch: pytest.MonkeyPatch):
|
|
|
|
import nonebot
|
|
|
|
|
|
|
|
def _patched_find(name: str):
|
|
|
|
assert False
|
|
|
|
|
|
|
|
monkeypatch.setattr("nonebot.plugin.load._find_manager_by_name", _patched_find)
|
|
|
|
|
|
|
|
nonebot.load_plugin("plugins.export")
|
|
|
|
|
|
|
|
nonebot.require("plugins.export")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_require_not_loaded(app: App, monkeypatch: pytest.MonkeyPatch):
|
|
|
|
import nonebot
|
2022-05-26 16:35:47 +08:00
|
|
|
from nonebot.plugin import PluginManager, _managers
|
2022-01-26 15:37:35 +08:00
|
|
|
|
|
|
|
m = PluginManager(["plugins.export"])
|
|
|
|
_managers.append(m)
|
|
|
|
|
|
|
|
origin_load = PluginManager.load_plugin
|
|
|
|
|
|
|
|
def _patched_load(self: PluginManager, name: str):
|
|
|
|
assert self is m
|
|
|
|
return origin_load(self, name)
|
|
|
|
|
|
|
|
monkeypatch.setattr(PluginManager, "load_plugin", _patched_load)
|
|
|
|
|
|
|
|
nonebot.require("plugins.export")
|
|
|
|
|
|
|
|
assert len(_managers) == 1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_require_not_declared(app: App):
|
|
|
|
import nonebot
|
|
|
|
from nonebot.plugin import _managers
|
|
|
|
|
|
|
|
nonebot.require("plugins.export")
|
|
|
|
|
|
|
|
assert len(_managers) == 1
|
|
|
|
assert _managers[-1].plugins == {"plugins.export"}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_require_not_found(app: App):
|
|
|
|
import nonebot
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
with pytest.raises(RuntimeError):
|
2022-01-26 15:37:35 +08:00
|
|
|
nonebot.require("some_plugin_not_exist")
|
2022-06-20 15:49:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_plugin_metadata(app: App, load_plugin: Set["Plugin"]):
|
|
|
|
import nonebot
|
|
|
|
from plugins.metadata import Config
|
|
|
|
|
|
|
|
plugin = nonebot.get_plugin("metadata")
|
|
|
|
assert plugin
|
|
|
|
assert plugin.metadata
|
|
|
|
assert asdict(plugin.metadata) == {
|
|
|
|
"name": "测试插件",
|
|
|
|
"description": "测试插件元信息",
|
|
|
|
"usage": "无法使用",
|
|
|
|
"config": Config,
|
|
|
|
"extra": {"author": "NoneBot"},
|
|
|
|
}
|