/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 bzrlib/tests/test_hooks.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-02 14:41:49 UTC
  • mto: This revision was merged to the branch mainline in revision 6441.
  • Revision ID: jelmer@samba.org-20120102144149-66kkvew1kylagrk9
Drop exception suppression support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    hooks as _mod_hooks,
23
23
    pyutils,
24
24
    tests,
25
 
    trace,
26
25
    )
27
26
from bzrlib.hooks import (
28
27
    HookPoint,
264
263
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
265
264
            callback_repr, repr(hook))
266
265
 
267
 
    def test_fire(self):
268
 
        executions = []
269
 
 
270
 
        def callback(arg):
271
 
            executions.append('callback')
272
 
            self.assertEquals('argument', arg)
273
 
 
274
 
        hook = HookPoint("foo", "no docs", None, None)
275
 
        hook.hook(callback, None)
276
 
        hook.hook(callback, None)
277
 
        result = hook.fire('argument')
278
 
        self.assertTrue(result, 'no exceptions raised')
279
 
        self.assertEqual(['callback', 'callback'], executions)
280
 
 
281
 
    def test_fire_passes_exceptions(self):
282
 
        class TestError(StandardError):
283
 
            __doc__ = """A test exception."""
284
 
 
285
 
        def callback(arg):
286
 
            raise TestError()
287
 
 
288
 
        hook = HookPoint("foo", "no docs", None, None)
289
 
        hook.hook(callback, None)
290
 
        try:
291
 
            result = hook.fire('argument')
292
 
            self.fail('execution should not reach here')
293
 
        except TestError, e:
294
 
            self.assertTrue(isinstance(e, TestError))
295
 
 
296
 
    def test_fire_with_passed_exceptions(self):
297
 
        arguments = []
298
 
        class TestPassedError(StandardError):
299
 
            __doc__ = """A test exception that should be passed."""
300
 
        class TestSuppressedError(StandardError):
301
 
            __doc__ = """A test exception that should be suppressed."""
302
 
 
303
 
        def callback(excClass):
304
 
            arguments.append(excClass)
305
 
            raise excClass()
306
 
 
307
 
        hook = HookPoint("foo", "no docs", None, None,
308
 
            suppress_exceptions=True, passed_exceptions=[TestPassedError])
309
 
        hook.hook(callback, None)
310
 
        try:
311
 
            # this exception should be suppressed, but logged
312
 
            result = hook.fire(TestSuppressedError)
313
 
            self.assertFalse(result)
314
 
 
315
 
            # this exception should be passed through
316
 
            result = hook.fire(TestPassedError)
317
 
            self.fail('execution should not reach here')
318
 
        except Exception, e:
319
 
            self.assertTrue(isinstance(e, TestPassedError))
320
 
        # hook should have been called twice
321
 
        self.assertEqual(2, len(arguments))
322
 
 
323
 
 
324
 
    def test_suppressed_fire_logs_exceptions(self):
325
 
        class TestError(StandardError):
326
 
            __doc__ = """A test exception."""
327
 
 
328
 
        def callback():
329
 
            raise TestError()
330
 
 
331
 
        hook = HookPoint("foo", "no docs", None, None, suppress_exceptions=True)
332
 
        hook.hook(callback, None)
333
 
 
334
 
        warnings = []
335
 
        def warning(*args):
336
 
            warnings.append(args[0] % args[1:])
337
 
        _warning = trace.warning
338
 
        trace.warning = warning
339
 
 
340
 
        try:
341
 
            hook.fire()
342
 
        except:
343
 
            self.fail('exceptions should be captured')
344
 
        finally:
345
 
            trace.warning = _warning
346
 
 
347
 
        self.assertEqual(1, len(warnings))
348
 
 
349
266
 
350
267
class TestHookRegistry(tests.TestCase):
351
268