~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commands.py

(vila) Fix test failures blocking package builds. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005-2011 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
from bzrlib.tests import TestCase
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import errno
 
18
import inspect
 
19
import sys
 
20
 
 
21
from bzrlib import (
 
22
    builtins,
 
23
    commands,
 
24
    config,
 
25
    errors,
 
26
    option,
 
27
    tests,
 
28
    trace,
 
29
    )
18
30
from bzrlib.commands import display_command
19
 
import errno
20
 
 
21
 
class TestCommands(TestCase):
 
31
from bzrlib.tests import TestSkipped
 
32
 
 
33
 
 
34
class TestCommands(tests.TestCase):
 
35
 
 
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)
 
42
            cmd_help = cmd.help()
 
43
            if not cmd_help or cmd_help == base_doc:
 
44
                commands_without_help.append(cmd_name)
 
45
        self.assertLength(0, commands_without_help)
 
46
 
22
47
    def test_display_command(self):
23
48
        """EPIPE message is selectively suppressed"""
24
49
        def pipe_thrower():
33
58
            raise IOError(errno.ESPIPE, "Bogus pipe error")
34
59
        self.assertRaises(IOError, other_thrower)
35
60
 
 
61
    def test_unicode_command(self):
 
62
        # This error is thrown when we can't find the command in the
 
63
        # list of available commands
 
64
        self.assertRaises(errors.BzrCommandError,
 
65
                          commands.run_bzr, [u'cmd\xb5'])
 
66
 
 
67
    def test_unicode_option(self):
 
68
        # This error is actually thrown by optparse, when it
 
69
        # can't find the given option
 
70
        import optparse
 
71
        if optparse.__version__ == "1.5.3":
 
72
            raise TestSkipped("optparse 1.5.3 can't handle unicode options")
 
73
        self.assertRaises(errors.BzrCommandError,
 
74
                          commands.run_bzr, ['log', u'--option\xb5'])
 
75
 
 
76
    @staticmethod
 
77
    def get_command(options):
 
78
        class cmd_foo(commands.Command):
 
79
            __doc__ = 'Bar'
 
80
 
 
81
            takes_options = options
 
82
 
 
83
        return cmd_foo()
 
84
 
 
85
    def test_help_hidden(self):
 
86
        c = self.get_command([option.Option('foo', hidden=True)])
 
87
        self.assertNotContainsRe(c.get_help_text(), '--foo')
 
88
 
 
89
    def test_help_not_hidden(self):
 
90
        c = self.get_command([option.Option('foo', hidden=False)])
 
91
        self.assertContainsRe(c.get_help_text(), '--foo')
 
92
 
 
93
 
 
94
class TestInsideCommand(tests.TestCaseInTempDir):
 
95
 
 
96
    def test_command_see_config_overrides(self):
 
97
        def run(cmd):
 
98
            # We override the run() command method so we can observe the
 
99
            # overrides from inside.
 
100
            c = config.GlobalStack()
 
101
            self.assertEquals('12', c.get('xx'))
 
102
            self.assertEquals('foo', c.get('yy'))
 
103
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
104
        self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
 
105
        c = config.GlobalStack()
 
106
        # 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
 
 
110
 
 
111
class TestInvokedAs(tests.TestCase):
 
112
 
 
113
    def test_invoked_as(self):
 
114
        """The command object knows the actual name used to invoke it."""
 
115
        commands.install_bzr_command_hooks()
 
116
        commands._register_builtin_commands()
 
117
        # get one from the real get_cmd_object.
 
118
        c = commands.get_cmd_object('ci')
 
119
        self.assertIsInstance(c, builtins.cmd_commit)
 
120
        self.assertEquals(c.invoked_as, 'ci')
 
121
 
 
122
 
 
123
class TestGetAlias(tests.TestCase):
 
124
 
 
125
    def _get_config(self, config_text):
 
126
        my_config = config.GlobalConfig.from_string(config_text)
 
127
        return my_config
 
128
 
 
129
    def test_simple(self):
 
130
        my_config = self._get_config("[ALIASES]\n"
 
131
            "diff=diff -r -2..-1\n")
 
132
        self.assertEqual([u'diff', u'-r', u'-2..-1'],
 
133
            commands.get_alias("diff", config=my_config))
 
134
 
 
135
    def test_single_quotes(self):
 
136
        my_config = self._get_config("[ALIASES]\n"
 
137
            "diff=diff -r -2..-1 --diff-options "
 
138
            "'--strip-trailing-cr -wp'\n")
 
139
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
 
140
                          u'--strip-trailing-cr -wp'],
 
141
                          commands.get_alias("diff", config=my_config))
 
142
 
 
143
    def test_double_quotes(self):
 
144
        my_config = self._get_config("[ALIASES]\n"
 
145
            "diff=diff -r -2..-1 --diff-options "
 
146
            "\"--strip-trailing-cr -wp\"\n")
 
147
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
 
148
                          u'--strip-trailing-cr -wp'],
 
149
                          commands.get_alias("diff", config=my_config))
 
150
 
 
151
    def test_unicode(self):
 
152
        my_config = self._get_config("[ALIASES]\n"
 
153
            u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
 
154
        self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
 
155
                          commands.get_alias("iam", config=my_config))
 
156
 
 
157
 
 
158
class TestSeeAlso(tests.TestCase):
 
159
    """Tests for the see also functional of Command."""
 
160
 
 
161
    @staticmethod
 
162
    def _get_command_with_see_also(see_also):
 
163
        class ACommand(commands.Command):
 
164
            __doc__ = """A sample command."""
 
165
            _see_also = see_also
 
166
        return ACommand()
 
167
 
 
168
    def test_default_subclass_no_see_also(self):
 
169
        command = self._get_command_with_see_also([])
 
170
        self.assertEqual([], command.get_see_also())
 
171
 
 
172
    def test__see_also(self):
 
173
        """When _see_also is defined, it sets the result of get_see_also()."""
 
174
        command = self._get_command_with_see_also(['bar', 'foo'])
 
175
        self.assertEqual(['bar', 'foo'], command.get_see_also())
 
176
 
 
177
    def test_deduplication(self):
 
178
        """Duplicates in _see_also are stripped out."""
 
179
        command = self._get_command_with_see_also(['foo', 'foo'])
 
180
        self.assertEqual(['foo'], command.get_see_also())
 
181
 
 
182
    def test_sorted(self):
 
183
        """_see_also is sorted by get_see_also."""
 
184
        command = self._get_command_with_see_also(['foo', 'bar'])
 
185
        self.assertEqual(['bar', 'foo'], command.get_see_also())
 
186
 
 
187
    def test_additional_terms(self):
 
188
        """Additional terms can be supplied and are deduped and sorted."""
 
189
        command = self._get_command_with_see_also(['foo', 'bar'])
 
190
        self.assertEqual(['bar', 'foo', 'gam'],
 
191
            command.get_see_also(['gam', 'bar', 'gam']))
 
192
 
 
193
 
 
194
class TestRegisterLazy(tests.TestCase):
 
195
 
 
196
    def setUp(self):
 
197
        super(TestRegisterLazy, self).setUp()
 
198
        import bzrlib.tests.fake_command
 
199
        del sys.modules['bzrlib.tests.fake_command']
 
200
        global lazy_command_imported
 
201
        lazy_command_imported = False
 
202
        commands.install_bzr_command_hooks()
 
203
 
 
204
    @staticmethod
 
205
    def remove_fake():
 
206
        commands.plugin_cmds.remove('fake')
 
207
 
 
208
    def assertIsFakeCommand(self, cmd_obj):
 
209
        from bzrlib.tests.fake_command import cmd_fake
 
210
        self.assertIsInstance(cmd_obj, cmd_fake)
 
211
 
 
212
    def test_register_lazy(self):
 
213
        """Ensure lazy registration works"""
 
214
        commands.plugin_cmds.register_lazy('cmd_fake', [],
 
215
                                           'bzrlib.tests.fake_command')
 
216
        self.addCleanup(self.remove_fake)
 
217
        self.assertFalse(lazy_command_imported)
 
218
        fake_instance = commands.get_cmd_object('fake')
 
219
        self.assertTrue(lazy_command_imported)
 
220
        self.assertIsFakeCommand(fake_instance)
 
221
 
 
222
    def test_get_unrelated_does_not_import(self):
 
223
        commands.plugin_cmds.register_lazy('cmd_fake', [],
 
224
                                           'bzrlib.tests.fake_command')
 
225
        self.addCleanup(self.remove_fake)
 
226
        commands.get_cmd_object('status')
 
227
        self.assertFalse(lazy_command_imported)
 
228
 
 
229
    def test_aliases(self):
 
230
        commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
 
231
                                           'bzrlib.tests.fake_command')
 
232
        self.addCleanup(self.remove_fake)
 
233
        fake_instance = commands.get_cmd_object('fake_alias')
 
234
        self.assertIsFakeCommand(fake_instance)
 
235
 
 
236
 
 
237
class TestExtendCommandHook(tests.TestCase):
 
238
 
 
239
    def test_fires_on_get_cmd_object(self):
 
240
        # The extend_command(cmd) hook fires when commands are delivered to the
 
241
        # ui, not simply at registration (because lazy registered plugin
 
242
        # commands are registered).
 
243
        # when they are simply created.
 
244
        hook_calls = []
 
245
        commands.install_bzr_command_hooks()
 
246
        commands.Command.hooks.install_named_hook(
 
247
            "extend_command", hook_calls.append, None)
 
248
        # create a command, should not fire
 
249
        class cmd_test_extend_command_hook(commands.Command):
 
250
            __doc__ = """A sample command."""
 
251
        self.assertEqual([], hook_calls)
 
252
        # -- as a builtin
 
253
        # register the command class, should not fire
 
254
        try:
 
255
            commands.builtin_command_registry.register(cmd_test_extend_command_hook)
 
256
            self.assertEqual([], hook_calls)
 
257
            # and ask for the object, should fire
 
258
            cmd = commands.get_cmd_object('test-extend-command-hook')
 
259
            # For resilience - to ensure all code paths hit it - we
 
260
            # fire on everything returned in the 'cmd_dict', which is currently
 
261
            # all known commands, so assert that cmd is in hook_calls
 
262
            self.assertSubset([cmd], hook_calls)
 
263
            del hook_calls[:]
 
264
        finally:
 
265
            commands.builtin_command_registry.remove('test-extend-command-hook')
 
266
        # -- as a plugin lazy registration
 
267
        try:
 
268
            # register the command class, should not fire
 
269
            commands.plugin_cmds.register_lazy('cmd_fake', [],
 
270
                                               'bzrlib.tests.fake_command')
 
271
            self.assertEqual([], hook_calls)
 
272
            # and ask for the object, should fire
 
273
            cmd = commands.get_cmd_object('fake')
 
274
            self.assertEqual([cmd], hook_calls)
 
275
        finally:
 
276
            commands.plugin_cmds.remove('fake')
 
277
 
 
278
 
 
279
class TestGetCommandHook(tests.TestCase):
 
280
 
 
281
    def test_fires_on_get_cmd_object(self):
 
282
        # The get_command(cmd) hook fires when commands are delivered to the
 
283
        # ui.
 
284
        commands.install_bzr_command_hooks()
 
285
        hook_calls = []
 
286
        class ACommand(commands.Command):
 
287
            __doc__ = """A sample command."""
 
288
        def get_cmd(cmd_or_None, cmd_name):
 
289
            hook_calls.append(('called', cmd_or_None, cmd_name))
 
290
            if cmd_name in ('foo', 'info'):
 
291
                return ACommand()
 
292
        commands.Command.hooks.install_named_hook(
 
293
            "get_command", get_cmd, None)
 
294
        # create a command directly, should not fire
 
295
        cmd = ACommand()
 
296
        self.assertEqual([], hook_calls)
 
297
        # ask by name, should fire and give us our command
 
298
        cmd = commands.get_cmd_object('foo')
 
299
        self.assertEqual([('called', None, 'foo')], hook_calls)
 
300
        self.assertIsInstance(cmd, ACommand)
 
301
        del hook_calls[:]
 
302
        # ask by a name that is supplied by a builtin - the hook should still
 
303
        # fire and we still get our object, but we should see the builtin
 
304
        # passed to the hook.
 
305
        cmd = commands.get_cmd_object('info')
 
306
        self.assertIsInstance(cmd, ACommand)
 
307
        self.assertEqual(1, len(hook_calls))
 
308
        self.assertEqual('info', hook_calls[0][2])
 
309
        self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
 
310
 
 
311
 
 
312
class TestGetMissingCommandHook(tests.TestCase):
 
313
 
 
314
    def hook_missing(self):
 
315
        """Hook get_missing_command for testing."""
 
316
        self.hook_calls = []
 
317
        class ACommand(commands.Command):
 
318
            __doc__ = """A sample command."""
 
319
        def get_missing_cmd(cmd_name):
 
320
            self.hook_calls.append(('called', cmd_name))
 
321
            if cmd_name in ('foo', 'info'):
 
322
                return ACommand()
 
323
        commands.Command.hooks.install_named_hook(
 
324
            "get_missing_command", get_missing_cmd, None)
 
325
        self.ACommand = ACommand
 
326
 
 
327
    def test_fires_on_get_cmd_object(self):
 
328
        # The get_missing_command(cmd) hook fires when commands are delivered to the
 
329
        # ui.
 
330
        self.hook_missing()
 
331
        # create a command directly, should not fire
 
332
        self.cmd = self.ACommand()
 
333
        self.assertEqual([], self.hook_calls)
 
334
        # ask by name, should fire and give us our command
 
335
        cmd = commands.get_cmd_object('foo')
 
336
        self.assertEqual([('called', 'foo')], self.hook_calls)
 
337
        self.assertIsInstance(cmd, self.ACommand)
 
338
        del self.hook_calls[:]
 
339
        # ask by a name that is supplied by a builtin - the hook should not
 
340
        # fire and we still get our object.
 
341
        commands.install_bzr_command_hooks()
 
342
        cmd = commands.get_cmd_object('info')
 
343
        self.assertNotEqual(None, cmd)
 
344
        self.assertEqual(0, len(self.hook_calls))
 
345
 
 
346
    def test_skipped_on_HelpCommandIndex_get_topics(self):
 
347
        # The get_missing_command(cmd_name) hook is not fired when
 
348
        # looking up help topics.
 
349
        self.hook_missing()
 
350
        topic = commands.HelpCommandIndex()
 
351
        topics = topic.get_topics('foo')
 
352
        self.assertEqual([], self.hook_calls)
 
353
 
 
354
 
 
355
class TestListCommandHook(tests.TestCase):
 
356
 
 
357
    def test_fires_on_all_command_names(self):
 
358
        # The list_commands() hook fires when all_command_names() is invoked.
 
359
        hook_calls = []
 
360
        commands.install_bzr_command_hooks()
 
361
        def list_my_commands(cmd_names):
 
362
            hook_calls.append('called')
 
363
            cmd_names.update(['foo', 'bar'])
 
364
            return cmd_names
 
365
        commands.Command.hooks.install_named_hook(
 
366
            "list_commands", list_my_commands, None)
 
367
        # Get a command, which should not trigger the hook.
 
368
        cmd = commands.get_cmd_object('info')
 
369
        self.assertEqual([], hook_calls)
 
370
        # Get all command classes (for docs and shell completion).
 
371
        cmds = list(commands.all_command_names())
 
372
        self.assertEqual(['called'], hook_calls)
 
373
        self.assertSubset(['foo', 'bar'], cmds)
 
374
 
 
375
class TestPreAndPostCommandHooks(tests.TestCase):
 
376
    class TestError(StandardError):
 
377
        __doc__ = """A test exception."""
 
378
 
 
379
    def test_pre_and_post_hooks(self):
 
380
        hook_calls = []
 
381
 
 
382
        def pre_command(cmd):
 
383
            self.assertEqual([], hook_calls)
 
384
            hook_calls.append('pre')
 
385
 
 
386
        def post_command(cmd):
 
387
            self.assertEqual(['pre', 'run'], hook_calls)
 
388
            hook_calls.append('post')
 
389
 
 
390
        def run(cmd):
 
391
            self.assertEqual(['pre'], hook_calls)
 
392
            hook_calls.append('run')
 
393
 
 
394
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
395
        commands.install_bzr_command_hooks()
 
396
        commands.Command.hooks.install_named_hook(
 
397
            "pre_command", pre_command, None)
 
398
        commands.Command.hooks.install_named_hook(
 
399
            "post_command", post_command, None)
 
400
 
 
401
        self.assertEqual([], hook_calls)
 
402
        self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
 
403
        self.assertEqual(['pre', 'run', 'post'], hook_calls)
 
404
 
 
405
    def test_post_hook_provided_exception(self):
 
406
        hook_calls = []
 
407
 
 
408
        def post_command(cmd):
 
409
            hook_calls.append('post')
 
410
 
 
411
        def run(cmd):
 
412
            hook_calls.append('run')
 
413
            raise self.TestError()
 
414
 
 
415
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
416
        commands.install_bzr_command_hooks()
 
417
        commands.Command.hooks.install_named_hook(
 
418
            "post_command", post_command, None)
 
419
 
 
420
        self.assertEqual([], hook_calls)
 
421
        self.assertRaises(self.TestError, commands.run_bzr, [u'rocks'])
 
422
        self.assertEqual(['run', 'post'], hook_calls)
 
423
 
 
424
    def test_pre_command_error(self):
 
425
        """Ensure an BzrCommandError in pre_command aborts the command"""
 
426
 
 
427
        hook_calls = []
 
428
 
 
429
        def pre_command(cmd):
 
430
            hook_calls.append('pre')
 
431
            # verify that all subclasses of BzrCommandError caught too
 
432
            raise errors.BzrOptionError()
 
433
 
 
434
        def post_command(cmd, e):
 
435
            self.fail('post_command should not be called')
 
436
 
 
437
        def run(cmd):
 
438
            self.fail('command should not be called')
 
439
 
 
440
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
441
        commands.install_bzr_command_hooks()
 
442
        commands.Command.hooks.install_named_hook(
 
443
            "pre_command", pre_command, None)
 
444
        commands.Command.hooks.install_named_hook(
 
445
            "post_command", post_command, None)
 
446
 
 
447
        self.assertEqual([], hook_calls)
 
448
        self.assertRaises(errors.BzrCommandError,
 
449
                          commands.run_bzr, [u'rocks'])
 
450
        self.assertEqual(['pre'], hook_calls)
 
451