136
134
self.assertEqual(['bar', 'foo', 'gam'],
137
135
command.get_see_also(['gam', 'bar', 'gam']))
140
class TestRegisterLazy(tests.TestCase):
143
import bzrlib.tests.fake_command
144
del sys.modules['bzrlib.tests.fake_command']
145
global lazy_command_imported
146
lazy_command_imported = False
150
commands.plugin_cmds.remove('fake')
152
def assertIsFakeCommand(self, cmd_obj):
153
from bzrlib.tests.fake_command import cmd_fake
154
self.assertIsInstance(cmd_obj, cmd_fake)
156
def test_register_lazy(self):
157
"""Ensure lazy registration works"""
158
commands.plugin_cmds.register_lazy('cmd_fake', [],
159
'bzrlib.tests.fake_command')
160
self.addCleanup(self.remove_fake)
161
self.assertFalse(lazy_command_imported)
162
fake_instance = commands.get_cmd_object('fake')
163
self.assertTrue(lazy_command_imported)
164
self.assertIsFakeCommand(fake_instance)
166
def test_get_unrelated_does_not_import(self):
167
commands.plugin_cmds.register_lazy('cmd_fake', [],
168
'bzrlib.tests.fake_command')
169
self.addCleanup(self.remove_fake)
170
commands.get_cmd_object('status')
171
self.assertFalse(lazy_command_imported)
173
def test_aliases(self):
174
commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
175
'bzrlib.tests.fake_command')
176
self.addCleanup(self.remove_fake)
177
fake_instance = commands.get_cmd_object('fake_alias')
178
self.assertIsFakeCommand(fake_instance)
181
class TestExtendCommandHook(tests.TestCase):
183
def test_fires_on_get_cmd_object(self):
184
# The extend_command(cmd) hook fires when commands are delivered to the
185
# ui, not simply at registration (because lazy registered plugin
186
# commands are registered).
187
# when they are simply created.
189
commands.Command.hooks.install_named_hook(
190
"extend_command", hook_calls.append, None)
191
# create a command, should not fire
192
class ACommand(commands.Command):
193
"""A sample command."""
195
self.assertEqual([], hook_calls)
197
# register the command class, should not fire
199
builtins.cmd_test_extend_command_hook = ACommand
200
self.assertEqual([], hook_calls)
201
# and ask for the object, should fire
202
cmd = commands.get_cmd_object('test-extend-command-hook')
203
# For resilience - to ensure all code paths hit it - we
204
# fire on everything returned in the 'cmd_dict', which is currently
205
# all known commands, so assert that cmd is in hook_calls
206
self.assertSubset([cmd], hook_calls)
209
del builtins.cmd_test_extend_command_hook
210
# -- as a plugin lazy registration
212
# register the command class, should not fire
213
commands.plugin_cmds.register_lazy('cmd_fake', [],
214
'bzrlib.tests.fake_command')
215
self.assertEqual([], hook_calls)
216
# and ask for the object, should fire
217
cmd = commands.get_cmd_object('fake')
218
self.assertEqual([cmd], hook_calls)
220
commands.plugin_cmds.remove('fake')