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)),
30
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
38
31
'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
34
class TestDeprecationWarnings(TestCase):
51
36
def capture_warning(self, message, category, stacklevel=None):
55
40
super(TestDeprecationWarnings, self).setUp()
56
41
self._warnings = []
58
@deprecated_method(deprecated_in((0, 7, 0)))
43
@symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
59
44
def deprecated_method(self):
60
45
"""Deprecated method docstring.
62
47
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
51
def test_deprecated_method(self):
92
52
expected_warning = (
93
53
"bzrlib.tests.test_symbol_versioning."
94
54
"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'
55
"was deprecated in version 0.7.", DeprecationWarning, 2)
56
expected_docstring = ('Deprecated method docstring.\n'
58
' This might explain stuff.\n'
60
' This method was deprecated in version 0.7.\n'
103
62
self.check_deprecated_callable(expected_warning, expected_docstring,
104
63
"deprecated_method",
105
64
"bzrlib.tests.test_symbol_versioning",
108
67
def test_deprecated_function(self):
109
68
expected_warning = (
110
"bzrlib.tests.test_symbol_versioning.sample_deprecated_function "
111
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
69
"bzrlib.tests.test_symbol_versioning.deprecated_function "
70
"was deprecated in version 0.7.", DeprecationWarning, 2)
112
71
expected_docstring = ('Deprecated function docstring.\n'
114
'This function was deprecated in version 0.7.0.\n'
73
'This function was deprecated in version 0.7.\n'
116
75
self.check_deprecated_callable(expected_warning, expected_docstring,
117
"sample_deprecated_function",
76
"deprecated_function",
118
77
"bzrlib.tests.test_symbol_versioning",
119
sample_deprecated_function)
121
80
def test_deprecated_list(self):
122
81
expected_warning = (
123
"Modifying a_deprecated_list was deprecated in version 0.9.0."
82
"Modifying a_deprecated_list was deprecated in version 0.9."
124
83
" Don't use me", DeprecationWarning, 3)
84
expected_doctstring = ('appending to a_deprecated_list is deprecated')
125
86
old_warning_method = symbol_versioning.warn
127
88
symbol_versioning.set_warning_method(self.capture_warning)
157
118
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
120
def check_deprecated_callable(self, expected_warning, expected_docstring,
180
121
expected_name, expected_module,
181
122
deprecated_callable):
183
# With -OO the docstring should just be the deprecated version
184
expected_docstring = expected_docstring.split('\n')[-2].lstrip()
185
123
old_warning_method = symbol_versioning.warn
187
125
symbol_versioning.set_warning_method(self.capture_warning)
209
147
"""We can get a deprecation string for a method or function."""
210
148
self.assertEqual('bzrlib.tests.test_symbol_versioning.'
211
149
'TestDeprecationWarnings.test_deprecation_string was deprecated in '
213
151
symbol_versioning.deprecation_string(
214
self.test_deprecation_string,
215
deprecated_in((0, 11, 0))))
152
self.test_deprecation_string, symbol_versioning.zero_eleven))
216
153
self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
217
'deprecated in version 0.11.0.',
154
'deprecated in version 0.11.',
218
155
symbol_versioning.deprecation_string(
219
156
symbol_versioning.deprecated_function,
220
deprecated_in((0, 11, 0))))
223
class TestSuppressAndActivate(TestCase):
227
existing_filters = list(warnings.filters)
229
warnings.filters[:] = existing_filters
230
self.addCleanup(restore)
231
# Clean out the filters so we have a clean slate.
232
warnings.resetwarnings()
234
def assertFirstWarning(self, action, category):
235
"""Test the first warning in the filters is correct"""
236
first = warnings.filters[0]
237
self.assertEqual((action, category), (first[0], first[2]))
239
def test_suppress_deprecation_warnings(self):
240
"""suppress_deprecation_warnings sets DeprecationWarning to ignored."""
241
symbol_versioning.suppress_deprecation_warnings()
242
self.assertFirstWarning('ignore', DeprecationWarning)
244
def test_set_restore_filters(self):
245
original_filters = warnings.filters[:]
246
symbol_versioning.suppress_deprecation_warnings()()
247
self.assertEqual(original_filters, warnings.filters)
249
def test_suppress_deprecation_with_warning_filter(self):
250
"""don't suppress if we already have a filter"""
251
warnings.filterwarnings('error', category=Warning)
252
self.assertFirstWarning('error', Warning)
253
self.assertEqual(1, len(warnings.filters))
254
symbol_versioning.suppress_deprecation_warnings(override=False)
255
self.assertFirstWarning('error', Warning)
256
self.assertEqual(1, len(warnings.filters))
258
def test_suppress_deprecation_with_filter(self):
259
"""don't suppress if we already have a filter"""
260
warnings.filterwarnings('error', category=DeprecationWarning)
261
self.assertFirstWarning('error', DeprecationWarning)
262
self.assertEqual(1, len(warnings.filters))
263
symbol_versioning.suppress_deprecation_warnings(override=False)
264
self.assertFirstWarning('error', DeprecationWarning)
265
self.assertEqual(1, len(warnings.filters))
266
symbol_versioning.suppress_deprecation_warnings(override=True)
267
self.assertFirstWarning('ignore', DeprecationWarning)
268
self.assertEqual(2, len(warnings.filters))
270
def test_activate_deprecation_no_error(self):
271
# First nuke the filters, so we know it is clean
272
symbol_versioning.activate_deprecation_warnings()
273
self.assertFirstWarning('default', DeprecationWarning)
275
def test_activate_deprecation_with_error(self):
276
# First nuke the filters, so we know it is clean
277
# Add a warning == error rule
278
warnings.filterwarnings('error', category=Warning)
279
self.assertFirstWarning('error', Warning)
280
self.assertEqual(1, len(warnings.filters))
281
symbol_versioning.activate_deprecation_warnings(override=False)
282
# There should not be a new warning
283
self.assertFirstWarning('error', Warning)
284
self.assertEqual(1, len(warnings.filters))
286
def test_activate_deprecation_with_DW_error(self):
287
# First nuke the filters, so we know it is clean
288
# Add a warning == error rule
289
warnings.filterwarnings('error', category=DeprecationWarning)
290
self.assertFirstWarning('error', DeprecationWarning)
291
self.assertEqual(1, len(warnings.filters))
292
symbol_versioning.activate_deprecation_warnings(override=False)
293
# There should not be a new warning
294
self.assertFirstWarning('error', DeprecationWarning)
295
self.assertEqual(1, len(warnings.filters))
296
symbol_versioning.activate_deprecation_warnings(override=True)
297
self.assertFirstWarning('default', DeprecationWarning)
298
self.assertEqual(2, len(warnings.filters))
157
symbol_versioning.zero_eleven))