/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 breezy/tests/test_cleanup.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from cStringIO import StringIO
18
17
import re
19
18
 
20
 
from bzrlib.cleanup import (
 
19
from ..cleanup import (
21
20
    _do_with_cleanups,
22
21
    _run_cleanup,
 
22
    ObjectWithCleanups,
23
23
    OperationWithCleanups,
24
24
    )
25
 
from bzrlib.tests import TestCase
26
 
from bzrlib import (
 
25
from .. import (
27
26
    debug,
28
 
    trace,
 
27
    tests,
29
28
    )
30
29
 
31
30
 
32
 
class CleanupsTestCase(TestCase):
 
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):
33
40
 
34
41
    def setUp(self):
35
42
        super(CleanupsTestCase, self).setUp()
80
87
        """The -Dcleanup debug flag causes cleanup errors to be reported to the
81
88
        user.
82
89
        """
83
 
        log = StringIO()
84
 
        trace.push_log_file(log)
85
90
        debug.debug_flags.add('cleanup')
86
91
        self.assertFalse(_run_cleanup(self.failing_cleanup))
87
92
        self.assertContainsRe(
88
 
            log.getvalue(),
89
 
            "bzr: warning: Cleanup failed:.*failing_cleanup goes boom")
 
93
            self.get_log(),
 
94
            "brz: warning: Cleanup failed:.*failing_cleanup goes boom")
90
95
 
91
96
    def test_prior_error_cleanup_succeeds(self):
92
97
        """Calling _run_cleanup from a finally block will not interfere with an
183
188
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
184
189
            self.trivial_func)
185
190
        self.assertLogContains('Cleanup failed:.*ErrorB')
186
 
        self.assertFalse('ErrorA' in self.get_log())
 
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)+'
 
196
            '.*ErrorB: Error B\n$')
187
197
 
188
198
    def make_two_failing_cleanup_funcs(self):
189
199
        def raise_a():
193
203
        return [(raise_a, (), {}), (raise_b, (), {})]
194
204
 
195
205
    def test_multiple_cleanup_failures_debug_flag(self):
196
 
        log = StringIO()
197
 
        trace.push_log_file(log)
198
206
        debug.debug_flags.add('cleanup')
199
207
        cleanups = self.make_two_failing_cleanup_funcs()
200
208
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
201
209
            self.trivial_func)
 
210
        trace_value = self.get_log()
202
211
        self.assertContainsRe(
203
 
            log.getvalue(), "bzr: warning: Cleanup failed:.*Error B\n")
204
 
        self.assertEqual(1, log.getvalue().count('bzr: warning:'),
205
 
                log.getvalue())
 
212
            trace_value, "brz: warning: Cleanup failed:.*Error B\n")
 
213
        self.assertEqual(1, trace_value.count('brz: warning:'))
206
214
 
207
215
    def test_func_and_cleanup_errors_debug_flag(self):
208
 
        log = StringIO()
209
 
        trace.push_log_file(log)
210
216
        debug.debug_flags.add('cleanup')
211
217
        cleanups = self.make_two_failing_cleanup_funcs()
212
218
        self.assertRaises(ZeroDivisionError, _do_with_cleanups, cleanups,
213
219
            self.failing_func)
214
 
        self.assertContainsRe(
215
 
            log.getvalue(), "bzr: warning: Cleanup failed:.*Error A\n")
216
 
        self.assertContainsRe(
217
 
            log.getvalue(), "bzr: warning: Cleanup failed:.*Error B\n")
218
 
        self.assertEqual(2, log.getvalue().count('bzr: warning:'))
 
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:'))
219
226
 
220
227
    def test_func_may_mutate_cleanups(self):
221
228
        """The main func may mutate the cleanups before it returns.
238
245
        """The -Dcleanup debug flag causes cleanup errors to be reported to the
239
246
        user.
240
247
        """
241
 
        log = StringIO()
242
 
        trace.push_log_file(log)
243
248
        debug.debug_flags.add('cleanup')
244
249
        self.assertRaises(ZeroDivisionError, _do_with_cleanups,
245
250
            [(self.failing_cleanup, (), {})], self.failing_func)
 
251
        trace_value = self.get_log()
246
252
        self.assertContainsRe(
247
 
            log.getvalue(),
248
 
            "bzr: warning: Cleanup failed:.*failing_cleanup goes boom")
249
 
        self.assertEqual(1, log.getvalue().count('bzr: warning:'))
250
 
 
251
 
 
252
 
class ErrorA(Exception): pass
253
 
class ErrorB(Exception): pass
 
253
            trace_value,
 
254
            "brz: warning: Cleanup failed:.*failing_cleanup goes boom")
 
255
        self.assertEqual(1, trace_value.count('brz: warning:'))
254
256
 
255
257
 
256
258
class TestOperationWithCleanups(CleanupsTestCase):
276
278
            [('func called', 'foo'), 'cleanup 1', 'cleanup 2', 'cleanup 3',
277
279
            'cleanup 4'], call_log)
278
280
 
 
281
 
 
282
class SampleWithCleanups(ObjectWithCleanups):
 
283
    """Minimal ObjectWithCleanups subclass."""
 
284
 
 
285
 
 
286
class TestObjectWithCleanups(tests.TestCase):
 
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])