mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 02:50:48 +00:00
✨ Feature: 嵌套插件名称作用域优化 (#2665)
This commit is contained in:
0
tests/dynamic/require_not_loaded/subplugin1.py
Normal file
0
tests/dynamic/require_not_loaded/subplugin1.py
Normal file
0
tests/dynamic/require_not_loaded/subplugin2.py
Normal file
0
tests/dynamic/require_not_loaded/subplugin2.py
Normal file
@ -8,5 +8,5 @@ manager = PluginManager(
|
||||
_managers.append(manager)
|
||||
|
||||
# test load nested plugin with require
|
||||
manager.load_plugin("nested_subplugin")
|
||||
manager.load_plugin("nested_subplugin2")
|
||||
manager.load_plugin("plugins.nested.plugins.nested_subplugin")
|
||||
manager.load_plugin("nested:nested_subplugin2")
|
||||
|
@ -29,8 +29,10 @@ async def test_matcher_info(app: App):
|
||||
assert matcher.module is sys.modules["plugins.matcher.matcher_info"]
|
||||
assert matcher.module_name == "plugins.matcher.matcher_info"
|
||||
|
||||
assert matcher._source.plugin_id == "matcher:matcher_info"
|
||||
assert matcher._source.plugin_name == "matcher_info"
|
||||
assert matcher.plugin is get_plugin("matcher_info")
|
||||
assert matcher.plugin is get_plugin("matcher:matcher_info")
|
||||
assert matcher.plugin_id == "matcher:matcher_info"
|
||||
assert matcher.plugin_name == "matcher_info"
|
||||
|
||||
assert (
|
||||
|
@ -10,18 +10,43 @@ async def test_get_plugin():
|
||||
# check simple plugin
|
||||
plugin = nonebot.get_plugin("export")
|
||||
assert plugin
|
||||
assert plugin.id_ == "export"
|
||||
assert plugin.name == "export"
|
||||
assert plugin.module_name == "plugins.export"
|
||||
|
||||
# check sub plugin
|
||||
plugin = nonebot.get_plugin("nested_subplugin")
|
||||
plugin = nonebot.get_plugin("nested:nested_subplugin")
|
||||
assert plugin
|
||||
assert plugin.id_ == "nested:nested_subplugin"
|
||||
assert plugin.name == "nested_subplugin"
|
||||
assert plugin.module_name == "plugins.nested.plugins.nested_subplugin"
|
||||
|
||||
# check get plugin by module name
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_plugin_by_module_name():
|
||||
# check get plugin by exact module name
|
||||
plugin = nonebot.get_plugin_by_module_name("plugins.nested")
|
||||
assert plugin
|
||||
assert plugin.id_ == "nested"
|
||||
assert plugin.name == "nested"
|
||||
assert plugin.module_name == "plugins.nested"
|
||||
|
||||
# check get plugin by sub module name
|
||||
plugin = nonebot.get_plugin_by_module_name("plugins.nested.utils")
|
||||
assert plugin
|
||||
assert plugin.id_ == "nested"
|
||||
assert plugin.name == "nested"
|
||||
assert plugin.module_name == "plugins.nested"
|
||||
|
||||
# check get plugin by sub plugin exact module name
|
||||
plugin = nonebot.get_plugin_by_module_name(
|
||||
"plugins.nested.plugins.nested_subplugin"
|
||||
)
|
||||
assert plugin
|
||||
assert plugin.id_ == "nested:nested_subplugin"
|
||||
assert plugin.name == "nested_subplugin"
|
||||
assert plugin.module_name == "plugins.nested.plugins.nested_subplugin"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_available_plugin():
|
||||
@ -31,8 +56,8 @@ async def test_get_available_plugin():
|
||||
_managers.append(PluginManager(["plugins.export", "plugin.require"]))
|
||||
|
||||
# check get available plugins
|
||||
plugin_names = nonebot.get_available_plugin_names()
|
||||
assert plugin_names == {"export", "require"}
|
||||
plugin_ids = nonebot.get_available_plugin_names()
|
||||
assert plugin_ids == {"export", "require"}
|
||||
finally:
|
||||
_managers.clear()
|
||||
_managers.extend(old_managers)
|
||||
|
@ -29,12 +29,13 @@ async def test_load_plugins(load_plugin: set[Plugin], load_builtin_plugin: set[P
|
||||
|
||||
# check simple plugin
|
||||
assert "plugins.export" in sys.modules
|
||||
assert "plugin._hidden" not in sys.modules
|
||||
|
||||
# check sub plugin
|
||||
plugin = nonebot.get_plugin("nested_subplugin")
|
||||
plugin = nonebot.get_plugin("nested:nested_subplugin")
|
||||
assert plugin
|
||||
assert "plugins.nested.plugins.nested_subplugin" in sys.modules
|
||||
assert plugin.parent_plugin == nonebot.get_plugin("nested")
|
||||
assert plugin.parent_plugin is nonebot.get_plugin("nested")
|
||||
|
||||
# check load again
|
||||
with pytest.raises(RuntimeError):
|
||||
@ -46,8 +47,8 @@ async def test_load_plugins(load_plugin: set[Plugin], load_builtin_plugin: set[P
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_nested_plugin():
|
||||
parent_plugin = nonebot.get_plugin("nested")
|
||||
sub_plugin = nonebot.get_plugin("nested_subplugin")
|
||||
sub_plugin2 = nonebot.get_plugin("nested_subplugin2")
|
||||
sub_plugin = nonebot.get_plugin("nested:nested_subplugin")
|
||||
sub_plugin2 = nonebot.get_plugin("nested:nested_subplugin2")
|
||||
assert parent_plugin
|
||||
assert sub_plugin
|
||||
assert sub_plugin2
|
||||
@ -89,12 +90,16 @@ async def test_require_loaded(monkeypatch: pytest.MonkeyPatch):
|
||||
|
||||
monkeypatch.setattr("nonebot.plugin.load._find_manager_by_name", _patched_find)
|
||||
|
||||
# require use module name
|
||||
nonebot.require("plugins.export")
|
||||
# require use plugin id
|
||||
nonebot.require("export")
|
||||
nonebot.require("nested:nested_subplugin")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_require_not_loaded(monkeypatch: pytest.MonkeyPatch):
|
||||
m = PluginManager(["dynamic.require_not_loaded"])
|
||||
m = PluginManager(["dynamic.require_not_loaded"], ["dynamic/require_not_loaded/"])
|
||||
_managers.append(m)
|
||||
num_managers = len(_managers)
|
||||
|
||||
@ -106,7 +111,11 @@ async def test_require_not_loaded(monkeypatch: pytest.MonkeyPatch):
|
||||
|
||||
monkeypatch.setattr(PluginManager, "load_plugin", _patched_load)
|
||||
|
||||
# require standalone plugin
|
||||
nonebot.require("dynamic.require_not_loaded")
|
||||
# require searched plugin
|
||||
nonebot.require("dynamic.require_not_loaded.subplugin1")
|
||||
nonebot.require("require_not_loaded:subplugin2")
|
||||
|
||||
assert len(_managers) == num_managers
|
||||
|
||||
|
@ -1,11 +1,17 @@
|
||||
import pytest
|
||||
|
||||
from nonebot.plugin import PluginManager
|
||||
from nonebot.plugin import PluginManager, _managers
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_plugin_name():
|
||||
m = PluginManager(plugins=["dynamic.manager"])
|
||||
_managers.append(m)
|
||||
|
||||
# load by plugin id
|
||||
module1 = m.load_plugin("manager")
|
||||
# load by module name
|
||||
module2 = m.load_plugin("dynamic.manager")
|
||||
assert module1
|
||||
assert module2
|
||||
assert module1 is module2
|
||||
|
@ -145,6 +145,7 @@ async def test_on(
|
||||
assert matcher.plugin is plugin
|
||||
assert matcher in plugin.matcher
|
||||
assert matcher.module is module
|
||||
assert matcher.plugin_id == "plugin"
|
||||
assert matcher.plugin_name == "plugin"
|
||||
assert matcher.module_name == "plugins.plugin.matchers"
|
||||
|
||||
@ -163,6 +164,7 @@ async def test_runtime_on():
|
||||
assert matcher.plugin is plugin
|
||||
assert matcher not in plugin.matcher
|
||||
assert matcher.module is module
|
||||
assert matcher.plugin_id == "plugin"
|
||||
assert matcher.plugin_name == "plugin"
|
||||
assert matcher.module_name == "plugins.plugin.matchers"
|
||||
finally:
|
||||
|
Reference in New Issue
Block a user