30
from bzrlib.commands import display_command
31
from bzrlib.tests import TestSkipped
30
from ..commands import display_command
31
from . import TestSkipped
34
34
class TestCommands(tests.TestCase):
61
63
def test_unicode_command(self):
62
64
# This error is thrown when we can't find the command in the
63
65
# list of available commands
64
self.assertRaises(errors.BzrCommandError,
66
self.assertRaises(errors.CommandError,
65
67
commands.run_bzr, [u'cmd\xb5'])
67
69
def test_unicode_option(self):
71
73
if optparse.__version__ == "1.5.3":
72
74
raise TestSkipped("optparse 1.5.3 can't handle unicode options")
73
self.assertRaises(errors.BzrCommandError,
75
self.assertRaises(errors.CommandError,
74
76
commands.run_bzr, ['log', u'--option\xb5'])
98
100
# We override the run() command method so we can observe the
99
101
# overrides from inside.
100
102
c = config.GlobalStack()
101
self.assertEquals('12', c.get('xx'))
102
self.assertEquals('foo', c.get('yy'))
103
self.assertEqual('12', c.get('xx'))
104
self.assertEqual('foo', c.get('yy'))
103
105
self.overrideAttr(builtins.cmd_rocks, 'run', run)
104
106
self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
105
107
c = config.GlobalStack()
106
108
# Ensure that we don't leak outside of the command
107
self.assertEquals(None, c.get('xx'))
108
self.assertEquals(None, c.get('yy'))
109
self.assertEqual(None, c.get('xx'))
110
self.assertEqual(None, c.get('yy'))
111
113
class TestInvokedAs(tests.TestCase):
117
119
# get one from the real get_cmd_object.
118
120
c = commands.get_cmd_object('ci')
119
121
self.assertIsInstance(c, builtins.cmd_commit)
120
self.assertEquals(c.invoked_as, 'ci')
122
self.assertEqual(c.invoked_as, 'ci')
123
125
class TestGetAlias(tests.TestCase):
129
131
def test_simple(self):
130
132
my_config = self._get_config("[ALIASES]\n"
131
"diff=diff -r -2..-1\n")
133
"diff=diff -r -2..-1\n")
132
134
self.assertEqual([u'diff', u'-r', u'-2..-1'],
133
commands.get_alias("diff", config=my_config))
135
commands.get_alias("diff", config=my_config))
135
137
def test_single_quotes(self):
136
138
my_config = self._get_config("[ALIASES]\n"
137
"diff=diff -r -2..-1 --diff-options "
138
"'--strip-trailing-cr -wp'\n")
139
"diff=diff -r -2..-1 --diff-options "
140
"'--strip-trailing-cr -wp'\n")
139
141
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
140
142
u'--strip-trailing-cr -wp'],
141
commands.get_alias("diff", config=my_config))
143
commands.get_alias("diff", config=my_config))
143
145
def test_double_quotes(self):
144
146
my_config = self._get_config("[ALIASES]\n"
145
"diff=diff -r -2..-1 --diff-options "
146
"\"--strip-trailing-cr -wp\"\n")
147
"diff=diff -r -2..-1 --diff-options "
148
"\"--strip-trailing-cr -wp\"\n")
147
149
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
148
150
u'--strip-trailing-cr -wp'],
149
commands.get_alias("diff", config=my_config))
151
commands.get_alias("diff", config=my_config))
151
153
def test_unicode(self):
152
154
my_config = self._get_config("[ALIASES]\n"
153
u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
155
u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
154
156
self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
155
commands.get_alias("iam", config=my_config))
157
commands.get_alias("iam", config=my_config))
158
160
class TestSeeAlso(tests.TestCase):
188
190
"""Additional terms can be supplied and are deduped and sorted."""
189
191
command = self._get_command_with_see_also(['foo', 'bar'])
190
192
self.assertEqual(['bar', 'foo', 'gam'],
191
command.get_see_also(['gam', 'bar', 'gam']))
193
command.get_see_also(['gam', 'bar', 'gam']))
194
196
class TestRegisterLazy(tests.TestCase):
197
199
super(TestRegisterLazy, self).setUp()
198
import bzrlib.tests.fake_command
199
del sys.modules['bzrlib.tests.fake_command']
200
import breezy.tests.fake_command
201
del sys.modules['breezy.tests.fake_command']
200
202
global lazy_command_imported
201
203
lazy_command_imported = False
202
204
commands.install_bzr_command_hooks()
206
208
commands.plugin_cmds.remove('fake')
208
210
def assertIsFakeCommand(self, cmd_obj):
209
from bzrlib.tests.fake_command import cmd_fake
211
from breezy.tests.fake_command import cmd_fake
210
212
self.assertIsInstance(cmd_obj, cmd_fake)
212
214
def test_register_lazy(self):
213
215
"""Ensure lazy registration works"""
214
216
commands.plugin_cmds.register_lazy('cmd_fake', [],
215
'bzrlib.tests.fake_command')
217
'breezy.tests.fake_command')
216
218
self.addCleanup(self.remove_fake)
217
219
self.assertFalse(lazy_command_imported)
218
220
fake_instance = commands.get_cmd_object('fake')
222
224
def test_get_unrelated_does_not_import(self):
223
225
commands.plugin_cmds.register_lazy('cmd_fake', [],
224
'bzrlib.tests.fake_command')
226
'breezy.tests.fake_command')
225
227
self.addCleanup(self.remove_fake)
226
228
commands.get_cmd_object('status')
227
229
self.assertFalse(lazy_command_imported)
229
231
def test_aliases(self):
230
232
commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
231
'bzrlib.tests.fake_command')
233
'breezy.tests.fake_command')
232
234
self.addCleanup(self.remove_fake)
233
235
fake_instance = commands.get_cmd_object('fake_alias')
234
236
self.assertIsFakeCommand(fake_instance)
246
248
commands.Command.hooks.install_named_hook(
247
249
"extend_command", hook_calls.append, None)
248
250
# create a command, should not fire
249
252
class cmd_test_extend_command_hook(commands.Command):
250
253
__doc__ = """A sample command."""
251
254
self.assertEqual([], hook_calls)
252
255
# -- as a builtin
253
256
# register the command class, should not fire
255
commands.builtin_command_registry.register(cmd_test_extend_command_hook)
258
commands.builtin_command_registry.register(
259
cmd_test_extend_command_hook)
256
260
self.assertEqual([], hook_calls)
257
261
# and ask for the object, should fire
258
262
cmd = commands.get_cmd_object('test-extend-command-hook')
262
266
self.assertSubset([cmd], hook_calls)
263
267
del hook_calls[:]
265
commands.builtin_command_registry.remove('test-extend-command-hook')
269
commands.builtin_command_registry.remove(
270
'test-extend-command-hook')
266
271
# -- as a plugin lazy registration
268
273
# register the command class, should not fire
269
274
commands.plugin_cmds.register_lazy('cmd_fake', [],
270
'bzrlib.tests.fake_command')
275
'breezy.tests.fake_command')
271
276
self.assertEqual([], hook_calls)
272
277
# and ask for the object, should fire
273
278
cmd = commands.get_cmd_object('fake')
284
289
commands.install_bzr_command_hooks()
286
292
class ACommand(commands.Command):
287
293
__doc__ = """A sample command."""
288
295
def get_cmd(cmd_or_None, cmd_name):
289
296
hook_calls.append(('called', cmd_or_None, cmd_name))
290
297
if cmd_name in ('foo', 'info'):
309
316
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.CommandError,
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.CommandError,
333
commands.get_cmd_object, 'statue')
334
self.assertEqual('unknown command "statue". Perhaps you meant "status"',
312
338
class TestGetMissingCommandHook(tests.TestCase):
314
340
def hook_missing(self):
315
341
"""Hook get_missing_command for testing."""
316
342
self.hook_calls = []
317
344
class ACommand(commands.Command):
318
345
__doc__ = """A sample command."""
319
347
def get_missing_cmd(cmd_name):
320
348
self.hook_calls.append(('called', cmd_name))
321
349
if cmd_name in ('foo', 'info'):
358
386
# The list_commands() hook fires when all_command_names() is invoked.
360
388
commands.install_bzr_command_hooks()
361
390
def list_my_commands(cmd_names):
362
391
hook_calls.append('called')
363
392
cmd_names.update(['foo', 'bar'])
372
401
self.assertEqual(['called'], hook_calls)
373
402
self.assertSubset(['foo', 'bar'], cmds)
375
405
class TestPreAndPostCommandHooks(tests.TestCase):
376
class TestError(StandardError):
406
class TestError(Exception):
377
407
__doc__ = """A test exception."""
379
409
def test_pre_and_post_hooks(self):
422
452
self.assertEqual(['run', 'post'], hook_calls)
424
454
def test_pre_command_error(self):
425
"""Ensure an BzrCommandError in pre_command aborts the command"""
455
"""Ensure an CommandError in pre_command aborts the command"""
429
459
def pre_command(cmd):
430
460
hook_calls.append('pre')
431
# verify that all subclasses of BzrCommandError caught too
432
raise errors.BzrOptionError()
461
# verify that all subclasses of CommandError caught too
462
raise commands.BzrOptionError()
434
464
def post_command(cmd, e):
435
465
self.fail('post_command should not be called')
445
475
"post_command", post_command, None)
447
477
self.assertEqual([], hook_calls)
448
self.assertRaises(errors.BzrCommandError,
478
self.assertRaises(errors.CommandError,
449
479
commands.run_bzr, [u'rocks'])
450
480
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'))