2023-02-22 23:32:48 +08:00
|
|
|
import os
|
2023-06-19 17:48:59 +08:00
|
|
|
import threading
|
2022-01-07 18:38:04 +08:00
|
|
|
from pathlib import Path
|
2023-06-19 17:48:59 +08:00
|
|
|
from typing import TYPE_CHECKING, Set, Generator
|
2022-01-07 18:38:04 +08:00
|
|
|
|
|
|
|
import pytest
|
2023-02-22 23:32:48 +08:00
|
|
|
from nonebug import NONEBOT_INIT_KWARGS
|
2023-06-19 17:48:59 +08:00
|
|
|
from werkzeug.serving import BaseWSGIServer, make_server
|
2023-02-22 23:32:48 +08:00
|
|
|
|
|
|
|
import nonebot
|
2023-06-19 17:48:59 +08:00
|
|
|
from nonebot.drivers import URL
|
|
|
|
from fake_server import request_handler
|
2023-02-22 23:32:48 +08:00
|
|
|
|
|
|
|
os.environ["CONFIG_FROM_ENV"] = '{"test": "test"}'
|
|
|
|
os.environ["CONFIG_OVERRIDE"] = "new"
|
2022-01-07 18:38:04 +08:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from nonebot.plugin import Plugin
|
|
|
|
|
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
def pytest_configure(config: pytest.Config) -> None:
|
|
|
|
config.stash[NONEBOT_INIT_KWARGS] = {"config_from_init": "init"}
|
2022-01-07 18:38:04 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def load_plugin(nonebug_init: None) -> Set["Plugin"]:
|
|
|
|
# preload global plugins
|
2022-01-07 18:38:04 +08:00
|
|
|
return nonebot.load_plugins(str(Path(__file__).parent / "plugins"))
|
|
|
|
|
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2023-05-30 15:20:31 +08:00
|
|
|
def load_builtin_plugin(nonebug_init: None) -> Set["Plugin"]:
|
|
|
|
# preload builtin plugins
|
|
|
|
return nonebot.load_builtin_plugins("echo", "single_session")
|
2023-06-19 17:48:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def server() -> Generator[BaseWSGIServer, None, None]:
|
|
|
|
server = make_server("127.0.0.1", 0, app=request_handler)
|
|
|
|
thread = threading.Thread(target=server.serve_forever)
|
|
|
|
thread.start()
|
|
|
|
try:
|
|
|
|
yield server
|
|
|
|
finally:
|
|
|
|
server.shutdown()
|
|
|
|
thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def server_url(server: BaseWSGIServer) -> URL:
|
|
|
|
return URL(f"http://{server.host}:{server.port}")
|