From dc6c19470138262985b69bcdc8745748b8e235d1 Mon Sep 17 00:00:00 2001 From: Akirami <66513481+A-kirami@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:13:35 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feature:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E4=BA=8B=E4=BB=B6=E5=93=8D=E5=BA=94=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E6=97=A5=E5=BF=97=20(#2355)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- nonebot/message.py | 15 +++++++-- tests/test_matcher/test_matcher.py | 51 ++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/nonebot/message.py b/nonebot/message.py index b1c18706..24bf1dbb 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -358,9 +358,18 @@ async def _check_matcher( return False try: - if not await Matcher.check_perm( - bot, event, stack, dependency_cache - ) or not await Matcher.check_rule(bot, event, state, stack, dependency_cache): + if not await Matcher.check_perm(bot, event, stack, dependency_cache): + logger.trace(f"Permission conditions not met for {Matcher}") + return False + except Exception as e: + logger.opt(colors=True, exception=e).error( + f"Permission check failed for {Matcher}." + ) + return False + + try: + if not await Matcher.check_rule(bot, event, state, stack, dependency_cache): + logger.trace(f"Rule conditions not met for {Matcher}") return False except Exception as e: logger.opt(colors=True, exception=e).error( diff --git a/tests/test_matcher/test_matcher.py b/tests/test_matcher/test_matcher.py index a86327c2..89fc1df6 100644 --- a/tests/test_matcher/test_matcher.py +++ b/tests/test_matcher/test_matcher.py @@ -4,11 +4,12 @@ from pathlib import Path import pytest from nonebug import App +from nonebot.rule import Rule from nonebot import get_plugin -from nonebot.permission import User from nonebot.matcher import Matcher, matchers from utils import FakeMessage, make_fake_event -from nonebot.message import check_and_run_matcher +from nonebot.permission import User, Permission +from nonebot.message import _check_matcher, check_and_run_matcher @pytest.mark.asyncio @@ -40,6 +41,50 @@ async def test_matcher_info(app: App): assert matcher._source.lineno == 3 +@pytest.mark.asyncio +async def test_matcher_check(app: App): + async def falsy(): + return False + + async def truthy(): + return True + + async def error(): + raise RuntimeError + + event = make_fake_event(_type="test")() + with app.provider.context({}): + test_perm_falsy = Matcher.new(permission=Permission(falsy)) + async with app.test_api() as ctx: + bot = ctx.create_bot() + assert await _check_matcher(test_perm_falsy, bot, event, {}) is False + + test_perm_truthy = Matcher.new(permission=Permission(truthy)) + async with app.test_api() as ctx: + bot = ctx.create_bot() + assert await _check_matcher(test_perm_truthy, bot, event, {}) is True + + test_perm_error = Matcher.new(permission=Permission(error)) + async with app.test_api() as ctx: + bot = ctx.create_bot() + assert await _check_matcher(test_perm_error, bot, event, {}) is False + + test_rule_falsy = Matcher.new(rule=Rule(falsy)) + async with app.test_api() as ctx: + bot = ctx.create_bot() + assert await _check_matcher(test_rule_falsy, bot, event, {}) is False + + test_rule_truthy = Matcher.new(rule=Rule(truthy)) + async with app.test_api() as ctx: + bot = ctx.create_bot() + assert await _check_matcher(test_rule_truthy, bot, event, {}) is True + + test_rule_error = Matcher.new(rule=Rule(error)) + async with app.test_api() as ctx: + bot = ctx.create_bot() + assert await _check_matcher(test_rule_error, bot, event, {}) is False + + @pytest.mark.asyncio async def test_matcher_handle(app: App): from plugins.matcher.matcher_process import test_handle @@ -95,7 +140,7 @@ async def test_matcher_receive(app: App): @pytest.mark.asyncio -async def test_matcher_(app: App): +async def test_matcher_combine(app: App): from plugins.matcher.matcher_process import test_combine message = FakeMessage("text")