♻️ rewrite dependency injection system

This commit is contained in:
yanyongyu
2021-12-12 18:19:08 +08:00
parent 6b5a5e53eb
commit 66ba25494a
17 changed files with 728 additions and 733 deletions

23
tests/plugins/depends.py Normal file
View File

@ -0,0 +1,23 @@
from nonebot import on_message
from nonebot.adapters import Event
from nonebot.params import Depends
test = on_message()
test2 = on_message()
runned = False
def dependency(event: Event):
# test cache
global runned
assert not runned
runned = True
return event
@test.handle()
@test2.handle()
async def handle(x: Event = Depends(dependency)):
# test dependency
return x

View File

@ -1,6 +1,12 @@
import os
import sys
from typing import TYPE_CHECKING, Set
import pytest
from utils import load_plugin
if TYPE_CHECKING:
from nonebot.plugin import Plugin
os.environ["CONFIG_FROM_ENV"] = "env"
@ -17,3 +23,14 @@ async def test_init(nonebug_init):
assert config.config_from_env == "env"
assert config.config_from_init == "init"
assert config.common_config == "common"
@pytest.mark.asyncio
async def test_load_plugin(load_plugin: Set["Plugin"]):
import nonebot
assert nonebot.get_loaded_plugins() == load_plugin
plugin = nonebot.get_plugin("depends")
assert plugin
assert plugin.module_name == "plugins.depends"
assert "plugins.depends" in sys.modules

14
tests/utils.py Normal file
View File

@ -0,0 +1,14 @@
from pathlib import Path
from typing import TYPE_CHECKING, Set
import pytest
if TYPE_CHECKING:
from nonebot.plugin import Plugin
@pytest.fixture
def load_plugin(nonebug_init: None) -> Set["Plugin"]:
import nonebot
return nonebot.load_plugins(str(Path(__file__).parent / "plugins"))