linkchecker/tests/__init__.py

289 lines
6.9 KiB
Python
Raw Normal View History

2014-01-08 21:33:04 +00:00
# Copyright (C) 2005-2014 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2009-07-24 21:58:20 +00:00
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import signal
import subprocess
import os
2011-04-26 09:54:04 +00:00
import sys
2009-03-07 14:00:02 +00:00
import socket
import unittest
import pytest
from contextlib import contextmanager
from functools import lru_cache, wraps
from linkcheck import init_i18n, LinkCheckerInterrupt
class TestBase(unittest.TestCase):
"""
Base class for tests.
"""
def setUp(self):
"""Ensure the current locale setting is the default.
Otherwise, warnings will get translated and will break tests."""
super().setUp()
os.environ["LANG"] = "C"
init_i18n()
2024-08-27 18:34:28 +00:00
@lru_cache(1)
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"""
if kwargs.get("shell"):
# for shell calls the command must be a string
cmd = " ".join(cmd)
return subprocess.call(cmd, **kwargs)
def run_checked(cmd, ret_ok=(0,), **kwargs):
"""Run command and raise OSError on error."""
retcode = run(cmd, **kwargs)
if retcode not in ret_ok:
msg = "Command `%s' returned non-zero exit status %d" % (cmd, retcode)
raise OSError(msg)
return retcode
def run_silent(cmd):
2009-03-08 18:34:00 +00:00
"""Run given command without output."""
2020-05-28 19:29:13 +00:00
null = open(os.name == "nt" and ":NUL" or "/dev/null", "w")
try:
return run(cmd, stdout=null, stderr=subprocess.STDOUT)
finally:
null.close()
def _need_func(testfunc, name, strict=True):
2010-02-22 07:02:19 +00:00
"""Decorator skipping test if given testfunc fails."""
2020-05-28 19:29:13 +00:00
def check_func(func):
@wraps(func)
def newfunc(*args, **kwargs):
2010-02-22 07:02:19 +00:00
if not testfunc():
skip("%s is not available" % name, strict)
2010-02-22 07:02:19 +00:00
return func(*args, **kwargs)
2020-05-28 19:29:13 +00:00
2010-02-22 07:02:19 +00:00
return newfunc
2020-05-28 19:29:13 +00:00
2010-02-22 07:02:19 +00:00
return check_func
@lru_cache(1)
def has_network():
2009-03-08 18:34:00 +00:00
"""Test if network is up."""
2009-03-07 14:00:02 +00:00
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("www.python.org", 80))
2009-03-07 14:00:02 +00:00
s.close()
return True
except Exception:
2009-03-07 14:00:02 +00:00
pass
return False
2020-05-28 19:29:13 +00:00
2010-02-22 07:02:19 +00:00
need_network = _need_func(has_network, "network")
@lru_cache(1)
def has_msgfmt():
2009-03-08 18:34:00 +00:00
"""Test if msgfmt is available."""
return run_silent(["msgfmt", "-V"]) == 0
2020-05-28 19:29:13 +00:00
2010-02-22 07:02:19 +00:00
need_msgfmt = _need_func(has_msgfmt, "msgfmt")
@lru_cache(1)
def has_posix():
2009-03-08 18:34:00 +00:00
"""Test if this is a POSIX system."""
return os.name == "posix"
2020-05-28 19:29:13 +00:00
need_posix = _need_func(has_posix, "POSIX system", False)
2010-02-22 07:02:19 +00:00
@lru_cache(1)
def has_windows():
2012-06-23 11:32:38 +00:00
"""Test if this is a Windows system."""
return os.name == "nt"
2020-05-28 19:29:13 +00:00
need_windows = _need_func(has_windows, "Windows system", False)
2012-06-23 11:32:38 +00:00
@lru_cache(1)
def has_linux():
2011-04-26 09:54:04 +00:00
"""Test if this is a Linux system."""
return sys.platform.startswith("linux")
2020-05-28 19:29:13 +00:00
need_linux = _need_func(has_linux, "Linux system", False)
2011-04-26 09:54:04 +00:00
@lru_cache(1)
def has_clamav():
2009-03-08 18:34:00 +00:00
"""Test if ClamAV daemon is installed and running."""
try:
cmd = ["grep", "LocalSocket", "/etc/clamav/clamd.conf"]
sock = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].split()[1]
if sock:
2009-03-07 14:00:02 +00:00
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(sock)
s.close()
return True
except Exception:
pass
return False
2020-05-28 19:29:13 +00:00
need_clamav = _need_func(has_clamav, "ClamAV", False) # XXX Plugin disabled
2010-02-22 07:02:19 +00:00
@lru_cache(1)
def has_proxy():
2009-03-08 18:34:00 +00:00
"""Test if proxy is running on port 8081."""
2009-02-28 12:06:44 +00:00
try:
2009-03-07 14:00:02 +00:00
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 8081))
s.close()
return True
except Exception:
return False
2009-02-28 12:06:44 +00:00
2020-05-28 19:29:13 +00:00
2010-02-22 07:02:19 +00:00
need_proxy = _need_func(has_proxy, "proxy")
@lru_cache(1)
def has_pyftpdlib():
"""Test if pyftpdlib is available."""
try:
import pyftpdlib
2020-05-28 19:29:13 +00:00
return True
except ImportError:
return False
2020-05-28 19:29:13 +00:00
need_pyftpdlib = _need_func(has_pyftpdlib, "pyftpdlib")
@lru_cache(1)
def has_x11():
"""Test if DISPLAY variable is set."""
2020-05-28 19:29:13 +00:00
return os.getenv("DISPLAY") is not None
2020-05-28 19:29:13 +00:00
need_x11 = _need_func(has_x11, "X11")
@lru_cache(1)
def has_geoip():
from linkcheck.plugins import locationinfo
return locationinfo.geoip is not None
need_geoip = _need_func(has_geoip, "geoip")
@lru_cache(1)
2013-01-09 22:02:47 +00:00
def has_word():
"""Test if Word is available."""
2014-04-28 16:13:45 +00:00
from linkcheck.plugins import parseword
2020-05-28 19:29:13 +00:00
2014-04-28 16:13:45 +00:00
return parseword.has_word()
2013-01-09 22:02:47 +00:00
2020-05-28 19:29:13 +00:00
need_word = _need_func(has_word, "Word", False)
2013-01-09 22:02:47 +00:00
@lru_cache(1)
2014-04-29 16:53:24 +00:00
def has_pdflib():
from linkcheck.plugins import parsepdf
2020-05-28 19:29:13 +00:00
2014-04-29 16:53:24 +00:00
return parsepdf.has_pdflib
2020-05-28 19:29:13 +00:00
need_pdflib = _need_func(has_pdflib, "pdflib")
2014-04-29 16:53:24 +00:00
@contextmanager
def _limit_time(seconds):
"""Raises LinkCheckerInterrupt if given number of seconds have passed."""
2020-05-28 19:29:13 +00:00
if os.name == "posix":
def signal_handler(signum, frame):
raise LinkCheckerInterrupt("timed out")
2020-05-28 19:29:13 +00:00
old_handler = signal.getsignal(signal.SIGALRM)
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
yield
2020-05-28 19:29:13 +00:00
if os.name == "posix":
signal.alarm(0)
if old_handler is not None:
signal.signal(signal.SIGALRM, old_handler)
def limit_time(seconds, skip=False):
2009-03-07 14:00:02 +00:00
"""Limit test time to the given number of seconds, else fail or skip."""
2020-05-28 19:29:13 +00:00
def run_limited(func):
def new_func(*args, **kwargs):
try:
2009-03-07 14:00:02 +00:00
with _limit_time(seconds):
return func(*args, **kwargs)
2014-07-15 13:41:59 +00:00
except LinkCheckerInterrupt as msg:
2009-03-07 14:00:02 +00:00
if skip:
pytest.skip("time limit of %d seconds exceeded" % seconds)
assert False, msg
2020-05-28 19:29:13 +00:00
new_func.__name__ = func.__name__
return new_func
2020-05-28 19:29:13 +00:00
return run_limited
def get_file(filename=None):
2011-12-17 15:38:25 +00:00
"""
Get file name located within 'data' directory.
"""
directory = os.path.join("tests", "checker", "data")
if filename:
2020-05-19 18:56:42 +00:00
return os.path.join(directory, filename)
return directory
2011-12-17 15:38:25 +00:00
2012-04-23 18:58:55 +00:00
2020-05-28 19:29:13 +00:00
if __name__ == "__main__":
2018-01-05 16:19:20 +00:00
print("has clamav", has_clamav())
print("has network", has_network())
print("has msgfmt", has_msgfmt())
print("has POSIX", has_posix())
print("has proxy", has_proxy())
print("has X11", has_x11())