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
21
21
from bzrlib import (
34
33
class TestCommands(tests.TestCase):
35
def test_all_commands_have_help(self):
36
commands._register_builtin_commands()
37
commands_without_help = set()
38
base_doc = inspect.getdoc(commands.Command)
39
for cmd_name in commands.all_command_names():
40
cmd = commands.get_cmd_object(cmd_name)
42
if not cmd_help or cmd_help == base_doc:
43
commands_without_help.append(cmd_name)
44
self.assertLength(0, commands_without_help)
36
46
def test_display_command(self):
37
47
"""EPIPE message is selectively suppressed"""
38
48
def pipe_thrower():
80
90
self.assertContainsRe(c.get_help_text(), '--foo')
93
class TestInsideCommand(tests.TestCaseInTempDir):
95
def test_command_see_config_overrides(self):
97
# We override the run() command method so we can observe the
98
# overrides from inside.
99
c = config.GlobalStack()
100
self.assertEquals('12', c.get('xx'))
101
self.assertEquals('foo', c.get('yy'))
102
self.overrideAttr(builtins.cmd_rocks, 'run', run)
103
self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
104
c = config.GlobalStack()
105
# Ensure that we don't leak outside of the command
106
self.assertEquals(None, c.get('xx'))
107
self.assertEquals(None, c.get('yy'))
110
class TestInvokedAs(tests.TestCase):
112
def test_invoked_as(self):
113
"""The command object knows the actual name used to invoke it."""
114
commands.install_bzr_command_hooks()
115
commands._register_builtin_commands()
116
# get one from the real get_cmd_object.
117
c = commands.get_cmd_object('ci')
118
self.assertIsInstance(c, builtins.cmd_commit)
119
self.assertEquals(c.invoked_as, 'ci')
83
122
class TestGetAlias(tests.TestCase):
85
124
def _get_config(self, config_text):
86
my_config = config.GlobalConfig()
87
config_file = StringIO(config_text.encode('utf-8'))
88
my_config._parser = my_config._get_parser(file=config_file)
125
my_config = config.GlobalConfig.from_string(config_text)
91
128
def test_simple(self):
120
157
class TestSeeAlso(tests.TestCase):
121
158
"""Tests for the see also functional of Command."""
161
def _get_command_with_see_also(see_also):
162
class ACommand(commands.Command):
163
__doc__ = """A sample command."""
123
167
def test_default_subclass_no_see_also(self):
124
class ACommand(commands.Command):
125
"""A sample command."""
168
command = self._get_command_with_see_also([])
127
169
self.assertEqual([], command.get_see_also())
129
171
def test__see_also(self):
130
172
"""When _see_also is defined, it sets the result of get_see_also()."""
131
class ACommand(commands.Command):
132
_see_also = ['bar', 'foo']
173
command = self._get_command_with_see_also(['bar', 'foo'])
134
174
self.assertEqual(['bar', 'foo'], command.get_see_also())
136
176
def test_deduplication(self):
137
177
"""Duplicates in _see_also are stripped out."""
138
class ACommand(commands.Command):
139
_see_also = ['foo', 'foo']
178
command = self._get_command_with_see_also(['foo', 'foo'])
141
179
self.assertEqual(['foo'], command.get_see_also())
143
181
def test_sorted(self):
144
182
"""_see_also is sorted by get_see_also."""
145
class ACommand(commands.Command):
146
_see_also = ['foo', 'bar']
183
command = self._get_command_with_see_also(['foo', 'bar'])
148
184
self.assertEqual(['bar', 'foo'], command.get_see_also())
150
186
def test_additional_terms(self):
151
187
"""Additional terms can be supplied and are deduped and sorted."""
152
class ACommand(commands.Command):
153
_see_also = ['foo', 'bar']
188
command = self._get_command_with_see_also(['foo', 'bar'])
155
189
self.assertEqual(['bar', 'foo', 'gam'],
156
190
command.get_see_also(['gam', 'bar', 'gam']))
211
245
commands.Command.hooks.install_named_hook(
212
246
"extend_command", hook_calls.append, None)
213
247
# create a command, should not fire
214
class ACommand(commands.Command):
215
"""A sample command."""
248
class cmd_test_extend_command_hook(commands.Command):
249
__doc__ = """A sample command."""
217
250
self.assertEqual([], hook_calls)
218
251
# -- as a builtin
219
252
# register the command class, should not fire
221
builtins.cmd_test_extend_command_hook = ACommand
254
commands.builtin_command_registry.register(cmd_test_extend_command_hook)
222
255
self.assertEqual([], hook_calls)
223
256
# and ask for the object, should fire
224
257
cmd = commands.get_cmd_object('test-extend-command-hook')
228
261
self.assertSubset([cmd], hook_calls)
229
262
del hook_calls[:]
231
del builtins.cmd_test_extend_command_hook
264
commands.builtin_command_registry.remove('test-extend-command-hook')
232
265
# -- as a plugin lazy registration
234
267
# register the command class, should not fire
250
283
commands.install_bzr_command_hooks()
252
285
class ACommand(commands.Command):
253
"""A sample command."""
286
__doc__ = """A sample command."""
254
287
def get_cmd(cmd_or_None, cmd_name):
255
288
hook_calls.append(('called', cmd_or_None, cmd_name))
256
289
if cmd_name in ('foo', 'info'):
281
314
"""Hook get_missing_command for testing."""
282
315
self.hook_calls = []
283
316
class ACommand(commands.Command):
284
"""A sample command."""
317
__doc__ = """A sample command."""
285
318
def get_missing_cmd(cmd_name):
286
319
self.hook_calls.append(('called', cmd_name))
287
320
if cmd_name in ('foo', 'info'):
337
370
cmds = list(commands.all_command_names())
338
371
self.assertEqual(['called'], hook_calls)
339
372
self.assertSubset(['foo', 'bar'], cmds)
341
class TestDeprecations(tests.TestCase):
343
def test_shlex_split_unicode_deprecation(self):
344
res = self.applyDeprecated(
345
symbol_versioning.deprecated_in((2, 2, 0)),
346
commands.shlex_split_unicode, 'whatever')