/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: Michael Ellerman
  • Date: 2006-05-31 08:44:29 UTC
  • mto: (1711.2.63 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1792.
  • Revision ID: michael@ellerman.id.au-20060531084429-35e5429abda9f560
Add optional location to ancestry and fix behaviour for checkouts.

This adds an optional location parameter to the ancestry command. It also
changes the behaviour of ancestry on checkouts such that if they have
been created with a subset of the branch history, only the subset is
shown by 'bzr ancestry'. Tests for all of that as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006 by Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
12
13
#
13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
 
17
18
"""Symbol versioning tests."""
18
19
 
19
 
import warnings
20
 
 
21
 
from bzrlib import symbol_versioning
22
 
from bzrlib.symbol_versioning import (
23
 
    deprecated_function,
24
 
    deprecated_in,
25
 
    deprecated_list,
26
 
    deprecated_method,
27
 
    )
 
20
import bzrlib.symbol_versioning as symbol_versioning
28
21
from bzrlib.tests import TestCase
29
22
 
30
23
 
31
 
@deprecated_function(deprecated_in((0, 7, 0)))
32
 
def sample_deprecated_function():
 
24
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
 
25
def deprecated_function():
33
26
    """Deprecated function docstring."""
34
27
    return 1
35
28
 
36
29
 
37
 
a_deprecated_list = symbol_versioning.deprecated_list(deprecated_in((0, 9, 0)),
38
 
    'a_deprecated_list', ['one'], extra="Don't use me")
39
 
 
40
 
 
41
 
a_deprecated_dict = symbol_versioning.DeprecatedDict(
42
 
    deprecated_in((0, 14, 0)),
43
 
    'a_deprecated_dict',
44
 
    dict(a=42),
45
 
    advice='Pull the other one!',
46
 
    )
47
 
 
48
 
 
49
30
class TestDeprecationWarnings(TestCase):
50
31
 
51
32
    def capture_warning(self, message, category, stacklevel=None):
55
36
        super(TestDeprecationWarnings, self).setUp()
56
37
        self._warnings = []
57
38
    
58
 
    @deprecated_method(deprecated_in((0, 7, 0)))
 
39
    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
59
40
    def deprecated_method(self):
60
41
        """Deprecated method docstring.
61
 
 
 
42
        
62
43
        This might explain stuff.
63
44
        """
64
45
        return 1
65
46
 
66
 
    @staticmethod
67
 
    @deprecated_function(deprecated_in((0, 7, 0)))
68
 
    def deprecated_static():
69
 
        """Deprecated static."""
70
 
        return 1
71
 
 
72
 
    def test_deprecated_static(self):
73
 
        # XXX: The results are not quite right because the class name is not
74
 
        # shown - however it is enough to give people a good indication of
75
 
        # where the problem is.
76
 
        expected_warning = (
77
 
            "bzrlib.tests.test_symbol_versioning."
78
 
            "deprecated_static "
79
 
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
80
 
        expected_docstring = (
81
 
            'Deprecated static.\n'
82
 
            '\n'
83
 
            'This function was deprecated in version 0.7.0.\n'
84
 
            )
85
 
        self.check_deprecated_callable(
86
 
            expected_warning, expected_docstring,
87
 
            "deprecated_static",
88
 
            "bzrlib.tests.test_symbol_versioning",
89
 
            self.deprecated_static)
90
 
 
91
47
    def test_deprecated_method(self):
92
48
        expected_warning = (
93
49
            "bzrlib.tests.test_symbol_versioning."
94
50
            "TestDeprecationWarnings.deprecated_method "
95
 
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
96
 
        expected_docstring = (
97
 
            'Deprecated method docstring.\n'
98
 
            '\n'
99
 
            '        This might explain stuff.\n'
100
 
            '        \n'
101
 
            '        This method was deprecated in version 0.7.0.\n'
102
 
            '        ')
 
51
            "was deprecated in version 0.7.", DeprecationWarning, 2)
 
52
        expected_docstring = ('Deprecated method docstring.\n'
 
53
                              '        \n'
 
54
                              '        This might explain stuff.\n'
 
55
                              '        \n'
 
56
                              '        This method was deprecated in version 0.7.\n'
 
57
                              '        ')
103
58
        self.check_deprecated_callable(expected_warning, expected_docstring,
104
59
                                       "deprecated_method",
105
60
                                       "bzrlib.tests.test_symbol_versioning",
107
62
 
108
63
    def test_deprecated_function(self):
109
64
        expected_warning = (
110
 
            "bzrlib.tests.test_symbol_versioning.sample_deprecated_function "
111
 
            "was deprecated in version 0.7.0.", DeprecationWarning, 2)
 
65
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
 
66
            "was deprecated in version 0.7.", DeprecationWarning, 2)
112
67
        expected_docstring = ('Deprecated function docstring.\n'
113
68
                              '\n'
114
 
                              'This function was deprecated in version 0.7.0.\n'
 
69
                              'This function was deprecated in version 0.7.\n'
115
70
                              )
116
71
        self.check_deprecated_callable(expected_warning, expected_docstring,
117
 
                                       "sample_deprecated_function",
 
72
                                       "deprecated_function",
118
73
                                       "bzrlib.tests.test_symbol_versioning",
119
 
                                       sample_deprecated_function)
120
 
 
121
 
    def test_deprecated_list(self):
122
 
        expected_warning = (
123
 
            "Modifying a_deprecated_list was deprecated in version 0.9.0."
124
 
            " Don't use me", DeprecationWarning, 3)
125
 
        old_warning_method = symbol_versioning.warn
126
 
        try:
127
 
            symbol_versioning.set_warning_method(self.capture_warning)
128
 
            self.assertEqual(['one'], a_deprecated_list)
129
 
            self.assertEqual([], self._warnings)
130
 
 
131
 
            a_deprecated_list.append('foo')
132
 
            self.assertEqual([expected_warning], self._warnings)
133
 
            self.assertEqual(['one', 'foo'], a_deprecated_list)
134
 
 
135
 
            a_deprecated_list.extend(['bar', 'baz'])
136
 
            self.assertEqual([expected_warning]*2, self._warnings)
137
 
            self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
138
 
 
139
 
            a_deprecated_list.insert(1, 'xxx')
140
 
            self.assertEqual([expected_warning]*3, self._warnings)
141
 
            self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
142
 
 
143
 
            a_deprecated_list.remove('foo')
144
 
            self.assertEqual([expected_warning]*4, self._warnings)
145
 
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
146
 
 
147
 
            val = a_deprecated_list.pop()
148
 
            self.assertEqual([expected_warning]*5, self._warnings)
149
 
            self.assertEqual('baz', val)
150
 
            self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
151
 
 
152
 
            val = a_deprecated_list.pop(1)
153
 
            self.assertEqual([expected_warning]*6, self._warnings)
154
 
            self.assertEqual('xxx', val)
155
 
            self.assertEqual(['one', 'bar'], a_deprecated_list)
156
 
        finally:
157
 
            symbol_versioning.set_warning_method(old_warning_method)
158
 
 
159
 
    def test_deprecated_dict(self):
160
 
        expected_warning = (
161
 
            "access to a_deprecated_dict was deprecated in version 0.14.0."
162
 
            " Pull the other one!", DeprecationWarning, 2)
163
 
        old_warning_method = symbol_versioning.warn
164
 
        try:
165
 
            symbol_versioning.set_warning_method(self.capture_warning)
166
 
            self.assertEqual(len(a_deprecated_dict), 1)
167
 
            self.assertEqual([expected_warning], self._warnings)
168
 
 
169
 
            a_deprecated_dict['b'] = 42
170
 
            self.assertEqual(a_deprecated_dict['b'], 42)
171
 
            self.assertTrue('b' in a_deprecated_dict)
172
 
            del a_deprecated_dict['b']
173
 
            self.assertFalse('b' in a_deprecated_dict)
174
 
            self.assertEqual([expected_warning] * 6, self._warnings)
175
 
        finally:
176
 
            symbol_versioning.set_warning_method(old_warning_method)
177
 
 
 
74
                                       deprecated_function)
178
75
 
179
76
    def check_deprecated_callable(self, expected_warning, expected_docstring,
180
77
                                  expected_name, expected_module,
181
78
                                  deprecated_callable):
182
 
        if __doc__ is None:
183
 
            # With -OO the docstring should just be the deprecated version
184
 
            expected_docstring = expected_docstring.split('\n')[-2].lstrip()
185
79
        old_warning_method = symbol_versioning.warn
186
80
        try:
187
81
            symbol_versioning.set_warning_method(self.capture_warning)
196
90
            self.assertTrue(deprecated_callable.is_deprecated)
197
91
        finally:
198
92
            symbol_versioning.set_warning_method(old_warning_method)
199
 
 
 
93
    
200
94
    def test_deprecated_passed(self):
201
95
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
202
96
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
204
98
        self.assertEqual(False,
205
99
                         symbol_versioning.deprecated_passed(
206
100
                            symbol_versioning.DEPRECATED_PARAMETER))
207
 
 
208
 
    def test_deprecation_string(self):
209
 
        """We can get a deprecation string for a method or function."""
210
 
        self.assertEqual('bzrlib.tests.test_symbol_versioning.'
211
 
            'TestDeprecationWarnings.test_deprecation_string was deprecated in '
212
 
            'version 0.11.0.',
213
 
            symbol_versioning.deprecation_string(
214
 
            self.test_deprecation_string,
215
 
            deprecated_in((0, 11, 0))))
216
 
        self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
217
 
            'deprecated in version 0.11.0.',
218
 
            symbol_versioning.deprecation_string(
219
 
                symbol_versioning.deprecated_function,
220
 
                deprecated_in((0, 11, 0))))
221
 
 
222
 
 
223
 
class TestSuppressAndActivate(TestCase):
224
 
 
225
 
    def setUp(self):
226
 
        TestCase.setUp(self)
227
 
        existing_filters = list(warnings.filters)
228
 
        def restore():
229
 
            warnings.filters[:] = existing_filters
230
 
        self.addCleanup(restore)
231
 
        # Clean out the filters so we have a clean slate.
232
 
        warnings.resetwarnings()
233
 
 
234
 
    def assertFirstWarning(self, action, category):
235
 
        """Test the first warning in the filters is correct"""
236
 
        first = warnings.filters[0]
237
 
        self.assertEqual((action, category), (first[0], first[2]))
238
 
 
239
 
    def test_suppress_deprecation_warnings(self):
240
 
        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
241
 
        symbol_versioning.suppress_deprecation_warnings()
242
 
        self.assertFirstWarning('ignore', DeprecationWarning)
243
 
 
244
 
    def test_suppress_deprecation_with_warning_filter(self):
245
 
        """don't suppress if we already have a filter"""
246
 
        warnings.filterwarnings('error', category=Warning)
247
 
        self.assertFirstWarning('error', Warning)
248
 
        self.assertEqual(1, len(warnings.filters))
249
 
        symbol_versioning.suppress_deprecation_warnings(override=False)
250
 
        self.assertFirstWarning('error', Warning)
251
 
        self.assertEqual(1, len(warnings.filters))
252
 
 
253
 
    def test_suppress_deprecation_with_filter(self):
254
 
        """don't suppress if we already have a filter"""
255
 
        warnings.filterwarnings('error', category=DeprecationWarning)
256
 
        self.assertFirstWarning('error', DeprecationWarning)
257
 
        self.assertEqual(1, len(warnings.filters))
258
 
        symbol_versioning.suppress_deprecation_warnings(override=False)
259
 
        self.assertFirstWarning('error', DeprecationWarning)
260
 
        self.assertEqual(1, len(warnings.filters))
261
 
        symbol_versioning.suppress_deprecation_warnings(override=True)
262
 
        self.assertFirstWarning('ignore', DeprecationWarning)
263
 
        self.assertEqual(2, len(warnings.filters))
264
 
 
265
 
    def test_activate_deprecation_no_error(self):
266
 
        # First nuke the filters, so we know it is clean
267
 
        symbol_versioning.activate_deprecation_warnings()
268
 
        self.assertFirstWarning('default', DeprecationWarning)
269
 
 
270
 
    def test_activate_deprecation_with_error(self):
271
 
        # First nuke the filters, so we know it is clean
272
 
        # Add a warning == error rule
273
 
        warnings.filterwarnings('error', category=Warning)
274
 
        self.assertFirstWarning('error', Warning)
275
 
        self.assertEqual(1, len(warnings.filters))
276
 
        symbol_versioning.activate_deprecation_warnings(override=False)
277
 
        # There should not be a new warning
278
 
        self.assertFirstWarning('error', Warning)
279
 
        self.assertEqual(1, len(warnings.filters))
280
 
 
281
 
    def test_activate_deprecation_with_DW_error(self):
282
 
        # First nuke the filters, so we know it is clean
283
 
        # Add a warning == error rule
284
 
        warnings.filterwarnings('error', category=DeprecationWarning)
285
 
        self.assertFirstWarning('error', DeprecationWarning)
286
 
        self.assertEqual(1, len(warnings.filters))
287
 
        symbol_versioning.activate_deprecation_warnings(override=False)
288
 
        # There should not be a new warning
289
 
        self.assertFirstWarning('error', DeprecationWarning)
290
 
        self.assertEqual(1, len(warnings.filters))
291
 
        symbol_versioning.activate_deprecation_warnings(override=True)
292
 
        self.assertFirstWarning('default', DeprecationWarning)
293
 
        self.assertEqual(2, len(warnings.filters))