diff --git a/examples/store-test.yml b/examples/store-test.yml index 3b5d48c9..930a5ea7 100644 --- a/examples/store-test.yml +++ b/examples/store-test.yml @@ -46,13 +46,6 @@ jobs: run: | git checkout `git describe --abbrev=0 --tags` poetry install - mkdir -p plugin_test/store - curl -sSL https://raw.githubusercontent.com/nonebot/registry/results/results.json -o plugin_test/store/previous_results.json - curl -sSL https://raw.githubusercontent.com/nonebot/registry/results/plugins.json -o plugin_test/store/previous_plugins.json - curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/adapters.json -o plugin_test/store/adapters.json - curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/bots.json -o plugin_test/store/bots.json - curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/drivers.json -o plugin_test/store/drivers.json - curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/plugins.json -o plugin_test/store/plugins.json - name: Test plugin if: ${{ !contains(fromJSON('["Bot", "Adapter", "Plugin"]'), github.event.client_payload.type) }} diff --git a/src/utils/constants.py b/src/utils/constants.py index 6f5dac70..5ab43bd2 100644 --- a/src/utils/constants.py +++ b/src/utils/constants.py @@ -8,6 +8,7 @@ # https://github.com/nonebot/nonebot2/tree/master/assets STORE_BASE_URL = "https://raw.githubusercontent.com/nonebot/nonebot2/master/assets" STORE_ADAPTERS_URL = f"{STORE_BASE_URL}/adapters.json" -STORE_BOT_URL = f"{STORE_BASE_URL}/bot.json" +STORE_BOTS_URL = f"{STORE_BASE_URL}/bots.json" STORE_DRIVERS_URL = f"{STORE_BASE_URL}/drivers.json" STORE_PLUGINS_URL = f"{STORE_BASE_URL}/plugins.json" +"""plugin_test.py 中也有一个常量,需要同时修改""" diff --git a/src/utils/store_test/__main__.py b/src/utils/store_test/__main__.py index f04b7a3c..e8d1b1c2 100644 --- a/src/utils/store_test/__main__.py +++ b/src/utils/store_test/__main__.py @@ -1,5 +1,5 @@ +import asyncio import os -from asyncio import run import click @@ -18,7 +18,7 @@ def main(limit: int, offset: int, force: bool, key: str | None): config = os.environ.get("PLUGIN_CONFIG") data = os.environ.get("PLUGIN_DATA") - run(test.run(key, config, data)) + asyncio.run(test.run(key, config, data)) if __name__ == "__main__": diff --git a/src/utils/store_test/constants.py b/src/utils/store_test/constants.py index 90981c11..c5def927 100644 --- a/src/utils/store_test/constants.py +++ b/src/utils/store_test/constants.py @@ -17,18 +17,3 @@ """ 生成的驱动器列表保存路径 """ PLUGINS_PATH = TEST_DIR / "plugins.json" """ 生成的插件列表保存路径 """ - -STORE_DIR = Path("plugin_test") / "store" -""" 商店信息文件夹 """ -STORE_ADAPTERS_PATH = STORE_DIR / "adapters.json" -""" 适配器列表文件路径 """ -STORE_BOTS_PATH = STORE_DIR / "bots.json" -""" 机器人列表文件路径 """ -STORE_DRIVERS_PATH = STORE_DIR / "drivers.json" -""" 驱动器列表文件路径 """ -STORE_PLUGINS_PATH = STORE_DIR / "plugins.json" -""" 插件列表文件路径 """ -PREVIOUS_RESULTS_PATH = STORE_DIR / "previous_results.json" -""" 上次测试生成的结果文件路径 """ -PREVIOUS_PLUGINS_PATH = STORE_DIR / "previous_plugins.json" -""" 上次测试生成的插件列表文件路径 """ diff --git a/src/utils/store_test/store.py b/src/utils/store_test/store.py index 673ff15f..202a4b30 100644 --- a/src/utils/store_test/store.py +++ b/src/utils/store_test/store.py @@ -1,21 +1,24 @@ import click +from src.utils.constants import ( + REGISTRY_PLUGINS_URL, + REGISTRY_RESULTS_URL, + STORE_ADAPTERS_URL, + STORE_BOTS_URL, + STORE_DRIVERS_URL, + STORE_PLUGINS_URL, +) + from .constants import ( ADAPTERS_PATH, BOTS_PATH, DRIVERS_PATH, PLUGIN_KEY_TEMPLATE, PLUGINS_PATH, - PREVIOUS_PLUGINS_PATH, - PREVIOUS_RESULTS_PATH, RESULTS_PATH, - STORE_ADAPTERS_PATH, - STORE_BOTS_PATH, - STORE_DRIVERS_PATH, - STORE_PLUGINS_PATH, ) from .models import Plugin, StorePlugin, TestResult -from .utils import dump_json, get_latest_version, load_json +from .utils import download_file, dump_json, get_latest_version from .validation import validate_plugin @@ -33,24 +36,26 @@ def __init__( self._force = force # NoneBot 仓库中的数据 - self._store_adapters = load_json(STORE_ADAPTERS_PATH) - self._store_bots = load_json(STORE_BOTS_PATH) - self._store_drivers = load_json(STORE_DRIVERS_PATH) + self._store_adapters = download_file(STORE_ADAPTERS_URL) + self._store_bots = download_file(STORE_BOTS_URL) + self._store_drivers = download_file(STORE_DRIVERS_URL) self._store_plugins: dict[str, StorePlugin] = { PLUGIN_KEY_TEMPLATE.format( project_link=plugin["project_link"], module_name=plugin["module_name"], ): plugin - for plugin in load_json(STORE_PLUGINS_PATH) + for plugin in download_file(STORE_PLUGINS_URL) } # 上次测试的结果 - self._previous_results: dict[str, TestResult] = load_json(PREVIOUS_RESULTS_PATH) + self._previous_results: dict[str, TestResult] = download_file( + REGISTRY_RESULTS_URL + ) self._previous_plugins: dict[str, Plugin] = { PLUGIN_KEY_TEMPLATE.format( project_link=plugin["project_link"], module_name=plugin["module_name"], ): plugin - for plugin in load_json(PREVIOUS_PLUGINS_PATH) + for plugin in download_file(REGISTRY_PLUGINS_URL) } def should_skip(self, key: str) -> bool: @@ -161,7 +166,6 @@ async def run( self, key: str | None = None, config: str | None = None, data: str | None = None ): """测试商店内插件情况""" - results, plugins = await self.test_plugins(key, config, data) # 保存测试结果与生成的列表 diff --git a/src/utils/store_test/utils.py b/src/utils/store_test/utils.py index a4f106ef..f4458a53 100644 --- a/src/utils/store_test/utils.py +++ b/src/utils/store_test/utils.py @@ -6,15 +6,6 @@ import httpx -def load_json(path: Path) -> dict: - """加载 JSON 文件""" - if not path.exists(): - raise Exception(f"文件 {path} 不存在") - - with open(path, encoding="utf8") as f: - return json.load(f) - - def dump_json(path: Path, data: dict | list): """保存 JSON 文件 @@ -47,3 +38,11 @@ def get_upload_time(project_link: str) -> str: """获取插件的上传时间""" data = get_pypi_data(project_link) return data["urls"][0]["upload_time_iso_8601"] + + +def download_file(url: str): + """下载文件""" + r = httpx.get(url) + if r.status_code != 200: + raise ValueError(f"下载文件失败:{r.text}") + return r.json() diff --git a/src/utils/validation/utils.py b/src/utils/validation/utils.py index d8e79749..cc1f6b98 100644 --- a/src/utils/validation/utils.py +++ b/src/utils/validation/utils.py @@ -2,7 +2,6 @@ from typing import TYPE_CHECKING import httpx -from nonebot import logger from pydantic_extra_types.color import Color, float_to_255 from src.utils.constants import STORE_ADAPTERS_URL @@ -26,7 +25,6 @@ def check_url(url: str) -> tuple[int, str]: 返回状态码,如果报错则返回 -1 """ - logger.info(f"检查网址 {url}") try: r = httpx.get(url, follow_redirects=True) return r.status_code, "" diff --git a/tests/utils/store_test/store/previous_plugins.json b/tests/utils/store_test/store/registry_plugins.json similarity index 100% rename from tests/utils/store_test/store/previous_plugins.json rename to tests/utils/store_test/store/registry_plugins.json diff --git a/tests/utils/store_test/store/previous_results.json b/tests/utils/store_test/store/registry_results.json similarity index 100% rename from tests/utils/store_test/store/previous_results.json rename to tests/utils/store_test/store/registry_results.json diff --git a/tests/utils/store_test/store/adapters.json b/tests/utils/store_test/store/store_adapters.json similarity index 100% rename from tests/utils/store_test/store/adapters.json rename to tests/utils/store_test/store/store_adapters.json diff --git a/tests/utils/store_test/store/bots.json b/tests/utils/store_test/store/store_bots.json similarity index 100% rename from tests/utils/store_test/store/bots.json rename to tests/utils/store_test/store/store_bots.json diff --git a/tests/utils/store_test/store/drivers.json b/tests/utils/store_test/store/store_drivers.json similarity index 100% rename from tests/utils/store_test/store/drivers.json rename to tests/utils/store_test/store/store_drivers.json diff --git a/tests/utils/store_test/store/plugins.json b/tests/utils/store_test/store/store_plugins.json similarity index 100% rename from tests/utils/store_test/store/plugins.json rename to tests/utils/store_test/store/store_plugins.json diff --git a/tests/utils/store_test/test_store_test.py b/tests/utils/store_test/test_store_test.py index dacd92ae..5e652b70 100644 --- a/tests/utils/store_test/test_store_test.py +++ b/tests/utils/store_test/test_store_test.py @@ -1,15 +1,34 @@ -import shutil +import json from pathlib import Path import pytest from pytest_mock import MockerFixture from respx import MockRouter +from src.utils.constants import ( + REGISTRY_PLUGINS_URL, + REGISTRY_RESULTS_URL, + STORE_ADAPTERS_URL, + STORE_BOTS_URL, + STORE_DRIVERS_URL, + STORE_PLUGINS_URL, +) + + +def load_json(name: str) -> dict: + path = Path(__file__).parent / "store" / f"{name}.json" + with path.open("r", encoding="utf-8") as f: + return json.load(f) + @pytest.fixture -def mocked_store_data(tmp_path: Path, mocker: MockerFixture) -> dict[str, Path]: +def mocked_store_data( + tmp_path: Path, + mocker: MockerFixture, + mocked_api: MockRouter, +) -> dict[str, Path]: plugin_test_path = tmp_path / "plugin_test" - store_path = plugin_test_path / "store" + plugin_test_path.mkdir() paths = { "results": plugin_test_path / "results.json", @@ -17,12 +36,6 @@ def mocked_store_data(tmp_path: Path, mocker: MockerFixture) -> dict[str, Path]: "bots": plugin_test_path / "bots.json", "drivers": plugin_test_path / "drivers.json", "plugins": plugin_test_path / "plugins.json", - "store_adapters": store_path / "adapters.json", - "store_bots": store_path / "bots.json", - "store_drivers": store_path / "drivers.json", - "store_plugins": store_path / "plugins.json", - "previous_results": store_path / "previous_results.json", - "previous_plugins": store_path / "previous_plugins.json", } mocker.patch( @@ -43,29 +56,13 @@ def mocked_store_data(tmp_path: Path, mocker: MockerFixture) -> dict[str, Path]: paths["plugins"], ) - mocker.patch( - "src.utils.store_test.store.STORE_ADAPTERS_PATH", - paths["store_adapters"], - ) - mocker.patch("src.utils.store_test.store.STORE_BOTS_PATH", paths["store_bots"]) - mocker.patch( - "src.utils.store_test.store.STORE_DRIVERS_PATH", - paths["store_drivers"], - ) - mocker.patch( - "src.utils.store_test.store.STORE_PLUGINS_PATH", - paths["store_plugins"], - ) - mocker.patch( - "src.utils.store_test.store.PREVIOUS_RESULTS_PATH", - paths["previous_results"], - ) - mocker.patch( - "src.utils.store_test.store.PREVIOUS_PLUGINS_PATH", - paths["previous_plugins"], - ) + mocked_api.get(REGISTRY_RESULTS_URL).respond(json=load_json("registry_results")) + mocked_api.get(REGISTRY_PLUGINS_URL).respond(json=load_json("registry_plugins")) + mocked_api.get(STORE_ADAPTERS_URL).respond(json=load_json("store_adapters")) + mocked_api.get(STORE_BOTS_URL).respond(json=load_json("STORE_BOTS")) + mocked_api.get(STORE_DRIVERS_URL).respond(json=load_json("store_drivers")) + mocked_api.get(STORE_PLUGINS_URL).respond(json=load_json("store_plugins")) - shutil.copytree(Path(__file__).parent / "store", store_path) return paths