/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
        """
100
100
        def failing_operation():
101
101
            try:
102
 
                1/0
 
102
                1 / 0
103
103
            finally:
104
104
                _run_cleanup(self.no_op_cleanup)
105
105
        self.assertRaises(ZeroDivisionError, failing_operation)
114
114
        """
115
115
        def failing_operation():
116
116
            try:
117
 
                1/0
 
117
                1 / 0
118
118
            finally:
119
119
                _run_cleanup(self.failing_cleanup)
120
120
        self.assertRaises(ZeroDivisionError, failing_operation)
144
144
 
145
145
    def failing_func(self):
146
146
        self.call_log.append('failing_func')
147
 
        1/0
 
147
        1 / 0
148
148
 
149
149
    def test_func_error_propagates(self):
150
150
        """Errors from the main function are propagated (after running
186
186
        """
187
187
        cleanups = self.make_two_failing_cleanup_funcs()
188
188
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
189
 
            self.trivial_func)
 
189
                          self.trivial_func)
190
190
        self.assertLogContains('Cleanup failed:.*ErrorB')
191
191
        # Error A may appear in the log (with Python 3 exception chaining), but
192
192
        # Error B should be the last error recorded.
198
198
    def make_two_failing_cleanup_funcs(self):
199
199
        def raise_a():
200
200
            raise ErrorA('Error A')
 
201
 
201
202
        def raise_b():
202
203
            raise ErrorB('Error B')
203
204
        return [(raise_a, (), {}), (raise_b, (), {})]
206
207
        debug.debug_flags.add('cleanup')
207
208
        cleanups = self.make_two_failing_cleanup_funcs()
208
209
        self.assertRaises(ErrorA, _do_with_cleanups, cleanups,
209
 
            self.trivial_func)
 
210
                          self.trivial_func)
210
211
        trace_value = self.get_log()
211
212
        self.assertContainsRe(
212
213
            trace_value, "brz: warning: Cleanup failed:.*Error B\n")
216
217
        debug.debug_flags.add('cleanup')
217
218
        cleanups = self.make_two_failing_cleanup_funcs()
218
219
        self.assertRaises(ZeroDivisionError, _do_with_cleanups, cleanups,
219
 
            self.failing_func)
 
220
                          self.failing_func)
220
221
        trace_value = self.get_log()
221
222
        self.assertContainsRe(
222
223
            trace_value, "brz: warning: Cleanup failed:.*Error A\n")
226
227
 
227
228
    def test_func_may_mutate_cleanups(self):
228
229
        """The main func may mutate the cleanups before it returns.
229
 
        
 
230
 
230
231
        This allows a function to gradually add cleanups as it acquires
231
232
        resources, rather than planning all the cleanups up-front.  The
232
233
        OperationWithCleanups helper relies on this working.
233
234
        """
234
235
        cleanups_list = []
 
236
 
235
237
        def func_that_adds_cleanups():
236
238
            self.call_log.append('func_that_adds_cleanups')
237
239
            cleanups_list.append((self.no_op_cleanup, (), {}))
247
249
        """
248
250
        debug.debug_flags.add('cleanup')
249
251
        self.assertRaises(ZeroDivisionError, _do_with_cleanups,
250
 
            [(self.failing_cleanup, (), {})], self.failing_func)
 
252
                          [(self.failing_cleanup, (), {})], self.failing_func)
251
253
        trace_value = self.get_log()
252
254
        self.assertContainsRe(
253
255
            trace_value,
264
266
        cleanup added during the func is run first.
265
267
        """
266
268
        call_log = []
 
269
 
267
270
        def func(op, foo):
268
271
            call_log.append(('func called', foo))
269
272
            op.add_cleanup(call_log.append, 'cleanup 2')
276
279
        self.assertEqual('result', result)
277
280
        self.assertEqual(
278
281
            [('func called', 'foo'), 'cleanup 1', 'cleanup 2', 'cleanup 3',
279
 
            'cleanup 4'], call_log)
 
282
             'cleanup 4'], call_log)
280
283
 
281
284
 
282
285
class SampleWithCleanups(ObjectWithCleanups):