~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: John Arbash Meinel
  • Date: 2008-09-26 22:14:42 UTC
  • mto: This revision was merged to the branch mainline in revision 3747.
  • Revision ID: john@arbash-meinel.com-20080926221442-3r67j99sr9rwe9w0
Make message optional, don't check the memory flag directly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#   and others
3
4
#
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
17
18
 
18
19
"""Symbol versioning tests."""
19
20
 
20
 
import bzrlib.symbol_versioning as symbol_versioning
 
21
import warnings
 
22
 
 
23
from bzrlib import symbol_versioning
21
24
from bzrlib.tests import TestCase
22
25
 
23
26
 
56
59
        """
57
60
        return 1
58
61
 
 
62
    @staticmethod
 
63
    @symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
 
64
    def deprecated_static():
 
65
        """Deprecated static."""
 
66
        return 1
 
67
 
 
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.
 
72
        expected_warning = (
 
73
            "bzrlib.tests.test_symbol_versioning."
 
74
            "deprecated_static "
 
75
            "was deprecated in version 0.7.", DeprecationWarning, 2)
 
76
        expected_docstring = (
 
77
            'Deprecated static.\n'
 
78
            '\n'
 
79
            'This function was deprecated in version 0.7.\n'
 
80
            )
 
81
        self.check_deprecated_callable(
 
82
            expected_warning, expected_docstring,
 
83
            "deprecated_static",
 
84
            "bzrlib.tests.test_symbol_versioning",
 
85
            self.deprecated_static)
 
86
 
59
87
    def test_deprecated_method(self):
60
88
        expected_warning = (
61
89
            "bzrlib.tests.test_symbol_versioning."
181
209
            symbol_versioning.deprecation_string(
182
210
                symbol_versioning.deprecated_function,
183
211
                symbol_versioning.zero_eleven))
 
212
 
 
213
 
 
214
class TestSuppressAndActivate(TestCase):
 
215
 
 
216
    def setUp(self):
 
217
        existing_filters = list(warnings.filters)
 
218
        def restore():
 
219
            warnings.filters[:] = existing_filters
 
220
        self.addCleanup(restore)
 
221
        # Clean out the filters so we have a clean slate.
 
222
        warnings.resetwarnings()
 
223
 
 
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]))
 
228
 
 
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)
 
233
 
 
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))
 
242
 
 
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))
 
254
 
 
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)
 
259
 
 
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))
 
270
 
 
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))