🐛 fix duplicate plugin check

This commit is contained in:
yanyongyu 2021-12-21 11:18:34 +08:00
parent cf8670c167
commit b900133ab4
2 changed files with 20 additions and 4 deletions

View File

@ -53,6 +53,15 @@ class PluginManager:
# get all previous ready to load plugins
previous_plugins = self._previous_plugins()
searched_plugins: Dict[str, Path] = {}
third_party_plugins: Set[str] = set()
for plugin in self.plugins:
name = plugin.rsplit(".", 1)[-1] if "." in plugin else plugin
if name in third_party_plugins or name in previous_plugins:
raise RuntimeError(
f"Plugin already exists: {name}! Check your plugin name"
)
third_party_plugins.add(plugin)
for module_info in pkgutil.iter_modules(self.search_path):
if module_info.name.startswith("_"):
@ -60,6 +69,7 @@ class PluginManager:
if (
module_info.name in searched_plugins.keys()
or module_info.name in previous_plugins
or module_info.name in third_party_plugins
):
raise RuntimeError(
f"Plugin already exists: {module_info.name}! Check your plugin name"
@ -74,7 +84,7 @@ class PluginManager:
self.searched_plugins = searched_plugins
return self.plugins | set(self.searched_plugins.keys())
return third_party_plugins | set(self.searched_plugins.keys())
def load_plugin(self, name) -> Optional[Plugin]:
try:

View File

@ -83,7 +83,13 @@ async def test_load_plugin(load_plugin: Set["Plugin"]):
plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin
)
assert loaded_plugins == load_plugin
plugin = nonebot.get_plugin("param_depend")
plugin = nonebot.get_plugin("export")
assert plugin
assert plugin.module_name == "plugins.param.param_depend"
assert "plugins.param.param_depend" in sys.modules
assert plugin.module_name == "plugins.export"
assert "plugins.export" in sys.modules
try:
nonebot.load_plugin("plugins.export")
assert False
except RuntimeError:
assert True