~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commands.py

  • Committer: Robert Collins
  • Date: 2009-03-12 23:04:45 UTC
  • mto: This revision was merged to the branch mainline in revision 4441.
  • Revision ID: robertc@robertcollins.net-20090312230445-wu5x0s0z9jrenu2y
Add Command lookup hooks: list_commands and get_command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
218
218
            self.assertEqual([cmd], hook_calls)
219
219
        finally:
220
220
            commands.plugin_cmds.remove('fake')
 
221
 
 
222
 
 
223
class TestGetCommandHook(tests.TestCase):
 
224
 
 
225
    def test_fires_on_get_cmd_object(self):
 
226
        # The get_command(cmd) hook fires when commands are delivered to the
 
227
        # ui.
 
228
        hook_calls = []
 
229
        class ACommand(commands.Command):
 
230
            """A sample command."""
 
231
        def get_cmd(cmd_or_None, cmd_name):
 
232
            hook_calls.append(('called', cmd_or_None, cmd_name))
 
233
            if cmd_name in ('foo', 'info'):
 
234
                return ACommand()
 
235
        commands.Command.hooks.install_named_hook(
 
236
            "get_command", get_cmd, None)
 
237
        # create a command directly, should not fire
 
238
        cmd = ACommand()
 
239
        self.assertEqual([], hook_calls)
 
240
        # ask by name, should fire and give us our command
 
241
        cmd = commands.get_cmd_object('foo')
 
242
        self.assertEqual([('called', None, 'foo')], hook_calls)
 
243
        self.assertIsInstance(cmd, ACommand)
 
244
        del hook_calls[:]
 
245
        # ask by a name that is supplied by a builtin - the hook should still
 
246
        # fire and we still get our object, but we should see the builtin
 
247
        # passed to the hook.
 
248
        cmd = commands.get_cmd_object('info')
 
249
        self.assertIsInstance(cmd, ACommand)
 
250
        self.assertEqual(1, len(hook_calls))
 
251
        self.assertEqual('info', hook_calls[0][2])
 
252
        self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
 
253
 
 
254
 
 
255
class TestListCommandHook(tests.TestCase):
 
256
 
 
257
    def test_fires_on_all_command_names(self):
 
258
        # The list_commands() hook fires when all_command_names() is invoked.
 
259
        hook_calls = []
 
260
        def list_my_commands(cmd_names):
 
261
            hook_calls.append('called')
 
262
            cmd_names.update(['foo', 'bar'])
 
263
            return cmd_names
 
264
        commands.Command.hooks.install_named_hook(
 
265
            "list_commands", list_my_commands, None)
 
266
        # Get a command, which should not trigger the hook.
 
267
        cmd = commands.get_cmd_object('info')
 
268
        self.assertEqual([], hook_calls)
 
269
        # Get all command classes (for docs and shell completion).
 
270
        cmds = list(commands.all_command_names())
 
271
        self.assertEqual(['called'], hook_calls)
 
272
        self.assertSubset(['foo', 'bar'], cmds)