1
# Copyright (C) 2006-2011 Canonical Ltd
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.
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.
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
17
"""Symbol versioning tests."""
21
from breezy import symbol_versioning
22
from breezy.symbol_versioning import (
27
from breezy.tests import TestCase
30
@deprecated_function(deprecated_in((0, 7, 0)))
31
def sample_deprecated_function():
32
"""Deprecated function docstring."""
36
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
37
'a_deprecated_list', ['one'], extra="Don't use me")
40
a_deprecated_dict = symbol_versioning.DeprecatedDict(
41
deprecated_in((0, 14, 0)),
44
advice='Pull the other one!',
48
class TestDeprecationWarnings(TestCase):
50
def capture_warning(self, message, category, stacklevel=None):
51
self._warnings.append((message, category, stacklevel))
54
super(TestDeprecationWarnings, self).setUp()
57
@deprecated_method(deprecated_in((0, 7, 0)))
58
def deprecated_method(self):
59
"""Deprecated method docstring.
61
This might explain stuff.
66
@deprecated_function(deprecated_in((0, 7, 0)))
67
def deprecated_static():
68
"""Deprecated static."""
71
def test_deprecated_static(self):
73
"breezy.tests.test_symbol_versioning.TestDeprecationWarnings."
75
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
76
expected_docstring = (
77
'Deprecated static.\n'
79
'This function was deprecated in version 0.7.0.\n'
81
self.check_deprecated_callable(
82
expected_warning, expected_docstring,
84
"breezy.tests.test_symbol_versioning",
85
self.deprecated_static)
87
def test_deprecated_method(self):
89
"breezy.tests.test_symbol_versioning."
90
"TestDeprecationWarnings.deprecated_method "
91
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
92
expected_docstring = (
93
'Deprecated method docstring.\n'
95
' This might explain stuff.\n'
97
' This method was deprecated in version 0.7.0.\n'
99
self.check_deprecated_callable(expected_warning, expected_docstring,
101
"breezy.tests.test_symbol_versioning",
102
self.deprecated_method)
104
def test_deprecated_function(self):
106
"breezy.tests.test_symbol_versioning.sample_deprecated_function "
107
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
108
expected_docstring = ('Deprecated function docstring.\n'
110
'This function was deprecated in version 0.7.0.\n'
112
self.check_deprecated_callable(expected_warning, expected_docstring,
113
"sample_deprecated_function",
114
"breezy.tests.test_symbol_versioning",
115
sample_deprecated_function)
117
def test_deprecated_list(self):
119
"Modifying a_deprecated_list was deprecated in version 0.9.0."
120
" Don't use me", DeprecationWarning, 3)
121
old_warning_method = symbol_versioning.warn
123
symbol_versioning.set_warning_method(self.capture_warning)
124
self.assertEqual(['one'], a_deprecated_list)
125
self.assertEqual([], self._warnings)
127
a_deprecated_list.append('foo')
128
self.assertEqual([expected_warning], self._warnings)
129
self.assertEqual(['one', 'foo'], a_deprecated_list)
131
a_deprecated_list.extend(['bar', 'baz'])
132
self.assertEqual([expected_warning] * 2, self._warnings)
133
self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
135
a_deprecated_list.insert(1, 'xxx')
136
self.assertEqual([expected_warning] * 3, self._warnings)
138
['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
140
a_deprecated_list.remove('foo')
141
self.assertEqual([expected_warning] * 4, self._warnings)
142
self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
144
val = a_deprecated_list.pop()
145
self.assertEqual([expected_warning] * 5, self._warnings)
146
self.assertEqual('baz', val)
147
self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
149
val = a_deprecated_list.pop(1)
150
self.assertEqual([expected_warning] * 6, self._warnings)
151
self.assertEqual('xxx', val)
152
self.assertEqual(['one', 'bar'], a_deprecated_list)
154
symbol_versioning.set_warning_method(old_warning_method)
156
def test_deprecated_dict(self):
158
"access to a_deprecated_dict was deprecated in version 0.14.0."
159
" Pull the other one!", DeprecationWarning, 2)
160
old_warning_method = symbol_versioning.warn
162
symbol_versioning.set_warning_method(self.capture_warning)
163
self.assertEqual(len(a_deprecated_dict), 1)
164
self.assertEqual([expected_warning], self._warnings)
166
a_deprecated_dict['b'] = 42
167
self.assertEqual(a_deprecated_dict['b'], 42)
168
self.assertTrue('b' in a_deprecated_dict)
169
del a_deprecated_dict['b']
170
self.assertFalse('b' in a_deprecated_dict)
171
self.assertEqual([expected_warning] * 6, self._warnings)
173
symbol_versioning.set_warning_method(old_warning_method)
175
def check_deprecated_callable(self, expected_warning, expected_docstring,
176
expected_name, expected_module,
177
deprecated_callable):
179
# With -OO the docstring should just be the deprecated version
180
expected_docstring = expected_docstring.split('\n')[-2].lstrip()
181
old_warning_method = symbol_versioning.warn
183
symbol_versioning.set_warning_method(self.capture_warning)
184
self.assertEqual(1, deprecated_callable())
185
self.assertEqual([expected_warning], self._warnings)
186
deprecated_callable()
187
self.assertEqual([expected_warning, expected_warning],
189
self.assertEqualDiff(expected_docstring,
190
deprecated_callable.__doc__)
191
self.assertEqualDiff(expected_name, deprecated_callable.__name__)
192
self.assertEqualDiff(
193
expected_module, deprecated_callable.__module__)
194
self.assertTrue(deprecated_callable.is_deprecated)
196
symbol_versioning.set_warning_method(old_warning_method)
198
def test_deprecated_passed(self):
199
self.assertEqual(True, symbol_versioning.deprecated_passed(None))
200
self.assertEqual(True, symbol_versioning.deprecated_passed(True))
201
self.assertEqual(True, symbol_versioning.deprecated_passed(False))
202
self.assertEqual(False,
203
symbol_versioning.deprecated_passed(
204
symbol_versioning.DEPRECATED_PARAMETER))
206
def test_deprecation_string(self):
207
"""We can get a deprecation string for a method or function."""
208
err_message = symbol_versioning.deprecation_string(
209
self.test_deprecation_string,
210
deprecated_in((0, 11, 0)))
211
self.assertEqual(err_message,
212
'breezy.tests.test_symbol_versioning.TestDeprecationWarnings.'
213
'test_deprecation_string was deprecated in '
216
self.assertEqual('breezy.symbol_versioning.deprecated_function was '
217
'deprecated in version 0.11.0.',
218
symbol_versioning.deprecation_string(
219
symbol_versioning.deprecated_function,
220
deprecated_in((0, 11, 0))))
223
class TestSuppressAndActivate(TestCase):
226
super(TestSuppressAndActivate, self).setUp()
227
existing_filters = list(warnings.filters)
230
warnings.filters[:] = existing_filters
231
self.addCleanup(restore)
232
# Clean out the filters so we have a clean slate.
233
warnings.resetwarnings()
235
def assertFirstWarning(self, action, category):
236
"""Test the first warning in the filters is correct"""
237
first = warnings.filters[0]
238
self.assertEqual((action, category), (first[0], first[2]))
240
def test_suppress_deprecation_warnings(self):
241
"""suppress_deprecation_warnings sets DeprecationWarning to ignored."""
242
symbol_versioning.suppress_deprecation_warnings()
243
self.assertFirstWarning('ignore', DeprecationWarning)
245
def test_set_restore_filters(self):
246
original_filters = warnings.filters[:]
247
symbol_versioning.suppress_deprecation_warnings()()
248
self.assertEqual(original_filters, warnings.filters)
250
def test_suppress_deprecation_with_warning_filter(self):
251
"""don't suppress if we already have a filter"""
252
warnings.filterwarnings('error', category=Warning)
253
self.assertFirstWarning('error', Warning)
254
self.assertEqual(1, len(warnings.filters))
255
symbol_versioning.suppress_deprecation_warnings(override=False)
256
self.assertFirstWarning('error', Warning)
257
self.assertEqual(1, len(warnings.filters))
259
def test_suppress_deprecation_with_filter(self):
260
"""don't suppress if we already have a filter"""
261
warnings.filterwarnings('error', category=DeprecationWarning)
262
self.assertFirstWarning('error', DeprecationWarning)
263
self.assertEqual(1, len(warnings.filters))
264
symbol_versioning.suppress_deprecation_warnings(override=False)
265
self.assertFirstWarning('error', DeprecationWarning)
266
self.assertEqual(1, len(warnings.filters))
267
symbol_versioning.suppress_deprecation_warnings(override=True)
268
self.assertFirstWarning('ignore', DeprecationWarning)
269
self.assertEqual(2, len(warnings.filters))
271
def test_activate_deprecation_no_error(self):
272
# First nuke the filters, so we know it is clean
273
symbol_versioning.activate_deprecation_warnings()
274
self.assertFirstWarning('default', DeprecationWarning)
276
def test_activate_deprecation_with_error(self):
277
# First nuke the filters, so we know it is clean
278
# Add a warning == error rule
279
warnings.filterwarnings('error', category=Warning)
280
self.assertFirstWarning('error', Warning)
281
self.assertEqual(1, len(warnings.filters))
282
symbol_versioning.activate_deprecation_warnings(override=False)
283
# There should not be a new warning
284
self.assertFirstWarning('error', Warning)
285
self.assertEqual(1, len(warnings.filters))
287
def test_activate_deprecation_with_DW_error(self):
288
# First nuke the filters, so we know it is clean
289
# Add a warning == error rule
290
warnings.filterwarnings('error', category=DeprecationWarning)
291
self.assertFirstWarning('error', DeprecationWarning)
292
self.assertEqual(1, len(warnings.filters))
293
symbol_versioning.activate_deprecation_warnings(override=False)
294
# There should not be a new warning
295
self.assertFirstWarning('error', DeprecationWarning)
296
self.assertEqual(1, len(warnings.filters))
297
symbol_versioning.activate_deprecation_warnings(override=True)
298
self.assertFirstWarning('default', DeprecationWarning)
299
self.assertEqual(2, len(warnings.filters))