/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
22
from breezy.symbol_versioning import (
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
23
    deprecated_function,
24
    deprecated_in,
25
    deprecated_method,
26
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
from breezy.tests import TestCase
1534.2.1 by Robert Collins
Implement deprecated_method
28
29
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
30
@deprecated_function(deprecated_in((0, 7, 0)))
31
def sample_deprecated_function():
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
32
    """Deprecated function docstring."""
1534.2.2 by Robert Collins
Implement deprecated_function.
33
    return 1
34
1534.2.1 by Robert Collins
Implement deprecated_method
35
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
36
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
37
                                                      'a_deprecated_list', ['one'], extra="Don't use me")
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
39
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
40
a_deprecated_dict = symbol_versioning.DeprecatedDict(
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
41
    deprecated_in((0, 14, 0)),
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
42
    'a_deprecated_dict',
43
    dict(a=42),
44
    advice='Pull the other one!',
45
    )
46
47
1534.2.1 by Robert Collins
Implement deprecated_method
48
class TestDeprecationWarnings(TestCase):
49
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
50
    def capture_warning(self, message, category, stacklevel=None):
51
        self._warnings.append((message, category, stacklevel))
1534.2.1 by Robert Collins
Implement deprecated_method
52
53
    def setUp(self):
54
        super(TestDeprecationWarnings, self).setUp()
55
        self._warnings = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
56
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
57
    @deprecated_method(deprecated_in((0, 7, 0)))
1534.2.1 by Robert Collins
Implement deprecated_method
58
    def deprecated_method(self):
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
59
        """Deprecated method docstring.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
60
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
61
        This might explain stuff.
62
        """
1534.2.2 by Robert Collins
Implement deprecated_function.
63
        return 1
1534.2.1 by Robert Collins
Implement deprecated_method
64
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
65
    @staticmethod
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
66
    @deprecated_function(deprecated_in((0, 7, 0)))
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
67
    def deprecated_static():
68
        """Deprecated static."""
69
        return 1
70
71
    def test_deprecated_static(self):
7479.2.1 by Jelmer Vernooij
Drop python2 support.
72
        expected_warning = (
73
            "breezy.tests.test_symbol_versioning.TestDeprecationWarnings."
74
            "deprecated_static "
75
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
76
        expected_docstring = (
77
            'Deprecated static.\n'
78
            '\n'
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
79
            'This function was deprecated in version 0.7.0.\n'
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
80
            )
81
        self.check_deprecated_callable(
82
            expected_warning, expected_docstring,
83
            "deprecated_static",
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
84
            "breezy.tests.test_symbol_versioning",
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
85
            self.deprecated_static)
86
1534.2.1 by Robert Collins
Implement deprecated_method
87
    def test_deprecated_method(self):
1534.2.2 by Robert Collins
Implement deprecated_function.
88
        expected_warning = (
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
89
            "breezy.tests.test_symbol_versioning."
1534.2.2 by Robert Collins
Implement deprecated_function.
90
            "TestDeprecationWarnings.deprecated_method "
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
91
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
92
        expected_docstring = (
93
            'Deprecated method docstring.\n'
94
            '\n'
95
            '        This might explain stuff.\n'
96
            '        \n'
97
            '        This method was deprecated in version 0.7.0.\n'
98
            '        ')
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
99
        self.check_deprecated_callable(expected_warning, expected_docstring,
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
100
                                       "deprecated_method",
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
101
                                       "breezy.tests.test_symbol_versioning",
1534.2.2 by Robert Collins
Implement deprecated_function.
102
                                       self.deprecated_method)
103
104
    def test_deprecated_function(self):
105
        expected_warning = (
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
106
            "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.
107
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
108
        expected_docstring = ('Deprecated function docstring.\n'
109
                              '\n'
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
110
                              'This function was deprecated in version 0.7.0.\n'
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
111
                              )
112
        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
113
                                       "sample_deprecated_function",
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
114
                                       "breezy.tests.test_symbol_versioning",
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
115
                                       sample_deprecated_function)
1534.2.2 by Robert Collins
Implement deprecated_function.
116
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.
117
    def test_deprecated_list(self):
118
        expected_warning = (
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
119
            "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.
120
            " Don't use me", DeprecationWarning, 3)
121
        old_warning_method = symbol_versioning.warn
122
        try:
123
            symbol_versioning.set_warning_method(self.capture_warning)
124
            self.assertEqual(['one'], a_deprecated_list)
125
            self.assertEqual([], self._warnings)
126
127
            a_deprecated_list.append('foo')
128
            self.assertEqual([expected_warning], self._warnings)
129
            self.assertEqual(['one', 'foo'], a_deprecated_list)
130
131
            a_deprecated_list.extend(['bar', 'baz'])
7143.15.2 by Jelmer Vernooij
Run autopep8.
132
            self.assertEqual([expected_warning] * 2, self._warnings)
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.
133
            self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
134
135
            a_deprecated_list.insert(1, 'xxx')
7143.15.2 by Jelmer Vernooij
Run autopep8.
136
            self.assertEqual([expected_warning] * 3, self._warnings)
137
            self.assertEqual(
138
                ['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
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.
139
140
            a_deprecated_list.remove('foo')
7143.15.2 by Jelmer Vernooij
Run autopep8.
141
            self.assertEqual([expected_warning] * 4, self._warnings)
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.
142
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
143
144
            val = a_deprecated_list.pop()
7143.15.2 by Jelmer Vernooij
Run autopep8.
145
            self.assertEqual([expected_warning] * 5, self._warnings)
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.
146
            self.assertEqual('baz', val)
147
            self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
148
149
            val = a_deprecated_list.pop(1)
7143.15.2 by Jelmer Vernooij
Run autopep8.
150
            self.assertEqual([expected_warning] * 6, self._warnings)
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.
151
            self.assertEqual('xxx', val)
152
            self.assertEqual(['one', 'bar'], a_deprecated_list)
153
        finally:
154
            symbol_versioning.set_warning_method(old_warning_method)
155
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
156
    def test_deprecated_dict(self):
157
        expected_warning = (
4634.50.7 by John Arbash Meinel
Fix the symbol versioning output now that we use the wide form.
158
            "access to a_deprecated_dict was deprecated in version 0.14.0."
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
159
            " Pull the other one!", DeprecationWarning, 2)
160
        old_warning_method = symbol_versioning.warn
161
        try:
162
            symbol_versioning.set_warning_method(self.capture_warning)
163
            self.assertEqual(len(a_deprecated_dict), 1)
164
            self.assertEqual([expected_warning], self._warnings)
165
166
            a_deprecated_dict['b'] = 42
167
            self.assertEqual(a_deprecated_dict['b'], 42)
168
            self.assertTrue('b' in a_deprecated_dict)
169
            del a_deprecated_dict['b']
170
            self.assertFalse('b' in a_deprecated_dict)
171
            self.assertEqual([expected_warning] * 6, self._warnings)
172
        finally:
173
            symbol_versioning.set_warning_method(old_warning_method)
174
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
175
    def check_deprecated_callable(self, expected_warning, expected_docstring,
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
176
                                  expected_name, expected_module,
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
177
                                  deprecated_callable):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
178
        if __doc__ is None:
179
            # With -OO the docstring should just be the deprecated version
180
            expected_docstring = expected_docstring.split('\n')[-2].lstrip()
1534.2.1 by Robert Collins
Implement deprecated_method
181
        old_warning_method = symbol_versioning.warn
182
        try:
183
            symbol_versioning.set_warning_method(self.capture_warning)
1534.2.2 by Robert Collins
Implement deprecated_function.
184
            self.assertEqual(1, deprecated_callable())
1534.2.1 by Robert Collins
Implement deprecated_method
185
            self.assertEqual([expected_warning], self._warnings)
1534.2.2 by Robert Collins
Implement deprecated_function.
186
            deprecated_callable()
1534.2.1 by Robert Collins
Implement deprecated_method
187
            self.assertEqual([expected_warning, expected_warning],
188
                             self._warnings)
7143.15.2 by Jelmer Vernooij
Run autopep8.
189
            self.assertEqualDiff(expected_docstring,
190
                                 deprecated_callable.__doc__)
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
191
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
7143.15.2 by Jelmer Vernooij
Run autopep8.
192
            self.assertEqualDiff(
193
                expected_module, deprecated_callable.__module__)
1581.1.1 by Robert Collins
Bugfix aliases to be backwards compatible with plugins providing command.run_argv.
194
            self.assertTrue(deprecated_callable.is_deprecated)
1534.2.1 by Robert Collins
Implement deprecated_method
195
        finally:
196
            symbol_versioning.set_warning_method(old_warning_method)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
197
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
198
    def test_deprecated_passed(self):
199
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
200
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
201
        self.assertEqual(True, symbol_versioning.deprecated_passed(False))
202
        self.assertEqual(False,
203
                         symbol_versioning.deprecated_passed(
7143.15.2 by Jelmer Vernooij
Run autopep8.
204
                             symbol_versioning.DEPRECATED_PARAMETER))
1982.3.1 by Robert Collins
New utility function symbol_versioning.deprecation_string. Returns the
205
206
    def test_deprecation_string(self):
207
        """We can get a deprecation string for a method or function."""
7045.4.23 by Jelmer Vernooij
Fix some help tests.
208
        err_message = symbol_versioning.deprecation_string(
3948.3.1 by Martin Pool
Remove old static deprecation template strings, and update style of their tests
209
            self.test_deprecation_string,
7045.4.23 by Jelmer Vernooij
Fix some help tests.
210
            deprecated_in((0, 11, 0)))
7067.14.2 by Jelmer Vernooij
Simplify.
211
        self.assertEqual(err_message,
7143.15.2 by Jelmer Vernooij
Run autopep8.
212
                         'breezy.tests.test_symbol_versioning.TestDeprecationWarnings.'
213
                         'test_deprecation_string was deprecated in '
214
                         'version 0.11.0.')
7045.4.23 by Jelmer Vernooij
Fix some help tests.
215
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
216
        self.assertEqual('breezy.symbol_versioning.deprecated_function was '
7143.15.2 by Jelmer Vernooij
Run autopep8.
217
                         'deprecated in version 0.11.0.',
218
                         symbol_versioning.deprecation_string(
219
                             symbol_versioning.deprecated_function,
220
                             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.
221
222
223
class TestSuppressAndActivate(TestCase):
224
225
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
226
        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.
227
        existing_filters = list(warnings.filters)
7143.15.2 by Jelmer Vernooij
Run autopep8.
228
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
        def restore():
230
            warnings.filters[:] = existing_filters
231
        self.addCleanup(restore)
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
232
        # Clean out the filters so we have a clean slate.
233
        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.
234
235
    def assertFirstWarning(self, action, category):
236
        """Test the first warning in the filters is correct"""
237
        first = warnings.filters[0]
238
        self.assertEqual((action, category), (first[0], first[2]))
239
240
    def test_suppress_deprecation_warnings(self):
241
        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
242
        symbol_versioning.suppress_deprecation_warnings()
243
        self.assertFirstWarning('ignore', DeprecationWarning)
244
5320.2.1 by Robert Collins
The symbol_versioning module can now cleanup after itself -
245
    def test_set_restore_filters(self):
246
        original_filters = warnings.filters[:]
247
        symbol_versioning.suppress_deprecation_warnings()()
248
        self.assertEqual(original_filters, warnings.filters)
249
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
250
    def test_suppress_deprecation_with_warning_filter(self):
251
        """don't suppress if we already have a filter"""
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
252
        warnings.filterwarnings('error', category=Warning)
253
        self.assertFirstWarning('error', Warning)
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
254
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
255
        symbol_versioning.suppress_deprecation_warnings(override=False)
256
        self.assertFirstWarning('error', Warning)
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
257
        self.assertEqual(1, len(warnings.filters))
258
259
    def test_suppress_deprecation_with_filter(self):
260
        """don't suppress if we already have a filter"""
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
261
        warnings.filterwarnings('error', category=DeprecationWarning)
262
        self.assertFirstWarning('error', DeprecationWarning)
263
        self.assertEqual(1, len(warnings.filters))
264
        symbol_versioning.suppress_deprecation_warnings(override=False)
265
        self.assertFirstWarning('error', DeprecationWarning)
266
        self.assertEqual(1, len(warnings.filters))
267
        symbol_versioning.suppress_deprecation_warnings(override=True)
268
        self.assertFirstWarning('ignore', DeprecationWarning)
269
        self.assertEqual(2, len(warnings.filters))
3427.5.5 by John Arbash Meinel
Disable suppression if there is already a filter present for Warnings
270
3427.5.3 by John Arbash Meinel
Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
271
    def test_activate_deprecation_no_error(self):
272
        # 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
273
        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.
274
        self.assertFirstWarning('default', DeprecationWarning)
275
276
    def test_activate_deprecation_with_error(self):
277
        # First nuke the filters, so we know it is clean
278
        # Add a warning == error rule
279
        warnings.filterwarnings('error', category=Warning)
280
        self.assertFirstWarning('error', Warning)
281
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
282
        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.
283
        # There should not be a new warning
284
        self.assertFirstWarning('error', Warning)
285
        self.assertEqual(1, len(warnings.filters))
286
287
    def test_activate_deprecation_with_DW_error(self):
288
        # First nuke the filters, so we know it is clean
289
        # Add a warning == error rule
290
        warnings.filterwarnings('error', category=DeprecationWarning)
291
        self.assertFirstWarning('error', DeprecationWarning)
292
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
293
        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.
294
        # There should not be a new warning
295
        self.assertFirstWarning('error', DeprecationWarning)
296
        self.assertEqual(1, len(warnings.filters))
3427.5.7 by John Arbash Meinel
Bring back always in the form of 'override'.
297
        symbol_versioning.activate_deprecation_warnings(override=True)
298
        self.assertFirstWarning('default', DeprecationWarning)
299
        self.assertEqual(2, len(warnings.filters))