~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Copyright (C) 2006, 2007 Canonical Ltd
#   Authors: Robert Collins <robert.collins@canonical.com>
#   and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Symbol versioning tests."""

import warnings

from bzrlib import symbol_versioning
from bzrlib.tests import TestCase


@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
def deprecated_function():
    """Deprecated function docstring."""
    return 1


a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
    'a_deprecated_list', ['one'], extra="Don't use me")


a_deprecated_dict = symbol_versioning.DeprecatedDict(
    symbol_versioning.zero_fourteen,
    'a_deprecated_dict',
    dict(a=42),
    advice='Pull the other one!',
    )


class TestDeprecationWarnings(TestCase):

    def capture_warning(self, message, category, stacklevel=None):
        self._warnings.append((message, category, stacklevel))

    def setUp(self):
        super(TestDeprecationWarnings, self).setUp()
        self._warnings = []

    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
    def deprecated_method(self):
        """Deprecated method docstring.

        This might explain stuff.
        """
        return 1

    @staticmethod
    @symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
    def deprecated_static():
        """Deprecated static."""
        return 1

    def test_deprecated_static(self):
        # XXX: The results are not quite right because the class name is not
        # shown - however it is enough to give people a good indication of
        # where the problem is.
        expected_warning = (
            "bzrlib.tests.test_symbol_versioning."
            "deprecated_static "
            "was deprecated in version 0.7.", DeprecationWarning, 2)
        expected_docstring = (
            'Deprecated static.\n'
            '\n'
            'This function was deprecated in version 0.7.\n'
            )
        self.check_deprecated_callable(
            expected_warning, expected_docstring,
            "deprecated_static",
            "bzrlib.tests.test_symbol_versioning",
            self.deprecated_static)

    def test_deprecated_method(self):
        expected_warning = (
            "bzrlib.tests.test_symbol_versioning."
            "TestDeprecationWarnings.deprecated_method "
            "was deprecated in version 0.7.", DeprecationWarning, 2)
        expected_docstring = ('Deprecated method docstring.\n'
                              '\n'
                              '        This might explain stuff.\n'
                              '        \n'
                              '        This method was deprecated in version 0.7.\n'
                              '        ')
        self.check_deprecated_callable(expected_warning, expected_docstring,
                                       "deprecated_method",
                                       "bzrlib.tests.test_symbol_versioning",
                                       self.deprecated_method)

    def test_deprecated_function(self):
        expected_warning = (
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
            "was deprecated in version 0.7.", DeprecationWarning, 2)
        expected_docstring = ('Deprecated function docstring.\n'
                              '\n'
                              'This function was deprecated in version 0.7.\n'
                              )
        self.check_deprecated_callable(expected_warning, expected_docstring,
                                       "deprecated_function",
                                       "bzrlib.tests.test_symbol_versioning",
                                       deprecated_function)

    def test_deprecated_list(self):
        expected_warning = (
            "Modifying a_deprecated_list was deprecated in version 0.9."
            " Don't use me", DeprecationWarning, 3)
        old_warning_method = symbol_versioning.warn
        try:
            symbol_versioning.set_warning_method(self.capture_warning)
            self.assertEqual(['one'], a_deprecated_list)
            self.assertEqual([], self._warnings)

            a_deprecated_list.append('foo')
            self.assertEqual([expected_warning], self._warnings)
            self.assertEqual(['one', 'foo'], a_deprecated_list)

            a_deprecated_list.extend(['bar', 'baz'])
            self.assertEqual([expected_warning]*2, self._warnings)
            self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)

            a_deprecated_list.insert(1, 'xxx')
            self.assertEqual([expected_warning]*3, self._warnings)
            self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)

            a_deprecated_list.remove('foo')
            self.assertEqual([expected_warning]*4, self._warnings)
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)

            val = a_deprecated_list.pop()
            self.assertEqual([expected_warning]*5, self._warnings)
            self.assertEqual('baz', val)
            self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)

            val = a_deprecated_list.pop(1)
            self.assertEqual([expected_warning]*6, self._warnings)
            self.assertEqual('xxx', val)
            self.assertEqual(['one', 'bar'], a_deprecated_list)
        finally:
            symbol_versioning.set_warning_method(old_warning_method)

    def test_deprecated_dict(self):
        expected_warning = (
            "access to a_deprecated_dict was deprecated in version 0.14."
            " Pull the other one!", DeprecationWarning, 2)
        old_warning_method = symbol_versioning.warn
        try:
            symbol_versioning.set_warning_method(self.capture_warning)
            self.assertEqual(len(a_deprecated_dict), 1)
            self.assertEqual([expected_warning], self._warnings)

            a_deprecated_dict['b'] = 42
            self.assertEqual(a_deprecated_dict['b'], 42)
            self.assertTrue('b' in a_deprecated_dict)
            del a_deprecated_dict['b']
            self.assertFalse('b' in a_deprecated_dict)
            self.assertEqual([expected_warning] * 6, self._warnings)
        finally:
            symbol_versioning.set_warning_method(old_warning_method)


    def check_deprecated_callable(self, expected_warning, expected_docstring,
                                  expected_name, expected_module,
                                  deprecated_callable):
        old_warning_method = symbol_versioning.warn
        try:
            symbol_versioning.set_warning_method(self.capture_warning)
            self.assertEqual(1, deprecated_callable())
            self.assertEqual([expected_warning], self._warnings)
            deprecated_callable()
            self.assertEqual([expected_warning, expected_warning],
                             self._warnings)
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
            self.assertEqualDiff(expected_module, deprecated_callable.__module__)
            self.assertTrue(deprecated_callable.is_deprecated)
        finally:
            symbol_versioning.set_warning_method(old_warning_method)

    def test_deprecated_passed(self):
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
        self.assertEqual(True, symbol_versioning.deprecated_passed(False))
        self.assertEqual(False,
                         symbol_versioning.deprecated_passed(
                            symbol_versioning.DEPRECATED_PARAMETER))

    def test_deprecation_string(self):
        """We can get a deprecation string for a method or function."""
        self.assertEqual('bzrlib.tests.test_symbol_versioning.'
            'TestDeprecationWarnings.test_deprecation_string was deprecated in '
            'version 0.11.',
            symbol_versioning.deprecation_string(
            self.test_deprecation_string, symbol_versioning.zero_eleven))
        self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
            'deprecated in version 0.11.',
            symbol_versioning.deprecation_string(
                symbol_versioning.deprecated_function,
                symbol_versioning.zero_eleven))


class TestSuppressAndActivate(TestCase):

    def setUp(self):
        TestCase.setUp(self)
        existing_filters = list(warnings.filters)
        def restore():
            warnings.filters[:] = existing_filters
        self.addCleanup(restore)
        # Clean out the filters so we have a clean slate.
        warnings.resetwarnings()

    def assertFirstWarning(self, action, category):
        """Test the first warning in the filters is correct"""
        first = warnings.filters[0]
        self.assertEqual((action, category), (first[0], first[2]))

    def test_suppress_deprecation_warnings(self):
        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
        symbol_versioning.suppress_deprecation_warnings()
        self.assertFirstWarning('ignore', DeprecationWarning)

    def test_suppress_deprecation_with_warning_filter(self):
        """don't suppress if we already have a filter"""
        warnings.filterwarnings('error', category=Warning)
        self.assertFirstWarning('error', Warning)
        self.assertEqual(1, len(warnings.filters))
        symbol_versioning.suppress_deprecation_warnings(override=False)
        self.assertFirstWarning('error', Warning)
        self.assertEqual(1, len(warnings.filters))

    def test_suppress_deprecation_with_filter(self):
        """don't suppress if we already have a filter"""
        warnings.filterwarnings('error', category=DeprecationWarning)
        self.assertFirstWarning('error', DeprecationWarning)
        self.assertEqual(1, len(warnings.filters))
        symbol_versioning.suppress_deprecation_warnings(override=False)
        self.assertFirstWarning('error', DeprecationWarning)
        self.assertEqual(1, len(warnings.filters))
        symbol_versioning.suppress_deprecation_warnings(override=True)
        self.assertFirstWarning('ignore', DeprecationWarning)
        self.assertEqual(2, len(warnings.filters))

    def test_activate_deprecation_no_error(self):
        # First nuke the filters, so we know it is clean
        symbol_versioning.activate_deprecation_warnings()
        self.assertFirstWarning('default', DeprecationWarning)

    def test_activate_deprecation_with_error(self):
        # First nuke the filters, so we know it is clean
        # Add a warning == error rule
        warnings.filterwarnings('error', category=Warning)
        self.assertFirstWarning('error', Warning)
        self.assertEqual(1, len(warnings.filters))
        symbol_versioning.activate_deprecation_warnings(override=False)
        # There should not be a new warning
        self.assertFirstWarning('error', Warning)
        self.assertEqual(1, len(warnings.filters))

    def test_activate_deprecation_with_DW_error(self):
        # First nuke the filters, so we know it is clean
        # Add a warning == error rule
        warnings.filterwarnings('error', category=DeprecationWarning)
        self.assertFirstWarning('error', DeprecationWarning)
        self.assertEqual(1, len(warnings.filters))
        symbol_versioning.activate_deprecation_warnings(override=False)
        # There should not be a new warning
        self.assertFirstWarning('error', DeprecationWarning)
        self.assertEqual(1, len(warnings.filters))
        symbol_versioning.activate_deprecation_warnings(override=True)
        self.assertFirstWarning('default', DeprecationWarning)
        self.assertEqual(2, len(warnings.filters))