summaryrefslogtreecommitdiff
path: root/passlib/tests/backports.py
blob: bde41cbb519b983d24268a2a3b398201c7977afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""backports of needed unittest2 features"""
#=========================================================
#imports
#=========================================================
from __future__ import with_statement
#core
import logging; log = logging.getLogger(__name__)
import re
import sys
##from warnings import warn
#site
#pkg
from passlib.utils.compat import base_string_types
#local
__all__ = [
    "TestCase",
    "skip", "skipIf", "skipUnless"
    "catch_warnings",
]

#=========================================================
# import latest unittest module available
#=========================================================
try:
    import unittest2 as unittest
    ut_version = 2
except ImportError:
    import unittest
    if sys.version_info < (2,7) or (3,0) <= sys.version_info < (3,2):
        # older versions of python will need to install the unittest2
        # backport (named unittest2_3k for 3.0/3.1)
        ##warn("please install unittest2 for python %d.%d, it will be required "
        ##     "as of passlib 1.x" % sys.version_info[:2])
        ut_version = 1
    else:
        ut_version = 2

#=========================================================
# backport SkipTest support using nose
#=========================================================
if ut_version < 2:
    # used to provide replacement SkipTest() error
    from nose.plugins.skip import SkipTest

    # hack up something to simulate skip() decorator
    import functools
    def skip(reason):
        def decorator(test_item):
            if isinstance(test_item, type) and issubclass(test_item, unittest.TestCase):
                class skip_wrapper(test_item):
                    def setUp(self):
                        raise SkipTest(reason)
            else:
                @functools.wraps(test_item)
                def skip_wrapper(*args, **kwargs):
                    raise SkipTest(reason)
            return skip_wrapper
        return decorator

    def skipIf(condition, reason):
        if condition:
            return skip(reason)
        else:
            return lambda item: item

    def skipUnless(condition, reason):
        if condition:
            return lambda item: item
        else:
            return skip(reason)

else:
    skip = unittest.skip
    skipIf = unittest.skipIf
    skipUnless = unittest.skipUnless

#=========================================================
# custom test harness
#=========================================================
class TestCase(unittest.TestCase):
    """backports a number of unittest2 features in TestCase"""
    #====================================================================
    # backport some methods from unittest2
    #====================================================================
    if ut_version < 2:

        #----------------------------------------------------------------
        # simplistic backport of addCleanup() framework
        #----------------------------------------------------------------
        _cleanups = None

        def addCleanup(self, function, *args, **kwds):
            queue = self._cleanups
            if queue is None:
                queue = self._cleanups = []
            queue.append((function, args, kwds))

        def doCleanups(self):
            queue = self._cleanups
            while queue:
                func, args, kwds = queue.pop()
                func(*args, **kwds)

        def tearDown(self):
            self.doCleanups()
            unittest.TestCase.tearDown(self)

        #----------------------------------------------------------------
        # backport skipTest (requires nose to work)
        #----------------------------------------------------------------
        def skipTest(self, reason):
            raise SkipTest(reason)

        #----------------------------------------------------------------
        # backport various assert tests added in unittest2
        #----------------------------------------------------------------
        def assertIs(self, real, correct, msg=None):
            if real is not correct:
                std = "got %r, expected would be %r" % (real, correct)
                msg = self._formatMessage(msg, std)
                raise self.failureException(msg)

        def assertIsNot(self, real, correct, msg=None):
            if real is correct:
                std = "got %r, expected would not be %r" % (real, correct)
                msg = self._formatMessage(msg, std)
                raise self.failureException(msg)

        def assertIsInstance(self, obj, klass, msg=None):
            if not isinstance(obj, klass):
                std = "got %r, expected instance of %r" % (obj, klass)
                msg = self._formatMessage(msg, std)
                raise self.failureException(msg)

        def assertAlmostEqual(self, first, second, places=None, msg=None, delta=None):
            """Fail if the two objects are unequal as determined by their
               difference rounded to the given number of decimal places
               (default 7) and comparing to zero, or by comparing that the
               between the two objects is more than the given delta.

               Note that decimal places (from zero) are usually not the same
               as significant digits (measured from the most signficant digit).

               If the two objects compare equal then they will automatically
               compare almost equal.
            """
            if first == second:
                # shortcut
                return
            if delta is not None and places is not None:
                raise TypeError("specify delta or places not both")

            if delta is not None:
                if abs(first - second) <= delta:
                    return

                standardMsg = '%s != %s within %s delta' % (repr(first),
                                                            repr(second),
                                                            repr(delta))
            else:
                if places is None:
                    places = 7

                if round(abs(second-first), places) == 0:
                    return

                standardMsg = '%s != %s within %r places' % (repr(first),
                                                              repr(second),
                                                              places)
            msg = self._formatMessage(msg, standardMsg)
            raise self.failureException(msg)

        def assertLess(self, left, right, msg=None):
            if left >= right:
                std = "%r not less than %r" % (left, right)
                raise self.failureException(self._formatMessage(msg, std))

        def assertGreater(self, left, right, msg=None):
            if left <= right:
                std = "%r not greater than %r" % (left, right)
                raise self.failureException(self._formatMessage(msg, std))

        def assertGreaterEqual(self, left, right, msg=None):
            if left < right:
                std = "%r less than %r" % (left, right)
                raise self.failureException(self._formatMessage(msg, std))

        def assertIn(self, elem, container, msg=None):
            if elem not in container:
                std = "%r not found in %r" % (elem, container)
                raise self.failureException(self._formatMessage(msg, std))

        def assertNotIn(self, elem, container, msg=None):
            if elem in container:
                std = "%r unexpectedly in %r" % (elem, container)
                raise self.failureException(self._formatMessage(msg, std))

        #----------------------------------------------------------------
        # override some unittest1 methods to support _formatMessage
        #----------------------------------------------------------------
        def assertEqual(self, real, correct, msg=None):
            if real != correct:
                std = "got %r, expected would equal %r" % (real, correct)
                msg = self._formatMessage(msg, std)
                raise self.failureException(msg)

        def assertNotEqual(self, real, correct, msg=None):
            if real == correct:
                std = "got %r, expected would not equal %r" % (real, correct)
                msg = self._formatMessage(msg, std)
                raise self.failureException(msg)

    #----------------------------------------------------------------
    # backport assertRegex() alias from 3.2 to 2.7/3.1
    #----------------------------------------------------------------
    if not hasattr(unittest.TestCase, "assertRegex"):
        if hasattr(unittest.TestCase, "assertRegexpMatches"):
            # was present in 2.7/3.1 under name assertRegexpMatches
            assertRegex = unittest.TestCase.assertRegexpMatches
        else:
            # 3.0 and <= 2.6 didn't have this method at all
            def assertRegex(self, text, expected_regex, msg=None):
                """Fail the test unless the text matches the regular expression."""
                if isinstance(expected_regex, base_string_types):
                    assert expected_regex, "expected_regex must not be empty."
                    expected_regex = re.compile(expected_regex)
                if not expected_regex.search(text):
                    msg = msg or "Regex didn't match: "
                    std = '%r not found in %r' % (msg, expected_regex.pattern, text)
                    raise self.failureException(self._formatMessage(msg, std))

    #============================================================
    #eoc
    #============================================================

#=============================================================================
# backport catch_warnings
#=============================================================================
try:
    from warnings import catch_warnings
except ImportError:
    # catch_warnings wasn't added until py26.
    # this adds backported copy from py26's stdlib
    # so we can use it under py25.

    class WarningMessage(object):

        """Holds the result of a single showwarning() call."""

        _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
                            "line")

        def __init__(self, message, category, filename, lineno, file=None,
                        line=None):
            local_values = locals()
            for attr in self._WARNING_DETAILS:
                setattr(self, attr, local_values[attr])
            self._category_name = category.__name__ if category else None

        def __str__(self):
            return ("{message : %r, category : %r, filename : %r, lineno : %s, "
                        "line : %r}" % (self.message, self._category_name,
                                        self.filename, self.lineno, self.line))


    class catch_warnings(object):

        """A context manager that copies and restores the warnings filter upon
        exiting the context.

        The 'record' argument specifies whether warnings should be captured by a
        custom implementation of warnings.showwarning() and be appended to a list
        returned by the context manager. Otherwise None is returned by the context
        manager. The objects appended to the list are arguments whose attributes
        mirror the arguments to showwarning().

        The 'module' argument is to specify an alternative module to the module
        named 'warnings' and imported under that name. This argument is only useful
        when testing the warnings module itself.

        """

        def __init__(self, record=False, module=None):
            """Specify whether to record warnings and if an alternative module
            should be used other than sys.modules['warnings'].

            For compatibility with Python 3.0, please consider all arguments to be
            keyword-only.

            """
            self._record = record
            self._module = sys.modules['warnings'] if module is None else module
            self._entered = False

        def __repr__(self):
            args = []
            if self._record:
                args.append("record=True")
            if self._module is not sys.modules['warnings']:
                args.append("module=%r" % self._module)
            name = type(self).__name__
            return "%s(%s)" % (name, ", ".join(args))

        def __enter__(self):
            if self._entered:
                raise RuntimeError("Cannot enter %r twice" % self)
            self._entered = True
            self._filters = self._module.filters
            self._module.filters = self._filters[:]
            self._showwarning = self._module.showwarning
            if self._record:
                log = []
                def showwarning(*args, **kwargs):
#                    self._showwarning(*args, **kwargs)
                    log.append(WarningMessage(*args, **kwargs))
                self._module.showwarning = showwarning
                return log
            else:
                return None

        def __exit__(self, *exc_info):
            if not self._entered:
                raise RuntimeError("Cannot exit %r without entering first" % self)
            self._module.filters = self._filters
            self._module.showwarning = self._showwarning

#=============================================================================
# eof
#=============================================================================