mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 12:06:45 +00:00
🐛 detect runtime plugin (#1857)
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
from typing import Type, Optional
|
||||
from typing import Type, Callable, Optional
|
||||
|
||||
import pytest
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.typing import T_RuleChecker
|
||||
from nonebot.matcher import Matcher, matchers
|
||||
from nonebot.rule import (
|
||||
@ -18,7 +19,74 @@ from nonebot.rule import (
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on():
|
||||
@pytest.mark.parametrize(
|
||||
"matcher_name, pre_rule_factory, has_permission",
|
||||
[
|
||||
pytest.param("matcher_on", None, True),
|
||||
pytest.param("matcher_on_metaevent", None, False),
|
||||
pytest.param("matcher_on_message", None, True),
|
||||
pytest.param("matcher_on_notice", None, False),
|
||||
pytest.param("matcher_on_request", None, False),
|
||||
pytest.param(
|
||||
"matcher_on_startswith", lambda e: StartswithRule(("test",)), True
|
||||
),
|
||||
pytest.param("matcher_on_endswith", lambda e: EndswithRule(("test",)), True),
|
||||
pytest.param("matcher_on_fullmatch", lambda e: FullmatchRule(("test",)), True),
|
||||
pytest.param("matcher_on_keyword", lambda e: KeywordsRule("test"), True),
|
||||
pytest.param("matcher_on_command", lambda e: CommandRule([("test",)]), True),
|
||||
pytest.param(
|
||||
"matcher_on_shell_command",
|
||||
lambda e: ShellCommandRule([("test",)], None),
|
||||
True,
|
||||
),
|
||||
pytest.param("matcher_on_regex", lambda e: RegexRule("test"), True),
|
||||
pytest.param("matcher_on_type", lambda e: IsTypeRule(e), True),
|
||||
pytest.param("matcher_sub_cmd", lambda e: CommandRule([("test", "sub")]), True),
|
||||
pytest.param(
|
||||
"matcher_sub_shell_cmd",
|
||||
lambda e: ShellCommandRule([("test", "sub")], None),
|
||||
True,
|
||||
),
|
||||
pytest.param("matcher_group_on", None, True),
|
||||
pytest.param("matcher_group_on_metaevent", None, False),
|
||||
pytest.param("matcher_group_on_message", None, True),
|
||||
pytest.param("matcher_group_on_notice", None, False),
|
||||
pytest.param("matcher_group_on_request", None, False),
|
||||
pytest.param(
|
||||
"matcher_group_on_startswith",
|
||||
lambda e: StartswithRule(("test",)),
|
||||
True,
|
||||
),
|
||||
pytest.param(
|
||||
"matcher_group_on_endswith",
|
||||
lambda e: EndswithRule(("test",)),
|
||||
True,
|
||||
),
|
||||
pytest.param(
|
||||
"matcher_group_on_fullmatch",
|
||||
lambda e: FullmatchRule(("test",)),
|
||||
True,
|
||||
),
|
||||
pytest.param("matcher_group_on_keyword", lambda e: KeywordsRule("test"), True),
|
||||
pytest.param(
|
||||
"matcher_group_on_command",
|
||||
lambda e: CommandRule([("test",)]),
|
||||
True,
|
||||
),
|
||||
pytest.param(
|
||||
"matcher_group_on_shell_command",
|
||||
lambda e: ShellCommandRule([("test",)], None),
|
||||
True,
|
||||
),
|
||||
pytest.param("matcher_group_on_regex", lambda e: RegexRule("test"), True),
|
||||
pytest.param("matcher_group_on_type", lambda e: IsTypeRule(e), True),
|
||||
],
|
||||
)
|
||||
async def test_on(
|
||||
matcher_name: str,
|
||||
pre_rule_factory: Optional[Callable[[Type[Event]], T_RuleChecker]],
|
||||
has_permission: bool,
|
||||
):
|
||||
import plugins.plugin.matchers as module
|
||||
from plugins.plugin.matchers import (
|
||||
TestEvent,
|
||||
@ -26,91 +94,56 @@ async def test_on():
|
||||
state,
|
||||
handler,
|
||||
priority,
|
||||
matcher_on,
|
||||
permission,
|
||||
expire_time,
|
||||
matcher_on_type,
|
||||
matcher_sub_cmd,
|
||||
matcher_group_on,
|
||||
matcher_on_regex,
|
||||
matcher_on_notice,
|
||||
matcher_on_command,
|
||||
matcher_on_keyword,
|
||||
matcher_on_message,
|
||||
matcher_on_request,
|
||||
matcher_on_endswith,
|
||||
matcher_on_fullmatch,
|
||||
matcher_on_metaevent,
|
||||
matcher_group_on_type,
|
||||
matcher_on_startswith,
|
||||
matcher_sub_shell_cmd,
|
||||
matcher_group_on_regex,
|
||||
matcher_group_on_notice,
|
||||
matcher_group_on_command,
|
||||
matcher_group_on_keyword,
|
||||
matcher_group_on_message,
|
||||
matcher_group_on_request,
|
||||
matcher_on_shell_command,
|
||||
matcher_group_on_endswith,
|
||||
matcher_group_on_fullmatch,
|
||||
matcher_group_on_metaevent,
|
||||
matcher_group_on_startswith,
|
||||
matcher_group_on_shell_command,
|
||||
)
|
||||
|
||||
matcher = getattr(module, matcher_name)
|
||||
assert issubclass(matcher, Matcher), f"{matcher_name} should be a Matcher"
|
||||
|
||||
pre_rule = pre_rule_factory(TestEvent) if pre_rule_factory else None
|
||||
|
||||
plugin = nonebot.get_plugin("plugin")
|
||||
assert plugin, "plugin should be loaded"
|
||||
|
||||
def _check(
|
||||
matcher: Type[Matcher],
|
||||
pre_rule: Optional[T_RuleChecker],
|
||||
has_permission: bool,
|
||||
):
|
||||
assert {dependent.call for dependent in matcher.rule.checkers} == (
|
||||
{pre_rule, rule} if pre_rule else {rule}
|
||||
)
|
||||
if has_permission:
|
||||
assert {dependent.call for dependent in matcher.permission.checkers} == {
|
||||
permission
|
||||
}
|
||||
else:
|
||||
assert not matcher.permission.checkers
|
||||
assert [dependent.call for dependent in matcher.handlers] == [handler]
|
||||
assert matcher.temp is True
|
||||
assert matcher.expire_time == expire_time
|
||||
assert matcher in matchers[priority]
|
||||
assert matcher.block is True
|
||||
assert matcher._default_state == state
|
||||
assert {dependent.call for dependent in matcher.rule.checkers} == (
|
||||
{pre_rule, rule} if pre_rule else {rule}
|
||||
)
|
||||
if has_permission:
|
||||
assert {dependent.call for dependent in matcher.permission.checkers} == {
|
||||
permission
|
||||
}
|
||||
else:
|
||||
assert not matcher.permission.checkers
|
||||
assert [dependent.call for dependent in matcher.handlers] == [handler]
|
||||
assert matcher.temp is True
|
||||
assert matcher.expire_time == expire_time
|
||||
assert matcher in matchers[priority]
|
||||
assert matcher.block is True
|
||||
assert matcher._default_state == state
|
||||
|
||||
assert matcher.plugin is plugin
|
||||
assert matcher in plugin.matcher
|
||||
assert matcher.module is module
|
||||
assert matcher.plugin_name == "plugin"
|
||||
assert matcher.module_name == "plugins.plugin.matchers"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_runtime_on():
|
||||
import plugins.plugin.matchers as module
|
||||
from plugins.plugin.matchers import matcher_on_factory
|
||||
|
||||
matcher = matcher_on_factory()
|
||||
|
||||
plugin = nonebot.get_plugin("plugin")
|
||||
assert plugin, "plugin should be loaded"
|
||||
|
||||
try:
|
||||
assert matcher.plugin is plugin
|
||||
assert matcher not in plugin.matcher
|
||||
assert matcher.module is module
|
||||
assert matcher.plugin_name == "plugin"
|
||||
assert matcher.module_name == "plugins.plugin.matchers"
|
||||
|
||||
_check(matcher_on, None, True)
|
||||
_check(matcher_on_metaevent, None, False)
|
||||
_check(matcher_on_message, None, True)
|
||||
_check(matcher_on_notice, None, False)
|
||||
_check(matcher_on_request, None, False)
|
||||
_check(matcher_on_startswith, StartswithRule(("test",)), True)
|
||||
_check(matcher_on_endswith, EndswithRule(("test",)), True)
|
||||
_check(matcher_on_fullmatch, FullmatchRule(("test",)), True)
|
||||
_check(matcher_on_keyword, KeywordsRule("test"), True)
|
||||
_check(matcher_on_command, CommandRule([("test",)]), True)
|
||||
_check(matcher_on_shell_command, ShellCommandRule([("test",)], None), True)
|
||||
_check(matcher_on_regex, RegexRule("test"), True)
|
||||
_check(matcher_on_type, IsTypeRule(TestEvent), True)
|
||||
_check(matcher_sub_cmd, CommandRule([("test", "sub")]), True)
|
||||
_check(matcher_sub_shell_cmd, ShellCommandRule([("test", "sub")], None), True)
|
||||
_check(matcher_group_on, None, True)
|
||||
_check(matcher_group_on_metaevent, None, False)
|
||||
_check(matcher_group_on_message, None, True)
|
||||
_check(matcher_group_on_notice, None, False)
|
||||
_check(matcher_group_on_request, None, False)
|
||||
_check(matcher_group_on_startswith, StartswithRule(("test",)), True)
|
||||
_check(matcher_group_on_endswith, EndswithRule(("test",)), True)
|
||||
_check(matcher_group_on_fullmatch, FullmatchRule(("test",)), True)
|
||||
_check(matcher_group_on_keyword, KeywordsRule("test"), True)
|
||||
_check(matcher_group_on_command, CommandRule([("test",)]), True)
|
||||
_check(matcher_group_on_shell_command, ShellCommandRule([("test",)], None), True)
|
||||
_check(matcher_group_on_regex, RegexRule("test"), True)
|
||||
_check(matcher_group_on_type, IsTypeRule(TestEvent), True)
|
||||
finally:
|
||||
matcher.destroy()
|
||||
|
Reference in New Issue
Block a user