def test_name_hook_and_retrieve_name(self): """name_hook puts the name in the names mapping.""" hooks = Hooks() hooks['set_rh'] = [] self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None) hooks.name_hook(None, 'demo') self.assertEqual("demo", hooks.get_hook_name(None))
def __init__(self): """Create the default hooks. These are all empty initially, because by default nothing should get notified. """ Hooks.__init__(self, "bzrlib.smart.server", "SmartTCPServer.hooks") self.add_hook( 'server_started', "Called by the bzr server when it starts serving a directory. " "server_started is called with (backing urls, public url), " "where backing_url is a list of URLs giving the " "server-specific directory locations, and public_url is the " "public URL for the directory being served.", (0, 16)) self.add_hook( 'server_started_ex', "Called by the bzr server when it starts serving a directory. " "server_started is called with (backing_urls, server_obj).", (1, 17)) self.add_hook( 'server_stopped', "Called by the bzr server when it stops serving a directory. " "server_stopped is called with the same parameters as the " "server_started hook: (backing_urls, public_url).", (0, 16)) self.add_hook( 'server_exception', "Called by the bzr server when an exception occurs. " "server_exception is called with the sys.exc_info() tuple " "return true for the hook if the exception has been handled, " "in which case the server will exit normally.", (2, 4))
def __init__(self): """Create the default hooks. These are all empty initially, because by default nothing should get notified. """ Hooks.__init__(self, "bzrlib.smart.server", "SmartTCPServer.hooks") self.add_hook('server_started', "Called by the bzr server when it starts serving a directory. " "server_started is called with (backing urls, public url), " "where backing_url is a list of URLs giving the " "server-specific directory locations, and public_url is the " "public URL for the directory being served.", (0, 16)) self.add_hook('server_started_ex', "Called by the bzr server when it starts serving a directory. " "server_started is called with (backing_urls, server_obj).", (1, 17)) self.add_hook('server_stopped', "Called by the bzr server when it stops serving a directory. " "server_stopped is called with the same parameters as the " "server_started hook: (backing_urls, public_url).", (0, 16)) self.add_hook('server_exception', "Called by the bzr server when an exception occurs. " "server_exception is called with the sys.exc_info() tuple " "return true for the hook if the exception has been handled, " "in which case the server will exit normally.", (2, 4))
def test_uninstall_multiple_named_hooks(self): # Multiple callbacks with the same label all get removed hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks.add_hook('set_rh', "Set revision history", (2, 0)) hooks.install_named_hook('set_rh', 1, "demo") hooks.install_named_hook('set_rh', 2, "demo") hooks.install_named_hook('set_rh', 3, "othername") self.assertEqual(3, len(hooks["set_rh"])) hooks.uninstall_named_hook("set_rh", "demo") self.assertEqual(1, len(hooks["set_rh"]))
def __init__(self): Hooks.__init__(self, "bzrlib.lock", "Lock.hooks") self.add_hook('lock_acquired', "Called with a bzrlib.lock.LockResult when a physical lock is " "acquired.", (1, 8)) self.add_hook('lock_released', "Called with a bzrlib.lock.LockResult when a physical lock is " "released.", (1, 8)) self.add_hook('lock_broken', "Called with a bzrlib.lock.LockResult when a physical lock is " "broken.", (1, 15))
def test_uninstall_named_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks.add_hook('set_rh', "Set revision history", (2, 0)) hooks.install_named_hook('set_rh', None, "demo") self.assertEqual(1, len(hooks["set_rh"])) hooks.uninstall_named_hook("set_rh", "demo") self.assertEqual(0, len(hooks["set_rh"]))
def __init__(self): Hooks.__init__(self, "bzrlib.lock", "Lock.hooks") self.add_hook( 'lock_acquired', "Called with a bzrlib.lock.LockResult when a physical lock is " "acquired.", (1, 8)) self.add_hook( 'lock_released', "Called with a bzrlib.lock.LockResult when a physical lock is " "released.", (1, 8)) self.add_hook( 'lock_broken', "Called with a bzrlib.lock.LockResult when a physical lock is " "broken.", (1, 15))
def __init__(self): """Create the default hooks. These are all empty initially, because by default nothing should get notified. """ Hooks.__init__(self) # Introduced in 0.16: # invoked whenever the server starts serving a directory. # The api signature is (backing urls, public url). self['server_started'] = [] # Introduced in 0.16: # invoked whenever the server stops serving a directory. # The api signature is (backing urls, public url). self['server_stopped'] = []
def test_install_named_hook_lazy_old(self): # An exception is raised if a lazy hook is raised for # an old style hook point. hooks = Hooks("bzrlib.tests.hooks", "some_hooks") hooks['set_rh'] = [] self.assertRaises(errors.UnsupportedOperation, hooks.install_named_hook_lazy, 'set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh', "demo")
def __init__(self): """Create the default hooks. These are all empty initially. """ Hooks.__init__(self, "bzrlib.msgeditor", "hooks") self.add_hook( 'set_commit_message', "Set a fixed commit message. " "set_commit_message is called with the " "bzrlib.commit.Commit object (so you can also change e.g. revision " "properties by editing commit.builder._revprops) and the message " "so far. set_commit_message must return the message to use or None" " if it should use the message editor as normal.", (2, 4)) self.add_hook( 'commit_message_template', "Called when a commit message is being generated. " "commit_message_template is called with the bzrlib.commit.Commit " "object and the message that is known so far. " "commit_message_template must return a new message to use (which " "could be the same as it was given). When there are multiple " "hooks registered for commit_message_template, they are chained " "with the result from the first passed into the second, and so " "on.", (1, 10))
def __init__(self): """Create the default hooks. These are all empty initially, because by default nothing should get notified. """ Hooks.__init__(self, "bzrlib.commands", "Command.hooks") self.add_hook('extend_command', "Called after creating a command object to allow modifications " "such as adding or removing options, docs etc. Called with the " "new bzrlib.commands.Command object.", (1, 13)) self.add_hook('get_command', "Called when creating a single command. Called with " "(cmd_or_None, command_name). get_command should either return " "the cmd_or_None parameter, or a replacement Command object that " "should be used for the command. Note that the Command.hooks " "hooks are core infrastructure. Many users will prefer to use " "bzrlib.commands.register_command or plugin_cmds.register_lazy.", (1, 17)) self.add_hook('get_missing_command', "Called when creating a single command if no command could be " "found. Called with (command_name). get_missing_command should " "either return None, or a Command object to be used for the " "command.", (1, 17)) self.add_hook('list_commands', "Called when enumerating commands. Called with a set of " "cmd_name strings for all the commands found so far. This set " " is safe to mutate - e.g. to remove a command. " "list_commands should return the updated set of command names.", (1, 17)) self.add_hook('pre_command', "Called prior to executing a command. Called with the command " "object.", (2, 6)) self.add_hook('post_command', "Called after executing a command. Called with the command " "object.", (2, 6))
class TestHooks(tests.TestCase): def test_docs(self): """docs() should return something reasonable about the Hooks.""" class MyHooks(Hooks): pass hooks = MyHooks("bzrlib.tests.test_hooks", "some_hooks") hooks['legacy'] = [] hooks.add_hook( 'post_tip_change', "Invoked after the tip of a branch changes. Called with " "a ChangeBranchTipParams object.", (1, 4)) hooks.add_hook( 'pre_tip_change', "Invoked before the tip of a branch changes. Called with " "a ChangeBranchTipParams object. Hooks should raise " "TipChangeRejected to signal that a tip change is not permitted.", (1, 6), None) self.assertEqualDiff( "MyHooks\n" "-------\n" "\n" "legacy\n" "~~~~~~\n" "\n" "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n" "\n" "post_tip_change\n" "~~~~~~~~~~~~~~~\n" "\n" "Introduced in: 1.4\n" "\n" "Invoked after the tip of a branch changes. Called with a\n" "ChangeBranchTipParams object.\n" "\n" "pre_tip_change\n" "~~~~~~~~~~~~~~\n" "\n" "Introduced in: 1.6\n" "\n" "Invoked before the tip of a branch changes. Called with a\n" "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n" "signal that a tip change is not permitted.\n", hooks.docs()) def test_install_named_hook_raises_unknown_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly', None, "") def test_install_named_hook_appends_known_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks['set_rh'] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertEqual(hooks['set_rh'], [None]) def test_install_named_hook_and_retrieve_name(self): hooks = Hooks("bzrlib.tests.test_hooks", "somehooks") hooks['set_rh'] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertEqual("demo", hooks.get_hook_name(None)) def test_uninstall_named_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks.add_hook('set_rh', "Set revision history", (2, 0)) hooks.install_named_hook('set_rh', None, "demo") self.assertEqual(1, len(hooks["set_rh"])) hooks.uninstall_named_hook("set_rh", "demo") self.assertEqual(0, len(hooks["set_rh"])) def test_uninstall_multiple_named_hooks(self): # Multiple callbacks with the same label all get removed hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks.add_hook('set_rh', "Set revision history", (2, 0)) hooks.install_named_hook('set_rh', 1, "demo") hooks.install_named_hook('set_rh', 2, "demo") hooks.install_named_hook('set_rh', 3, "othername") self.assertEqual(3, len(hooks["set_rh"])) hooks.uninstall_named_hook("set_rh", "demo") self.assertEqual(1, len(hooks["set_rh"])) def test_uninstall_named_hook_unknown_callable(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks.add_hook('set_rh', "Set revision hsitory", (2, 0)) self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh", "demo") def test_uninstall_named_hook_raises_unknown_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") self.assertRaises(errors.UnknownHook, hooks.uninstall_named_hook, 'silly', "") def test_uninstall_named_hook_old_style(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks["set_rh"] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertRaises(errors.UnsupportedOperation, hooks.uninstall_named_hook, "set_rh", "demo") hooks = Hooks("bzrlib.tests.test_hooks", "TestHooks.hooks") def test_install_lazy_named_hook(self): # When the hook points are not yet registered the hook is # added to the _lazy_hooks dictionary in bzrlib.hooks. self.hooks.add_hook('set_rh', "doc", (0, 15)) set_rh = lambda: None install_lazy_named_hook('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh', set_rh, "demo") set_rh_lazy_hooks = _mod_hooks._lazy_hooks[('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh')] self.assertEqual(1, len(set_rh_lazy_hooks)) self.assertEqual(set_rh, set_rh_lazy_hooks[0][0].get_obj()) self.assertEqual("demo", set_rh_lazy_hooks[0][1]) self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh]) set_rh = lambda: None def test_install_named_hook_lazy(self): hooks = Hooks("bzrlib.tests.hooks", "some_hooks") hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None) hooks.install_named_hook_lazy('set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh', "demo") self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh]) def test_install_named_hook_lazy_old(self): # An exception is raised if a lazy hook is raised for # an old style hook point. hooks = Hooks("bzrlib.tests.hooks", "some_hooks") hooks['set_rh'] = [] self.assertRaises(errors.UnsupportedOperation, hooks.install_named_hook_lazy, 'set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh', "demo") def test_valid_lazy_hooks(self): # Make sure that all the registered lazy hooks are referring to existing # hook points which allow lazy registration. for key, callbacks in _mod_hooks._lazy_hooks.iteritems(): (module_name, member_name, hook_name) = key obj = pyutils.get_named_object(module_name, member_name) self.assertEqual(obj._module, module_name) self.assertEqual(obj._member_name, member_name) self.assertTrue(hook_name in obj) self.assertIs(callbacks, obj[hook_name]._callbacks)
def test_install_named_hook_lazy(self): hooks = Hooks("bzrlib.tests.hooks", "some_hooks") hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None) hooks.install_named_hook_lazy('set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh', "demo") self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
def test_uninstall_named_hook_old_style(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks["set_rh"] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertRaises(errors.UnsupportedOperation, hooks.uninstall_named_hook, "set_rh", "demo")
def test_uninstall_named_hook_unknown_callable(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks.add_hook('set_rh', "Set revision hsitory", (2, 0)) self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh", "demo")
def test_install_named_hook_and_retrieve_name(self): hooks = Hooks("bzrlib.tests.test_hooks", "somehooks") hooks['set_rh'] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertEqual("demo", hooks.get_hook_name(None))
def test_get_unnamed_hook_name_is_unnamed(self): hooks = Hooks() hooks['set_rh'] = [] self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None) self.assertEqual("No hook name", hooks.get_hook_name(None))
def test_install_named_hook_and_retrieve_name(self): hooks = Hooks() hooks['set_rh'] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertEqual("demo", hooks.get_hook_name(None))
def test_install_hook_appends_known_hook(self): """install_hook should append the callable for known hooks.""" hooks = Hooks() hooks['set_rh'] = [] self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None) self.assertEqual(hooks['set_rh'], [None])
def test_install_named_hook_raises_unknown_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly', None, "")
def test_install_named_hook_appends_known_hook(self): hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks") hooks['set_rh'] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertEqual(hooks['set_rh'], [None])
def test_install_named_hook_raises_unknown_hook(self): hooks = Hooks() self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly', None, "")
def test_install_named_hook_appends_known_hook(self): hooks = Hooks() hooks['set_rh'] = [] hooks.install_named_hook('set_rh', None, "demo") self.assertEqual(hooks['set_rh'], [None])
def test_install_hook_raises_unknown_hook(self): """install_hook should raise UnknownHook if a hook is unknown.""" hooks = Hooks() self.assertRaises(UnknownHook, self.applyDeprecated, one_five, hooks.install_hook, 'silly', None)