diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8f02e2c..43fe260 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,23 +6,12 @@ ci: autoupdate_commit_msg: "chore: auto update by pre-commit hooks" repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.1.9 + rev: v0.2.2 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] stages: [commit] - - - repo: https://github.com/pycqa/isort - rev: 5.13.2 - hooks: - - id: isort - stages: [commit] - - - repo: https://github.com/psf/black - rev: 23.12.1 - hooks: - - id: black - stages: [commit] + - id: ruff-format - repo: https://github.com/pre-commit/mirrors-prettier rev: v4.0.0-alpha.8 diff --git a/nonebot_plugin_user/matchers.py b/nonebot_plugin_user/matchers.py index d7497e8..b02a66a 100644 --- a/nonebot_plugin_user/matchers.py +++ b/nonebot_plugin_user/matchers.py @@ -109,7 +109,7 @@ async def _( session.level, ) await bind_cmd.finish( - f"命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind {token}\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。" + f"命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind {token}\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。" # noqa: E501 ) # 绑定流程 @@ -122,7 +122,7 @@ async def _( token = generate_token() tokens[token] = (session.platform, session.platform_id, user_id, None) await bind_cmd.finish( - f"令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind {token}\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。" + f"令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind {token}\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。" # noqa: E501 ) # 群内绑定的第二步,会在目标平台发送令牌 # 此时 platform_id 和 platform 为原始平台的信息 diff --git a/pyproject.toml b/pyproject.toml index c4f4135..35258d9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,14 +32,6 @@ pytest-mock = "^3.10.0" pytest-asyncio = "^0.23.5" freezegun = "^1.2.2" -[tool.black] -line-length = 88 - -[tool.isort] -profile = "black" -line_length = 88 -skip_gitignore = true - [tool.pytest.ini_options] asyncio_mode = "auto" @@ -50,8 +42,12 @@ typeCheckingMode = "basic" defineConstant = { PYDANTIC_V2 = true } [tool.ruff] -select = ["E", "W", "F", "UP", "C", "T", "PYI", "Q"] -ignore = ["E402", "E501", "C901", "UP037"] +line-length = 88 +target-version = "py38" + +[tool.ruff.lint] +select = ["E", "W", "F", "UP", "C", "T", "PYI", "PT", "Q"] +ignore = ["E402", "C901", "UP037"] [tool.nonebot] adapters = [ diff --git a/tests/conftest.py b/tests/conftest.py index 843d963..31ac9b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,13 +20,13 @@ def pytest_configure(config: pytest.Config) -> None: @pytest.fixture(scope="session", autouse=True) -def load_adapters(nonebug_init: None): +def _load_adapters(nonebug_init: None): driver = nonebot.get_driver() driver.register_adapter(OnebotV11Adapter) driver.register_adapter(OnebotV12Adapter) -@pytest.fixture +@pytest.fixture() async def app(app: App, mocker: MockerFixture, tmp_path: Path): # 加载插件 nonebot.require("nonebot_plugin_user") @@ -47,7 +47,7 @@ async def app(app: App, mocker: MockerFixture, tmp_path: Path): await session.execute(delete(User)) -@pytest.fixture +@pytest.fixture() async def session(app: App): from nonebot_plugin_orm import get_session @@ -72,6 +72,6 @@ def set_timestamp(mapper, connection, target): event.remove(User, "before_insert", set_timestamp) -@pytest.fixture(scope="function") +@pytest.fixture() def patch_current_time(): return patch_time diff --git a/tests/test_bind_group.py b/tests/test_bind_group.py index 096a44e..110c440 100644 --- a/tests/test_bind_group.py +++ b/tests/test_bind_group.py @@ -48,7 +48,7 @@ async def test_bind_group(app: App, patch_current_time, mocker: MockerFixture): ctx.receive_event(bot, event) ctx.should_call_send( event, - "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", + "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) @@ -63,7 +63,7 @@ async def test_bind_group(app: App, patch_current_time, mocker: MockerFixture): ctx.receive_event(bot, event) ctx.should_call_send( event, - "令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind nonebot/123456\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。", + "令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind nonebot/123456\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) @@ -150,7 +150,7 @@ async def test_bind_group_different_user( ctx.receive_event(bot, event) ctx.should_call_send( event, - "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", + "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) @@ -165,7 +165,7 @@ async def test_bind_group_different_user( ctx.receive_event(bot, event) ctx.should_call_send( event, - "令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind nonebot/123456\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。", + "令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind nonebot/123456\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) diff --git a/tests/test_bind_private.py b/tests/test_bind_private.py index 9aa0344..0c33a8b 100644 --- a/tests/test_bind_private.py +++ b/tests/test_bind_private.py @@ -48,7 +48,7 @@ async def test_bind_private(app: App, patch_current_time, mocker: MockerFixture) ctx.receive_event(bot, event) ctx.should_call_send( event, - "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", + "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) @@ -135,7 +135,7 @@ async def test_bind_private_invalid_token( ctx.receive_event(bot, event) ctx.should_call_send( event, - "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", + "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind nonebot/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) @@ -223,7 +223,7 @@ async def test_bind_private_prefix(app: App, patch_current_time, mocker: MockerF ctx.receive_event(bot, event) ctx.should_call_send( event, - "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind test/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", + "命令 bind 可用于在多个平台间绑定用户数据。绑定过程中,原始平台的用户数据将完全保留,而目标平台的用户数据将被原始平台的数据所覆盖。\n请确认当前平台是你的目标平台,并在 5 分钟内使用你的账号在原始平台内向机器人发送以下文本:\n/bind test/123456\n绑定完成后,你可以随时使用「bind -r」来解除绑定状态。", # noqa: E501 True, ) ctx.should_finished(bind_cmd) diff --git a/tests/test_remove_bind.py b/tests/test_remove_bind.py index 227b059..1de01f3 100644 --- a/tests/test_remove_bind.py +++ b/tests/test_remove_bind.py @@ -75,12 +75,8 @@ async def test_bind_not_exist(app: App): """不存在的账户""" from nonebot_plugin_user.utils import remove_bind, set_bind - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="找不到用户信息"): await remove_bind("qq", "1") - assert str(e.value) == "找不到用户信息" - - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="找不到用户信息"): await set_bind("qq", "1", 2) - - assert str(e.value) == "找不到用户信息" diff --git a/tests/test_user.py b/tests/test_user.py index b8b2452..3e4b5fa 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -40,11 +40,9 @@ async def test_user(app: App, patch_current_time): user = await get_user_by_id(1) assert user.id == 1 - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="找不到用户信息"): await get_user_by_id(2) - assert str(e.value) == "找不到用户信息" - async def test_user_set_name(app: App, patch_current_time): """设置用户名""" @@ -116,11 +114,9 @@ async def test_user_set_name(app: App, patch_current_time): ) ctx.should_finished(user_cmd) - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="找不到用户信息"): await set_user_name("123", "qq", "not exist") - assert str(e.value) == "找不到用户信息" - async def test_user_session(app: App, patch_current_time): """用户会话相关的测试"""