From f70b07e5a38b0b4ff231e1fb9fe118f3a64a5f1f Mon Sep 17 00:00:00 2001 From: AkashiCoin Date: Mon, 26 Aug 2024 23:54:07 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=20(#1583)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨添加代码检查 * 使用Sourcery的建议 --- .pre-commit-config.yaml | 33 ++++++++++++++++ pyproject.toml | 88 ++++++++++++++++++++++++++++++++++++++++- scripts/bot_check.py | 12 +++--- tests/__init__.py | 0 tests/conftest.py | 37 +++++++++++++++++ 5 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..c5280f74 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,33 @@ +default_install_hook_types: [pre-commit, prepare-commit-msg] +ci: + autofix_commit_msg: ":rotating_light: auto fix by pre-commit hooks" + autofix_prs: true + autoupdate_branch: dev + autoupdate_schedule: weekly + autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks" +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.5.6 + 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: 24.8.0 + hooks: + - id: black + stages: [commit] + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v4.0.0-alpha.8 + hooks: + - id: prettier + types_or: [javascript, jsx, ts, tsx, markdown, yaml, json] + stages: [commit] \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index dc2d6501..c6ec88a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ version = "0.1.1" description = "基于 Nonebot2 和 go-cqhttp 开发,以 postgresql 作为数据库,非常可爱的绪山真寻bot" authors = ["HibiKier <775757368@qq.com>"] license = "AGPL" +package-mode = false [[tool.poetry.source]] name = "ali" @@ -15,7 +16,7 @@ python = "^3.10" playwright = "^1.41.1" nonebot-adapter-onebot = "^2.3.1" nonebot-plugin-apscheduler = "^0.3.0" -tortoise-orm = {extras = ["asyncpg"], version = "^0.20.0"} +tortoise-orm = { extras = ["asyncpg"], version = "^0.20.0" } cattrs = "^23.2.3" ruamel-yaml = "^0.18.5" strenum = "^0.4.15" @@ -43,11 +44,94 @@ cn2an = "^0.5.22" aiohttp = "^3.9.5" dateparser = "^1.2.0" bilireq = "0.2.3post0" -python-jose = {extras = ["cryptography"], version = "^3.3.0"} +python-jose = { extras = ["cryptography"], version = "^3.3.0" } python-multipart = "^0.0.9" nonebot-plugin-alconna = "^0.51.1" [tool.poetry.dev-dependencies] +nonebug = "^0.3.2" +pytest-cov = "^5.0.0" +pytest-mock = "^3.6.1" +pytest-asyncio = "^0.23.5" +pytest-xdist = "^3.3.1" +respx = "^0.21.1" + +[tool.nonebot] +plugins = [ + "nonebot_plugin_apscheduler", + "nonebot_plugin_session", + "nonebot_plugin_htmlrender", + "nonebot_plugin_userinfo", + "nonebot_plugin_alconna", +] +plugin_dirs = ["zhenxun/services", "zhenxun/builtin_plugins", "zhenxun/plugins"] +adapters = [ + { name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" }, + { name = "DoDo", module_name = "nonebot.adapters.dodo" }, + { name = "开黑啦", module_name = "nonebot.adapters.kaiheila" }, +] + +[tool.black] +line-length = 88 +target-version = ["py39", "py310", "py311", "py312"] +include = '\.pyi?$' +extend-exclude = ''' +''' + +[tool.isort] +profile = "black" +line_length = 88 +length_sort = true +skip_gitignore = true +force_sort_within_sections = true +src_paths = ["zhenxun", "tests"] +extra_standard_library = ["typing_extensions"] + +[tool.ruff] +line-length = 88 +target-version = "py310" + +[tool.ruff.lint] +select = [ + "F", # Pyflakes + "W", # pycodestyle warnings + "E", # pycodestyle errors + "UP", # pyupgrade + "ASYNC", # flake8-async + "C4", # flake8-comprehensions + "T10", # flake8-debugger + "T20", # flake8-print + "PYI", # flake8-pyi + "PT", # flake8-pytest-style + "Q", # flake8-quotes + "RUF", # Ruff-specific rules +] +ignore = [ + "E402", # module-import-not-at-top-of-file + "UP037", # quoted-annotation + "RUF001", # ambiguous-unicode-character-string + "RUF002", # ambiguous-unicode-character-docstring + "RUF003", # ambiguous-unicode-character-comment +] + +[tool.ruff.lint.flake8-pytest-style] +fixture-parentheses = false +mark-parentheses = false + +[tool.pyright] +pythonVersion = "3.9" +pythonPlatform = "All" +defineConstant = { PYDANTIC_V2 = true } +executionEnvironments = [ + { root = "./tests", extraPaths = [ + "./", + ] }, + { root = "./" }, +] + +typeCheckingMode = "standard" +reportShadowedImports = false +disableBytesTypePromotions = true [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/scripts/bot_check.py b/scripts/bot_check.py index 2f68c1ed..771eada1 100644 --- a/scripts/bot_check.py +++ b/scripts/bot_check.py @@ -1,15 +1,14 @@ import re + import nonebot # from nonebot.adapters.discord import Adapter as DiscordAdapter from nonebot.adapters.dodo import Adapter as DoDoAdapter from nonebot.adapters.kaiheila import Adapter as KaiheilaAdapter from nonebot.adapters.onebot.v11 import Adapter as OneBotV11Adapter -from regex import F nonebot.init() -from zhenxun.models.plugin_info import PluginInfo driver = nonebot.get_driver() driver.register_adapter(OneBotV11Adapter) @@ -23,11 +22,14 @@ nonebot.load_plugins("zhenxun/plugins") all_plugins = [name.replace(":", ".") for name in nonebot.get_available_plugin_names()] print("所有插件:", all_plugins) -loaded_plugins = tuple(re.sub(r"^zhenxun\.(plugins|builtin_plugins)\.", "", plugin.module_name) for plugin in nonebot.get_loaded_plugins()) +loaded_plugins = tuple( + re.sub(r"^zhenxun\.(plugins|builtin_plugins)\.", "", plugin.module_name) + for plugin in nonebot.get_loaded_plugins() +) print("已加载插件:", loaded_plugins) for plugin in all_plugins.copy(): - if plugin.startswith(("platform")): + if plugin.startswith(("platform",)): print(f"平台插件:{plugin}") elif plugin.endswith(loaded_plugins): print(f"已加载插件:{plugin}") @@ -40,4 +42,4 @@ if all_plugins: print("出现未加载的插件:", all_plugins) exit(1) else: - print("所有插件均已加载") \ No newline at end of file + print("所有插件均已加载") diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..cc181291 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,37 @@ +from pathlib import Path + +import nonebot +import pytest +from nonebot.plugin import Plugin +from nonebug import NONEBOT_INIT_KWARGS +from nonebug.app import App +from pytest_mock import MockerFixture +from respx import MockRouter + + +def pytest_configure(config: pytest.Config) -> None: + config.stash[NONEBOT_INIT_KWARGS] = { + "driver": "~fastapi+~httpx+~websockets", + "superusers": ["AkashiCoin"], + "command_start": "", + "session_running_expression": "别急呀,小真寻要宕机了!QAQ", + "image_to_bytes": False, + "nickname": ["真寻", "小真寻", "绪山真寻", "小寻子"], + "session_expire_timeout": 30, + "self_nickname": "小真寻", + "db_url": "sqlite://:memory:", + "platform_superusers": {"qq": ["qq_su"], "dodo": ["dodo_su"]}, + "host": "127.0.0.1", + "port": 8080, + } + + +@pytest.fixture(scope="session", autouse=True) +def load_plugin(nonebug_init: None) -> set[Plugin]: + return nonebot.load_plugins("zhenxun.plugins") + + +@pytest.fixture +async def app(app: App, tmp_path: Path, mocker: MockerFixture): + mocker.patch("nonebot.drivers.websockets.connect", return_value=MockRouter()) + return app