14
13
# You should have received a copy of the GNU General Public License
15
14
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
17
"""Symbol versioning tests."""
20
import bzrlib.symbol_versioning as symbol_versioning
21
from bzrlib import symbol_versioning
22
from bzrlib.symbol_versioning import (
21
27
from bzrlib.tests import TestCase
24
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
25
def deprecated_function():
30
@deprecated_function(deprecated_in((0, 7, 0)))
31
def sample_deprecated_function():
26
32
"""Deprecated function docstring."""
30
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
36
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
31
37
'a_deprecated_list', ['one'], extra="Don't use me")
34
40
a_deprecated_dict = symbol_versioning.DeprecatedDict(
35
symbol_versioning.zero_fourteen,
41
deprecated_in((0, 14, 0)),
36
42
'a_deprecated_dict',
38
44
advice='Pull the other one!',
48
54
super(TestDeprecationWarnings, self).setUp()
49
55
self._warnings = []
51
@symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
57
@deprecated_method(deprecated_in((0, 7, 0)))
52
58
def deprecated_method(self):
53
59
"""Deprecated method docstring.
55
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):
72
# XXX: The results are not quite right because the class name is not
73
# shown - however it is enough to give people a good indication of
74
# where the problem is.
76
"bzrlib.tests.test_symbol_versioning."
78
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
79
expected_docstring = (
80
'Deprecated static.\n'
82
'This function was deprecated in version 0.7.0.\n'
84
self.check_deprecated_callable(
85
expected_warning, expected_docstring,
87
"bzrlib.tests.test_symbol_versioning",
88
self.deprecated_static)
59
90
def test_deprecated_method(self):
60
91
expected_warning = (
61
92
"bzrlib.tests.test_symbol_versioning."
62
93
"TestDeprecationWarnings.deprecated_method "
63
"was deprecated in version 0.7.", DeprecationWarning, 2)
64
expected_docstring = ('Deprecated method docstring.\n'
66
' This might explain stuff.\n'
68
' This method was deprecated in version 0.7.\n'
94
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
95
expected_docstring = (
96
'Deprecated method docstring.\n'
98
' This might explain stuff.\n'
100
' This method was deprecated in version 0.7.0.\n'
70
102
self.check_deprecated_callable(expected_warning, expected_docstring,
71
103
"deprecated_method",
72
104
"bzrlib.tests.test_symbol_versioning",
75
107
def test_deprecated_function(self):
76
108
expected_warning = (
77
"bzrlib.tests.test_symbol_versioning.deprecated_function "
78
"was deprecated in version 0.7.", DeprecationWarning, 2)
109
"bzrlib.tests.test_symbol_versioning.sample_deprecated_function "
110
"was deprecated in version 0.7.0.", DeprecationWarning, 2)
79
111
expected_docstring = ('Deprecated function docstring.\n'
81
'This function was deprecated in version 0.7.\n'
113
'This function was deprecated in version 0.7.0.\n'
83
115
self.check_deprecated_callable(expected_warning, expected_docstring,
84
"deprecated_function",
116
"sample_deprecated_function",
85
117
"bzrlib.tests.test_symbol_versioning",
118
sample_deprecated_function)
88
120
def test_deprecated_list(self):
89
121
expected_warning = (
90
"Modifying a_deprecated_list was deprecated in version 0.9."
122
"Modifying a_deprecated_list was deprecated in version 0.9.0."
91
123
" Don't use me", DeprecationWarning, 3)
92
124
old_warning_method = symbol_versioning.warn
173
208
"""We can get a deprecation string for a method or function."""
174
209
self.assertEqual('bzrlib.tests.test_symbol_versioning.'
175
210
'TestDeprecationWarnings.test_deprecation_string was deprecated in '
177
212
symbol_versioning.deprecation_string(
178
self.test_deprecation_string, symbol_versioning.zero_eleven))
213
self.test_deprecation_string,
214
deprecated_in((0, 11, 0))))
179
215
self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
180
'deprecated in version 0.11.',
216
'deprecated in version 0.11.0.',
181
217
symbol_versioning.deprecation_string(
182
218
symbol_versioning.deprecated_function,
183
symbol_versioning.zero_eleven))
219
deprecated_in((0, 11, 0))))
222
class TestSuppressAndActivate(TestCase):
225
super(TestSuppressAndActivate, self).setUp()
226
existing_filters = list(warnings.filters)
228
warnings.filters[:] = existing_filters
229
self.addCleanup(restore)
230
# Clean out the filters so we have a clean slate.
231
warnings.resetwarnings()
233
def assertFirstWarning(self, action, category):
234
"""Test the first warning in the filters is correct"""
235
first = warnings.filters[0]
236
self.assertEqual((action, category), (first[0], first[2]))
238
def test_suppress_deprecation_warnings(self):
239
"""suppress_deprecation_warnings sets DeprecationWarning to ignored."""
240
symbol_versioning.suppress_deprecation_warnings()
241
self.assertFirstWarning('ignore', DeprecationWarning)
243
def test_set_restore_filters(self):
244
original_filters = warnings.filters[:]
245
symbol_versioning.suppress_deprecation_warnings()()
246
self.assertEqual(original_filters, warnings.filters)
248
def test_suppress_deprecation_with_warning_filter(self):
249
"""don't suppress if we already have a filter"""
250
warnings.filterwarnings('error', category=Warning)
251
self.assertFirstWarning('error', Warning)
252
self.assertEqual(1, len(warnings.filters))
253
symbol_versioning.suppress_deprecation_warnings(override=False)
254
self.assertFirstWarning('error', Warning)
255
self.assertEqual(1, len(warnings.filters))
257
def test_suppress_deprecation_with_filter(self):
258
"""don't suppress if we already have a filter"""
259
warnings.filterwarnings('error', category=DeprecationWarning)
260
self.assertFirstWarning('error', DeprecationWarning)
261
self.assertEqual(1, len(warnings.filters))
262
symbol_versioning.suppress_deprecation_warnings(override=False)
263
self.assertFirstWarning('error', DeprecationWarning)
264
self.assertEqual(1, len(warnings.filters))
265
symbol_versioning.suppress_deprecation_warnings(override=True)
266
self.assertFirstWarning('ignore', DeprecationWarning)
267
self.assertEqual(2, len(warnings.filters))
269
def test_activate_deprecation_no_error(self):
270
# First nuke the filters, so we know it is clean
271
symbol_versioning.activate_deprecation_warnings()
272
self.assertFirstWarning('default', DeprecationWarning)
274
def test_activate_deprecation_with_error(self):
275
# First nuke the filters, so we know it is clean
276
# Add a warning == error rule
277
warnings.filterwarnings('error', category=Warning)
278
self.assertFirstWarning('error', Warning)
279
self.assertEqual(1, len(warnings.filters))
280
symbol_versioning.activate_deprecation_warnings(override=False)
281
# There should not be a new warning
282
self.assertFirstWarning('error', Warning)
283
self.assertEqual(1, len(warnings.filters))
285
def test_activate_deprecation_with_DW_error(self):
286
# First nuke the filters, so we know it is clean
287
# Add a warning == error rule
288
warnings.filterwarnings('error', category=DeprecationWarning)
289
self.assertFirstWarning('error', DeprecationWarning)
290
self.assertEqual(1, len(warnings.filters))
291
symbol_versioning.activate_deprecation_warnings(override=False)
292
# There should not be a new warning
293
self.assertFirstWarning('error', DeprecationWarning)
294
self.assertEqual(1, len(warnings.filters))
295
symbol_versioning.activate_deprecation_warnings(override=True)
296
self.assertFirstWarning('default', DeprecationWarning)
297
self.assertEqual(2, len(warnings.filters))