33
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
34
'a_deprecated_list', ['one'], extra="Don't use me")
37
a_deprecated_dict = symbol_versioning.DeprecatedDict(
38
symbol_versioning.zero_fourteen,
41
advice='Pull the other one!',
30
45
class TestDeprecationWarnings(TestCase):
32
def capture_warning(self, message, category):
33
self._warnings.append((message, category))
47
def capture_warning(self, message, category, stacklevel=None):
48
self._warnings.append((message, category, stacklevel))
36
51
super(TestDeprecationWarnings, self).setUp()
63
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
64
def deprecated_static():
65
"""Deprecated static."""
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.
73
"bzrlib.tests.test_symbol_versioning."
75
"was deprecated in version 0.7.", DeprecationWarning, 2)
76
expected_docstring = (
77
'Deprecated static.\n'
79
'This function was deprecated in version 0.7.\n'
81
self.check_deprecated_callable(
82
expected_warning, expected_docstring,
84
"bzrlib.tests.test_symbol_versioning",
85
self.deprecated_static)
47
87
def test_deprecated_method(self):
48
88
expected_warning = (
49
89
"bzrlib.tests.test_symbol_versioning."
50
90
"TestDeprecationWarnings.deprecated_method "
51
"was deprecated in version 0.7.", DeprecationWarning)
91
"was deprecated in version 0.7.", DeprecationWarning, 2)
52
92
expected_docstring = ('Deprecated method docstring.\n'
54
94
' This might explain stuff.\n'
73
113
"bzrlib.tests.test_symbol_versioning",
74
114
deprecated_function)
116
def test_deprecated_list(self):
118
"Modifying a_deprecated_list was deprecated in version 0.9."
119
" Don't use me", DeprecationWarning, 3)
120
old_warning_method = symbol_versioning.warn
122
symbol_versioning.set_warning_method(self.capture_warning)
123
self.assertEqual(['one'], a_deprecated_list)
124
self.assertEqual([], self._warnings)
126
a_deprecated_list.append('foo')
127
self.assertEqual([expected_warning], self._warnings)
128
self.assertEqual(['one', 'foo'], a_deprecated_list)
130
a_deprecated_list.extend(['bar', 'baz'])
131
self.assertEqual([expected_warning]*2, self._warnings)
132
self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
134
a_deprecated_list.insert(1, 'xxx')
135
self.assertEqual([expected_warning]*3, self._warnings)
136
self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
138
a_deprecated_list.remove('foo')
139
self.assertEqual([expected_warning]*4, self._warnings)
140
self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
142
val = a_deprecated_list.pop()
143
self.assertEqual([expected_warning]*5, self._warnings)
144
self.assertEqual('baz', val)
145
self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
147
val = a_deprecated_list.pop(1)
148
self.assertEqual([expected_warning]*6, self._warnings)
149
self.assertEqual('xxx', val)
150
self.assertEqual(['one', 'bar'], a_deprecated_list)
152
symbol_versioning.set_warning_method(old_warning_method)
154
def test_deprecated_dict(self):
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
160
symbol_versioning.set_warning_method(self.capture_warning)
161
self.assertEqual(len(a_deprecated_dict), 1)
162
self.assertEqual([expected_warning], self._warnings)
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)
171
symbol_versioning.set_warning_method(old_warning_method)
76
174
def check_deprecated_callable(self, expected_warning, expected_docstring,
77
175
expected_name, expected_module,
78
176
deprecated_callable):
87
185
self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
88
186
self.assertEqualDiff(expected_name, deprecated_callable.__name__)
89
187
self.assertEqualDiff(expected_module, deprecated_callable.__module__)
188
self.assertTrue(deprecated_callable.is_deprecated)
91
190
symbol_versioning.set_warning_method(old_warning_method)
192
def test_deprecated_passed(self):
193
self.assertEqual(True, symbol_versioning.deprecated_passed(None))
194
self.assertEqual(True, symbol_versioning.deprecated_passed(True))
195
self.assertEqual(True, symbol_versioning.deprecated_passed(False))
196
self.assertEqual(False,
197
symbol_versioning.deprecated_passed(
198
symbol_versioning.DEPRECATED_PARAMETER))
200
def test_deprecation_string(self):
201
"""We can get a deprecation string for a method or function."""
202
self.assertEqual('bzrlib.tests.test_symbol_versioning.'
203
'TestDeprecationWarnings.test_deprecation_string was deprecated in '
205
symbol_versioning.deprecation_string(
206
self.test_deprecation_string, symbol_versioning.zero_eleven))
207
self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
208
'deprecated in version 0.11.',
209
symbol_versioning.deprecation_string(
210
symbol_versioning.deprecated_function,
211
symbol_versioning.zero_eleven))
214
class TestSuppressAndActivate(TestCase):
217
existing_filters = list(warnings.filters)
219
warnings.filters[:] = existing_filters
220
self.addCleanup(restore)
221
# Clean out the filters so we have a clean slate.
222
warnings.resetwarnings()
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]))
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)
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))
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))
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)
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))
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))