/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_commands.py

  • Committer: Brian de Alwis
  • Date: 2011-12-22 21:10:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6441.
  • Revision ID: bsd@acm.org-20111222211031-v4oe963fa0g9evah
Modified proposal as described on Dec 12:
  * Add a boolean "suppress_exceptions" to HookPoint, that defaults to False.
    If True, then exceptions raised when firing the hookpoints are
    suppressed and a warning message reported on bzr.log.

  * Add a list "passed_exceptions" to HookPoint that contains a list
    of exceptions that should be passed-through when suppress_exceptions=True

  * rename Hooks.run() to Hooks.fire() that now returns a boolean. 
    Returning True means all hooks passed with no exceptions raised. 
    If False, then one or more hooks raised an exception.

  * Change 'pre_command' definition to pass BzrCommandError

Show diffs side-by-side

added added

removed removed

Lines of Context:
374
374
        self.assertSubset(['foo', 'bar'], cmds)
375
375
 
376
376
class TestPreAndPostCommandHooks(tests.TestCase):
 
377
    class TestError(StandardError):
 
378
        __doc__ = """A test exception."""
377
379
 
378
380
    def test_pre_and_post_hooks(self):
379
381
        hook_calls = []
404
406
    def test_post_hook_provided_exception(self):
405
407
        hook_calls = []
406
408
 
407
 
        class TestError(StandardError):
408
 
            __doc__ = """A test exception."""
409
 
 
410
409
        def post_command(cmd, e):
411
 
            self.assertTrue(isinstance(e, TestError))
 
410
            self.assertTrue(isinstance(e, self.TestError))
412
411
            hook_calls.append('post')
413
412
 
414
413
        def run(cmd):
415
414
            hook_calls.append('run')
416
 
            raise TestError()
 
415
            raise self.TestError()
417
416
 
418
417
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
419
418
        commands.install_bzr_command_hooks()
421
420
            "post_command", post_command, None)
422
421
 
423
422
        self.assertEqual([], hook_calls)
424
 
        try:
425
 
            self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
426
 
            self.fail('should have raised TestError')
427
 
        except TestError:
428
 
            pass
 
423
        self.assertRaises(self.TestError,
 
424
                          commands.run_bzr, [u'rocks'])
429
425
        self.assertEqual(['run', 'post'], hook_calls)
 
426
 
 
427
    def test_pre_exception(self):
 
428
        """Ensure an exception in pre_command does not abort the command"""
 
429
 
 
430
        hook_calls = []
 
431
 
 
432
        def pre_command(cmd):
 
433
            hook_calls.append('pre')
 
434
            raise self.TestError()
 
435
 
 
436
        def post_command(cmd, e):
 
437
            hook_calls.append('post')
 
438
 
 
439
        def run(cmd):
 
440
            hook_calls.append('run')
 
441
 
 
442
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
443
        commands.install_bzr_command_hooks()
 
444
        commands.Command.hooks.install_named_hook(
 
445
            "pre_command", pre_command, None)
 
446
        commands.Command.hooks.install_named_hook(
 
447
            "post_command", post_command, None)
 
448
 
 
449
        self.assertEqual([], hook_calls)
 
450
        self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
 
451
        self.assertEqual(['pre', 'run', 'post'], hook_calls)
 
452
 
 
453
 
 
454
    def test_pre_command_error(self):
 
455
        """Ensure an BzrCommandError in pre_command aborts the command"""
 
456
 
 
457
        hook_calls = []
 
458
 
 
459
        def pre_command(cmd):
 
460
            hook_calls.append('pre')
 
461
            # verify that all subclasses of BzrCommandError caught too
 
462
            raise errors.BzrOptionError()
 
463
 
 
464
        def post_command(cmd, e):
 
465
            self.fail('post_command should not be called')
 
466
 
 
467
        def run(cmd):
 
468
            self.fail('command should not be called')
 
469
 
 
470
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
471
        commands.install_bzr_command_hooks()
 
472
        commands.Command.hooks.install_named_hook(
 
473
            "pre_command", pre_command, None)
 
474
        commands.Command.hooks.install_named_hook(
 
475
            "post_command", post_command, None)
 
476
 
 
477
        self.assertEqual([], hook_calls)
 
478
        self.assertRaises(errors.BzrCommandError,
 
479
                          commands.run_bzr, [u'rocks'])
 
480
        self.assertEqual(['pre'], hook_calls)
 
481