/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#   and others
3
4
#
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
17
18
 
18
19
"""Symbol versioning tests."""
19
20
 
20
 
import bzrlib.symbol_versioning as symbol_versioning
 
21
import warnings
 
22
 
 
23
from bzrlib import symbol_versioning
21
24
from bzrlib.tests import TestCase
22
25
 
23
26
 
31
34
    'a_deprecated_list', ['one'], extra="Don't use me")
32
35
 
33
36
 
 
37
a_deprecated_dict = symbol_versioning.DeprecatedDict(
 
38
    symbol_versioning.zero_fourteen,
 
39
    'a_deprecated_dict',
 
40
    dict(a=42),
 
41
    advice='Pull the other one!',
 
42
    )
 
43
 
 
44
 
34
45
class TestDeprecationWarnings(TestCase):
35
46
 
36
47
    def capture_warning(self, message, category, stacklevel=None):
48
59
        """
49
60
        return 1
50
61
 
 
62
    @staticmethod
 
63
    @symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
 
64
    def deprecated_static():
 
65
        """Deprecated static."""
 
66
        return 1
 
67
 
 
68
    def test_deprecated_static(self):
 
69
        # XXX: The results are not quite right because the class name is not
 
70
        # shown - however it is enough to give people a good indication of
 
71
        # where the problem is.
 
72
        expected_warning = (
 
73
            "bzrlib.tests.test_symbol_versioning."
 
74
            "deprecated_static "
 
75
            "was deprecated in version 0.7.", DeprecationWarning, 2)
 
76
        expected_docstring = (
 
77
            'Deprecated static.\n'
 
78
            '\n'
 
79
            'This function was deprecated in version 0.7.\n'
 
80
            )
 
81
        self.check_deprecated_callable(
 
82
            expected_warning, expected_docstring,
 
83
            "deprecated_static",
 
84
            "bzrlib.tests.test_symbol_versioning",
 
85
            self.deprecated_static)
 
86
 
51
87
    def test_deprecated_method(self):
52
88
        expected_warning = (
53
89
            "bzrlib.tests.test_symbol_versioning."
81
117
        expected_warning = (
82
118
            "Modifying a_deprecated_list was deprecated in version 0.9."
83
119
            " Don't use me", DeprecationWarning, 3)
84
 
        expected_doctstring = ('appending to a_deprecated_list is deprecated')
85
 
 
86
120
        old_warning_method = symbol_versioning.warn
87
121
        try:
88
122
            symbol_versioning.set_warning_method(self.capture_warning)
117
151
        finally:
118
152
            symbol_versioning.set_warning_method(old_warning_method)
119
153
 
 
154
    def test_deprecated_dict(self):
 
155
        expected_warning = (
 
156
            "access to a_deprecated_dict was deprecated in version 0.14."
 
157
            " Pull the other one!", DeprecationWarning, 2)
 
158
        old_warning_method = symbol_versioning.warn
 
159
        try:
 
160
            symbol_versioning.set_warning_method(self.capture_warning)
 
161
            self.assertEqual(len(a_deprecated_dict), 1)
 
162
            self.assertEqual([expected_warning], self._warnings)
 
163
 
 
164
            a_deprecated_dict['b'] = 42
 
165
            self.assertEqual(a_deprecated_dict['b'], 42)
 
166
            self.assertTrue('b' in a_deprecated_dict)
 
167
            del a_deprecated_dict['b']
 
168
            self.assertFalse('b' in a_deprecated_dict)
 
169
            self.assertEqual([expected_warning] * 6, self._warnings)
 
170
        finally:
 
171
            symbol_versioning.set_warning_method(old_warning_method)
 
172
 
 
173
 
120
174
    def check_deprecated_callable(self, expected_warning, expected_docstring,
121
175
                                  expected_name, expected_module,
122
176
                                  deprecated_callable):
155
209
            symbol_versioning.deprecation_string(
156
210
                symbol_versioning.deprecated_function,
157
211
                symbol_versioning.zero_eleven))
 
212
 
 
213
 
 
214
class TestSuppressAndActivate(TestCase):
 
215
 
 
216
    def setUp(self):
 
217
        existing_filters = list(warnings.filters)
 
218
        def restore():
 
219
            warnings.filters[:] = existing_filters
 
220
        self.addCleanup(restore)
 
221
        # Clean out the filters so we have a clean slate.
 
222
        warnings.resetwarnings()
 
223
 
 
224
    def assertFirstWarning(self, action, category):
 
225
        """Test the first warning in the filters is correct"""
 
226
        first = warnings.filters[0]
 
227
        self.assertEqual((action, category), (first[0], first[2]))
 
228
 
 
229
    def test_suppress_deprecation_warnings(self):
 
230
        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
 
231
        symbol_versioning.suppress_deprecation_warnings()
 
232
        self.assertFirstWarning('ignore', DeprecationWarning)
 
233
 
 
234
    def test_suppress_deprecation_with_warning_filter(self):
 
235
        """don't suppress if we already have a filter"""
 
236
        warnings.filterwarnings('error', category=Warning)
 
237
        self.assertFirstWarning('error', Warning)
 
238
        self.assertEqual(1, len(warnings.filters))
 
239
        symbol_versioning.suppress_deprecation_warnings(override=False)
 
240
        self.assertFirstWarning('error', Warning)
 
241
        self.assertEqual(1, len(warnings.filters))
 
242
 
 
243
    def test_suppress_deprecation_with_filter(self):
 
244
        """don't suppress if we already have a filter"""
 
245
        warnings.filterwarnings('error', category=DeprecationWarning)
 
246
        self.assertFirstWarning('error', DeprecationWarning)
 
247
        self.assertEqual(1, len(warnings.filters))
 
248
        symbol_versioning.suppress_deprecation_warnings(override=False)
 
249
        self.assertFirstWarning('error', DeprecationWarning)
 
250
        self.assertEqual(1, len(warnings.filters))
 
251
        symbol_versioning.suppress_deprecation_warnings(override=True)
 
252
        self.assertFirstWarning('ignore', DeprecationWarning)
 
253
        self.assertEqual(2, len(warnings.filters))
 
254
 
 
255
    def test_activate_deprecation_no_error(self):
 
256
        # First nuke the filters, so we know it is clean
 
257
        symbol_versioning.activate_deprecation_warnings()
 
258
        self.assertFirstWarning('default', DeprecationWarning)
 
259
 
 
260
    def test_activate_deprecation_with_error(self):
 
261
        # First nuke the filters, so we know it is clean
 
262
        # Add a warning == error rule
 
263
        warnings.filterwarnings('error', category=Warning)
 
264
        self.assertFirstWarning('error', Warning)
 
265
        self.assertEqual(1, len(warnings.filters))
 
266
        symbol_versioning.activate_deprecation_warnings(override=False)
 
267
        # There should not be a new warning
 
268
        self.assertFirstWarning('error', Warning)
 
269
        self.assertEqual(1, len(warnings.filters))
 
270
 
 
271
    def test_activate_deprecation_with_DW_error(self):
 
272
        # First nuke the filters, so we know it is clean
 
273
        # Add a warning == error rule
 
274
        warnings.filterwarnings('error', category=DeprecationWarning)
 
275
        self.assertFirstWarning('error', DeprecationWarning)
 
276
        self.assertEqual(1, len(warnings.filters))
 
277
        symbol_versioning.activate_deprecation_warnings(override=False)
 
278
        # There should not be a new warning
 
279
        self.assertFirstWarning('error', DeprecationWarning)
 
280
        self.assertEqual(1, len(warnings.filters))
 
281
        symbol_versioning.activate_deprecation_warnings(override=True)
 
282
        self.assertFirstWarning('default', DeprecationWarning)
 
283
        self.assertEqual(2, len(warnings.filters))