/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: Martin
  • Date: 2017-08-28 14:16:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6779.
  • Revision ID: gzlist@googlemail.com-20170828141619-daya3i5svbfc98d8
Fix test_cleanup on Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    ObjectWithCleanups,
23
23
    OperationWithCleanups,
24
24
    )
25
 
from ..sixish import (
26
 
    BytesIO,
27
 
    )
28
 
from . import TestCase
29
25
from .. import (
30
26
    debug,
31
 
    trace,
 
27
    tests,
32
28
    )
33
29
 
34
30
 
35
 
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):
36
40
 
37
41
    def setUp(self):
38
42
        super(CleanupsTestCase, self).setUp()
83
87
        """The -Dcleanup debug flag causes cleanup errors to be reported to the
84
88
        user.
85
89
        """
86
 
        log = BytesIO()
87
 
        trace.push_log_file(log)
88
90
        debug.debug_flags.add('cleanup')
89
91
        self.assertFalse(_run_cleanup(self.failing_cleanup))
90
92
        self.assertContainsRe(
91
 
            log.getvalue(),
 
93
            self.get_log(),
92
94
            "brz: warning: Cleanup failed:.*failing_cleanup goes boom")
93
95
 
94
96
    def test_prior_error_cleanup_succeeds(self):
186
188
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
187
189
            self.trivial_func)
188
190
        self.assertLogContains('Cleanup failed:.*ErrorB')
189
 
        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$')
190
197
 
191
198
    def make_two_failing_cleanup_funcs(self):
192
199
        def raise_a():
196
203
        return [(raise_a, (), {}), (raise_b, (), {})]
197
204
 
198
205
    def test_multiple_cleanup_failures_debug_flag(self):
199
 
        log = BytesIO()
200
 
        trace.push_log_file(log)
201
206
        debug.debug_flags.add('cleanup')
202
207
        cleanups = self.make_two_failing_cleanup_funcs()
203
208
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
204
209
            self.trivial_func)
 
210
        trace_value = self.get_log()
205
211
        self.assertContainsRe(
206
 
            log.getvalue(), "brz: warning: Cleanup failed:.*Error B\n")
207
 
        self.assertEqual(1, log.getvalue().count('brz: warning:'),
208
 
                log.getvalue())
 
212
            trace_value, "brz: warning: Cleanup failed:.*Error B\n")
 
213
        self.assertEqual(1, trace_value.count('brz: warning:'))
209
214
 
210
215
    def test_func_and_cleanup_errors_debug_flag(self):
211
 
        log = BytesIO()
212
 
        trace.push_log_file(log)
213
216
        debug.debug_flags.add('cleanup')
214
217
        cleanups = self.make_two_failing_cleanup_funcs()
215
218
        self.assertRaises(ZeroDivisionError, _do_with_cleanups, cleanups,
216
219
            self.failing_func)
217
 
        self.assertContainsRe(
218
 
            log.getvalue(), "brz: warning: Cleanup failed:.*Error A\n")
219
 
        self.assertContainsRe(
220
 
            log.getvalue(), "brz: warning: Cleanup failed:.*Error B\n")
221
 
        self.assertEqual(2, log.getvalue().count('brz: 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:'))
222
226
 
223
227
    def test_func_may_mutate_cleanups(self):
224
228
        """The main func may mutate the cleanups before it returns.
241
245
        """The -Dcleanup debug flag causes cleanup errors to be reported to the
242
246
        user.
243
247
        """
244
 
        log = BytesIO()
245
 
        trace.push_log_file(log)
246
248
        debug.debug_flags.add('cleanup')
247
249
        self.assertRaises(ZeroDivisionError, _do_with_cleanups,
248
250
            [(self.failing_cleanup, (), {})], self.failing_func)
 
251
        trace_value = self.get_log()
249
252
        self.assertContainsRe(
250
 
            log.getvalue(),
 
253
            trace_value,
251
254
            "brz: warning: Cleanup failed:.*failing_cleanup goes boom")
252
 
        self.assertEqual(1, log.getvalue().count('brz: warning:'))
253
 
 
254
 
 
255
 
class ErrorA(Exception): pass
256
 
class ErrorB(Exception): pass
 
255
        self.assertEqual(1, trace_value.count('brz: warning:'))
257
256
 
258
257
 
259
258
class TestOperationWithCleanups(CleanupsTestCase):
281
280
 
282
281
 
283
282
class SampleWithCleanups(ObjectWithCleanups):
284
 
 
285
 
    pass
286
 
 
287
 
 
288
 
class TestObjectWithCleanups(TestCase):
 
283
    """Minimal ObjectWithCleanups subclass."""
 
284
 
 
285
 
 
286
class TestObjectWithCleanups(tests.TestCase):
289
287
 
290
288
    def test_object_with_cleanups(self):
291
289
        a = []