~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-18 10:42:07 UTC
  • mfrom: (6437.3.6 2.5)
  • mto: (6437.3.7 2.5)
  • mto: This revision was merged to the branch mainline in revision 6441.
  • Revision ID: jelmer@vernstok.nl-20120118104207-yc5fhsaz4mupjcam
Merge 2.5 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Symbol versioning tests."""
 
18
 
 
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
    )
 
27
from bzrlib.tests import TestCase
 
28
 
 
29
 
 
30
@deprecated_function(deprecated_in((0, 7, 0)))
 
31
def sample_deprecated_function():
 
32
    """Deprecated function docstring."""
 
33
    return 1
 
34
 
 
35
 
 
36
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
 
37
    'a_deprecated_list', ['one'], extra="Don't use me")
 
38
 
 
39
 
 
40
a_deprecated_dict = symbol_versioning.DeprecatedDict(
 
41
    deprecated_in((0, 14, 0)),
 
42
    'a_deprecated_dict',
 
43
    dict(a=42),
 
44
    advice='Pull the other one!',
 
45
    )
 
46
 
 
47
 
 
48
class TestDeprecationWarnings(TestCase):
 
49
 
 
50
    def capture_warning(self, message, category, stacklevel=None):
 
51
        self._warnings.append((message, category, stacklevel))
 
52
 
 
53
    def setUp(self):
 
54
        super(TestDeprecationWarnings, self).setUp()
 
55
        self._warnings = []
 
56
    
 
57
    @deprecated_method(deprecated_in((0, 7, 0)))
 
58
    def deprecated_method(self):
 
59
        """Deprecated method docstring.
 
60
 
 
61
        This might explain stuff.
 
62
        """
 
63
        return 1
 
64
 
 
65
    @staticmethod
 
66
    @deprecated_function(deprecated_in((0, 7, 0)))
 
67
    def deprecated_static():
 
68
        """Deprecated static."""
 
69
        return 1
 
70
 
 
71
    def test_deprecated_static(self):
 
72
        # XXX: The results are not quite right because the class name is not
 
73
        # shown - however it is enough to give people a good indication of
 
74
        # where the problem is.
 
75
        expected_warning = (
 
76
            "bzrlib.tests.test_symbol_versioning."
 
77
            "deprecated_static "
 
78
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
 
79
        expected_docstring = (
 
80
            'Deprecated static.\n'
 
81
            '\n'
 
82
            'This function was deprecated in version 0.7.0.\n'
 
83
            )
 
84
        self.check_deprecated_callable(
 
85
            expected_warning, expected_docstring,
 
86
            "deprecated_static",
 
87
            "bzrlib.tests.test_symbol_versioning",
 
88
            self.deprecated_static)
 
89
 
 
90
    def test_deprecated_method(self):
 
91
        expected_warning = (
 
92
            "bzrlib.tests.test_symbol_versioning."
 
93
            "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
            '        ')
 
102
        self.check_deprecated_callable(expected_warning, expected_docstring,
 
103
                                       "deprecated_method",
 
104
                                       "bzrlib.tests.test_symbol_versioning",
 
105
                                       self.deprecated_method)
 
106
 
 
107
    def test_deprecated_function(self):
 
108
        expected_warning = (
 
109
            "bzrlib.tests.test_symbol_versioning.sample_deprecated_function "
 
110
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
 
111
        expected_docstring = ('Deprecated function docstring.\n'
 
112
                              '\n'
 
113
                              'This function was deprecated in version 0.7.0.\n'
 
114
                              )
 
115
        self.check_deprecated_callable(expected_warning, expected_docstring,
 
116
                                       "sample_deprecated_function",
 
117
                                       "bzrlib.tests.test_symbol_versioning",
 
118
                                       sample_deprecated_function)
 
119
 
 
120
    def test_deprecated_list(self):
 
121
        expected_warning = (
 
122
            "Modifying a_deprecated_list was deprecated in version 0.9.0."
 
123
            " Don't use me", DeprecationWarning, 3)
 
124
        old_warning_method = symbol_versioning.warn
 
125
        try:
 
126
            symbol_versioning.set_warning_method(self.capture_warning)
 
127
            self.assertEqual(['one'], a_deprecated_list)
 
128
            self.assertEqual([], self._warnings)
 
129
 
 
130
            a_deprecated_list.append('foo')
 
131
            self.assertEqual([expected_warning], self._warnings)
 
132
            self.assertEqual(['one', 'foo'], a_deprecated_list)
 
133
 
 
134
            a_deprecated_list.extend(['bar', 'baz'])
 
135
            self.assertEqual([expected_warning]*2, self._warnings)
 
136
            self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
 
137
 
 
138
            a_deprecated_list.insert(1, 'xxx')
 
139
            self.assertEqual([expected_warning]*3, self._warnings)
 
140
            self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
 
141
 
 
142
            a_deprecated_list.remove('foo')
 
143
            self.assertEqual([expected_warning]*4, self._warnings)
 
144
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
 
145
 
 
146
            val = a_deprecated_list.pop()
 
147
            self.assertEqual([expected_warning]*5, self._warnings)
 
148
            self.assertEqual('baz', val)
 
149
            self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
 
150
 
 
151
            val = a_deprecated_list.pop(1)
 
152
            self.assertEqual([expected_warning]*6, self._warnings)
 
153
            self.assertEqual('xxx', val)
 
154
            self.assertEqual(['one', 'bar'], a_deprecated_list)
 
155
        finally:
 
156
            symbol_versioning.set_warning_method(old_warning_method)
 
157
 
 
158
    def test_deprecated_dict(self):
 
159
        expected_warning = (
 
160
            "access to a_deprecated_dict was deprecated in version 0.14.0."
 
161
            " Pull the other one!", DeprecationWarning, 2)
 
162
        old_warning_method = symbol_versioning.warn
 
163
        try:
 
164
            symbol_versioning.set_warning_method(self.capture_warning)
 
165
            self.assertEqual(len(a_deprecated_dict), 1)
 
166
            self.assertEqual([expected_warning], self._warnings)
 
167
 
 
168
            a_deprecated_dict['b'] = 42
 
169
            self.assertEqual(a_deprecated_dict['b'], 42)
 
170
            self.assertTrue('b' in a_deprecated_dict)
 
171
            del a_deprecated_dict['b']
 
172
            self.assertFalse('b' in a_deprecated_dict)
 
173
            self.assertEqual([expected_warning] * 6, self._warnings)
 
174
        finally:
 
175
            symbol_versioning.set_warning_method(old_warning_method)
 
176
 
 
177
 
 
178
    def check_deprecated_callable(self, expected_warning, expected_docstring,
 
179
                                  expected_name, expected_module,
 
180
                                  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
        old_warning_method = symbol_versioning.warn
 
185
        try:
 
186
            symbol_versioning.set_warning_method(self.capture_warning)
 
187
            self.assertEqual(1, deprecated_callable())
 
188
            self.assertEqual([expected_warning], self._warnings)
 
189
            deprecated_callable()
 
190
            self.assertEqual([expected_warning, expected_warning],
 
191
                             self._warnings)
 
192
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
 
193
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
 
194
            self.assertEqualDiff(expected_module, deprecated_callable.__module__)
 
195
            self.assertTrue(deprecated_callable.is_deprecated)
 
196
        finally:
 
197
            symbol_versioning.set_warning_method(old_warning_method)
 
198
 
 
199
    def test_deprecated_passed(self):
 
200
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
 
201
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
 
202
        self.assertEqual(True, symbol_versioning.deprecated_passed(False))
 
203
        self.assertEqual(False,
 
204
                         symbol_versioning.deprecated_passed(
 
205
                            symbol_versioning.DEPRECATED_PARAMETER))
 
206
 
 
207
    def test_deprecation_string(self):
 
208
        """We can get a deprecation string for a method or function."""
 
209
        self.assertEqual('bzrlib.tests.test_symbol_versioning.'
 
210
            'TestDeprecationWarnings.test_deprecation_string was deprecated in '
 
211
            'version 0.11.0.',
 
212
            symbol_versioning.deprecation_string(
 
213
            self.test_deprecation_string,
 
214
            deprecated_in((0, 11, 0))))
 
215
        self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
 
216
            'deprecated in version 0.11.0.',
 
217
            symbol_versioning.deprecation_string(
 
218
                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))