From 6d14986bd6bdf657566f215ae61dba0cd8008469 Mon Sep 17 00:00:00 2001 From: Chris Mayo Date: Tue, 27 Aug 2024 19:34:28 +0100 Subject: [PATCH 1/4] Add tests.running_in_ci() --- tests/__init__.py | 5 +++++ tests/checker/test_httpbin.py | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index b99a563a..ab756bf7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -38,6 +38,11 @@ class TestBase(unittest.TestCase): init_i18n() +@lru_cache(1) +def running_in_ci(): + return "CI" in os.environ + + def run(cmd, verbosity=0, **kwargs): """Run command without error checking. @return: command return code""" diff --git a/tests/checker/test_httpbin.py b/tests/checker/test_httpbin.py index db17f6c9..6c4d6128 100644 --- a/tests/checker/test_httpbin.py +++ b/tests/checker/test_httpbin.py @@ -16,15 +16,14 @@ """ Test http stuff with httpbin.org. """ -import os import re -from tests import need_network +from tests import need_network, running_in_ci from . import LinkCheckTest def get_httpbin_url(path): """Get httpbin URL.""" - if "CI" in os.environ: + if running_in_ci(): host = "localhost:8080" else: host = "httpbin.org" From 618c7305d60bbf130e6b213b1f41ba9f613481d0 Mon Sep 17 00:00:00 2001 From: Chris Mayo Date: Tue, 27 Aug 2024 19:34:28 +0100 Subject: [PATCH 2/4] Fail tests instead of skipping if resources are unavailable in CI --- tests/__init__.py | 19 +++++++++++++------ tests/checker/ftpserver.py | 5 +++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index ab756bf7..0bfe671e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -43,6 +43,13 @@ def running_in_ci(): return "CI" in os.environ +def skip(reason, strict=True): + if strict and running_in_ci(): + pytest.fail(reason) + else: + pytest.skip(reason) + + def run(cmd, verbosity=0, **kwargs): """Run command without error checking. @return: command return code""" @@ -70,14 +77,14 @@ def run_silent(cmd): null.close() -def _need_func(testfunc, name): +def _need_func(testfunc, name, strict=True): """Decorator skipping test if given testfunc fails.""" def check_func(func): @wraps(func) def newfunc(*args, **kwargs): if not testfunc(): - pytest.skip("%s is not available" % name) + skip("%s is not available" % name, strict) return func(*args, **kwargs) return newfunc @@ -116,7 +123,7 @@ def has_posix(): return os.name == "posix" -need_posix = _need_func(has_posix, "POSIX system") +need_posix = _need_func(has_posix, "POSIX system", False) @lru_cache(1) @@ -125,7 +132,7 @@ def has_windows(): return os.name == "nt" -need_windows = _need_func(has_windows, "Windows system") +need_windows = _need_func(has_windows, "Windows system", False) @lru_cache(1) @@ -134,7 +141,7 @@ def has_linux(): return sys.platform.startswith("linux") -need_linux = _need_func(has_linux, "Linux system") +need_linux = _need_func(has_linux, "Linux system", False) @lru_cache(1) @@ -212,7 +219,7 @@ def has_word(): return parseword.has_word() -need_word = _need_func(has_word, "Word") +need_word = _need_func(has_word, "Word", False) @lru_cache(1) diff --git a/tests/checker/ftpserver.py b/tests/checker/ftpserver.py index 8643f616..e3e01379 100644 --- a/tests/checker/ftpserver.py +++ b/tests/checker/ftpserver.py @@ -19,8 +19,9 @@ Define http test support classes for LinkChecker tests. import os import time import threading -import pytest from ftplib import FTP + +from tests import skip from . import LinkCheckTest @@ -61,7 +62,7 @@ def start_server(host, port): from pyftpdlib.servers import FTPServer from pyftpdlib import __ver__ as pyftpdlib_version except ImportError: - pytest.skip("pyftpdlib is not available") + skip("pyftpdlib is not available") return authorizer = DummyAuthorizer() datadir = os.path.join(os.path.dirname(__file__), "data") From 83958558d370efb07c22b3f923553eeb013c6cc0 Mon Sep 17 00:00:00 2001 From: Chris Mayo Date: Tue, 27 Aug 2024 19:34:28 +0100 Subject: [PATCH 3/4] Fix TestFile.test_bad_file() skip message --- tests/checker/test_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/checker/test_file.py b/tests/checker/test_file.py index b8098d63..f2a12935 100644 --- a/tests/checker/test_file.py +++ b/tests/checker/test_file.py @@ -135,7 +135,7 @@ class TestFile(LinkCheckTest): # Fails on NT platforms and I am too lazy to fix # Cause: url get quoted %7C which gets lowercased to # %7c and this fails. - pytest.skip("Not running on NT") + pytest.skip("Running on Windows") url = "file:/%(curdir)s/%(datadir)s/file.txt" % self.get_attrs() nurl = self.norm(url) resultlines = [ From 48496b38540974dbd93818e66444966d0951e31f Mon Sep 17 00:00:00 2001 From: Chris Mayo Date: Tue, 27 Aug 2024 19:34:28 +0100 Subject: [PATCH 4/4] Use a geoip tox factor GeoIP requires libGeoIP which tox deps cannot install. Likewise the GeoLiteCountry database which is also required by TestLocationInfo(). The last GeoIP release was in 2014, and libGeoIP has been EOL since 2022, but it is still available in Debian/Ubuntu. --- .github/workflows/build.yml | 2 +- tox.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index afb35ffc..d5e7b51c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -105,7 +105,7 @@ jobs: - name: Run tests run: | python -m hatchling build -t sdist --hooks-only - python -m tox -e ${{ matrix.toxenv }} + python -m tox -e ${{ matrix.toxenv }}-geoip - name: Report to coveralls uses: coverallsapp/github-action@f350da2c033043742f89e8c0b7b5145a1616da6d # v2.1.2 diff --git a/tox.ini b/tox.ini index 2878d082..90b356cc 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ deps = pyopenssl pytest-xdist pytest-cov - GeoIP + geoip: GeoIP [testenv] usedevelop = true