mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
✨ Feature: 优化检查事件响应器的日志 (#2355)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
9b8772b590
commit
dc6c194701
@ -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(
|
||||||
|
@ -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")
|
||||||
|
Loading…
Reference in New Issue
Block a user