mirror of
https://github.com/Hopiu/linkchecker.git
synced 2026-04-15 20:01:03 +00:00
Use decorators for test resources
This commit is contained in:
parent
ddacfc94e4
commit
9a280607e0
10 changed files with 69 additions and 64 deletions
|
|
@ -61,6 +61,18 @@ def _run (cmd):
|
|||
return -1
|
||||
|
||||
|
||||
def _need_func (testfunc, name):
|
||||
"""Decorator skipping test if given testfunc fails."""
|
||||
def check_func (func):
|
||||
def newfunc (*args, **kwargs):
|
||||
if not testfunc():
|
||||
raise SkipTest("%s is not available" % name)
|
||||
return func(*args, **kwargs)
|
||||
newfunc.func_name = func.func_name
|
||||
return newfunc
|
||||
return check_func
|
||||
|
||||
|
||||
@memoized
|
||||
def has_network ():
|
||||
"""Test if network is up."""
|
||||
|
|
@ -73,18 +85,24 @@ def has_network ():
|
|||
pass
|
||||
return False
|
||||
|
||||
need_network = _need_func(has_network, "network")
|
||||
|
||||
|
||||
@memoized
|
||||
def has_msgfmt ():
|
||||
"""Test if msgfmt is available."""
|
||||
return _run(["msgfmt", "-V"]) == 0
|
||||
|
||||
need_msgfmt = _need_func(has_msgfmt, "msgfmt")
|
||||
|
||||
|
||||
@memoized
|
||||
def has_posix ():
|
||||
"""Test if this is a POSIX system."""
|
||||
return os.name == "posix"
|
||||
|
||||
need_posix = _need_func(has_posix, "POSIX system")
|
||||
|
||||
|
||||
@memoized
|
||||
def has_clamav ():
|
||||
|
|
@ -101,6 +119,8 @@ def has_clamav ():
|
|||
pass
|
||||
return False
|
||||
|
||||
need_clamav = _need_func(has_clamav, "ClamAV")
|
||||
|
||||
|
||||
@memoized
|
||||
def has_proxy ():
|
||||
|
|
@ -114,6 +134,21 @@ def has_proxy ():
|
|||
pass
|
||||
return False
|
||||
|
||||
need_proxy = _need_func(has_proxy, "proxy")
|
||||
|
||||
|
||||
@memoized
|
||||
def has_newsserver ():
|
||||
try:
|
||||
import nntplib
|
||||
nntp = nntplib.NNTP(NNTP_SERVER, usenetrc=False)
|
||||
nntp.close()
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
need_newsserver = _need_func(has_newsserver, "newsserver")
|
||||
|
||||
|
||||
@memoized
|
||||
def has_pyqt ():
|
||||
|
|
@ -125,6 +160,7 @@ def has_pyqt ():
|
|||
pass
|
||||
return False
|
||||
|
||||
need_pyqt = _need_func(has_pyqt, "PyQT")
|
||||
|
||||
@contextmanager
|
||||
def _limit_time (seconds):
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@
|
|||
"""
|
||||
Test ftp checking.
|
||||
"""
|
||||
from tests import has_network
|
||||
from nose import SkipTest
|
||||
from tests import need_network
|
||||
from . import LinkCheckTest
|
||||
|
||||
|
||||
|
|
@ -27,10 +26,9 @@ class TestFtp (LinkCheckTest):
|
|||
Test ftp: link checking.
|
||||
"""
|
||||
|
||||
@need_network
|
||||
def test_ftp (self):
|
||||
# ftp two slashes
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
url = u"ftp://ftp.de.debian.org/"
|
||||
resultlines = [
|
||||
u"url %s" % url,
|
||||
|
|
@ -40,10 +38,9 @@ class TestFtp (LinkCheckTest):
|
|||
]
|
||||
self.direct(url, resultlines)
|
||||
|
||||
@need_network
|
||||
def test_ftp_slashes (self):
|
||||
# ftp one slash
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
url = u"ftp:/ftp.de.debian.org/"
|
||||
nurl = self.norm(url)
|
||||
resultlines = [
|
||||
|
|
@ -76,10 +73,9 @@ class TestFtp (LinkCheckTest):
|
|||
]
|
||||
self.direct(url, resultlines)
|
||||
|
||||
@need_network
|
||||
def test_ftp_many_slashes (self):
|
||||
# ftp two dir slashes
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
url = u"ftp://ftp.de.debian.org//debian/"
|
||||
nurl = self.norm(url)
|
||||
resultlines = [
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@
|
|||
"""
|
||||
Test news checking.
|
||||
"""
|
||||
from tests import has_network
|
||||
from nose import SkipTest
|
||||
from tests import need_network
|
||||
from . import LinkCheckTest
|
||||
|
||||
|
||||
|
|
@ -27,9 +26,8 @@ class TestHttps (LinkCheckTest):
|
|||
Test https: link checking.
|
||||
"""
|
||||
|
||||
@need_network
|
||||
def test_https (self):
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
url = u"https://www.amazon.de/"
|
||||
resultlines = [
|
||||
u"url %s" % url,
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@
|
|||
"""
|
||||
Test mail checking.
|
||||
"""
|
||||
from tests import has_network
|
||||
from nose import SkipTest
|
||||
from tests import need_network
|
||||
from . import LinkCheckTest
|
||||
|
||||
|
||||
|
|
@ -28,10 +27,9 @@ class TestMail (LinkCheckTest):
|
|||
Test mailto: link checking.
|
||||
"""
|
||||
|
||||
@need_network
|
||||
def test_good_mail (self):
|
||||
# some good mailto addrs
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
url = self.norm(u"mailto:Dude <calvin@users.sourceforge.net> , "\
|
||||
"Killer <calvin@users.sourceforge.net>?subject=bla")
|
||||
resultlines = [
|
||||
|
|
@ -95,10 +93,9 @@ class TestMail (LinkCheckTest):
|
|||
]
|
||||
self.direct(url, resultlines)
|
||||
|
||||
@need_network
|
||||
def test_warn_mail (self):
|
||||
# some mailto addrs with warnings
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
# contains non-quoted characters
|
||||
url = u"mailto:calvin@users.sourceforge.net?subject=äöü"
|
||||
qurl = self.norm(url)
|
||||
|
|
@ -174,19 +171,17 @@ class TestMail (LinkCheckTest):
|
|||
self.mail_error(u"mailto:Bastian Kleineidam <calvin@users.sourceforge.net?foo=bar>",
|
||||
cache_key=u"mailto:calvin@users.sourceforge.net?foo=bar")
|
||||
|
||||
@need_network
|
||||
def test_valid_mail (self):
|
||||
# valid mail addresses
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
for char in u"!#$&'*+-/=^_`.{|}~":
|
||||
addr = u'abc%sdef@sourceforge.net' % char
|
||||
self.mail_valid(u"mailto:%s" % addr,
|
||||
warning=u"Unverified address: 550 <%s> Unrouteable address." % addr,
|
||||
cache_key=u"mailto:%s" % addr)
|
||||
|
||||
@need_network
|
||||
def test_unicode_mail (self):
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
mailto = u"mailto:ölvin@users.sourceforge.net"
|
||||
url = self.norm(mailto, encoding="iso-8859-1")
|
||||
resultlines = [
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@
|
|||
"""
|
||||
Test news checking.
|
||||
"""
|
||||
from tests import has_network, limit_time, memoized
|
||||
from nose import SkipTest
|
||||
from tests import need_newsserver, limit_time
|
||||
from . import LinkCheckTest
|
||||
|
||||
# Changes often, as servers tend to get invalid. Thus it is necessary
|
||||
|
|
@ -30,26 +29,13 @@ NNTP_INFO = u"200 news.netfront.net InterNetNews NNRP server INN 2.4.6 (20090304
|
|||
NNTP_TIMEOUT_SECS = 8
|
||||
|
||||
|
||||
@memoized
|
||||
def has_newsserver ():
|
||||
try:
|
||||
nntp = nntplib.NNTP(NNTP_SERVER, usenetrc=False)
|
||||
nntp.close()
|
||||
return True
|
||||
except :
|
||||
return False
|
||||
|
||||
|
||||
class TestNews (LinkCheckTest):
|
||||
"""Test nntp: and news: link checking."""
|
||||
|
||||
def newstest (self, url, resultlines):
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
if not has_newsserver():
|
||||
raise SkipTest("no newswerver available")
|
||||
self.direct(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
def test_news_without_host (self):
|
||||
# news testing
|
||||
url = u"news:comp.os.linux.misc"
|
||||
|
|
@ -72,6 +58,7 @@ class TestNews (LinkCheckTest):
|
|||
]
|
||||
self.newstest(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
def test_snews_with_group (self):
|
||||
url = u"snews:de.comp.os.unix.linux.misc"
|
||||
nurl = self.norm(url)
|
||||
|
|
@ -85,6 +72,7 @@ class TestNews (LinkCheckTest):
|
|||
]
|
||||
self.newstest(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
def test_illegal_syntax (self):
|
||||
# illegal syntax
|
||||
url = u"news:§$%&/´`(§%"
|
||||
|
|
@ -99,6 +87,7 @@ class TestNews (LinkCheckTest):
|
|||
]
|
||||
self.newstest(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
@limit_time(NNTP_TIMEOUT_SECS, skip=True)
|
||||
def test_nntp_with_host (self):
|
||||
url = u"nntp://%s/comp.lang.python" % NNTP_SERVER
|
||||
|
|
@ -112,6 +101,7 @@ class TestNews (LinkCheckTest):
|
|||
]
|
||||
self.newstest(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
@limit_time(NNTP_TIMEOUT_SECS, skip=True)
|
||||
def test_article_span (self):
|
||||
url = u"nntp://%s/comp.lang.python/1-5" % NNTP_SERVER
|
||||
|
|
@ -125,6 +115,7 @@ class TestNews (LinkCheckTest):
|
|||
]
|
||||
self.newstest(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
def test_article_span_no_host (self):
|
||||
url = u"news:comp.lang.python/1-5"
|
||||
resultlines = [
|
||||
|
|
@ -136,6 +127,7 @@ class TestNews (LinkCheckTest):
|
|||
]
|
||||
self.newstest(url, resultlines)
|
||||
|
||||
@need_newsserver
|
||||
@limit_time(NNTP_TIMEOUT_SECS, skip=True)
|
||||
def test_host_no_group (self):
|
||||
url = u"nntp://%s/" % NNTP_SERVER
|
||||
|
|
|
|||
|
|
@ -18,24 +18,21 @@
|
|||
Test virus filter.
|
||||
"""
|
||||
import unittest
|
||||
from tests import has_clamav
|
||||
from nose import SkipTest
|
||||
from tests import need_clamav
|
||||
from linkcheck import clamav
|
||||
|
||||
|
||||
class TestClamav (unittest.TestCase):
|
||||
|
||||
@need_clamav
|
||||
def testClean (self):
|
||||
if not has_clamav():
|
||||
raise SkipTest("no ClamAV available")
|
||||
data = ""
|
||||
infected, errors = clamav.scan(data)
|
||||
self.assertFalse(infected)
|
||||
self.assertFalse(errors)
|
||||
|
||||
@need_clamav
|
||||
def testInfected (self):
|
||||
if not has_clamav():
|
||||
raise SkipTest("no ClamAV available")
|
||||
data = '<object data="ms-its:mhtml:file://'+ \
|
||||
'C:\\foo.mht!${PATH}/' + \
|
||||
'EXPLOIT.CHM::' + \
|
||||
|
|
|
|||
|
|
@ -16,15 +16,13 @@
|
|||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
import unittest
|
||||
import sys
|
||||
from tests import has_pyqt
|
||||
from nose import SkipTest
|
||||
from tests import need_pyqt
|
||||
|
||||
class TestGui (unittest.TestCase):
|
||||
"""Test OMT GUI client."""
|
||||
|
||||
@need_pyqt
|
||||
def test_gui (self):
|
||||
if not has_pyqt():
|
||||
raise SkipTest("no PyQt available")
|
||||
from PyQt4 import QtCore, QtGui, QtTest
|
||||
from linkcheck.gui import LinkCheckerMain
|
||||
app = QtGui.QApplication(sys.argv)
|
||||
|
|
|
|||
|
|
@ -19,21 +19,18 @@ Test network functions.
|
|||
"""
|
||||
|
||||
import unittest
|
||||
from tests import has_posix
|
||||
from nose import SkipTest
|
||||
from tests import need_posix
|
||||
import linkcheck.network
|
||||
|
||||
|
||||
class TestNetwork (unittest.TestCase):
|
||||
"""Test network functions."""
|
||||
|
||||
@need_posix
|
||||
def test_ifreq_size (self):
|
||||
if not has_posix():
|
||||
raise SkipTest("no POSIX system")
|
||||
self.assertTrue(linkcheck.network.ifreq_size() > 0)
|
||||
|
||||
@need_posix
|
||||
def test_interfaces (self):
|
||||
if not has_posix():
|
||||
raise SkipTest("no POSIX system")
|
||||
ifc = linkcheck.network.IfConfig()
|
||||
ifc.getInterfaceList()
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@ Test gettext .po files.
|
|||
import unittest
|
||||
import os
|
||||
import glob
|
||||
from tests import has_msgfmt, has_posix
|
||||
from nose import SkipTest
|
||||
from tests import need_msgfmt, need_posix
|
||||
|
||||
|
||||
pofiles = None
|
||||
|
|
@ -39,10 +38,10 @@ def get_pofiles ():
|
|||
class TestPo (unittest.TestCase):
|
||||
"""Test .po file syntax."""
|
||||
|
||||
@need_posix
|
||||
@need_msgfmt
|
||||
def test_pos (self):
|
||||
"""Test .po files syntax."""
|
||||
if not (has_msgfmt() and has_posix()):
|
||||
raise SkipTest("no msgfmt and POSIX available")
|
||||
for f in get_pofiles():
|
||||
ret = os.system("msgfmt -c -o - %s > /dev/null" % f)
|
||||
self.assertEquals(ret, 0, msg="PO-file syntax error in %r" % f)
|
||||
|
|
|
|||
|
|
@ -19,8 +19,7 @@ Test robots.txt parsing.
|
|||
"""
|
||||
|
||||
import unittest
|
||||
from tests import has_network
|
||||
from nose import SkipTest
|
||||
from tests import need_network
|
||||
import linkcheck.robotparser2
|
||||
|
||||
|
||||
|
|
@ -46,22 +45,20 @@ class TestRobotParser (unittest.TestCase):
|
|||
if a != b:
|
||||
self.fail("%s != %s (%s)" % (a, b, ac))
|
||||
|
||||
@need_network
|
||||
def test_nonexisting_robots (self):
|
||||
"""
|
||||
Test access of a non-existing robots.txt file.
|
||||
"""
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
# robots.txt that does not exist
|
||||
self.rp.set_url('http://www.lycos.com/robots.txt')
|
||||
self.rp.read()
|
||||
self.check(self.rp.can_fetch('Mozilla',
|
||||
'http://www.lycos.com/search'), True)
|
||||
|
||||
@need_network
|
||||
def test_password_robots (self):
|
||||
# whole site is password-protected.
|
||||
if not has_network():
|
||||
raise SkipTest("no network available")
|
||||
self.rp.set_url('http://mueblesmoraleda.com/robots.txt')
|
||||
self.rp.read()
|
||||
self.check(self.rp.can_fetch("*",
|
||||
|
|
|
|||
Loading…
Reference in a new issue