tests: add a needs_net mark for tests that require Internet access

use pytest-httpbin for test_regex

closes #113
This commit is contained in:
lilydjwg 2019-10-15 17:27:34 +08:00
parent 484778218d
commit 6f48b774ae
20 changed files with 44 additions and 26 deletions

View file

@ -28,6 +28,7 @@ setup(
tests_require = [ tests_require = [
'pytest', 'pytest',
'pytest-asyncio', 'pytest-asyncio',
'pytest-httpbin',
'flaky', 'flaky',
], ],
entry_points = { entry_points = {

View file

@ -70,3 +70,9 @@ def raise_on_logger_msg():
return event_dict['event'] return event_dict['event']
structlog.configure([proc]) structlog.configure([proc])
def pytest_configure(config):
# register an additional marker
config.addinivalue_line(
'markers', 'needs_net: mark test to require Internet access',
)

View file

@ -2,7 +2,7 @@
# Copyright (c) 2017 Yen Chi Hsuan <yan12125 at gmail dot com> # Copyright (c) 2017 Yen Chi Hsuan <yan12125 at gmail dot com>
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_android_addon(get_version): 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" assert await get_version("android-google-play-apk-expansion", {"android_sdk": "extras;google;market_apk_expansion", "repo": "addon"}) == "1.r03"

View file

@ -2,7 +2,7 @@
# Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al. # Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_anitya(get_version): async def test_anitya(get_version):
assert await get_version("shutter", {"anitya": "fedora/shutter"}) == "0.94.3" assert await get_version("shutter", {"anitya": "fedora/shutter"}) == "0.94.3"

View file

@ -3,7 +3,7 @@
from flaky import flaky from flaky import flaky
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
@flaky @flaky
async def test_archpkg(get_version): async def test_archpkg(get_version):

View file

@ -3,7 +3,7 @@
from flaky import flaky from flaky import flaky
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
@flaky(max_runs=10) @flaky(max_runs=10)
async def test_aur(get_version): async def test_aur(get_version):

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_bitbucket(get_version): async def test_bitbucket(get_version):
assert await get_version("example", {"bitbucket": "prawee/git-tag"}) == "20150303" assert await get_version("example", {"bitbucket": "prawee/git-tag"}) == "20150303"

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_cpan(get_version): async def test_cpan(get_version):
assert await get_version("POE-Component-Server-HTTPServer", {"cpan": None}) == "0.9.2" assert await get_version("POE-Component-Server-HTTPServer", {"cpan": None}) == "0.9.2"

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_cratesio(get_version): async def test_cratesio(get_version):
assert await get_version("example", {"cratesio": None}) == "0.1.0" assert await get_version("example", {"cratesio": None}) == "0.1.0"

View file

@ -3,7 +3,7 @@
from flaky import flaky from flaky import flaky
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
@flaky(max_runs=10) @flaky(max_runs=10)
async def test_debianpkg(get_version): async def test_debianpkg(get_version):

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_gems(get_version): async def test_gems(get_version):
assert await get_version("example", {"gems": None}) == "1.0.2" assert await get_version("example", {"gems": None}) == "1.0.2"

View file

@ -5,6 +5,7 @@ import os
import re import re
import pytest import pytest
pytestmark = [pytest.mark.asyncio, pytestmark = [pytest.mark.asyncio,
pytest.mark.needs_net,
pytest.mark.skipif("NVCHECKER_GITHUB_TOKEN" not in os.environ, pytest.mark.skipif("NVCHECKER_GITHUB_TOKEN" not in os.environ,
reason="requires NVCHECKER_GITHUB_TOKEN, or it fails too much")] reason="requires NVCHECKER_GITHUB_TOKEN, or it fails too much")]

View file

@ -4,6 +4,7 @@
import os import os
import pytest import pytest
pytestmark = [pytest.mark.asyncio, pytestmark = [pytest.mark.asyncio,
pytest.mark.needs_net,
pytest.mark.skipif("NVCHECKER_GITLAB_TOKEN_GITLAB_COM" not in os.environ, pytest.mark.skipif("NVCHECKER_GITLAB_TOKEN_GITLAB_COM" not in os.environ,
reason="requires NVCHECKER_GITLAB_TOKEN_GITLAB_COM")] reason="requires NVCHECKER_GITLAB_TOKEN_GITLAB_COM")]

View file

@ -5,6 +5,7 @@ import os
import pytest import pytest
import contextlib import contextlib
pytestmark = [pytest.mark.asyncio, pytestmark = [pytest.mark.asyncio,
pytest.mark.needs_net,
pytest.mark.skipif(os.environ.get('TRAVIS') == 'true', pytest.mark.skipif(os.environ.get('TRAVIS') == 'true',
reason="rate-limited per IP")] reason="rate-limited per IP")]

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_hackage(get_version): async def test_hackage(get_version):
assert await get_version("sessions", {"hackage": None}) == "2008.7.18" assert await get_version("sessions", {"hackage": None}) == "2008.7.18"

View file

@ -8,7 +8,7 @@ import contextlib
from nvchecker.source import HTTPError from nvchecker.source import HTTPError
import pytest import pytest
pytestmark = [pytest.mark.asyncio] pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
@contextlib.contextmanager @contextlib.contextmanager
def unset_github_token_env(): def unset_github_token_env():

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_npm(get_version): async def test_npm(get_version):
assert await get_version("example", {"npm": None}) == "0.0.0" assert await get_version("example", {"npm": None}) == "0.0.0"

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_packagist(get_version): async def test_packagist(get_version):
assert await get_version("butterfly/example-web-application", {"packagist": None}) == "1.2.0" assert await get_version("butterfly/example-web-application", {"packagist": None}) == "1.2.0"

View file

@ -2,7 +2,7 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
pytestmark = pytest.mark.asyncio pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_pypi(get_version): async def test_pypi(get_version):
assert await get_version("example", {"pypi": None}) == "0.1.0" assert await get_version("example", {"pypi": None}) == "0.1.0"

View file

@ -1,32 +1,40 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2019 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2019 lilydjwg <lilydjwg@gmail.com>, et al.
import base64
import pytest import pytest
import pytest_httpbin
assert pytest_httpbin # for pyflakes
pytestmark = pytest.mark.asyncio 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", { ua = await get_version("example", {
"url": "https://httpbin.org/get", "url": httpbin.url + "/get",
"regex": r'"User-Agent": "([^"]+)"', "regex": r'"User-Agent":\s*"([^"]+)"',
}) })
assert ua.startswith("lilydjwg/nvchecker") 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", { assert await get_version("example", {
"url": "https://httpbin.org/get", "url": httpbin.url + "/get",
"regex": r'"User-Agent": "(\w+)"', "regex": r'"User-Agent":\s*"(\w+)"',
"user_agent": "Meow", "user_agent": "Meow",
}) == "Meow" }) == "Meow"
async def test_regex(get_version): async def test_regex(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"url": "http://example.net/", "url": httpbin.url + "/base64/" + base64_encode("version 1.12 released"),
"regex": r'for (\w+) examples', "regex": r'version ([0-9.]+)',
}) == "illustrative" }) == "1.12"
async def test_missing_ok(get_version): async def test_missing_ok(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"url": "http://example.net/", "url": httpbin.url + "/base64/" + base64_encode("something not there"),
"regex": "foobar", "regex": "foobar",
"missing_ok": True, "missing_ok": True,
}) is None }) is None