nvchecker/tests/conftest.py

112 lines
2.7 KiB
Python
Raw Permalink Normal View History

2020-08-11 09:43:03 +00:00
# MIT licensed
# Copyright (c) 2020, 2024 lilydjwg <lilydjwg@gmail.com>, et al.
2020-08-11 09:43:03 +00:00
2017-07-04 09:04:29 +00:00
import asyncio
import structlog
import os
from pathlib import Path
from typing import TYPE_CHECKING, Dict
2017-07-04 09:04:29 +00:00
if TYPE_CHECKING:
import tomli as tomllib
else:
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
2020-08-17 07:11:14 +00:00
import pytest
import pytest_asyncio
2018-05-08 10:18:10 +00:00
2020-08-17 07:11:14 +00:00
from nvchecker import core
from nvchecker import __main__ as main
from nvchecker.util import Entries, ResultData, RawResult
2020-08-17 07:11:14 +00:00
2020-08-20 07:18:05 +00:00
use_keyfile = False
2020-08-17 07:11:14 +00:00
async def run(
entries: Entries, max_concurrency: int = 20,
) -> Dict[str, str]:
task_sem = asyncio.Semaphore(max_concurrency)
2020-08-17 07:11:14 +00:00
result_q: asyncio.Queue[RawResult] = asyncio.Queue()
keyfile = os.environ.get('KEYFILE')
2020-08-20 07:18:05 +00:00
if use_keyfile and keyfile:
filepath = Path(keyfile)
keymanager = core.KeyManager(filepath)
2020-08-17 07:11:14 +00:00
else:
keymanager = core.KeyManager(None)
dispatcher = core.setup_httpclient()
entry_waiter = core.EntryWaiter()
futures = dispatcher.dispatch(
entries, task_sem, result_q,
keymanager, entry_waiter, 1, {},
2020-08-17 07:11:14 +00:00
)
2018-05-08 10:18:10 +00:00
oldvers: ResultData = {}
result_coro = core.process_result(oldvers, result_q, entry_waiter)
2020-08-17 07:11:14 +00:00
runner_coro = core.run_tasks(futures)
results, _has_failures = await main.run(result_coro, runner_coro)
return {k: r.version for k, r in results.items()}
2018-05-08 10:18:10 +00:00
2023-12-09 11:17:31 +00:00
@pytest_asyncio.fixture(scope="session")
2020-08-17 07:11:14 +00:00
async def get_version():
async def __call__(name, config):
entries = {name: config}
newvers = await run(entries)
2020-08-17 08:21:02 +00:00
return newvers.get(name)
2018-05-08 10:18:10 +00:00
return __call__
2017-07-04 09:04:29 +00:00
2023-12-09 11:17:31 +00:00
@pytest_asyncio.fixture(scope="session")
2020-08-17 07:11:14 +00:00
async def run_str():
async def __call__(str):
entries = tomllib.loads(str)
2020-08-17 07:11:14 +00:00
newvers = await run(entries)
return newvers.popitem()[1]
2017-07-04 09:04:29 +00:00
2020-08-17 07:11:14 +00:00
return __call__
2017-07-04 09:04:29 +00:00
2023-12-09 11:17:31 +00:00
@pytest_asyncio.fixture(scope="session")
2020-08-17 07:11:14 +00:00
async def run_str_multi():
async def __call__(str):
entries = tomllib.loads(str)
2020-08-17 07:11:14 +00:00
newvers = await run(entries)
return newvers
2017-07-04 09:04:29 +00:00
return __call__
2017-07-04 09:04:29 +00:00
@pytest.fixture(scope="session", autouse=True)
def raise_on_logger_msg():
def proc(logger, method_name, event_dict):
if method_name in ('warning', 'error'):
2019-03-07 08:45:14 +00:00
if 'exc_info' in event_dict:
exc = event_dict['exc_info']
if isinstance(exc, Exception):
raise exc
else: # exc_info=True
raise
if not event_dict['event'].startswith(('rate limited', 'no-result')):
raise RuntimeError(event_dict['event'])
return event_dict['event']
structlog.configure([proc])
def pytest_configure(config):
# register an additional marker
config.addinivalue_line(
'markers', 'needs_net: mark test to require Internet access',
)
2020-08-20 07:18:05 +00:00
@pytest.fixture
def keyfile():
global use_keyfile
if 'KEYFILE' not in os.environ:
pytest.skip('KEYFILE not set')
return
use_keyfile = True
yield
use_keyfile = False