1
# Copyright (C) 2006, 2007 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Symbol versioning tests."""
23
from bzrlib import symbol_versioning
24
from bzrlib.tests import TestCase
27
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
28
def deprecated_function():
29
"""Deprecated function docstring."""
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!',
45
class TestDeprecationWarnings(TestCase):
47
def capture_warning(self, message, category, stacklevel=None):
48
self._warnings.append((message, category, stacklevel))
51
super(TestDeprecationWarnings, self).setUp()
54
@symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
55
def deprecated_method(self):
56
"""Deprecated method docstring.
58
This might explain stuff.
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)
87
def test_deprecated_method(self):
89
"bzrlib.tests.test_symbol_versioning."
90
"TestDeprecationWarnings.deprecated_method "
91
"was deprecated in version 0.7.", DeprecationWarning, 2)
92
expected_docstring = ('Deprecated method docstring.\n'
94
' This might explain stuff.\n'
96
' This method was deprecated in version 0.7.\n'
98
self.check_deprecated_callable(expected_warning, expected_docstring,
100
"bzrlib.tests.test_symbol_versioning",
101
self.deprecated_method)
103
def test_deprecated_function(self):
105
"bzrlib.tests.test_symbol_versioning.deprecated_function "
106
"was deprecated in version 0.7.", DeprecationWarning, 2)
107
expected_docstring = ('Deprecated function docstring.\n'
109
'This function was deprecated in version 0.7.\n'
111
self.check_deprecated_callable(expected_warning, expected_docstring,
112
"deprecated_function",
113
"bzrlib.tests.test_symbol_versioning",
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)
174
def check_deprecated_callable(self, expected_warning, expected_docstring,
175
expected_name, expected_module,
176
deprecated_callable):
177
old_warning_method = symbol_versioning.warn
179
symbol_versioning.set_warning_method(self.capture_warning)
180
self.assertEqual(1, deprecated_callable())
181
self.assertEqual([expected_warning], self._warnings)
182
deprecated_callable()
183
self.assertEqual([expected_warning, expected_warning],
185
self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
186
self.assertEqualDiff(expected_name, deprecated_callable.__name__)
187
self.assertEqualDiff(expected_module, deprecated_callable.__module__)
188
self.assertTrue(deprecated_callable.is_deprecated)
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))