def test_cleanup_with_args_kwargs(self): def func_taking_args_kwargs(*args, **kwargs): self.call_log.append(('func', args, kwargs)) _run_cleanup(func_taking_args_kwargs, 'an arg', kwarg='foo') self.assertEqual([('func', ('an arg', ), { 'kwarg': 'foo' })], self.call_log)
def test_cleanup_error_debug_flag(self): """The -Dcleanup debug flag causes cleanup errors to be reported to the user. """ log = StringIO() trace.push_log_file(log) debug.debug_flags.add('cleanup') self.assertFalse(_run_cleanup(self.failing_cleanup)) self.assertContainsRe( log.getvalue(), "bzr: warning: Cleanup failed:.*failing_cleanup goes boom")
def test_cleanup_error(self): """An error from the cleanup function is logged by _run_cleanup, but not propagated. This is there's no way for _run_cleanup to know if there's an existing exception in this situation:: try: some_func() finally: _run_cleanup(cleanup_func) So, the best _run_cleanup can do is always log errors but never raise them. """ self.assertFalse(_run_cleanup(self.failing_cleanup)) self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom')
def failing_operation(): try: 1/0 finally: _run_cleanup(self.no_op_cleanup)
def test_cleanup_with_args_kwargs(self): def func_taking_args_kwargs(*args, **kwargs): self.call_log.append(('func', args, kwargs)) _run_cleanup(func_taking_args_kwargs, 'an arg', kwarg='foo') self.assertEqual( [('func', ('an arg',), {'kwarg': 'foo'})], self.call_log)
def test_no_errors(self): """The function passed to _run_cleanup is run.""" self.assertTrue(_run_cleanup(self.no_op_cleanup)) self.assertEqual(['no_op_cleanup'], self.call_log)
def failing_operation(): try: 1 / 0 finally: _run_cleanup(self.no_op_cleanup)