From 4291cda244e9947b628d58185b25022c9aece56f Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Mon, 23 Dec 2024 10:09:06 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=87=AA=E6=A3=80=E6=8F=92=E4=BB=B6=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=84=E5=88=99=E5=88=A4=E6=96=AD=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0nonebug=E9=85=8D=E7=BD=AE=20(#1792)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :arrow_up: Expand the range of nonebug version restrictions and update nonebot-plugin-alconna. * :white_check_mark: Update pytest configuration. * :white_check_mark: Add pytest hook to tag async tests with session-scoped event loop. * ⚡️ 优化自检插件逻辑,增加规则判断 --------- Co-authored-by: BalconyJH --- poetry.lock | 22 ++++----- pyproject.toml | 3 +- requirements.txt | 2 +- tests/conftest.py | 8 +++ zhenxun/builtin_plugins/check/__init__.py | 60 +++++++++++++---------- 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/poetry.lock b/poetry.lock index e04606b2..861717b2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2227,23 +2227,23 @@ reference = "aliyun" [[package]] name = "nonebot-plugin-alconna" -version = "0.53.1" +version = "0.54.1" description = "Alconna Adapter for Nonebot" optional = false python-versions = ">=3.9" files = [ - {file = "nonebot_plugin_alconna-0.53.1-py3-none-any.whl", hash = "sha256:a7734d4c7f1b2fedec586b7d235da119ffaa23d0e17a8aa0a0acc0e76a1662d0"}, - {file = "nonebot_plugin_alconna-0.53.1.tar.gz", hash = "sha256:dc9eef228bb819d05ce9816f8a5a1bead455ee85d1ea7d5e409f9fb11665805e"}, + {file = "nonebot_plugin_alconna-0.54.1-py3-none-any.whl", hash = "sha256:4edb4b081cd64ce37717c7a92d31aadd2cf287a5a0adc2ac86ed82d9bcad5048"}, + {file = "nonebot_plugin_alconna-0.54.1.tar.gz", hash = "sha256:66fae03120b8eff25bb0027d65f149e399aa6f73c7585ebdd388d1904cecdeee"}, ] [package.dependencies] -arclet-alconna = ">=1.8.31,<2.0" +arclet-alconna = ">=1.8.35,<2.0" arclet-alconna-tools = ">=0.7.10" importlib-metadata = ">=4.13.0" -nepattern = ">=0.7.4,<1.0" +nepattern = ">=0.7.7,<1.0" nonebot-plugin-waiter = ">=0.6.0" nonebot2 = ">=2.3.0" -tarina = ">=0.6.3,<0.7" +tarina = ">=0.6.8,<0.7" [package.source] type = "legacy" @@ -2420,13 +2420,13 @@ reference = "aliyun" [[package]] name = "nonebug" -version = "0.3.7" +version = "0.4.3" description = "nonebot2 test framework" optional = false -python-versions = ">=3.8,<4.0" +python-versions = ">=3.9,<4.0" files = [ - {file = "nonebug-0.3.7-py3-none-any.whl", hash = "sha256:c39f462aafe20660602a8b789a575db6c9346ab5b6f1985eb9d98b861528299a"}, - {file = "nonebug-0.3.7.tar.gz", hash = "sha256:8a75183400681f34eafc7caa2bb6dd511c3b5660c59264f1c379a088c7ac2247"}, + {file = "nonebug-0.4.3-py3-none-any.whl", hash = "sha256:eb9b2c8ab3d45459a4f00ebdaae90729e9e9628575c0685fca4c871dd4cfd425"}, + {file = "nonebug-0.4.3.tar.gz", hash = "sha256:e9592d2c7a42b76f4a336f98726cba92e1300f6bab155c8822e865919786f10c"}, ] [package.dependencies] @@ -4932,4 +4932,4 @@ reference = "aliyun" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "d2be32cc7ba17b302a015de327b8e8d815bee185400547f4024fd93e141d3ac8" +content-hash = "37341271bbd43fcb6af878cb09491459cf21b13c24659a5fd94f9c2b5248a5d8" diff --git a/pyproject.toml b/pyproject.toml index 79545671..c45473f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,7 @@ nonebot-plugin-uninfo = "^0.4.1" nonebot-plugin-alconna = "^0.54.0" [tool.poetry.dev-dependencies] -nonebug = "^0.4.2" +nonebug = "^0.4" pytest-cov = "^5.0.0" pytest-mock = "^3.6.1" pytest-asyncio = "^0.23.5" @@ -137,6 +137,7 @@ disableBytesTypePromotions = true [tool.pytest.ini_options] asyncio_mode = "auto" +asyncio_default_fixture_loop_scope = "session" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/requirements.txt b/requirements.txt index e3bbf0ab..75f1aa73 100644 --- a/requirements.txt +++ b/requirements.txt @@ -57,7 +57,7 @@ nonebot-adapter-discord==0.1.8 ; python_version >= "3.10" and python_version < " nonebot-adapter-dodo==0.1.4 ; python_version >= "3.10" and python_version < "4.0" nonebot-adapter-kaiheila==0.3.4 ; python_version >= "3.10" and python_version < "4.0" nonebot-adapter-onebot==2.4.6 ; python_version >= "3.10" and python_version < "4.0" -nonebot-plugin-alconna==0.53.1 ; python_version >= "3.10" and python_version < "4.0" +nonebot-plugin-alconna==0.54.1 ; python_version >= "3.10" and python_version < "4.0" nonebot-plugin-apscheduler==0.3.0 ; python_version >= "3.10" and python_version < "4.0" nonebot-plugin-htmlrender==0.3.5 ; python_version >= "3.10" and python_version < "4.0" nonebot-plugin-session==0.2.3 ; python_version >= "3.10" and python_version < "4.0" diff --git a/tests/conftest.py b/tests/conftest.py index a1b7bf2f..cf29b754 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ from nonebug import NONEBOT_INIT_KWARGS from nonebug.app import App from nonebug.mixin.process import MatcherContext import pytest +from pytest_asyncio import is_async_test from pytest_mock import MockerFixture from respx import MockRouter @@ -22,6 +23,13 @@ def get_response_json(path: str) -> dict: ) +def pytest_collection_modifyitems(items: list[pytest.Item]): + pytest_asyncio_tests = (item for item in items if is_async_test(item)) + session_scope_marker = pytest.mark.asyncio(loop_scope="session") + for async_test in pytest_asyncio_tests: + async_test.add_marker(session_scope_marker, append=False) + + def pytest_configure(config: pytest.Config) -> None: config.stash[NONEBOT_INIT_KWARGS] = { "driver": "~fastapi+~httpx+~websockets", diff --git a/zhenxun/builtin_plugins/check/__init__.py b/zhenxun/builtin_plugins/check/__init__.py index 047d44f6..1d4d0628 100644 --- a/zhenxun/builtin_plugins/check/__init__.py +++ b/zhenxun/builtin_plugins/check/__init__.py @@ -2,7 +2,7 @@ from nonebot import on_notice from nonebot.adapters.onebot.v11 import PokeNotifyEvent from nonebot.permission import SUPERUSER from nonebot.plugin import PluginMetadata -from nonebot.rule import to_me +from nonebot.rule import Rule, to_me from nonebot_plugin_alconna import Alconna, on_alconna from nonebot_plugin_htmlrender import template_to_pic @@ -41,9 +41,32 @@ __plugin_meta__ = PluginMetadata( ) +def commandRule() -> Rule: + return Rule(lambda: Config.get_config("check", "type") in {"message", "mix"}) + + +def noticeRule() -> Rule: + return Rule(lambda: Config.get_config("check", "type") in {"poke", "mix"}) + + +_self_check_matcher = _self_check_matcher = on_alconna( + Alconna("自检"), + rule=to_me() & commandRule(), + permission=SUPERUSER, + block=True, + priority=1, +) + +_self_check_poke_matcher = on_notice( + priority=5, + permission=SUPERUSER, + block=False, + rule=notice_rule(PokeNotifyEvent) & to_me() & noticeRule(), +) + + async def handle_self_check(): try: - logger.info("触发自检") data = await get_status_info() image = await template_to_pic( template_path=str((TEMPLATE_PATH / "check").absolute()), @@ -56,34 +79,17 @@ async def handle_self_check(): wait=2, ) await MessageUtils.build_message(image).send() - logger.info("自检成功") + logger.info("自检成功", "自检") except Exception as e: await MessageUtils.build_message(f"自检失败: {e}").send() - logger.error("自检失败", e=e) + logger.error("自检失败", "自检", e=e) -check_type = Config.get_config("check", "type") - -if check_type in {"message", "mix"}: - # message - _self_check_matcher = on_alconna( - Alconna("自检"), rule=to_me(), permission=SUPERUSER, block=True, priority=1 - ) - - @_self_check_matcher.handle() - async def handle_message_check(): - await handle_self_check() +@_self_check_matcher.handle() +async def handle_message_check(): + await handle_self_check() -if check_type in {"poke", "mix"}: - # poke - _self_check_poke_matcher = on_notice( - priority=5, - permission=SUPERUSER, - block=False, - rule=notice_rule(PokeNotifyEvent) & to_me(), - ) - - @_self_check_poke_matcher.handle() - async def handle_poke_check(event: PokeNotifyEvent): - await handle_self_check() +@_self_check_poke_matcher.handle() +async def handle_poke_check(): + await handle_self_check()