Skip to content

Commit

Permalink
improve: 直接获取所需数据
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Feb 6, 2024
1 parent 82588e8 commit 82289c0
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 81 deletions.
7 changes: 0 additions & 7 deletions examples/store-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 中也有一个常量,需要同时修改"""
4 changes: 2 additions & 2 deletions src/utils/store_test/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
import os
from asyncio import run

import click

Expand All @@ -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__":
Expand Down
15 changes: 0 additions & 15 deletions src/utils/store_test/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
""" 上次测试生成的插件列表文件路径 """
32 changes: 18 additions & 14 deletions src/utils/store_test/store.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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)

# 保存测试结果与生成的列表
Expand Down
17 changes: 8 additions & 9 deletions src/utils/store_test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 文件
Expand Down Expand Up @@ -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()
2 changes: 0 additions & 2 deletions src/utils/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, ""
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 28 additions & 31 deletions tests/utils/store_test/test_store_test.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
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",
"adapters": plugin_test_path / "adapters.json",
"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(
Expand All @@ -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


Expand Down

0 comments on commit 82289c0

Please sign in to comment.