From 6f48b774ae6bffea857e97bed04dd856c7d8aa55 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 15 Oct 2019 17:27:34 +0800 Subject: [PATCH] tests: add a needs_net mark for tests that require Internet access use pytest-httpbin for test_regex closes #113 --- setup.py | 1 + tests/conftest.py | 6 ++++++ tests/test_android.py | 2 +- tests/test_anitya.py | 2 +- tests/test_archpkg.py | 2 +- tests/test_aur.py | 2 +- tests/test_bitbucket.py | 2 +- tests/test_cpan.py | 2 +- tests/test_cratesio.py | 2 +- tests/test_debianpkg.py | 2 +- tests/test_gems.py | 2 +- tests/test_github.py | 1 + tests/test_gitlab.py | 1 + tests/test_gitlab_local.py | 1 + tests/test_hackage.py | 2 +- tests/test_keyfile.py | 2 +- tests/test_npm.py | 2 +- tests/test_packagist.py | 2 +- tests/test_pypi.py | 2 +- tests/test_regex.py | 32 ++++++++++++++++++++------------ 20 files changed, 44 insertions(+), 26 deletions(-) diff --git a/setup.py b/setup.py index 2deac9e..0ebda0c 100755 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ setup( tests_require = [ 'pytest', 'pytest-asyncio', + 'pytest-httpbin', 'flaky', ], entry_points = { diff --git a/tests/conftest.py b/tests/conftest.py index d624ec6..1763d01 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -70,3 +70,9 @@ def raise_on_logger_msg(): 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', + ) diff --git a/tests/test_android.py b/tests/test_android.py index 36f5260..36cd0b1 100644 --- a/tests/test_android.py +++ b/tests/test_android.py @@ -2,7 +2,7 @@ # Copyright (c) 2017 Yen Chi Hsuan import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_android_addon(get_version): assert await get_version("android-google-play-apk-expansion", {"android_sdk": "extras;google;market_apk_expansion", "repo": "addon"}) == "1.r03" diff --git a/tests/test_anitya.py b/tests/test_anitya.py index b6cc19a..296130f 100644 --- a/tests/test_anitya.py +++ b/tests/test_anitya.py @@ -2,7 +2,7 @@ # Copyright (c) 2017 Felix Yan , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_anitya(get_version): assert await get_version("shutter", {"anitya": "fedora/shutter"}) == "0.94.3" diff --git a/tests/test_archpkg.py b/tests/test_archpkg.py index f03783f..a057054 100644 --- a/tests/test_archpkg.py +++ b/tests/test_archpkg.py @@ -3,7 +3,7 @@ from flaky import flaky import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] @flaky async def test_archpkg(get_version): diff --git a/tests/test_aur.py b/tests/test_aur.py index 331528e..abce95d 100644 --- a/tests/test_aur.py +++ b/tests/test_aur.py @@ -3,7 +3,7 @@ from flaky import flaky import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] @flaky(max_runs=10) async def test_aur(get_version): diff --git a/tests/test_bitbucket.py b/tests/test_bitbucket.py index 3f03446..55734be 100644 --- a/tests/test_bitbucket.py +++ b/tests/test_bitbucket.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_bitbucket(get_version): assert await get_version("example", {"bitbucket": "prawee/git-tag"}) == "20150303" diff --git a/tests/test_cpan.py b/tests/test_cpan.py index b067e47..a35092e 100644 --- a/tests/test_cpan.py +++ b/tests/test_cpan.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_cpan(get_version): assert await get_version("POE-Component-Server-HTTPServer", {"cpan": None}) == "0.9.2" diff --git a/tests/test_cratesio.py b/tests/test_cratesio.py index a5c383b..44cf525 100644 --- a/tests/test_cratesio.py +++ b/tests/test_cratesio.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_cratesio(get_version): assert await get_version("example", {"cratesio": None}) == "0.1.0" diff --git a/tests/test_debianpkg.py b/tests/test_debianpkg.py index 5dcfe83..d7829a7 100644 --- a/tests/test_debianpkg.py +++ b/tests/test_debianpkg.py @@ -3,7 +3,7 @@ from flaky import flaky import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] @flaky(max_runs=10) async def test_debianpkg(get_version): diff --git a/tests/test_gems.py b/tests/test_gems.py index 844a291..a249d04 100644 --- a/tests/test_gems.py +++ b/tests/test_gems.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_gems(get_version): assert await get_version("example", {"gems": None}) == "1.0.2" diff --git a/tests/test_github.py b/tests/test_github.py index 518e04e..58226e0 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -5,6 +5,7 @@ import os import re import pytest pytestmark = [pytest.mark.asyncio, + pytest.mark.needs_net, pytest.mark.skipif("NVCHECKER_GITHUB_TOKEN" not in os.environ, reason="requires NVCHECKER_GITHUB_TOKEN, or it fails too much")] diff --git a/tests/test_gitlab.py b/tests/test_gitlab.py index 700848a..f593c28 100644 --- a/tests/test_gitlab.py +++ b/tests/test_gitlab.py @@ -4,6 +4,7 @@ import os import pytest pytestmark = [pytest.mark.asyncio, + pytest.mark.needs_net, pytest.mark.skipif("NVCHECKER_GITLAB_TOKEN_GITLAB_COM" not in os.environ, reason="requires NVCHECKER_GITLAB_TOKEN_GITLAB_COM")] diff --git a/tests/test_gitlab_local.py b/tests/test_gitlab_local.py index 82d2882..d39dc5f 100644 --- a/tests/test_gitlab_local.py +++ b/tests/test_gitlab_local.py @@ -5,6 +5,7 @@ import os import pytest import contextlib pytestmark = [pytest.mark.asyncio, + pytest.mark.needs_net, pytest.mark.skipif(os.environ.get('TRAVIS') == 'true', reason="rate-limited per IP")] diff --git a/tests/test_hackage.py b/tests/test_hackage.py index bc3446d..74bd87a 100644 --- a/tests/test_hackage.py +++ b/tests/test_hackage.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_hackage(get_version): assert await get_version("sessions", {"hackage": None}) == "2008.7.18" diff --git a/tests/test_keyfile.py b/tests/test_keyfile.py index 2eec251..b512f96 100644 --- a/tests/test_keyfile.py +++ b/tests/test_keyfile.py @@ -8,7 +8,7 @@ import contextlib from nvchecker.source import HTTPError import pytest -pytestmark = [pytest.mark.asyncio] +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] @contextlib.contextmanager def unset_github_token_env(): diff --git a/tests/test_npm.py b/tests/test_npm.py index f3ba06f..ef9f5ac 100644 --- a/tests/test_npm.py +++ b/tests/test_npm.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_npm(get_version): assert await get_version("example", {"npm": None}) == "0.0.0" diff --git a/tests/test_packagist.py b/tests/test_packagist.py index 7d23086..df68fe0 100644 --- a/tests/test_packagist.py +++ b/tests/test_packagist.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_packagist(get_version): assert await get_version("butterfly/example-web-application", {"packagist": None}) == "1.2.0" diff --git a/tests/test_pypi.py b/tests/test_pypi.py index 0bf935d..4f71fab 100644 --- a/tests/test_pypi.py +++ b/tests/test_pypi.py @@ -2,7 +2,7 @@ # Copyright (c) 2013-2017 lilydjwg , et al. import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_pypi(get_version): assert await get_version("example", {"pypi": None}) == "0.1.0" diff --git a/tests/test_regex.py b/tests/test_regex.py index 552c68c..54e2b3c 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -1,32 +1,40 @@ # MIT licensed # Copyright (c) 2013-2019 lilydjwg , et al. +import base64 + import pytest +import pytest_httpbin +assert pytest_httpbin # for pyflakes + pytestmark = pytest.mark.asyncio -async def test_regex_httpbin_default_user_agent(get_version): +def base64_encode(s): + return base64.b64encode(s.encode('utf-8')).decode('ascii') + +async def test_regex_httpbin_default_user_agent(get_version, httpbin): ua = await get_version("example", { - "url": "https://httpbin.org/get", - "regex": r'"User-Agent": "([^"]+)"', + "url": httpbin.url + "/get", + "regex": r'"User-Agent":\s*"([^"]+)"', }) assert ua.startswith("lilydjwg/nvchecker") -async def test_regex_httpbin_user_agent(get_version): +async def test_regex_httpbin_user_agent(get_version, httpbin): assert await get_version("example", { - "url": "https://httpbin.org/get", - "regex": r'"User-Agent": "(\w+)"', + "url": httpbin.url + "/get", + "regex": r'"User-Agent":\s*"(\w+)"', "user_agent": "Meow", }) == "Meow" -async def test_regex(get_version): +async def test_regex(get_version, httpbin): assert await get_version("example", { - "url": "http://example.net/", - "regex": r'for (\w+) examples', - }) == "illustrative" + "url": httpbin.url + "/base64/" + base64_encode("version 1.12 released"), + "regex": r'version ([0-9.]+)', + }) == "1.12" -async def test_missing_ok(get_version): +async def test_missing_ok(get_version, httpbin): assert await get_version("example", { - "url": "http://example.net/", + "url": httpbin.url + "/base64/" + base64_encode("something not there"), "regex": "foobar", "missing_ok": True, }) is None