Feature: 优化检查事件响应器的日志 (#2355)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Akirami 2023-09-12 15:13:35 +08:00 committed by GitHub
parent 9b8772b590
commit dc6c194701
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 6 deletions

View File

@ -358,9 +358,18 @@ async def _check_matcher(
return False return False
try: try:
if not await Matcher.check_perm( if not await Matcher.check_perm(bot, event, stack, dependency_cache):
bot, event, stack, dependency_cache logger.trace(f"Permission conditions not met for {Matcher}")
) or not await Matcher.check_rule(bot, event, state, stack, dependency_cache): return False
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Permission check failed for {Matcher}.</bg #f8bbd0></r>"
)
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 return False
except Exception as e: except Exception as e:
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(

View File

@ -4,11 +4,12 @@ from pathlib import Path
import pytest import pytest
from nonebug import App from nonebug import App
from nonebot.rule import Rule
from nonebot import get_plugin from nonebot import get_plugin
from nonebot.permission import User
from nonebot.matcher import Matcher, matchers from nonebot.matcher import Matcher, matchers
from utils import FakeMessage, make_fake_event 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 @pytest.mark.asyncio
@ -40,6 +41,50 @@ async def test_matcher_info(app: App):
assert matcher._source.lineno == 3 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 @pytest.mark.asyncio
async def test_matcher_handle(app: App): async def test_matcher_handle(app: App):
from plugins.matcher.matcher_process import test_handle from plugins.matcher.matcher_process import test_handle
@ -95,7 +140,7 @@ async def test_matcher_receive(app: App):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_matcher_(app: App): async def test_matcher_combine(app: App):
from plugins.matcher.matcher_process import test_combine from plugins.matcher.matcher_process import test_combine
message = FakeMessage("text") message = FakeMessage("text")