Skip to content

Commit

Permalink
Switch to using relative imports for test helpers in test/tests_*.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
isislovecruft committed Jun 25, 2015
1 parent 50f6e22 commit 6d309ca
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 83 deletions.
48 changes: 26 additions & 22 deletions bridgedb/util.py
Expand Up @@ -153,6 +153,7 @@ def levenshteinDistance(s1, s2, len1=None, len2=None,
the number of characters which must be changed in **s1** to make it
identical to **s2**.
>>> from bridgedb.util import levenshteinDistance
>>> levenshteinDistance('cat', 'cat')
0
>>> levenshteinDistance('cat', 'hat')
Expand Down Expand Up @@ -188,6 +189,7 @@ def isascii(s):
Note that this function differs from the str.is* methods in that
it returns True for the empty string, rather than False.
>>> from bridgedb.util import isascii
>>> isascii('\x80')
False
>>> isascii('foo\tbar\rbaz\n')
Expand All @@ -206,6 +208,7 @@ def isascii_noncontrol(s):
Note that this function differs from the str.is* methods in that
it returns True for the empty string, rather than False.
>>> from bridgedb.util import isascii_noncontrol
>>> isascii_noncontrol('\x80')
False
>>> isascii_noncontrol('foo\tbar\rbaz\n')
Expand All @@ -220,6 +223,7 @@ def isascii_noncontrol(s):
def replaceControlChars(text, replacement=None, encoding="utf-8"):
"""Remove ASCII control characters [0-31, 92, 127].
>>> from bridgedb.util import replaceControlChars
>>> replaceControlChars('foo\n bar\\ baz\r \t\0quux\n')
'foo bar baz quux'
>>> replaceControlChars("\bI wonder if I'm outside the quotes now")
Expand Down Expand Up @@ -344,36 +348,36 @@ class mixin:
>>> from bridgedb.util import mixin
>>>
>>> class ClassA(object):
>>> def sayWhich(self):
>>> print("ClassA.sayWhich() called.")
>>> def doSuperThing(self):
>>> super(ClassA, self).__repr__()
>>> def doThing(self):
>>> print("ClassA is doing a thing.")
>>>
... def sayWhich(self):
... print("ClassA.sayWhich() called.")
... def doSuperThing(self):
... print("%s" % super(ClassA, self))
... def doThing(self):
... print("ClassA is doing a thing.")
...
>>> class ClassB(ClassA):
>>> def sayWhich(self):
>>> print("ClassB.sayWhich() called.")
>>> def doSuperThing(self):
>>> super(ClassB, self).__repr__()
>>> def doOtherThing(self):
>>> print("ClassB is doing something else.")
>>>
... def sayWhich(self):
... print("ClassB.sayWhich() called.")
... def doSuperThing(self):
... print("%s" % super(ClassB, self))
... def doOtherThing(self):
... print("ClassB is doing something else.")
...
>>> class ClassM(mixin):
>>> def sayWhich(self):
>>> print("ClassM.sayWhich() called.")
>>>
... def sayWhich(self):
... print("ClassM.sayWhich() called.")
...
>>> ClassM.register(ClassA)
>>>
>>> class ClassC(ClassM, ClassB):
>>> def sayWhich(self):
>>> super(ClassC, self).sayWhich()
>>>
... def sayWhich(self):
... super(ClassC, self).sayWhich()
...
>>> c = ClassC()
>>> c.sayWhich()
ClassM.saywhich() called.
ClassM.sayWhich() called.
>>> c.doSuperThing()
<super: <class 'ClassA'>, NULL>
<super: <class 'ClassB'>, <ClassC object>>
>>> c.doThing()
ClassA is doing a thing.
>>> c.doOtherThing()
Expand Down
3 changes: 2 additions & 1 deletion test/email_helpers.py
Expand Up @@ -18,7 +18,8 @@
from bridgedb.email.distributor import TooSoonEmail
from bridgedb.email.server import MailServerContext
from bridgedb.schedule import Unscheduled
from bridgedb.test import util

from . import util


EMAIL_DIST = True
Expand Down
3 changes: 2 additions & 1 deletion test/https_helpers.py
Expand Up @@ -15,9 +15,10 @@

from twisted.web.test import requesthelper

from bridgedb.test import util
from bridgedb.persistent import Conf

from . import util


SERVER_PUBLIC_FQDN = 'bridges.torproject.org'
SERVER_PUBLIC_EXTERNAL_IP = '38.229.72.19'
Expand Down
20 changes: 11 additions & 9 deletions test/legacy_Tests.py
Expand Up @@ -30,19 +30,21 @@
from bridgedb.email.distributor import IgnoreEmail
from bridgedb.email.distributor import TooSoonEmail
from bridgedb.parse import addr
from bridgedb.test.util import bracketIPv6
from bridgedb.test.util import randomIP
from bridgedb.test.util import randomIPv4
from bridgedb.test.util import randomIPv6
from bridgedb.test.util import randomIPString
from bridgedb.test.util import randomIPv4String
from bridgedb.test.util import randomIPv6String
from bridgedb.test.util import randomPort
from bridgedb.test.util import randomValidIPv6

from .util import bracketIPv6
from .util import randomIP
from .util import randomIPv4
from .util import randomIPv6
from .util import randomIPString
from .util import randomIPv4String
from .util import randomIPv6String
from .util import randomPort
from .util import randomValidIPv6

from math import log

warnings.filterwarnings('ignore', '.*tmpnam.*')
warnings.filterwarnings('ignore', '.*Config.*')


def randomPortSpec():
Expand Down
37 changes: 19 additions & 18 deletions test/test_Tests.py
Expand Up @@ -4,13 +4,13 @@
#
# :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis@torproject.org>
# please also see AUTHORS file
# :copyright: (c) 2013, Isis Lovecruft
# (c) 2007-2013, The Tor Project, Inc.
# (c) 2007-2013, all entities within the AUTHORS file
# :copyright: (c) 2013-2015, Isis Lovecruft
# (c) 2007-2015, The Tor Project, Inc.
# (c) 2007-2015, all entities within the AUTHORS file
# :license: 3-Clause BSD, see LICENSE for licensing information

"""Class wrappers to adapt BridgeDB old unittests in :mod:`bridgedb.Tests`
(now kept in :mod:`bridgedb.test.legacy_Tests`) to be compatible with the
"""Class wrappers to adapt BridgeDB old unittests in ``bridgedb.Tests``
(now kept in ``test/legacy_Tests``) to be compatible with the
newer :api:`twisted.trial` unittests in this directory.
"""

Expand All @@ -22,16 +22,17 @@
import glob
import logging
import os
import warnings

from twisted.python import monkey
from twisted.trial import unittest

from bridgedb.test import legacy_Tests as Tests
from bridgedb.test import deprecated
from . import legacy_Tests as Tests
from . import deprecated


logging.disable(50)


warnings.filterwarnings('ignore', module="bridgedb\.test\.legacy_Tests")
pyunit = __import__('unittest')


Expand Down Expand Up @@ -64,24 +65,24 @@ def generateTrialAdaptedDoctestsSuite():

def monkeypatchTests():
"""Monkeypatch the old unittests, replacing new, refactored code with their
original equivalents from :mod:`bridgedb.test.deprecated`.
original equivalents from :mod:`deprecated`.
The first patch replaces the newer parsing function,
:func:`~bridgedb.parse.networkstatus.parseALine`, with the older,
:func:`deprecated one <bridgedb.test.deprecated.parseORAddressLine>` (the
:func:`deprecated one <deprecated.parseORAddressLine>` (the
old function was previously located at
``bridgedb.Bridges.parseORAddressLine``).
The second patch replaces the new :class:`~bridgedb.parse.addr.PortList`,
with the :class:`older one <bridgedb.test.deprecated.PortList>` (which
with the :class:`older one <deprecated.PortList>` (which
was previously located at ``bridgedb.Bridges.PortList``).
The third, forth, and fifth monkeypatches add some module-level attributes
back into :mod:`bridgedb.Bridges`.
:rtype: :api:`~twisted.python.monkey.MonkeyPatcher`
:returns: A :api:`~twisted.python.monkey.MonkeyPatcher`, preloaded with
patches from :mod:`bridgedb.test.deprecated`.
patches from :mod:`deprecated`.
"""
patcher = monkey.MonkeyPatcher()
patcher.addPatch(Tests.bridgedb.Bridges, 'PluggableTransport',
Expand Down Expand Up @@ -189,14 +190,14 @@ def testMethod(*args, **kwargs):


class OldUnittests(unittest.TestCase):
"""A wrapper around :mod:`bridgedb.Tests` to produce :api:`~twisted.trial`
"""A wrapper around :mod:`legacy_Tests` to produce :api:`~twisted.trial`
compatible output.
Generates a :api:`twisted.trial.unittest.TestCase` containing a
test for each of the individual tests in :mod:`bridgedb.Tests`.
test for each of the individual tests in :mod:`legacy_Tests`.
Each test in this :api:`~twisted.trial.unittest.TestCase`` is dynamically
generated from one of the old unittests in :mod:`bridgedb.Tests`. Then,
generated from one of the old unittests in :mod:`legacy_Tests`. Then,
the class is wrapped to cause the results reporting mechanisms to be
:api:`~twisted.trial` compatible.
Expand All @@ -209,13 +210,13 @@ class OldUnittests(unittest.TestCase):


class MonkeypatchedOldUnittests(unittest.TestCase):
"""A wrapper around :mod:`bridgedb.Tests` to produce :api:`~twisted.trial`
"""A wrapper around :mod:`legacy_Tests` to produce :api:`~twisted.trial`
compatible output.
For each test in this ``TestCase``, one of the old unittests in
bridgedb/Tests.py is run. For all of the tests, some functions and classes
are :api:`twisted.python.monkey.MonkeyPatcher.patch`ed with old,
deprecated code from :mod:`bridgedb.test.deprecated` to ensure that any
deprecated code from :mod:`deprecated` to ensure that any
new code has not caused any regressions.
"""
__metaclass__ = DynamicTestCaseMeta
Expand Down
4 changes: 2 additions & 2 deletions test/test_bridgedb.py
Expand Up @@ -21,8 +21,8 @@
from twisted.trial.unittest import FailTest
from twisted.trial.unittest import SkipTest

from bridgedb.test.util import processExists
from bridgedb.test.util import getBridgeDBPID
from .util import processExists
from .util import getBridgeDBPID


class BridgeDBCliTest(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions test/test_bridges.py
Expand Up @@ -148,10 +148,10 @@ class has compatible behaviour with the expected behaviour of the old
.. data: OldTest (enum)
These tests were refactored from the old tests for
:class:`~bridgedb.test.deprecated.Bridge`, which lived in
``deprecated.Bridge`, which lived in
``lib/bridgedb/test/test_Bridges.py``. For the translations from the old
tests in ``bridgedb.test.test_Bridges.BridgeClassTest`` to their new
equivalents here in ``bridgedb.test.test_bridges.BridgeIntegrationTests``,
tests in ``test_Bridges.BridgeClassTest`` to their new
equivalents here in ``test_bridges.BridgeIntegrationTests``,
which should test for the same things as their old equivalents, see the
following table:
Expand Down
5 changes: 3 additions & 2 deletions test/test_crypto.py
Expand Up @@ -33,8 +33,9 @@
from bridgedb import crypto
from bridgedb import txrecaptcha
from bridgedb.persistent import Conf
from bridgedb.test.util import fileCheckDecorator
from bridgedb.test.email_helpers import _createConfig

from .util import fileCheckDecorator
from .email_helpers import _createConfig


logging.disable(50)
Expand Down
7 changes: 4 additions & 3 deletions test/test_email_autoresponder.py
Expand Up @@ -26,9 +26,10 @@
from bridgedb.email import autoresponder
from bridgedb.email.server import SMTPMessage
from bridgedb.email.distributor import TooSoonEmail
from bridgedb.test.email_helpers import _createConfig
from bridgedb.test.email_helpers import _createMailServerContext
from bridgedb.test.email_helpers import DummyEmailDistributorWithState

from .email_helpers import _createConfig
from .email_helpers import _createMailServerContext
from .email_helpers import DummyEmailDistributorWithState


class CreateResponseBodyTests(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion test/test_email_distributor.py
Expand Up @@ -28,7 +28,8 @@
from bridgedb.parse.addr import BadEmail
from bridgedb.parse.addr import UnsupportedDomain
from bridgedb.parse.addr import normalizeEmail
from bridgedb.test.util import generateFakeBridges

from .util import generateFakeBridges

logging.disable(50)

Expand Down
7 changes: 4 additions & 3 deletions test/test_email_server.py
Expand Up @@ -36,9 +36,10 @@
from bridgedb.email.distributor import TooSoonEmail
from bridgedb.parse.addr import BadEmail
from bridgedb.schedule import Unscheduled
from bridgedb.test import util
from bridgedb.test.email_helpers import _createConfig
from bridgedb.test.email_helpers import _createMailServerContext

from . import util
from .email_helpers import _createConfig
from .email_helpers import _createMailServerContext


class SMTPMessageTests(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions test/test_https.py
Expand Up @@ -35,8 +35,8 @@
from twisted.trial.unittest import FailTest
from twisted.trial.unittest import SkipTest

from bridgedb.test.util import processExists
from bridgedb.test.util import getBridgeDBPID
from .util import processExists
from .util import getBridgeDBPID

HTTP_ROOT = 'http://127.0.0.1:6788'
CAPTCHA_RESPONSE = 'Tvx74Pmy'
Expand Down
7 changes: 4 additions & 3 deletions test/test_https_distributor.py
Expand Up @@ -26,9 +26,10 @@
from bridgedb.https import distributor
from bridgedb.https.request import HTTPSBridgeRequest
from bridgedb.proxy import ProxySet
from bridgedb.test.util import randomValidIPv4String
from bridgedb.test.util import generateFakeBridges
from bridgedb.test.https_helpers import DummyRequest

from .util import randomValidIPv4String
from .util import generateFakeBridges
from .https_helpers import DummyRequest

logging.disable(50)

Expand Down
11 changes: 6 additions & 5 deletions test/test_https_server.py
Expand Up @@ -29,11 +29,12 @@

from bridgedb.https import server
from bridgedb.schedule import ScheduledInterval
from bridgedb.test.https_helpers import _createConfig
from bridgedb.test.https_helpers import DummyRequest
from bridgedb.test.https_helpers import DummyHTTPSDistributor
from bridgedb.test.util import DummyBridge
from bridgedb.test.util import DummyMaliciousBridge

from .https_helpers import _createConfig
from .https_helpers import DummyRequest
from .https_helpers import DummyHTTPSDistributor
from .util import DummyBridge
from .util import DummyMaliciousBridge


# For additional logger output for debugging, comment out the following:
Expand Down
2 changes: 1 addition & 1 deletion test/test_parse_descriptors.py
Expand Up @@ -33,7 +33,7 @@
else:
HAS_STEM = True

from bridgedb.test.util import Benchmarker
from .util import Benchmarker


BRIDGE_NETWORKSTATUS_0 = '''\
Expand Down
4 changes: 2 additions & 2 deletions test/test_smtp.py
Expand Up @@ -15,8 +15,8 @@
from twisted.trial.unittest import FailTest
from twisted.trial.unittest import SkipTest

from bridgedb.test.util import processExists
from bridgedb.test.util import getBridgeDBPID
from .util import processExists
from .util import getBridgeDBPID

# ------------- SMTP Client Config
SMTP_DEBUG_LEVEL = 0 # set to 1 to see SMTP message exchange
Expand Down
2 changes: 1 addition & 1 deletion test/test_translations.py
Expand Up @@ -11,7 +11,7 @@
from twisted.trial import unittest

from bridgedb import translations
from bridgedb.test.test_https_server import DummyRequest
from .https_helpers import DummyRequest


REALISH_HEADERS = {
Expand Down

0 comments on commit 6d309ca

Please sign in to comment.