218
218
self.assertEqual([cmd], hook_calls)
220
220
commands.plugin_cmds.remove('fake')
223
class TestGetCommandHook(tests.TestCase):
225
def test_fires_on_get_cmd_object(self):
226
# The get_command(cmd) hook fires when commands are delivered to the
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'):
235
commands.Command.hooks.install_named_hook(
236
"get_command", get_cmd, None)
237
# create a command directly, should not fire
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)
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)
255
class TestListCommandHook(tests.TestCase):
257
def test_fires_on_all_command_names(self):
258
# The list_commands() hook fires when all_command_names() is invoked.
260
def list_my_commands(cmd_names):
261
hook_calls.append('called')
262
cmd_names.update(['foo', 'bar'])
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)