2017-07-04 09:04:29 +00:00
|
|
|
import configparser
|
|
|
|
import pytest
|
|
|
|
import asyncio
|
2018-05-08 10:18:10 +00:00
|
|
|
import io
|
2018-09-21 04:17:21 +00:00
|
|
|
import structlog
|
2017-07-04 09:04:29 +00:00
|
|
|
|
|
|
|
from nvchecker.get_version import get_version as _get_version
|
2018-10-10 09:17:02 +00:00
|
|
|
from nvchecker.get_version import _cache
|
2018-05-08 10:18:10 +00:00
|
|
|
from nvchecker.core import Source
|
|
|
|
|
|
|
|
class TestSource(Source):
|
2018-10-10 09:08:28 +00:00
|
|
|
def __init__(self, future, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._future = future
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
def on_update(self, name, version, oldver):
|
|
|
|
self._future.set_result(version)
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
def on_no_result(self, name):
|
|
|
|
self._future.set_result(None)
|
2018-05-08 11:15:36 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
def on_exception(self, name, exc):
|
|
|
|
self._future.set_exception(exc)
|
2018-05-08 10:18:10 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2018-10-10 09:22:02 +00:00
|
|
|
async def run_source():
|
|
|
|
async def __call__(conf, *, clear_cache=False):
|
|
|
|
if clear_cache:
|
|
|
|
_cache.clear()
|
2018-10-10 09:17:02 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
future = asyncio.Future()
|
|
|
|
file = io.StringIO(conf)
|
|
|
|
file.name = '<StringIO>'
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
s = TestSource(future, file)
|
|
|
|
await s.check()
|
|
|
|
return await future
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
return __call__
|
2017-07-04 09:04:29 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
async def get_version():
|
2018-10-10 09:08:28 +00:00
|
|
|
async def __call__(name, config):
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
if isinstance(config, dict):
|
|
|
|
_config = configparser.ConfigParser(dict_type=dict, allow_no_value=True)
|
|
|
|
_config.read_dict({name: config})
|
|
|
|
config = _config[name]
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
return await _get_version(name, config)
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
return __call__
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2017-12-22 06:15:05 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-07-04 09:04:29 +00:00
|
|
|
def event_loop(request):
|
2018-10-10 09:08:28 +00:00
|
|
|
"""Override pytest-asyncio's event_loop fixture,
|
|
|
|
Don't create an instance of the default event loop for each test case.
|
|
|
|
"""
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
yield loop
|
2018-09-21 04:17:21 +00:00
|
|
|
|
2019-03-07 08:25:54 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2018-09-21 04:17:21 +00:00
|
|
|
def raise_on_logger_msg():
|
2018-10-10 09:08:28 +00:00
|
|
|
def proc(logger, method_name, event_dict):
|
2018-12-15 11:44:26 +00:00
|
|
|
if method_name in ('warning', 'error'):
|
2019-03-07 08:45:14 +00:00
|
|
|
if 'exc_info' in event_dict:
|
|
|
|
raise event_dict['exc_info']
|
2019-05-01 09:26:10 +00:00
|
|
|
if not event_dict['event'].startswith(('rate limited', 'no-result')):
|
2019-04-08 04:15:49 +00:00
|
|
|
raise RuntimeError(event_dict['event'])
|
2018-10-10 09:08:28 +00:00
|
|
|
return event_dict['event']
|
2018-09-21 04:17:21 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
structlog.configure([proc])
|
2019-10-15 09:27:34 +00:00
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
# register an additional marker
|
|
|
|
config.addinivalue_line(
|
|
|
|
'markers', 'needs_net: mark test to require Internet access',
|
|
|
|
)
|