Skip to content

Commit

Permalink
🎨 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashiCoin committed Sep 1, 2024
1 parent df1a84b commit 0348dc5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/bot_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 60 additions & 13 deletions tests/builtin_plugins/auto_update/test_check_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down
35 changes: 18 additions & 17 deletions tests/builtin_plugins/plugin_store/test_add_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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,
)
Expand All @@ -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",
Expand All @@ -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,
)
Expand Down

0 comments on commit 0348dc5

Please sign in to comment.