From 770e808f1d1c2a30461190f621df1e815256ad33 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Wed, 26 Jan 2022 15:37:35 +0800 Subject: [PATCH] :white_check_mark: add require tests --- nonebot/plugin/manager.py | 2 +- tests/test_init.py | 27 ----------- tests/test_plugin/test_load.py | 89 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 28 deletions(-) create mode 100644 tests/test_plugin/test_load.py diff --git a/nonebot/plugin/manager.py b/nonebot/plugin/manager.py index f359db57..260ce9fc 100644 --- a/nonebot/plugin/manager.py +++ b/nonebot/plugin/manager.py @@ -98,7 +98,7 @@ class PluginManager: return third_party_plugins | set(self.searched_plugins.keys()) - def load_plugin(self, name) -> Optional[Plugin]: + def load_plugin(self, name: str) -> Optional[Plugin]: try: if name in self.plugins: module = importlib.import_module(name) diff --git a/tests/test_init.py b/tests/test_init.py index 1d241728..6d36dccd 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,12 +1,7 @@ import os -import sys -from typing import TYPE_CHECKING, Set import pytest -if TYPE_CHECKING: - from nonebot.plugin import Plugin - os.environ["CONFIG_FROM_ENV"] = '{"test": "test"}' @@ -74,25 +69,3 @@ async def test_get(monkeypatch: pytest.MonkeyPatch, nonebug_clear): assert get_bot() == "test" assert get_bot("test") == "test" assert get_bots() == {"test": "test"} - - -@pytest.mark.asyncio -async def test_load_plugin(load_plugin: Set["Plugin"]): - import nonebot - - loaded_plugins = set( - plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin - ) - assert loaded_plugins == load_plugin - plugin = nonebot.get_plugin("export") - assert plugin - assert plugin.module_name == "plugins.export" - assert "plugins.export" in sys.modules - - try: - nonebot.load_plugin("plugins.export") - assert False - except RuntimeError: - assert True - - assert nonebot.load_plugin("some_plugin_no_exist") is None diff --git a/tests/test_plugin/test_load.py b/tests/test_plugin/test_load.py new file mode 100644 index 00000000..c0c0c8f6 --- /dev/null +++ b/tests/test_plugin/test_load.py @@ -0,0 +1,89 @@ +import sys +from typing import TYPE_CHECKING, Set + +import pytest +from nonebug import App + +if TYPE_CHECKING: + from nonebot.plugin import Plugin + + +@pytest.mark.asyncio +async def test_load_plugin(load_plugin: Set["Plugin"]): + import nonebot + + loaded_plugins = set( + plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin + ) + assert loaded_plugins == load_plugin + plugin = nonebot.get_plugin("export") + assert plugin + assert plugin.module_name == "plugins.export" + assert "plugins.export" in sys.modules + + try: + nonebot.load_plugin("plugins.export") + assert False + except RuntimeError: + assert True + + assert nonebot.load_plugin("some_plugin_not_exist") is None + + +@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 + from nonebot.plugin import _managers + from nonebot.plugin.manager import PluginManager + + 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 + from nonebot.plugin import _managers + + try: + nonebot.require("some_plugin_not_exist") + assert False + except RuntimeError: + assert True