~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: Martin Pool
  • Date: 2009-11-16 02:26:32 UTC
  • mto: This revision was merged to the branch mainline in revision 4880.
  • Revision ID: mbp@sourcefrog.net-20091116022632-261trs2osy8oupe3
Convert version-info to use TextUIOutputStream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
1
# Copyright (C) 2006, 2007, 2009 Canonical Ltd
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
13
12
#
14
13
# You should have received a copy of the GNU General Public License
15
14
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
16
 
18
17
"""Symbol versioning tests."""
19
18
 
20
 
import bzrlib.symbol_versioning as symbol_versioning
 
19
import warnings
 
20
 
 
21
from bzrlib import symbol_versioning
 
22
from bzrlib.symbol_versioning import (
 
23
    deprecated_function,
 
24
    deprecated_in,
 
25
    deprecated_list,
 
26
    deprecated_method,
 
27
    )
21
28
from bzrlib.tests import TestCase
22
29
 
23
30
 
24
 
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
25
 
def deprecated_function():
 
31
@deprecated_function(deprecated_in((0, 7, 0)))
 
32
def sample_deprecated_function():
26
33
    """Deprecated function docstring."""
27
34
    return 1
28
35
 
29
36
 
30
 
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
 
37
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
31
38
    'a_deprecated_list', ['one'], extra="Don't use me")
32
39
 
33
40
 
34
41
a_deprecated_dict = symbol_versioning.DeprecatedDict(
35
 
    symbol_versioning.zero_fourteen,
 
42
    deprecated_in((0, 14, 0)),
36
43
    'a_deprecated_dict',
37
44
    dict(a=42),
38
45
    advice='Pull the other one!',
48
55
        super(TestDeprecationWarnings, self).setUp()
49
56
        self._warnings = []
50
57
    
51
 
    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
 
58
    @deprecated_method(deprecated_in((0, 7, 0)))
52
59
    def deprecated_method(self):
53
60
        """Deprecated method docstring.
54
 
        
 
61
 
55
62
        This might explain stuff.
56
63
        """
57
64
        return 1
58
65
 
 
66
    @staticmethod
 
67
    @deprecated_function(deprecated_in((0, 7, 0)))
 
68
    def deprecated_static():
 
69
        """Deprecated static."""
 
70
        return 1
 
71
 
 
72
    def test_deprecated_static(self):
 
73
        # XXX: The results are not quite right because the class name is not
 
74
        # shown - however it is enough to give people a good indication of
 
75
        # where the problem is.
 
76
        expected_warning = (
 
77
            "bzrlib.tests.test_symbol_versioning."
 
78
            "deprecated_static "
 
79
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
 
80
        expected_docstring = (
 
81
            'Deprecated static.\n'
 
82
            '\n'
 
83
            'This function was deprecated in version 0.7.0.\n'
 
84
            )
 
85
        self.check_deprecated_callable(
 
86
            expected_warning, expected_docstring,
 
87
            "deprecated_static",
 
88
            "bzrlib.tests.test_symbol_versioning",
 
89
            self.deprecated_static)
 
90
 
59
91
    def test_deprecated_method(self):
60
92
        expected_warning = (
61
93
            "bzrlib.tests.test_symbol_versioning."
62
94
            "TestDeprecationWarnings.deprecated_method "
63
 
            "was deprecated in version 0.7.", DeprecationWarning, 2)
64
 
        expected_docstring = ('Deprecated method docstring.\n'
65
 
                              '        \n'
66
 
                              '        This might explain stuff.\n'
67
 
                              '        \n'
68
 
                              '        This method was deprecated in version 0.7.\n'
69
 
                              '        ')
 
95
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
 
96
        expected_docstring = (
 
97
            'Deprecated method docstring.\n'
 
98
            '\n'
 
99
            '        This might explain stuff.\n'
 
100
            '        \n'
 
101
            '        This method was deprecated in version 0.7.0.\n'
 
102
            '        ')
70
103
        self.check_deprecated_callable(expected_warning, expected_docstring,
71
104
                                       "deprecated_method",
72
105
                                       "bzrlib.tests.test_symbol_versioning",
74
107
 
75
108
    def test_deprecated_function(self):
76
109
        expected_warning = (
77
 
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
78
 
            "was deprecated in version 0.7.", DeprecationWarning, 2)
 
110
            "bzrlib.tests.test_symbol_versioning.sample_deprecated_function "
 
111
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
79
112
        expected_docstring = ('Deprecated function docstring.\n'
80
113
                              '\n'
81
 
                              'This function was deprecated in version 0.7.\n'
 
114
                              'This function was deprecated in version 0.7.0.\n'
82
115
                              )
83
116
        self.check_deprecated_callable(expected_warning, expected_docstring,
84
 
                                       "deprecated_function",
 
117
                                       "sample_deprecated_function",
85
118
                                       "bzrlib.tests.test_symbol_versioning",
86
 
                                       deprecated_function)
 
119
                                       sample_deprecated_function)
87
120
 
88
121
    def test_deprecated_list(self):
89
122
        expected_warning = (
90
 
            "Modifying a_deprecated_list was deprecated in version 0.9."
 
123
            "Modifying a_deprecated_list was deprecated in version 0.9.0."
91
124
            " Don't use me", DeprecationWarning, 3)
92
125
        old_warning_method = symbol_versioning.warn
93
126
        try:
125
158
 
126
159
    def test_deprecated_dict(self):
127
160
        expected_warning = (
128
 
            "access to a_deprecated_dict was deprecated in version 0.14."
 
161
            "access to a_deprecated_dict was deprecated in version 0.14.0."
129
162
            " Pull the other one!", DeprecationWarning, 2)
130
163
        old_warning_method = symbol_versioning.warn
131
164
        try:
160
193
            self.assertTrue(deprecated_callable.is_deprecated)
161
194
        finally:
162
195
            symbol_versioning.set_warning_method(old_warning_method)
163
 
    
 
196
 
164
197
    def test_deprecated_passed(self):
165
198
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
166
199
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
173
206
        """We can get a deprecation string for a method or function."""
174
207
        self.assertEqual('bzrlib.tests.test_symbol_versioning.'
175
208
            'TestDeprecationWarnings.test_deprecation_string was deprecated in '
176
 
            'version 0.11.',
 
209
            'version 0.11.0.',
177
210
            symbol_versioning.deprecation_string(
178
 
            self.test_deprecation_string, symbol_versioning.zero_eleven))
 
211
            self.test_deprecation_string,
 
212
            deprecated_in((0, 11, 0))))
179
213
        self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
180
 
            'deprecated in version 0.11.',
 
214
            'deprecated in version 0.11.0.',
181
215
            symbol_versioning.deprecation_string(
182
216
                symbol_versioning.deprecated_function,
183
 
                symbol_versioning.zero_eleven))
 
217
                deprecated_in((0, 11, 0))))
 
218
 
 
219
 
 
220
class TestSuppressAndActivate(TestCase):
 
221
 
 
222
    def setUp(self):
 
223
        TestCase.setUp(self)
 
224
        existing_filters = list(warnings.filters)
 
225
        def restore():
 
226
            warnings.filters[:] = existing_filters
 
227
        self.addCleanup(restore)
 
228
        # Clean out the filters so we have a clean slate.
 
229
        warnings.resetwarnings()
 
230
 
 
231
    def assertFirstWarning(self, action, category):
 
232
        """Test the first warning in the filters is correct"""
 
233
        first = warnings.filters[0]
 
234
        self.assertEqual((action, category), (first[0], first[2]))
 
235
 
 
236
    def test_suppress_deprecation_warnings(self):
 
237
        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
 
238
        symbol_versioning.suppress_deprecation_warnings()
 
239
        self.assertFirstWarning('ignore', DeprecationWarning)
 
240
 
 
241
    def test_suppress_deprecation_with_warning_filter(self):
 
242
        """don't suppress if we already have a filter"""
 
243
        warnings.filterwarnings('error', category=Warning)
 
244
        self.assertFirstWarning('error', Warning)
 
245
        self.assertEqual(1, len(warnings.filters))
 
246
        symbol_versioning.suppress_deprecation_warnings(override=False)
 
247
        self.assertFirstWarning('error', Warning)
 
248
        self.assertEqual(1, len(warnings.filters))
 
249
 
 
250
    def test_suppress_deprecation_with_filter(self):
 
251
        """don't suppress if we already have a filter"""
 
252
        warnings.filterwarnings('error', category=DeprecationWarning)
 
253
        self.assertFirstWarning('error', DeprecationWarning)
 
254
        self.assertEqual(1, len(warnings.filters))
 
255
        symbol_versioning.suppress_deprecation_warnings(override=False)
 
256
        self.assertFirstWarning('error', DeprecationWarning)
 
257
        self.assertEqual(1, len(warnings.filters))
 
258
        symbol_versioning.suppress_deprecation_warnings(override=True)
 
259
        self.assertFirstWarning('ignore', DeprecationWarning)
 
260
        self.assertEqual(2, len(warnings.filters))
 
261
 
 
262
    def test_activate_deprecation_no_error(self):
 
263
        # First nuke the filters, so we know it is clean
 
264
        symbol_versioning.activate_deprecation_warnings()
 
265
        self.assertFirstWarning('default', DeprecationWarning)
 
266
 
 
267
    def test_activate_deprecation_with_error(self):
 
268
        # First nuke the filters, so we know it is clean
 
269
        # Add a warning == error rule
 
270
        warnings.filterwarnings('error', category=Warning)
 
271
        self.assertFirstWarning('error', Warning)
 
272
        self.assertEqual(1, len(warnings.filters))
 
273
        symbol_versioning.activate_deprecation_warnings(override=False)
 
274
        # There should not be a new warning
 
275
        self.assertFirstWarning('error', Warning)
 
276
        self.assertEqual(1, len(warnings.filters))
 
277
 
 
278
    def test_activate_deprecation_with_DW_error(self):
 
279
        # First nuke the filters, so we know it is clean
 
280
        # Add a warning == error rule
 
281
        warnings.filterwarnings('error', category=DeprecationWarning)
 
282
        self.assertFirstWarning('error', DeprecationWarning)
 
283
        self.assertEqual(1, len(warnings.filters))
 
284
        symbol_versioning.activate_deprecation_warnings(override=False)
 
285
        # There should not be a new warning
 
286
        self.assertFirstWarning('error', DeprecationWarning)
 
287
        self.assertEqual(1, len(warnings.filters))
 
288
        symbol_versioning.activate_deprecation_warnings(override=True)
 
289
        self.assertFirstWarning('default', DeprecationWarning)
 
290
        self.assertEqual(2, len(warnings.filters))