/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4797.32.2 by John Arbash Meinel
merge 2.1, resolving NEWS conflict.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
import re
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from ..cleanup import (
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
20
    _do_with_cleanups,
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
21
    _run_cleanup,
5158.8.2 by Martin Pool
Add simple test case for ObjectWithCleanups
22
    ObjectWithCleanups,
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
23
    OperationWithCleanups,
4634.85.3 by Andrew Bennetts
Lots more tests.
24
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
25
from .. import (
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
26
    debug,
6770.3.1 by Martin
Fix test_cleanup on Python 3
27
    tests,
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
28
    )
4634.85.3 by Andrew Bennetts
Lots more tests.
29
30
6770.3.1 by Martin
Fix test_cleanup on Python 3
31
class ErrorA(Exception):
32
    """Sample exception type A."""
33
34
35
class ErrorB(Exception):
36
    """Sample exception type B."""
37
38
39
class CleanupsTestCase(tests.TestCase):
4634.85.3 by Andrew Bennetts
Lots more tests.
40
41
    def setUp(self):
42
        super(CleanupsTestCase, self).setUp()
43
        self.call_log = []
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
44
45
    def no_op_cleanup(self):
4634.85.3 by Andrew Bennetts
Lots more tests.
46
        self.call_log.append('no_op_cleanup')
47
48
    def assertLogContains(self, regex):
4794.1.15 by Robert Collins
Review feedback.
49
        self.assertContainsRe(self.get_log(), regex, re.DOTALL)
4634.85.3 by Andrew Bennetts
Lots more tests.
50
51
    def failing_cleanup(self):
52
        self.call_log.append('failing_cleanup')
53
        raise Exception("failing_cleanup goes boom!")
54
55
56
class TestRunCleanup(CleanupsTestCase):
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
57
58
    def test_no_errors(self):
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
59
        """The function passed to _run_cleanup is run."""
60
        self.assertTrue(_run_cleanup(self.no_op_cleanup))
4634.85.3 by Andrew Bennetts
Lots more tests.
61
        self.assertEqual(['no_op_cleanup'], self.call_log)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
62
4634.85.4 by Andrew Bennetts
Another test.
63
    def test_cleanup_with_args_kwargs(self):
64
        def func_taking_args_kwargs(*args, **kwargs):
65
            self.call_log.append(('func', args, kwargs))
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
66
        _run_cleanup(func_taking_args_kwargs, 'an arg', kwarg='foo')
4634.85.4 by Andrew Bennetts
Another test.
67
        self.assertEqual(
68
            [('func', ('an arg',), {'kwarg': 'foo'})], self.call_log)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
69
70
    def test_cleanup_error(self):
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
71
        """An error from the cleanup function is logged by _run_cleanup, but not
4634.85.3 by Andrew Bennetts
Lots more tests.
72
        propagated.
73
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
74
        This is there's no way for _run_cleanup to know if there's an existing
4634.85.3 by Andrew Bennetts
Lots more tests.
75
        exception in this situation::
76
            try:
77
              some_func()
78
            finally:
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
79
              _run_cleanup(cleanup_func)
80
        So, the best _run_cleanup can do is always log errors but never raise
4634.85.3 by Andrew Bennetts
Lots more tests.
81
        them.
82
        """
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
83
        self.assertFalse(_run_cleanup(self.failing_cleanup))
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
84
        self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom')
85
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
86
    def test_cleanup_error_debug_flag(self):
4634.85.6 by Andrew Bennetts
More tests.
87
        """The -Dcleanup debug flag causes cleanup errors to be reported to the
88
        user.
89
        """
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
90
        debug.debug_flags.add('cleanup')
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
91
        self.assertFalse(_run_cleanup(self.failing_cleanup))
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
92
        self.assertContainsRe(
6770.3.1 by Martin
Fix test_cleanup on Python 3
93
            self.get_log(),
6681.2.5 by Jelmer Vernooij
Fix tests.
94
            "brz: warning: Cleanup failed:.*failing_cleanup goes boom")
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
95
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
96
    def test_prior_error_cleanup_succeeds(self):
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
97
        """Calling _run_cleanup from a finally block will not interfere with an
4634.85.3 by Andrew Bennetts
Lots more tests.
98
        exception from the try block.
99
        """
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
100
        def failing_operation():
101
            try:
102
                1/0
103
            finally:
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
104
                _run_cleanup(self.no_op_cleanup)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
105
        self.assertRaises(ZeroDivisionError, failing_operation)
4634.85.3 by Andrew Bennetts
Lots more tests.
106
        self.assertEqual(['no_op_cleanup'], self.call_log)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
107
108
    def test_prior_error_cleanup_fails(self):
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
109
        """Calling _run_cleanup from a finally block will not interfere with an
4634.85.3 by Andrew Bennetts
Lots more tests.
110
        exception from the try block even when the cleanup itself raises an
111
        exception.
112
113
        The cleanup exception will be logged.
114
        """
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
115
        def failing_operation():
116
            try:
117
                1/0
118
            finally:
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
119
                _run_cleanup(self.failing_cleanup)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
120
        self.assertRaises(ZeroDivisionError, failing_operation)
121
        self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom')
122
4634.85.3 by Andrew Bennetts
Lots more tests.
123
124
class TestDoWithCleanups(CleanupsTestCase):
125
126
    def trivial_func(self):
127
        self.call_log.append('trivial_func')
128
        return 'trivial result'
129
130
    def test_runs_func(self):
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
131
        """_do_with_cleanups runs the function it is given, and returns the
4634.85.3 by Andrew Bennetts
Lots more tests.
132
        result.
133
        """
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
134
        result = _do_with_cleanups([], self.trivial_func)
4634.85.3 by Andrew Bennetts
Lots more tests.
135
        self.assertEqual('trivial result', result)
136
137
    def test_runs_cleanups(self):
138
        """Cleanup functions are run (in the given order)."""
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
139
        cleanup_func_1 = (self.call_log.append, ('cleanup 1',), {})
140
        cleanup_func_2 = (self.call_log.append, ('cleanup 2',), {})
141
        _do_with_cleanups([cleanup_func_1, cleanup_func_2], self.trivial_func)
4634.85.3 by Andrew Bennetts
Lots more tests.
142
        self.assertEqual(
143
            ['trivial_func', 'cleanup 1', 'cleanup 2'], self.call_log)
144
145
    def failing_func(self):
146
        self.call_log.append('failing_func')
147
        1/0
148
149
    def test_func_error_propagates(self):
150
        """Errors from the main function are propagated (after running
151
        cleanups).
152
        """
153
        self.assertRaises(
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
154
            ZeroDivisionError, _do_with_cleanups,
155
            [(self.no_op_cleanup, (), {})], self.failing_func)
4634.85.3 by Andrew Bennetts
Lots more tests.
156
        self.assertEqual(['failing_func', 'no_op_cleanup'], self.call_log)
157
158
    def test_func_error_trumps_cleanup_error(self):
159
        """Errors from the main function a propagated even if a cleanup raises
160
        an error.
161
162
        The cleanup error is be logged.
163
        """
164
        self.assertRaises(
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
165
            ZeroDivisionError, _do_with_cleanups,
166
            [(self.failing_cleanup, (), {})], self.failing_func)
4634.85.3 by Andrew Bennetts
Lots more tests.
167
        self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom')
168
169
    def test_func_passes_and_error_from_cleanup(self):
170
        """An error from a cleanup is propagated when the main function doesn't
171
        raise an error.  Later cleanups are still executed.
172
        """
173
        exc = self.assertRaises(
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
174
            Exception, _do_with_cleanups,
175
            [(self.failing_cleanup, (), {}), (self.no_op_cleanup, (), {})],
176
            self.trivial_func)
4634.85.3 by Andrew Bennetts
Lots more tests.
177
        self.assertEqual('failing_cleanup goes boom!', exc.args[0])
178
        self.assertEqual(
179
            ['trivial_func', 'failing_cleanup', 'no_op_cleanup'],
180
            self.call_log)
181
182
    def test_multiple_cleanup_failures(self):
183
        """When multiple cleanups fail (as tends to happen when something has
184
        gone wrong), the first error is propagated, and subsequent errors are
185
        logged.
186
        """
4634.85.6 by Andrew Bennetts
More tests.
187
        cleanups = self.make_two_failing_cleanup_funcs()
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
188
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
189
            self.trivial_func)
4634.85.3 by Andrew Bennetts
Lots more tests.
190
        self.assertLogContains('Cleanup failed:.*ErrorB')
6770.3.1 by Martin
Fix test_cleanup on Python 3
191
        # Error A may appear in the log (with Python 3 exception chaining), but
192
        # Error B should be the last error recorded.
193
        self.assertContainsRe(
194
            self.get_log(),
195
            'Traceback \\(most recent call last\\):\n(  .*\n)+'
6779 by Jelmer Vernooij
Merge lp:~gz/brz/py3_test_cleanup
196
            '.*ErrorB: Error B\n$')
4634.85.3 by Andrew Bennetts
Lots more tests.
197
4634.85.6 by Andrew Bennetts
More tests.
198
    def make_two_failing_cleanup_funcs(self):
199
        def raise_a():
200
            raise ErrorA('Error A')
201
        def raise_b():
202
            raise ErrorB('Error B')
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
203
        return [(raise_a, (), {}), (raise_b, (), {})]
4634.85.6 by Andrew Bennetts
More tests.
204
205
    def test_multiple_cleanup_failures_debug_flag(self):
206
        debug.debug_flags.add('cleanup')
207
        cleanups = self.make_two_failing_cleanup_funcs()
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
208
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
209
            self.trivial_func)
6770.3.1 by Martin
Fix test_cleanup on Python 3
210
        trace_value = self.get_log()
4634.85.6 by Andrew Bennetts
More tests.
211
        self.assertContainsRe(
6770.3.1 by Martin
Fix test_cleanup on Python 3
212
            trace_value, "brz: warning: Cleanup failed:.*Error B\n")
213
        self.assertEqual(1, trace_value.count('brz: warning:'))
4634.85.6 by Andrew Bennetts
More tests.
214
215
    def test_func_and_cleanup_errors_debug_flag(self):
216
        debug.debug_flags.add('cleanup')
217
        cleanups = self.make_two_failing_cleanup_funcs()
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
218
        self.assertRaises(ZeroDivisionError, _do_with_cleanups, cleanups,
219
            self.failing_func)
6770.3.1 by Martin
Fix test_cleanup on Python 3
220
        trace_value = self.get_log()
221
        self.assertContainsRe(
222
            trace_value, "brz: warning: Cleanup failed:.*Error A\n")
223
        self.assertContainsRe(
224
            trace_value, "brz: warning: Cleanup failed:.*Error B\n")
225
        self.assertEqual(2, trace_value.count('brz: warning:'))
4634.85.6 by Andrew Bennetts
More tests.
226
4634.85.3 by Andrew Bennetts
Lots more tests.
227
    def test_func_may_mutate_cleanups(self):
228
        """The main func may mutate the cleanups before it returns.
229
        
230
        This allows a function to gradually add cleanups as it acquires
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
231
        resources, rather than planning all the cleanups up-front.  The
232
        OperationWithCleanups helper relies on this working.
4634.85.3 by Andrew Bennetts
Lots more tests.
233
        """
234
        cleanups_list = []
235
        def func_that_adds_cleanups():
236
            self.call_log.append('func_that_adds_cleanups')
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
237
            cleanups_list.append((self.no_op_cleanup, (), {}))
4634.85.3 by Andrew Bennetts
Lots more tests.
238
            return 'result'
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
239
        result = _do_with_cleanups(cleanups_list, func_that_adds_cleanups)
4634.85.3 by Andrew Bennetts
Lots more tests.
240
        self.assertEqual('result', result)
241
        self.assertEqual(
242
            ['func_that_adds_cleanups', 'no_op_cleanup'], self.call_log)
4634.85.6 by Andrew Bennetts
More tests.
243
244
    def test_cleanup_error_debug_flag(self):
245
        """The -Dcleanup debug flag causes cleanup errors to be reported to the
246
        user.
247
        """
248
        debug.debug_flags.add('cleanup')
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
249
        self.assertRaises(ZeroDivisionError, _do_with_cleanups,
250
            [(self.failing_cleanup, (), {})], self.failing_func)
6770.3.1 by Martin
Fix test_cleanup on Python 3
251
        trace_value = self.get_log()
4634.85.6 by Andrew Bennetts
More tests.
252
        self.assertContainsRe(
6770.3.1 by Martin
Fix test_cleanup on Python 3
253
            trace_value,
6681.2.5 by Jelmer Vernooij
Fix tests.
254
            "brz: warning: Cleanup failed:.*failing_cleanup goes boom")
6770.3.1 by Martin
Fix test_cleanup on Python 3
255
        self.assertEqual(1, trace_value.count('brz: warning:'))
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
256
257
258
class TestOperationWithCleanups(CleanupsTestCase):
259
260
    def test_cleanup_ordering(self):
261
        """Cleanups are added in LIFO order.
262
263
        So cleanups added before run is called are run last, and the last
264
        cleanup added during the func is run first.
265
        """
266
        call_log = []
267
        def func(op, foo):
268
            call_log.append(('func called', foo))
269
            op.add_cleanup(call_log.append, 'cleanup 2')
270
            op.add_cleanup(call_log.append, 'cleanup 1')
271
            return 'result'
272
        owc = OperationWithCleanups(func)
273
        owc.add_cleanup(call_log.append, 'cleanup 4')
274
        owc.add_cleanup(call_log.append, 'cleanup 3')
275
        result = owc.run('foo')
276
        self.assertEqual('result', result)
277
        self.assertEqual(
278
            [('func called', 'foo'), 'cleanup 1', 'cleanup 2', 'cleanup 3',
279
            'cleanup 4'], call_log)
280
5158.8.2 by Martin Pool
Add simple test case for ObjectWithCleanups
281
282
class SampleWithCleanups(ObjectWithCleanups):
6770.3.1 by Martin
Fix test_cleanup on Python 3
283
    """Minimal ObjectWithCleanups subclass."""
284
285
286
class TestObjectWithCleanups(tests.TestCase):
5158.8.2 by Martin Pool
Add simple test case for ObjectWithCleanups
287
288
    def test_object_with_cleanups(self):
289
        a = []
290
        s = SampleWithCleanups()
291
        s.add_cleanup(a.append, 42)
292
        s.cleanup_now()
293
        self.assertEqual(a, [42])