/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2006-2011 Canonical Ltd
1534.2.1 by Robert Collins
Implement deprecated_method
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1534.2.1 by Robert Collins
Implement deprecated_method
16
17
"""Symbol versioning tests."""
18
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
19
import warnings
20
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
21
from breezy import symbol_versioning
7067.14.1 by Jelmer Vernooij
Fix symbol versioning on Python 3.
22
from breezy.sixish import PY3
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
23
from breezy.symbol_versioning import (
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
24
    deprecated_function,
25
    deprecated_in,
26
    deprecated_method,
27
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
28
from breezy.tests import TestCase
1534.2.1 by Robert Collins
Implement deprecated_method
29
30
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
31
@deprecated_function(deprecated_in((0, 7, 0)))
32
def sample_deprecated_function():
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
33
    """Deprecated function docstring."""
1534.2.2 by Robert Collins
Implement deprecated_function.
34
    return 1
35
1534.2.1 by Robert Collins
Implement deprecated_method
36
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
37
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
38
    'a_deprecated_list', ['one'], extra="Don't use me")
39
40
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
41
a_deprecated_dict = symbol_versioning.DeprecatedDict(
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
42
    deprecated_in((0, 14, 0)),
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
43
    'a_deprecated_dict',
44
    dict(a=42),
45
    advice='Pull the other one!',
46
    )
47
48
1534.2.1 by Robert Collins
Implement deprecated_method
49
class TestDeprecationWarnings(TestCase):
50
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
51
    def capture_warning(self, message, category, stacklevel=None):
52
        self._warnings.append((message, category, stacklevel))
1534.2.1 by Robert Collins
Implement deprecated_method
53
54
    def setUp(self):
55
        super(TestDeprecationWarnings, self).setUp()
56
        self._warnings = []
57
    
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
58
    @deprecated_method(deprecated_in((0, 7, 0)))
1534.2.1 by Robert Collins
Implement deprecated_method
59
    def deprecated_method(self):
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
60
        """Deprecated method docstring.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
61
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
62
        This might explain stuff.
63
        """
1534.2.2 by Robert Collins
Implement deprecated_function.
64
        return 1
1534.2.1 by Robert Collins
Implement deprecated_method
65
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
66
    @staticmethod
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
67
    @deprecated_function(deprecated_in((0, 7, 0)))
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
68
    def deprecated_static():
69
        """Deprecated static."""
70
        return 1
71
72
    def test_deprecated_static(self):
7067.14.1 by Jelmer Vernooij
Fix symbol versioning on Python 3.
73
        if PY3:
74
            expected_warning = (
75
                "breezy.tests.test_symbol_versioning.TestDeprecationWarnings."
76
                "deprecated_static "
77
                "was deprecated in version 0.7.0.", DeprecationWarning, 2)
78
        else:
79
            # XXX: The results are not quite right because the class name is not
80
            # shown on Python 2- however it is enough to give people a good indication of
81
            # where the problem is.
82
            expected_warning = (
83
                "breezy.tests.test_symbol_versioning."
84
                "deprecated_static "
85
                "was deprecated in version 0.7.0.", DeprecationWarning, 2)
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
86
        expected_docstring = (
87
            'Deprecated static.\n'
88
            '\n'
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
89
            'This function was deprecated in version 0.7.0.\n'
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
90
            )
91
        self.check_deprecated_callable(
92
            expected_warning, expected_docstring,
93
            "deprecated_static",
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
94
            "breezy.tests.test_symbol_versioning",
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
95
            self.deprecated_static)
96
1534.2.1 by Robert Collins
Implement deprecated_method
97
    def test_deprecated_method(self):
1534.2.2 by Robert Collins
Implement deprecated_function.
98
        expected_warning = (
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
99
            "breezy.tests.test_symbol_versioning."
1534.2.2 by Robert Collins
Implement deprecated_function.
100
            "TestDeprecationWarnings.deprecated_method "
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
101
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
102
        expected_docstring = (
103
            'Deprecated method docstring.\n'
104
            '\n'
105
            '        This might explain stuff.\n'
106
            '        \n'
107
            '        This method was deprecated in version 0.7.0.\n'
108
            '        ')
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
109
        self.check_deprecated_callable(expected_warning, expected_docstring,
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
110
                                       "deprecated_method",
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
111
                                       "breezy.tests.test_symbol_versioning",
1534.2.2 by Robert Collins
Implement deprecated_function.
112
                                       self.deprecated_method)
113
114
    def test_deprecated_function(self):
115
        expected_warning = (
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
116
            "breezy.tests.test_symbol_versioning.sample_deprecated_function "
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
117
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
118
        expected_docstring = ('Deprecated function docstring.\n'
119
                              '\n'
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
120
                              'This function was deprecated in version 0.7.0.\n'
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
121
                              )
122
        self.check_deprecated_callable(expected_warning, expected_docstring,
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
123
                                       "sample_deprecated_function",
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
124
                                       "breezy.tests.test_symbol_versioning",
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
125
                                       sample_deprecated_function)
1534.2.2 by Robert Collins
Implement deprecated_function.
126
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
127
    def test_deprecated_list(self):
128
        expected_warning = (
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
129
            "Modifying a_deprecated_list was deprecated in version 0.9.0."
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
130
            " Don't use me", DeprecationWarning, 3)
131
        old_warning_method = symbol_versioning.warn
132
        try:
133
            symbol_versioning.set_warning_method(self.capture_warning)
134
            self.assertEqual(['one'], a_deprecated_list)
135
            self.assertEqual([], self._warnings)
136
137
            a_deprecated_list.append('foo')
138
            self.assertEqual([expected_warning], self._warnings)
139
            self.assertEqual(['one', 'foo'], a_deprecated_list)
140
141
            a_deprecated_list.extend(['bar', 'baz'])
142
            self.assertEqual([expected_warning]*2, self._warnings)
143
            self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
144
145
            a_deprecated_list.insert(1, 'xxx')
146
            self.assertEqual([expected_warning]*3, self._warnings)
147
            self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
148
149
            a_deprecated_list.remove('foo')
150
            self.assertEqual([expected_warning]*4, self._warnings)
151
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
152
153
            val = a_deprecated_list.pop()
154
            self.assertEqual([expected_warning]*5, self._warnings)
155
            self.assertEqual('baz', val)
156
            self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
157
158
            val = a_deprecated_list.pop(1)
159
            self.assertEqual([expected_warning]*6, self._warnings)
160
            self.assertEqual('xxx', val)
161
            self.assertEqual(['one', 'bar'], a_deprecated_list)
162
        finally:
163
            symbol_versioning.set_warning_method(old_warning_method)
164
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
165
    def test_deprecated_dict(self):
166
        expected_warning = (
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
167
            "access to a_deprecated_dict was deprecated in version 0.14.0."
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
168
            " Pull the other one!", DeprecationWarning, 2)
169
        old_warning_method = symbol_versioning.warn
170
        try:
171
            symbol_versioning.set_warning_method(self.capture_warning)
172
            self.assertEqual(len(a_deprecated_dict), 1)
173
            self.assertEqual([expected_warning], self._warnings)
174
175
            a_deprecated_dict['b'] = 42
176
            self.assertEqual(a_deprecated_dict['b'], 42)
177
            self.assertTrue('b' in a_deprecated_dict)
178
            del a_deprecated_dict['b']
179
            self.assertFalse('b' in a_deprecated_dict)
180
            self.assertEqual([expected_warning] * 6, self._warnings)
181
        finally:
182
            symbol_versioning.set_warning_method(old_warning_method)
183
184
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
185
    def check_deprecated_callable(self, expected_warning, expected_docstring,
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
186
                                  expected_name, expected_module,
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
187
                                  deprecated_callable):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
188
        if __doc__ is None:
189
            # With -OO the docstring should just be the deprecated version
190
            expected_docstring = expected_docstring.split('\n')[-2].lstrip()
1534.2.1 by Robert Collins
Implement deprecated_method
191
        old_warning_method = symbol_versioning.warn
192
        try:
193
            symbol_versioning.set_warning_method(self.capture_warning)
1534.2.2 by Robert Collins
Implement deprecated_function.
194
            self.assertEqual(1, deprecated_callable())
1534.2.1 by Robert Collins
Implement deprecated_method
195
            self.assertEqual([expected_warning], self._warnings)
1534.2.2 by Robert Collins
Implement deprecated_function.
196
            deprecated_callable()
1534.2.1 by Robert Collins
Implement deprecated_method
197
            self.assertEqual([expected_warning, expected_warning],
198
                             self._warnings)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
199
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
200
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
201
            self.assertEqualDiff(expected_module, deprecated_callable.__module__)
1581.1.1 by Robert Collins
Bugfix aliases to be backwards compatible with plugins providing command.run_argv.
202
            self.assertTrue(deprecated_callable.is_deprecated)
1534.2.1 by Robert Collins
Implement deprecated_method
203
        finally:
204
            symbol_versioning.set_warning_method(old_warning_method)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
205
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
206
    def test_deprecated_passed(self):
207
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
208
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
209
        self.assertEqual(True, symbol_versioning.deprecated_passed(False))
210
        self.assertEqual(False,
211
                         symbol_versioning.deprecated_passed(
1534.4.32 by Robert Collins
Rename deprecated_nonce to DEPRECATED_PARAMETER
212
                            symbol_versioning.DEPRECATED_PARAMETER))
1982.3.1 by Robert Collins
New utility function symbol_versioning.deprecation_string. Returns the
213
214
    def test_deprecation_string(self):
215
        """We can get a deprecation string for a method or function."""
7045.4.23 by Jelmer Vernooij
Fix some help tests.
216
        err_message = symbol_versioning.deprecation_string(
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
217
            self.test_deprecation_string,
7045.4.23 by Jelmer Vernooij
Fix some help tests.
218
            deprecated_in((0, 11, 0)))
7067.14.2 by Jelmer Vernooij
Simplify.
219
        self.assertEqual(err_message,
7045.4.23 by Jelmer Vernooij
Fix some help tests.
220
                 'breezy.tests.test_symbol_versioning.TestDeprecationWarnings.'
221
                 'test_deprecation_string was deprecated in '
222
                 'version 0.11.0.')
223
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
224
        self.assertEqual('breezy.symbol_versioning.deprecated_function was '
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
225
            'deprecated in version 0.11.0.',
1982.3.1 by Robert Collins
New utility function symbol_versioning.deprecation_string. Returns the
226
            symbol_versioning.deprecation_string(
227
                symbol_versioning.deprecated_function,
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
228
                deprecated_in((0, 11, 0))))
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
229
230
231
class TestSuppressAndActivate(TestCase):
232
233
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
234
        super(TestSuppressAndActivate, self).setUp()
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
235
        existing_filters = list(warnings.filters)
236
        def restore():
237
            warnings.filters[:] = existing_filters
238
        self.addCleanup(restore)
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
239
        # Clean out the filters so we have a clean slate.
240
        warnings.resetwarnings()
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
241
242
    def assertFirstWarning(self, action, category):
243
        """Test the first warning in the filters is correct"""
244
        first = warnings.filters[0]
245
        self.assertEqual((action, category), (first[0], first[2]))
246
247
    def test_suppress_deprecation_warnings(self):
248
        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
249
        symbol_versioning.suppress_deprecation_warnings()
250
        self.assertFirstWarning('ignore', DeprecationWarning)
251
5320.2.1 by Robert Collins
The symbol_versioning module can now cleanup after itself -
252
    def test_set_restore_filters(self):
253
        original_filters = warnings.filters[:]
254
        symbol_versioning.suppress_deprecation_warnings()()
255
        self.assertEqual(original_filters, warnings.filters)
256
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
257
    def test_suppress_deprecation_with_warning_filter(self):
258
        """don't suppress if we already have a filter"""
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
259
        warnings.filterwarnings('error', category=Warning)
260
        self.assertFirstWarning('error', Warning)
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
261
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
262
        symbol_versioning.suppress_deprecation_warnings(override=False)
263
        self.assertFirstWarning('error', Warning)
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
264
        self.assertEqual(1, len(warnings.filters))
265
266
    def test_suppress_deprecation_with_filter(self):
267
        """don't suppress if we already have a filter"""
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
268
        warnings.filterwarnings('error', category=DeprecationWarning)
269
        self.assertFirstWarning('error', DeprecationWarning)
270
        self.assertEqual(1, len(warnings.filters))
271
        symbol_versioning.suppress_deprecation_warnings(override=False)
272
        self.assertFirstWarning('error', DeprecationWarning)
273
        self.assertEqual(1, len(warnings.filters))
274
        symbol_versioning.suppress_deprecation_warnings(override=True)
275
        self.assertFirstWarning('ignore', DeprecationWarning)
276
        self.assertEqual(2, len(warnings.filters))
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
277
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
278
    def test_activate_deprecation_no_error(self):
279
        # First nuke the filters, so we know it is clean
3427.5.6 by John Arbash Meinel
remove the parameter from activate, and just have it always False
280
        symbol_versioning.activate_deprecation_warnings()
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
281
        self.assertFirstWarning('default', DeprecationWarning)
282
283
    def test_activate_deprecation_with_error(self):
284
        # First nuke the filters, so we know it is clean
285
        # Add a warning == error rule
286
        warnings.filterwarnings('error', category=Warning)
287
        self.assertFirstWarning('error', Warning)
288
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
289
        symbol_versioning.activate_deprecation_warnings(override=False)
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
290
        # There should not be a new warning
291
        self.assertFirstWarning('error', Warning)
292
        self.assertEqual(1, len(warnings.filters))
293
294
    def test_activate_deprecation_with_DW_error(self):
295
        # First nuke the filters, so we know it is clean
296
        # Add a warning == error rule
297
        warnings.filterwarnings('error', category=DeprecationWarning)
298
        self.assertFirstWarning('error', DeprecationWarning)
299
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
300
        symbol_versioning.activate_deprecation_warnings(override=False)
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
301
        # There should not be a new warning
302
        self.assertFirstWarning('error', DeprecationWarning)
303
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
304
        symbol_versioning.activate_deprecation_warnings(override=True)
305
        self.assertFirstWarning('default', DeprecationWarning)
306
        self.assertEqual(2, len(warnings.filters))