13
14
# You should have received a copy of the GNU General Public License
14
15
# 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
"""Symbol versioning tests."""
21
from bzrlib import symbol_versioning
22
from bzrlib.symbol_versioning import (
20
import bzrlib.symbol_versioning as symbol_versioning
28
21
from bzrlib.tests import TestCase
31
@deprecated_function(deprecated_in((0, 7, 0)))
32
def sample_deprecated_function():
24
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
25
def deprecated_function():
33
26
"""Deprecated function docstring."""
37
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
38
'a_deprecated_list', ['one'], extra="Don't use me")
41
a_deprecated_dict = symbol_versioning.DeprecatedDict(
42
deprecated_in((0, 14, 0)),
45
advice='Pull the other one!',
49
30
class TestDeprecationWarnings(TestCase):
51
def capture_warning(self, message, category, stacklevel=None):
52
self._warnings.append((message, category, stacklevel))
32
def capture_warning(self, message, category):
33
self._warnings.append((message, category))
55
36
super(TestDeprecationWarnings, self).setUp()
56
37
self._warnings = []
58
@deprecated_method(deprecated_in((0, 7, 0)))
39
@symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
59
40
def deprecated_method(self):
60
41
"""Deprecated method docstring.
62
43
This might explain stuff.
67
@deprecated_function(deprecated_in((0, 7, 0)))
68
def deprecated_static():
69
"""Deprecated static."""
72
def test_deprecated_static(self):
73
# XXX: The results are not quite right because the class name is not
74
# shown - however it is enough to give people a good indication of
75
# where the problem is.
77
"bzrlib.tests.test_symbol_versioning."
79
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
80
expected_docstring = (
81
'Deprecated static.\n'
83
'This function was deprecated in version 0.7.0.\n'
85
self.check_deprecated_callable(
86
expected_warning, expected_docstring,
88
"bzrlib.tests.test_symbol_versioning",
89
self.deprecated_static)
91
47
def test_deprecated_method(self):
92
48
expected_warning = (
93
49
"bzrlib.tests.test_symbol_versioning."
94
50
"TestDeprecationWarnings.deprecated_method "
95
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
96
expected_docstring = (
97
'Deprecated method docstring.\n'
99
' This might explain stuff.\n'
101
' This method was deprecated in version 0.7.0.\n'
51
"was deprecated in version 0.7.", DeprecationWarning)
52
expected_docstring = ('Deprecated method docstring.\n'
54
' This might explain stuff.\n'
56
' This method was deprecated in version 0.7.\n'
103
58
self.check_deprecated_callable(expected_warning, expected_docstring,
104
59
"deprecated_method",
105
60
"bzrlib.tests.test_symbol_versioning",
108
63
def test_deprecated_function(self):
109
64
expected_warning = (
110
"bzrlib.tests.test_symbol_versioning.sample_deprecated_function "
111
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
65
"bzrlib.tests.test_symbol_versioning.deprecated_function "
66
"was deprecated in version 0.7.", DeprecationWarning)
112
67
expected_docstring = ('Deprecated function docstring.\n'
114
'This function was deprecated in version 0.7.0.\n'
69
'This function was deprecated in version 0.7.\n'
116
71
self.check_deprecated_callable(expected_warning, expected_docstring,
117
"sample_deprecated_function",
72
"deprecated_function",
118
73
"bzrlib.tests.test_symbol_versioning",
119
sample_deprecated_function)
121
def test_deprecated_list(self):
123
"Modifying a_deprecated_list was deprecated in version 0.9.0."
124
" Don't use me", DeprecationWarning, 3)
125
old_warning_method = symbol_versioning.warn
127
symbol_versioning.set_warning_method(self.capture_warning)
128
self.assertEqual(['one'], a_deprecated_list)
129
self.assertEqual([], self._warnings)
131
a_deprecated_list.append('foo')
132
self.assertEqual([expected_warning], self._warnings)
133
self.assertEqual(['one', 'foo'], a_deprecated_list)
135
a_deprecated_list.extend(['bar', 'baz'])
136
self.assertEqual([expected_warning]*2, self._warnings)
137
self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
139
a_deprecated_list.insert(1, 'xxx')
140
self.assertEqual([expected_warning]*3, self._warnings)
141
self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
143
a_deprecated_list.remove('foo')
144
self.assertEqual([expected_warning]*4, self._warnings)
145
self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
147
val = a_deprecated_list.pop()
148
self.assertEqual([expected_warning]*5, self._warnings)
149
self.assertEqual('baz', val)
150
self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
152
val = a_deprecated_list.pop(1)
153
self.assertEqual([expected_warning]*6, self._warnings)
154
self.assertEqual('xxx', val)
155
self.assertEqual(['one', 'bar'], a_deprecated_list)
157
symbol_versioning.set_warning_method(old_warning_method)
159
def test_deprecated_dict(self):
161
"access to a_deprecated_dict was deprecated in version 0.14.0."
162
" Pull the other one!", DeprecationWarning, 2)
163
old_warning_method = symbol_versioning.warn
165
symbol_versioning.set_warning_method(self.capture_warning)
166
self.assertEqual(len(a_deprecated_dict), 1)
167
self.assertEqual([expected_warning], self._warnings)
169
a_deprecated_dict['b'] = 42
170
self.assertEqual(a_deprecated_dict['b'], 42)
171
self.assertTrue('b' in a_deprecated_dict)
172
del a_deprecated_dict['b']
173
self.assertFalse('b' in a_deprecated_dict)
174
self.assertEqual([expected_warning] * 6, self._warnings)
176
symbol_versioning.set_warning_method(old_warning_method)
179
76
def check_deprecated_callable(self, expected_warning, expected_docstring,
180
77
expected_name, expected_module,
190
87
self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
191
88
self.assertEqualDiff(expected_name, deprecated_callable.__name__)
192
89
self.assertEqualDiff(expected_module, deprecated_callable.__module__)
193
self.assertTrue(deprecated_callable.is_deprecated)
195
91
symbol_versioning.set_warning_method(old_warning_method)
197
93
def test_deprecated_passed(self):
198
94
self.assertEqual(True, symbol_versioning.deprecated_passed(None))
199
95
self.assertEqual(True, symbol_versioning.deprecated_passed(True))
200
96
self.assertEqual(True, symbol_versioning.deprecated_passed(False))
201
97
self.assertEqual(False,
202
98
symbol_versioning.deprecated_passed(
203
symbol_versioning.DEPRECATED_PARAMETER))
205
def test_deprecation_string(self):
206
"""We can get a deprecation string for a method or function."""
207
self.assertEqual('bzrlib.tests.test_symbol_versioning.'
208
'TestDeprecationWarnings.test_deprecation_string was deprecated in '
210
symbol_versioning.deprecation_string(
211
self.test_deprecation_string,
212
deprecated_in((0, 11, 0))))
213
self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
214
'deprecated in version 0.11.0.',
215
symbol_versioning.deprecation_string(
216
symbol_versioning.deprecated_function,
217
deprecated_in((0, 11, 0))))
220
class TestSuppressAndActivate(TestCase):
224
existing_filters = list(warnings.filters)
226
warnings.filters[:] = existing_filters
227
self.addCleanup(restore)
228
# Clean out the filters so we have a clean slate.
229
warnings.resetwarnings()
231
def assertFirstWarning(self, action, category):
232
"""Test the first warning in the filters is correct"""
233
first = warnings.filters[0]
234
self.assertEqual((action, category), (first[0], first[2]))
236
def test_suppress_deprecation_warnings(self):
237
"""suppress_deprecation_warnings sets DeprecationWarning to ignored."""
238
symbol_versioning.suppress_deprecation_warnings()
239
self.assertFirstWarning('ignore', DeprecationWarning)
241
def test_suppress_deprecation_with_warning_filter(self):
242
"""don't suppress if we already have a filter"""
243
warnings.filterwarnings('error', category=Warning)
244
self.assertFirstWarning('error', Warning)
245
self.assertEqual(1, len(warnings.filters))
246
symbol_versioning.suppress_deprecation_warnings(override=False)
247
self.assertFirstWarning('error', Warning)
248
self.assertEqual(1, len(warnings.filters))
250
def test_suppress_deprecation_with_filter(self):
251
"""don't suppress if we already have a filter"""
252
warnings.filterwarnings('error', category=DeprecationWarning)
253
self.assertFirstWarning('error', DeprecationWarning)
254
self.assertEqual(1, len(warnings.filters))
255
symbol_versioning.suppress_deprecation_warnings(override=False)
256
self.assertFirstWarning('error', DeprecationWarning)
257
self.assertEqual(1, len(warnings.filters))
258
symbol_versioning.suppress_deprecation_warnings(override=True)
259
self.assertFirstWarning('ignore', DeprecationWarning)
260
self.assertEqual(2, len(warnings.filters))
262
def test_activate_deprecation_no_error(self):
263
# First nuke the filters, so we know it is clean
264
symbol_versioning.activate_deprecation_warnings()
265
self.assertFirstWarning('default', DeprecationWarning)
267
def test_activate_deprecation_with_error(self):
268
# First nuke the filters, so we know it is clean
269
# Add a warning == error rule
270
warnings.filterwarnings('error', category=Warning)
271
self.assertFirstWarning('error', Warning)
272
self.assertEqual(1, len(warnings.filters))
273
symbol_versioning.activate_deprecation_warnings(override=False)
274
# There should not be a new warning
275
self.assertFirstWarning('error', Warning)
276
self.assertEqual(1, len(warnings.filters))
278
def test_activate_deprecation_with_DW_error(self):
279
# First nuke the filters, so we know it is clean
280
# Add a warning == error rule
281
warnings.filterwarnings('error', category=DeprecationWarning)
282
self.assertFirstWarning('error', DeprecationWarning)
283
self.assertEqual(1, len(warnings.filters))
284
symbol_versioning.activate_deprecation_warnings(override=False)
285
# There should not be a new warning
286
self.assertFirstWarning('error', DeprecationWarning)
287
self.assertEqual(1, len(warnings.filters))
288
symbol_versioning.activate_deprecation_warnings(override=True)
289
self.assertFirstWarning('default', DeprecationWarning)
290
self.assertEqual(2, len(warnings.filters))
99
symbol_versioning.deprecated_nonce))