From 0348dc53bc9351581b51e58d652c14930513312b Mon Sep 17 00:00:00 2001 From: AkashiCoin Date: Sun, 1 Sep 2024 14:41:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/bot_check.yml | 1 - .../auto_update/test_check_update.py | 73 +++++++++++++++---- .../plugin_store/test_add_plugin.py | 35 ++++----- 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/.github/workflows/bot_check.yml b/.github/workflows/bot_check.yml index 4d9b4bbad..f47d41062 100644 --- a/.github/workflows/bot_check.yml +++ b/.github/workflows/bot_check.yml @@ -49,7 +49,6 @@ jobs: rm -rf poetry.lock poetry source remove ali poetry install --no-root - poetry run pip install pydantic==1.10 - name: Run tests run: poetry run pytest --cov=zhenxun --cov-report xml diff --git a/tests/builtin_plugins/auto_update/test_check_update.py b/tests/builtin_plugins/auto_update/test_check_update.py index 5b686a3ff..a71ee75b0 100644 --- a/tests/builtin_plugins/auto_update/test_check_update.py +++ b/tests/builtin_plugins/auto_update/test_check_update.py @@ -10,7 +10,6 @@ from tests.config import BotId, UserId, GroupId, MessageId from tests.utils import ( - get_content_bytes, get_response_json, _v11_group_message_event, _v11_private_message_send, @@ -19,35 +18,83 @@ def init_mocked_api(mocked_api: MockRouter) -> None: mocked_api.get( - "https://api.github.com/repos/HibiKier/zhenxun_bot/releases/latest", + url="https://api.github.com/repos/HibiKier/zhenxun_bot/releases/latest", name="release_latest", - ).respond(200, json=get_response_json("release_latest.json")) + ).respond(json=get_response_json(path="release_latest.json")) mocked_api.get( - "https://raw.githubusercontent.com/HibiKier/zhenxun_bot/dev/__version__", + url="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/dev/__version__", name="dev_branch_version", - ).respond(200, text="__version__: v0.2.2") + ).respond(text="__version__: v0.2.2") mocked_api.get( - "https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/__version__", + url="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/__version__", name="main_branch_version", - ).respond(200, text="__version__: v0.2.2") + ).respond(text="__version__: v0.2.2") mocked_api.get( - "https://api.github.com/repos/HibiKier/zhenxun_bot/tarball/v0.2.2", + url="https://api.github.com/repos/HibiKier/zhenxun_bot/tarball/v0.2.2", name="release_download_url", ).respond( - 302, + status_code=302, headers={ "Location": "https://codeload.github.com/HibiKier/zhenxun_bot/legacy.tar.gz/refs/tags/v0.2.2" }, ) + import io + import tarfile + + tar_buffer = io.BytesIO() + + from zhenxun.builtin_plugins.auto_update.config import ( + REQ_TXT_FILE, + PYPROJECT_FILE, + PYPROJECT_LOCK_FILE, + ) + + # 指定要添加到压缩文件中的文件路径列表 + file_paths: list[Path] = [ + PYPROJECT_FILE, + PYPROJECT_LOCK_FILE, + REQ_TXT_FILE, + ] + + # 打开一个tarfile对象,写入到上面创建的BytesIO对象中 + with tarfile.open(mode="w:gz", fileobj=tar_buffer) as tar: + _extracted_from_init_mocked_api_43(tarfile, tar, file_paths, io) mocked_api.get( - "https://codeload.github.com/HibiKier/zhenxun_bot/legacy.tar.gz/refs/tags/v0.2.2", + url="https://codeload.github.com/HibiKier/zhenxun_bot/legacy.tar.gz/refs/tags/v0.2.2", name="release_download_url_redirect", ).respond( - 200, - content=get_content_bytes("download_latest_file.tar.gz"), + content=tar_buffer.getvalue(), ) +# TODO Rename this here and in `init_mocked_api` +def _extracted_from_init_mocked_api_43(tarfile, tar, file_paths, io): + folder_name = "my_folder" + tarinfo = tarfile.TarInfo(folder_name) + tarinfo.type = tarfile.DIRTYPE + tarinfo.mode = 0o755 + tar.addfile(tarinfo) + + # 读取并添加指定的文件 + for file_path in file_paths: + # 读取文件内容 + with open(file_path, "rb") as file: + file_content = file.read() + + # 使用BytesIO创建文件内容 + file_buffer = io.BytesIO(file_content) + + # 创建TarInfo对象 + tarinfo = tarfile.TarInfo( + f"{folder_name}/{file_path.name}" + ) # 使用文件名作为tar中的名字 + tarinfo.mode = 0o644 # 设置文件夹权限 + tarinfo.size = len(file_content) + + # 添加文件 + tar.addfile(tarinfo, fileobj=file_buffer) + + async def test_check_update_release( app: App, mocker: MockerFixture, @@ -60,7 +107,7 @@ async def test_check_update_release( """ from zhenxun.builtin_plugins.auto_update import _matcher - init_mocked_api(mocked_api) + init_mocked_api(mocked_api=mocked_api) mocker.patch( "zhenxun.builtin_plugins.auto_update._data_source.REPLACE_FOLDERS", diff --git a/tests/builtin_plugins/plugin_store/test_add_plugin.py b/tests/builtin_plugins/plugin_store/test_add_plugin.py index 9d26062fe..1930e23b2 100644 --- a/tests/builtin_plugins/plugin_store/test_add_plugin.py +++ b/tests/builtin_plugins/plugin_store/test_add_plugin.py @@ -7,6 +7,7 @@ from pytest_mock import MockerFixture from nonebot.adapters.onebot.v11 import Bot from nonebot.adapters.onebot.v11.message import Message +from nonebot.adapters.onebot.v11.event import GroupMessageEvent from tests.config import BotId, UserId, GroupId, MessageId from tests.utils import get_response_json, _v11_group_message_event @@ -32,11 +33,11 @@ def init_mocked_api(mocked_api: MockRouter) -> None: mocked_api.get( "https://api.github.com/repos/xuanerwa/zhenxun_github_sub/contents/", name="github_sub_plugin_contents", - ).respond(200, json=get_response_json("github_sub_plugin_contents.json")) + ).respond(json=get_response_json("github_sub_plugin_contents.json")) mocked_api.get( "https://api.github.com/repos/xuanerwa/zhenxun_github_sub/contents/github_sub?ref=main", name="github_sub_plugin_api", - ).respond(200, json=get_response_json("github_sub_plugin_api.json")) + ).respond(json=get_response_json("github_sub_plugin_api.json")) mocked_api.get( "https://raw.githubusercontent.com/xuanerwa/zhenxun_github_sub/main/github_sub/__init__.py", name="github_sub_plugin_file_init", @@ -55,7 +56,7 @@ async def test_add_plugin_basic( """ from zhenxun.builtin_plugins.plugin_store import _matcher - init_mocked_api(mocked_api) + init_mocked_api(mocked_api=mocked_api) mocker.patch( "zhenxun.builtin_plugins.plugin_store.data_source.BASE_PATH", return_value=tmp_path / "zhenxun", @@ -65,26 +66,26 @@ async def test_add_plugin_basic( async with app.test_matcher(_matcher) as ctx: bot = create_bot(ctx) - bot = cast(Bot, bot) + bot: Bot = cast(Bot, bot) raw_message = f"添加插件 {plugin_id}" - event = _v11_group_message_event( - raw_message, + event: GroupMessageEvent = _v11_group_message_event( + message=raw_message, self_id=BotId.QQ_BOT, user_id=UserId.SUPERUSER, group_id=GroupId.GROUP_ID_LEVEL_5, message_id=MessageId.MESSAGE_ID, to_me=True, ) - ctx.receive_event(bot, event) + ctx.receive_event(bot=bot, event=event) ctx.should_call_send( event=event, - message=Message(f"正在添加插件 Id: {plugin_id}"), + message=Message(message=f"正在添加插件 Id: {plugin_id}"), result=None, bot=bot, ) ctx.should_call_send( event=event, - message=Message("插件 识图 安装成功! 重启后生效"), + message=Message(message="插件 识图 安装成功! 重启后生效"), result=None, bot=bot, ) @@ -106,7 +107,7 @@ async def test_add_plugin_extra( """ from zhenxun.builtin_plugins.plugin_store import _matcher - init_mocked_api(mocked_api) + init_mocked_api(mocked_api=mocked_api) mocker.patch( "zhenxun.builtin_plugins.plugin_store.data_source.BASE_PATH", return_value=tmp_path / "zhenxun", @@ -116,26 +117,26 @@ async def test_add_plugin_extra( async with app.test_matcher(_matcher) as ctx: bot = create_bot(ctx) - bot = cast(Bot, bot) - raw_message = f"添加插件 {plugin_id}" - event = _v11_group_message_event( - raw_message, + bot: Bot = cast(Bot, bot) + raw_message: str = f"添加插件 {plugin_id}" + event: GroupMessageEvent = _v11_group_message_event( + message=raw_message, self_id=BotId.QQ_BOT, user_id=UserId.SUPERUSER, group_id=GroupId.GROUP_ID_LEVEL_5, message_id=MessageId.MESSAGE_ID, to_me=True, ) - ctx.receive_event(bot, event) + ctx.receive_event(bot=bot, event=event) ctx.should_call_send( event=event, - message=Message(f"正在添加插件 Id: {plugin_id}"), + message=Message(message=f"正在添加插件 Id: {plugin_id}"), result=None, bot=bot, ) ctx.should_call_send( event=event, - message=Message("插件 github订阅 安装成功! 重启后生效"), + message=Message(message="插件 github订阅 安装成功! 重启后生效"), result=None, bot=bot, )