Skip to content

Commit

Permalink
style: 更新 ruff 配置 (#108)
Browse files Browse the repository at this point in the history
移除 isort 和 black,仅使用 ruff 进行格式化。
  • Loading branch information
he0119 authored Feb 24, 2024
1 parent 1f7ec3b commit 95012fd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 48 deletions.
15 changes: 2 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_user/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

# 绑定流程
Expand All @@ -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 为原始平台的信息
Expand Down
16 changes: 6 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 = [
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand All @@ -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
8 changes: 4 additions & 4 deletions tests/test_bind_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bind_private.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions tests/test_remove_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "找不到用户信息"
8 changes: 2 additions & 6 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""设置用户名"""
Expand Down Expand Up @@ -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):
"""用户会话相关的测试"""
Expand Down

0 comments on commit 95012fd

Please sign in to comment.