Example #1
0
def monkeypatchTests():
    """Monkeypatch the old unittests, replacing new, refactored code with their
    original equivalents from :mod:`deprecated`.

    The first patch replaces the newer parsing function,
    :func:`~bridgedb.parse.networkstatus.parseALine`, with the older,
    :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 <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:`deprecated`.
    """
    patcher = monkey.MonkeyPatcher()
    patcher.addPatch(Tests.bridgedb.Bridges, 'PluggableTransport',
                     deprecated.PluggableTransport)
    patcher.addPatch(Tests.bridgedb.Bridges, 'Bridge', deprecated.Bridge)
    return patcher
Example #2
0
    def patch(self, obj, attribute, value):
        """
        Monkey patch an object for the duration of the test.

        The monkey patch will be reverted at the end of the test using the
        L{addCleanup} mechanism.

        The L{monkey.MonkeyPatcher} is returned so that users can restore and
        re-apply the monkey patch within their tests.

        @param obj: The object to monkey patch.
        @param attribute: The name of the attribute to change.
        @param value: The value to set the attribute to.
        @return: A L{monkey.MonkeyPatcher} object.
        """
        monkeyPatch = monkey.MonkeyPatcher((obj, attribute, value))
        monkeyPatch.patch()
        self.addCleanup(monkeyPatch.restore)
        return monkeyPatch
Example #3
0
def monkeypatchTests():
    """Monkeypatch the old unittests, replacing new, refactored code with their
    original equivalents from :mod:`bridgedb.test.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
    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
    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`.
    """
    patcher = monkey.MonkeyPatcher()
    patcher.addPatch(Tests.networkstatus, 'parseALine',
                     deprecated.parseORAddressLine)
    patcher.addPatch(Tests.addr, 'PortList', deprecated.PortList)
    patcher.addPatch(Tests.bridgedb.Bridges, 'PORTSPEC_LEN', 16)
    patcher.addPatch(Tests.bridgedb.Bridges, 're_ipv4', deprecated.re_ipv4)
    patcher.addPatch(Tests.bridgedb.Bridges, 're_ipv6', deprecated.re_ipv6)
    patcher.addPatch(Tests.bridgedb.Bridges, 'HEX_FP_LEN', 40)
    patcher.addPatch(Tests.bridgedb.Bridges, 'toHex', binascii.b2a_hex)
    patcher.addPatch(Tests.bridgedb.Bridges, 'fromHex', binascii.a2b_hex)
    patcher.addPatch(Tests.bridgedb.Bridges, 'is_valid_fingerprint',
                     deprecated.is_valid_fingerprint)
    patcher.addPatch(Tests.bridgedb.Bridges, 'PluggableTransport',
                     deprecated.PluggableTransport)
    patcher.addPatch(Tests.bridgedb.Bridges, 'Bridge',
                     deprecated.Bridge)
    return patcher