add test_keyfile

This commit is contained in:
lilydjwg 2018-05-08 18:18:10 +08:00
parent cf817ffe27
commit 6fcf5869ad
2 changed files with 61 additions and 0 deletions

View File

@ -1,8 +1,34 @@
import configparser import configparser
import pytest import pytest
import asyncio import asyncio
import io
from nvchecker.get_version import get_version as _get_version from nvchecker.get_version import get_version as _get_version
from nvchecker.core import Source
class TestSource(Source):
def __init__(self, future, *args, **kwargs):
super().__init__(*args, **kwargs)
self._future = future
def on_update(self, name, version, oldver):
self._future.set_result(version)
def on_exception(self, name, exc):
self._future.set_exception(exc)
@pytest.fixture(scope="module")
async def run_source():
async def __call__(conf):
future = asyncio.Future()
file = io.StringIO(conf)
file.name = '<StringIO>'
s = TestSource(future, file)
await s.check()
return await future
return __call__
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
async def get_version(): async def get_version():

35
tests/test_keyfile.py Normal file
View File

@ -0,0 +1,35 @@
# MIT licensed
# Copyright (c) 2018 lilydjwg <lilydjwg@gmail.com>, et al.
import tempfile
import pytest
pytestmark = [pytest.mark.asyncio]
async def test_keyfile_missing(run_source):
test_conf = '''\
[example]
github = harry-sanabria/ReleaseTestRepo
'''
assert await run_source(test_conf) == '20140122.012101'
async def test_keyfile_invalid(run_source):
with tempfile.NamedTemporaryFile(mode='w+') as f:
f.write('''\
[keys]
github = xxx
''')
f.flush()
test_conf = f'''\
[example]
github = harry-sanabria/ReleaseTestRepo
[__config__]
keyfile = {f.name}
'''
try:
await run_source(test_conf)
except Exception as e:
assert e.code == 401