30
from ..commands import display_command
31
from . import TestSkipped
30
from brzlib.commands import display_command
31
from brzlib.tests import TestSkipped
34
34
class TestCommands(tests.TestCase):
131
129
def test_simple(self):
132
130
my_config = self._get_config("[ALIASES]\n"
133
"diff=diff -r -2..-1\n")
131
"diff=diff -r -2..-1\n")
134
132
self.assertEqual([u'diff', u'-r', u'-2..-1'],
135
commands.get_alias("diff", config=my_config))
133
commands.get_alias("diff", config=my_config))
137
135
def test_single_quotes(self):
138
136
my_config = self._get_config("[ALIASES]\n"
139
"diff=diff -r -2..-1 --diff-options "
140
"'--strip-trailing-cr -wp'\n")
137
"diff=diff -r -2..-1 --diff-options "
138
"'--strip-trailing-cr -wp'\n")
141
139
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
142
140
u'--strip-trailing-cr -wp'],
143
commands.get_alias("diff", config=my_config))
141
commands.get_alias("diff", config=my_config))
145
143
def test_double_quotes(self):
146
144
my_config = self._get_config("[ALIASES]\n"
147
"diff=diff -r -2..-1 --diff-options "
148
"\"--strip-trailing-cr -wp\"\n")
145
"diff=diff -r -2..-1 --diff-options "
146
"\"--strip-trailing-cr -wp\"\n")
149
147
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
150
148
u'--strip-trailing-cr -wp'],
151
commands.get_alias("diff", config=my_config))
149
commands.get_alias("diff", config=my_config))
153
151
def test_unicode(self):
154
152
my_config = self._get_config("[ALIASES]\n"
155
u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
153
u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
156
154
self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
157
commands.get_alias("iam", config=my_config))
155
commands.get_alias("iam", config=my_config))
160
158
class TestSeeAlso(tests.TestCase):
190
188
"""Additional terms can be supplied and are deduped and sorted."""
191
189
command = self._get_command_with_see_also(['foo', 'bar'])
192
190
self.assertEqual(['bar', 'foo', 'gam'],
193
command.get_see_also(['gam', 'bar', 'gam']))
191
command.get_see_also(['gam', 'bar', 'gam']))
196
194
class TestRegisterLazy(tests.TestCase):
199
197
super(TestRegisterLazy, self).setUp()
200
import breezy.tests.fake_command
201
del sys.modules['breezy.tests.fake_command']
198
import brzlib.tests.fake_command
199
del sys.modules['brzlib.tests.fake_command']
202
200
global lazy_command_imported
203
201
lazy_command_imported = False
204
202
commands.install_bzr_command_hooks()
208
206
commands.plugin_cmds.remove('fake')
210
208
def assertIsFakeCommand(self, cmd_obj):
211
from breezy.tests.fake_command import cmd_fake
209
from brzlib.tests.fake_command import cmd_fake
212
210
self.assertIsInstance(cmd_obj, cmd_fake)
214
212
def test_register_lazy(self):
215
213
"""Ensure lazy registration works"""
216
214
commands.plugin_cmds.register_lazy('cmd_fake', [],
217
'breezy.tests.fake_command')
215
'brzlib.tests.fake_command')
218
216
self.addCleanup(self.remove_fake)
219
217
self.assertFalse(lazy_command_imported)
220
218
fake_instance = commands.get_cmd_object('fake')
224
222
def test_get_unrelated_does_not_import(self):
225
223
commands.plugin_cmds.register_lazy('cmd_fake', [],
226
'breezy.tests.fake_command')
224
'brzlib.tests.fake_command')
227
225
self.addCleanup(self.remove_fake)
228
226
commands.get_cmd_object('status')
229
227
self.assertFalse(lazy_command_imported)
231
229
def test_aliases(self):
232
230
commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
233
'breezy.tests.fake_command')
231
'brzlib.tests.fake_command')
234
232
self.addCleanup(self.remove_fake)
235
233
fake_instance = commands.get_cmd_object('fake_alias')
236
234
self.assertIsFakeCommand(fake_instance)
248
246
commands.Command.hooks.install_named_hook(
249
247
"extend_command", hook_calls.append, None)
250
248
# create a command, should not fire
252
249
class cmd_test_extend_command_hook(commands.Command):
253
250
__doc__ = """A sample command."""
254
251
self.assertEqual([], hook_calls)
255
252
# -- as a builtin
256
253
# register the command class, should not fire
258
commands.builtin_command_registry.register(
259
cmd_test_extend_command_hook)
255
commands.builtin_command_registry.register(cmd_test_extend_command_hook)
260
256
self.assertEqual([], hook_calls)
261
257
# and ask for the object, should fire
262
258
cmd = commands.get_cmd_object('test-extend-command-hook')
266
262
self.assertSubset([cmd], hook_calls)
267
263
del hook_calls[:]
269
commands.builtin_command_registry.remove(
270
'test-extend-command-hook')
265
commands.builtin_command_registry.remove('test-extend-command-hook')
271
266
# -- as a plugin lazy registration
273
268
# register the command class, should not fire
274
269
commands.plugin_cmds.register_lazy('cmd_fake', [],
275
'breezy.tests.fake_command')
270
'brzlib.tests.fake_command')
276
271
self.assertEqual([], hook_calls)
277
272
# and ask for the object, should fire
278
273
cmd = commands.get_cmd_object('fake')
289
284
commands.install_bzr_command_hooks()
292
286
class ACommand(commands.Command):
293
287
__doc__ = """A sample command."""
295
288
def get_cmd(cmd_or_None, cmd_name):
296
289
hook_calls.append(('called', cmd_or_None, cmd_name))
297
290
if cmd_name in ('foo', 'info'):
316
309
self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
319
class TestCommandNotFound(tests.TestCase):
322
super(TestCommandNotFound, self).setUp()
323
commands._register_builtin_commands()
324
commands.install_bzr_command_hooks()
326
def test_not_found_no_suggestion(self):
327
e = self.assertRaises(errors.BzrCommandError,
328
commands.get_cmd_object, 'idontexistand')
329
self.assertEqual('unknown command "idontexistand"', str(e))
331
def test_not_found_with_suggestion(self):
332
e = self.assertRaises(errors.BzrCommandError,
333
commands.get_cmd_object, 'statue')
334
self.assertEqual('unknown command "statue". Perhaps you meant "status"',
338
312
class TestGetMissingCommandHook(tests.TestCase):
340
314
def hook_missing(self):
341
315
"""Hook get_missing_command for testing."""
342
316
self.hook_calls = []
344
317
class ACommand(commands.Command):
345
318
__doc__ = """A sample command."""
347
319
def get_missing_cmd(cmd_name):
348
320
self.hook_calls.append(('called', cmd_name))
349
321
if cmd_name in ('foo', 'info'):
386
358
# The list_commands() hook fires when all_command_names() is invoked.
388
360
commands.install_bzr_command_hooks()
390
361
def list_my_commands(cmd_names):
391
362
hook_calls.append('called')
392
363
cmd_names.update(['foo', 'bar'])
401
372
self.assertEqual(['called'], hook_calls)
402
373
self.assertSubset(['foo', 'bar'], cmds)
405
375
class TestPreAndPostCommandHooks(tests.TestCase):
406
class TestError(Exception):
376
class TestError(StandardError):
407
377
__doc__ = """A test exception."""
409
379
def test_pre_and_post_hooks(self):
459
429
def pre_command(cmd):
460
430
hook_calls.append('pre')
461
431
# verify that all subclasses of BzrCommandError caught too
462
raise commands.BzrOptionError()
432
raise errors.BzrOptionError()
464
434
def post_command(cmd, e):
465
435
self.fail('post_command should not be called')
479
449
commands.run_bzr, [u'rocks'])
480
450
self.assertEqual(['pre'], hook_calls)
483
class GuessCommandTests(tests.TestCase):
486
super(GuessCommandTests, self).setUp()
487
commands._register_builtin_commands()
488
commands.install_bzr_command_hooks()
490
def test_guess_override(self):
491
self.assertEqual('ci', commands.guess_command('ic'))
493
def test_guess(self):
494
commands.get_cmd_object('status')
495
self.assertEqual('status', commands.guess_command('statue'))
498
self.assertIs(None, commands.guess_command('nothingisevenclose'))