~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: Robert Collins
  • Date: 2007-10-23 22:14:32 UTC
  • mto: (2592.6.3 repository)
  • mto: This revision was merged to the branch mainline in revision 2967.
  • Revision ID: robertc@robertcollins.net-20071023221432-j8zndh1oiegql3cu
* Commit updates the state of the working tree via a delta rather than
  supplying entirely new basis trees. For commit of a single specified file
  this reduces the wall clock time for commit by roughly a 30%.
  (Robert Collins, Martin Pool)

Show diffs side-by-side

added added

removed removed

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