14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from cStringIO import StringIO
30
from ..commands import display_command
31
from . import TestSkipped
29
from bzrlib.commands import display_command
30
from bzrlib.tests import TestSkipped
34
33
class TestCommands(tests.TestCase):
36
def test_all_commands_have_help(self):
37
commands._register_builtin_commands()
38
commands_without_help = set()
39
base_doc = inspect.getdoc(commands.Command)
40
for cmd_name in commands.all_command_names():
41
cmd = commands.get_cmd_object(cmd_name)
43
if not cmd_help or cmd_help == base_doc:
44
commands_without_help.append(cmd_name)
45
self.assertLength(0, commands_without_help)
47
35
def test_display_command(self):
48
36
"""EPIPE message is selectively suppressed"""
49
37
def pipe_thrower():
50
38
raise IOError(errno.EPIPE, "Bogus pipe error")
51
39
self.assertRaises(IOError, pipe_thrower)
59
45
def other_thrower():
60
46
raise IOError(errno.ESPIPE, "Bogus pipe error")
93
79
self.assertContainsRe(c.get_help_text(), '--foo')
96
class TestInsideCommand(tests.TestCaseInTempDir):
98
def test_command_see_config_overrides(self):
100
# We override the run() command method so we can observe the
101
# overrides from inside.
102
c = config.GlobalStack()
103
self.assertEqual('12', c.get('xx'))
104
self.assertEqual('foo', c.get('yy'))
105
self.overrideAttr(builtins.cmd_rocks, 'run', run)
106
self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
107
c = config.GlobalStack()
108
# Ensure that we don't leak outside of the command
109
self.assertEqual(None, c.get('xx'))
110
self.assertEqual(None, c.get('yy'))
113
class TestInvokedAs(tests.TestCase):
115
def test_invoked_as(self):
116
"""The command object knows the actual name used to invoke it."""
117
commands.install_bzr_command_hooks()
118
commands._register_builtin_commands()
119
# get one from the real get_cmd_object.
120
c = commands.get_cmd_object('ci')
121
self.assertIsInstance(c, builtins.cmd_commit)
122
self.assertEqual(c.invoked_as, 'ci')
125
82
class TestGetAlias(tests.TestCase):
127
84
def _get_config(self, config_text):
128
my_config = config.GlobalConfig.from_string(config_text)
85
my_config = config.GlobalConfig()
86
config_file = StringIO(config_text.encode('utf-8'))
87
my_config._parser = my_config._get_parser(file=config_file)
131
90
def test_simple(self):
132
91
my_config = self._get_config("[ALIASES]\n"
133
"diff=diff -r -2..-1\n")
92
"diff=diff -r -2..-1\n")
134
93
self.assertEqual([u'diff', u'-r', u'-2..-1'],
135
commands.get_alias("diff", config=my_config))
94
commands.get_alias("diff", config=my_config))
137
96
def test_single_quotes(self):
138
97
my_config = self._get_config("[ALIASES]\n"
139
"diff=diff -r -2..-1 --diff-options "
140
"'--strip-trailing-cr -wp'\n")
98
"diff=diff -r -2..-1 --diff-options "
99
"'--strip-trailing-cr -wp'\n")
141
100
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
142
101
u'--strip-trailing-cr -wp'],
143
commands.get_alias("diff", config=my_config))
102
commands.get_alias("diff", config=my_config))
145
104
def test_double_quotes(self):
146
105
my_config = self._get_config("[ALIASES]\n"
147
"diff=diff -r -2..-1 --diff-options "
148
"\"--strip-trailing-cr -wp\"\n")
106
"diff=diff -r -2..-1 --diff-options "
107
"\"--strip-trailing-cr -wp\"\n")
149
108
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
150
109
u'--strip-trailing-cr -wp'],
151
commands.get_alias("diff", config=my_config))
110
commands.get_alias("diff", config=my_config))
153
112
def test_unicode(self):
154
113
my_config = self._get_config("[ALIASES]\n"
155
u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
114
u"iam=whoami 'Erik B\u00e5gfors <erik@bagfors.nu>'\n")
156
115
self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
157
commands.get_alias("iam", config=my_config))
116
commands.get_alias("iam", config=my_config))
160
119
class TestSeeAlso(tests.TestCase):
161
120
"""Tests for the see also functional of Command."""
164
def _get_command_with_see_also(see_also):
165
class ACommand(commands.Command):
166
__doc__ = """A sample command."""
170
122
def test_default_subclass_no_see_also(self):
171
command = self._get_command_with_see_also([])
123
class ACommand(commands.Command):
124
"""A sample command."""
172
126
self.assertEqual([], command.get_see_also())
174
128
def test__see_also(self):
175
129
"""When _see_also is defined, it sets the result of get_see_also()."""
176
command = self._get_command_with_see_also(['bar', 'foo'])
130
class ACommand(commands.Command):
131
_see_also = ['bar', 'foo']
177
133
self.assertEqual(['bar', 'foo'], command.get_see_also())
179
135
def test_deduplication(self):
180
136
"""Duplicates in _see_also are stripped out."""
181
command = self._get_command_with_see_also(['foo', 'foo'])
137
class ACommand(commands.Command):
138
_see_also = ['foo', 'foo']
182
140
self.assertEqual(['foo'], command.get_see_also())
184
142
def test_sorted(self):
185
143
"""_see_also is sorted by get_see_also."""
186
command = self._get_command_with_see_also(['foo', 'bar'])
144
class ACommand(commands.Command):
145
_see_also = ['foo', 'bar']
187
147
self.assertEqual(['bar', 'foo'], command.get_see_also())
189
149
def test_additional_terms(self):
190
150
"""Additional terms can be supplied and are deduped and sorted."""
191
command = self._get_command_with_see_also(['foo', 'bar'])
151
class ACommand(commands.Command):
152
_see_also = ['foo', 'bar']
192
154
self.assertEqual(['bar', 'foo', 'gam'],
193
command.get_see_also(['gam', 'bar', 'gam']))
155
command.get_see_also(['gam', 'bar', 'gam']))
196
158
class TestRegisterLazy(tests.TestCase):
199
super(TestRegisterLazy, self).setUp()
200
import breezy.tests.fake_command
201
del sys.modules['breezy.tests.fake_command']
161
tests.TestCase.setUp(self)
162
import bzrlib.tests.fake_command
163
del sys.modules['bzrlib.tests.fake_command']
202
164
global lazy_command_imported
203
165
lazy_command_imported = False
204
166
commands.install_bzr_command_hooks()
316
274
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
277
class TestGetMissingCommandHook(tests.TestCase):
340
def hook_missing(self):
341
"""Hook get_missing_command for testing."""
279
def test_fires_on_get_cmd_object(self):
280
# The get_missing_command(cmd) hook fires when commands are delivered to the
344
283
class ACommand(commands.Command):
345
__doc__ = """A sample command."""
284
"""A sample command."""
347
285
def get_missing_cmd(cmd_name):
348
self.hook_calls.append(('called', cmd_name))
286
hook_calls.append(('called', cmd_name))
349
287
if cmd_name in ('foo', 'info'):
350
288
return ACommand()
351
289
commands.Command.hooks.install_named_hook(
352
290
"get_missing_command", get_missing_cmd, None)
353
self.ACommand = ACommand
355
def test_fires_on_get_cmd_object(self):
356
# The get_missing_command(cmd) hook fires when commands are delivered to the
359
291
# create a command directly, should not fire
360
self.cmd = self.ACommand()
361
self.assertEqual([], self.hook_calls)
293
self.assertEqual([], hook_calls)
362
294
# ask by name, should fire and give us our command
363
295
cmd = commands.get_cmd_object('foo')
364
self.assertEqual([('called', 'foo')], self.hook_calls)
365
self.assertIsInstance(cmd, self.ACommand)
366
del self.hook_calls[:]
296
self.assertEqual([('called', 'foo')], hook_calls)
297
self.assertIsInstance(cmd, ACommand)
367
299
# ask by a name that is supplied by a builtin - the hook should not
368
300
# fire and we still get our object.
369
301
commands.install_bzr_command_hooks()
370
302
cmd = commands.get_cmd_object('info')
371
303
self.assertNotEqual(None, cmd)
372
self.assertEqual(0, len(self.hook_calls))
374
def test_skipped_on_HelpCommandIndex_get_topics(self):
375
# The get_missing_command(cmd_name) hook is not fired when
376
# looking up help topics.
378
topic = commands.HelpCommandIndex()
379
topics = topic.get_topics('foo')
380
self.assertEqual([], self.hook_calls)
304
self.assertEqual(0, len(hook_calls))
383
307
class TestListCommandHook(tests.TestCase):
400
323
cmds = list(commands.all_command_names())
401
324
self.assertEqual(['called'], hook_calls)
402
325
self.assertSubset(['foo', 'bar'], cmds)
405
class TestPreAndPostCommandHooks(tests.TestCase):
406
class TestError(Exception):
407
__doc__ = """A test exception."""
409
def test_pre_and_post_hooks(self):
412
def pre_command(cmd):
413
self.assertEqual([], hook_calls)
414
hook_calls.append('pre')
416
def post_command(cmd):
417
self.assertEqual(['pre', 'run'], hook_calls)
418
hook_calls.append('post')
421
self.assertEqual(['pre'], hook_calls)
422
hook_calls.append('run')
424
self.overrideAttr(builtins.cmd_rocks, 'run', run)
425
commands.install_bzr_command_hooks()
426
commands.Command.hooks.install_named_hook(
427
"pre_command", pre_command, None)
428
commands.Command.hooks.install_named_hook(
429
"post_command", post_command, None)
431
self.assertEqual([], hook_calls)
432
self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
433
self.assertEqual(['pre', 'run', 'post'], hook_calls)
435
def test_post_hook_provided_exception(self):
438
def post_command(cmd):
439
hook_calls.append('post')
442
hook_calls.append('run')
443
raise self.TestError()
445
self.overrideAttr(builtins.cmd_rocks, 'run', run)
446
commands.install_bzr_command_hooks()
447
commands.Command.hooks.install_named_hook(
448
"post_command", post_command, None)
450
self.assertEqual([], hook_calls)
451
self.assertRaises(self.TestError, commands.run_bzr, [u'rocks'])
452
self.assertEqual(['run', 'post'], hook_calls)
454
def test_pre_command_error(self):
455
"""Ensure an BzrCommandError in pre_command aborts the command"""
459
def pre_command(cmd):
460
hook_calls.append('pre')
461
# verify that all subclasses of BzrCommandError caught too
462
raise commands.BzrOptionError()
464
def post_command(cmd, e):
465
self.fail('post_command should not be called')
468
self.fail('command should not be called')
470
self.overrideAttr(builtins.cmd_rocks, 'run', run)
471
commands.install_bzr_command_hooks()
472
commands.Command.hooks.install_named_hook(
473
"pre_command", pre_command, None)
474
commands.Command.hooks.install_named_hook(
475
"post_command", post_command, None)
477
self.assertEqual([], hook_calls)
478
self.assertRaises(errors.BzrCommandError,
479
commands.run_bzr, [u'rocks'])
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'))