From 6fcf5869adb5fe9f2838b3de4886a3e8291752f1 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 8 May 2018 18:18:10 +0800 Subject: [PATCH] add test_keyfile --- tests/conftest.py | 26 ++++++++++++++++++++++++++ tests/test_keyfile.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/test_keyfile.py diff --git a/tests/conftest.py b/tests/conftest.py index 102dffa..d5c9a75 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,34 @@ import configparser import pytest import asyncio +import io 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 = '' + + s = TestSource(future, file) + await s.check() + return await future + + return __call__ @pytest.fixture(scope="module") async def get_version(): diff --git a/tests/test_keyfile.py b/tests/test_keyfile.py new file mode 100644 index 0000000..3e51dea --- /dev/null +++ b/tests/test_keyfile.py @@ -0,0 +1,35 @@ +# MIT licensed +# Copyright (c) 2018 lilydjwg , 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