/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commands.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
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
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
16
16
 
 
17
from cStringIO import StringIO
17
18
import errno
18
19
import inspect
19
20
import sys
20
21
 
21
 
from .. import (
 
22
from bzrlib import (
22
23
    builtins,
23
24
    commands,
24
25
    config,
25
26
    errors,
26
27
    option,
 
28
    symbol_versioning,
27
29
    tests,
28
 
    trace,
29
30
    )
30
 
from ..commands import display_command
31
 
from . import TestSkipped
 
31
from bzrlib.commands import display_command
 
32
from bzrlib.tests import TestSkipped
32
33
 
33
34
 
34
35
class TestCommands(tests.TestCase):
49
50
        def pipe_thrower():
50
51
            raise IOError(errno.EPIPE, "Bogus pipe error")
51
52
        self.assertRaises(IOError, pipe_thrower)
52
 
 
53
53
        @display_command
54
54
        def non_thrower():
55
55
            pipe_thrower()
56
56
        non_thrower()
57
 
 
58
57
        @display_command
59
58
        def other_thrower():
60
59
            raise IOError(errno.ESPIPE, "Bogus pipe error")
93
92
        self.assertContainsRe(c.get_help_text(), '--foo')
94
93
 
95
94
 
96
 
class TestInsideCommand(tests.TestCaseInTempDir):
97
 
 
98
 
    def test_command_see_config_overrides(self):
99
 
        def run(cmd):
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'))
111
 
 
112
 
 
113
 
class TestInvokedAs(tests.TestCase):
114
 
 
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')
123
 
 
124
 
 
125
95
class TestGetAlias(tests.TestCase):
126
96
 
127
97
    def _get_config(self, config_text):
128
 
        my_config = config.GlobalConfig.from_string(config_text)
 
98
        my_config = config.GlobalConfig()
 
99
        config_file = StringIO(config_text.encode('utf-8'))
 
100
        my_config._parser = my_config._get_parser(file=config_file)
129
101
        return my_config
130
102
 
131
103
    def test_simple(self):
132
104
        my_config = self._get_config("[ALIASES]\n"
133
 
                                     "diff=diff -r -2..-1\n")
 
105
            "diff=diff -r -2..-1\n")
134
106
        self.assertEqual([u'diff', u'-r', u'-2..-1'],
135
 
                         commands.get_alias("diff", config=my_config))
 
107
            commands.get_alias("diff", config=my_config))
136
108
 
137
109
    def test_single_quotes(self):
138
110
        my_config = self._get_config("[ALIASES]\n"
139
 
                                     "diff=diff -r -2..-1 --diff-options "
140
 
                                     "'--strip-trailing-cr -wp'\n")
 
111
            "diff=diff -r -2..-1 --diff-options "
 
112
            "'--strip-trailing-cr -wp'\n")
141
113
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
142
114
                          u'--strip-trailing-cr -wp'],
143
 
                         commands.get_alias("diff", config=my_config))
 
115
                          commands.get_alias("diff", config=my_config))
144
116
 
145
117
    def test_double_quotes(self):
146
118
        my_config = self._get_config("[ALIASES]\n"
147
 
                                     "diff=diff -r -2..-1 --diff-options "
148
 
                                     "\"--strip-trailing-cr -wp\"\n")
 
119
            "diff=diff -r -2..-1 --diff-options "
 
120
            "\"--strip-trailing-cr -wp\"\n")
149
121
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
150
122
                          u'--strip-trailing-cr -wp'],
151
 
                         commands.get_alias("diff", config=my_config))
 
123
                          commands.get_alias("diff", config=my_config))
152
124
 
153
125
    def test_unicode(self):
154
126
        my_config = self._get_config("[ALIASES]\n"
155
 
                                     u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
 
127
            u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
156
128
        self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
157
 
                         commands.get_alias("iam", config=my_config))
 
129
                          commands.get_alias("iam", config=my_config))
158
130
 
159
131
 
160
132
class TestSeeAlso(tests.TestCase):
190
162
        """Additional terms can be supplied and are deduped and sorted."""
191
163
        command = self._get_command_with_see_also(['foo', 'bar'])
192
164
        self.assertEqual(['bar', 'foo', 'gam'],
193
 
                         command.get_see_also(['gam', 'bar', 'gam']))
 
165
            command.get_see_also(['gam', 'bar', 'gam']))
194
166
 
195
167
 
196
168
class TestRegisterLazy(tests.TestCase):
197
169
 
198
170
    def setUp(self):
199
 
        super(TestRegisterLazy, self).setUp()
200
 
        import breezy.tests.fake_command
201
 
        del sys.modules['breezy.tests.fake_command']
 
171
        tests.TestCase.setUp(self)
 
172
        import bzrlib.tests.fake_command
 
173
        del sys.modules['bzrlib.tests.fake_command']
202
174
        global lazy_command_imported
203
175
        lazy_command_imported = False
204
176
        commands.install_bzr_command_hooks()
208
180
        commands.plugin_cmds.remove('fake')
209
181
 
210
182
    def assertIsFakeCommand(self, cmd_obj):
211
 
        from breezy.tests.fake_command import cmd_fake
 
183
        from bzrlib.tests.fake_command import cmd_fake
212
184
        self.assertIsInstance(cmd_obj, cmd_fake)
213
185
 
214
186
    def test_register_lazy(self):
215
187
        """Ensure lazy registration works"""
216
188
        commands.plugin_cmds.register_lazy('cmd_fake', [],
217
 
                                           'breezy.tests.fake_command')
 
189
                                           'bzrlib.tests.fake_command')
218
190
        self.addCleanup(self.remove_fake)
219
191
        self.assertFalse(lazy_command_imported)
220
192
        fake_instance = commands.get_cmd_object('fake')
223
195
 
224
196
    def test_get_unrelated_does_not_import(self):
225
197
        commands.plugin_cmds.register_lazy('cmd_fake', [],
226
 
                                           'breezy.tests.fake_command')
 
198
                                           'bzrlib.tests.fake_command')
227
199
        self.addCleanup(self.remove_fake)
228
200
        commands.get_cmd_object('status')
229
201
        self.assertFalse(lazy_command_imported)
230
202
 
231
203
    def test_aliases(self):
232
204
        commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
233
 
                                           'breezy.tests.fake_command')
 
205
                                           'bzrlib.tests.fake_command')
234
206
        self.addCleanup(self.remove_fake)
235
207
        fake_instance = commands.get_cmd_object('fake_alias')
236
208
        self.assertIsFakeCommand(fake_instance)
248
220
        commands.Command.hooks.install_named_hook(
249
221
            "extend_command", hook_calls.append, None)
250
222
        # create a command, should not fire
251
 
 
252
223
        class cmd_test_extend_command_hook(commands.Command):
253
224
            __doc__ = """A sample command."""
254
225
        self.assertEqual([], hook_calls)
255
226
        # -- as a builtin
256
227
        # register the command class, should not fire
257
228
        try:
258
 
            commands.builtin_command_registry.register(
259
 
                cmd_test_extend_command_hook)
 
229
            commands.builtin_command_registry.register(cmd_test_extend_command_hook)
260
230
            self.assertEqual([], hook_calls)
261
231
            # and ask for the object, should fire
262
232
            cmd = commands.get_cmd_object('test-extend-command-hook')
266
236
            self.assertSubset([cmd], hook_calls)
267
237
            del hook_calls[:]
268
238
        finally:
269
 
            commands.builtin_command_registry.remove(
270
 
                'test-extend-command-hook')
 
239
            commands.builtin_command_registry.remove('test-extend-command-hook')
271
240
        # -- as a plugin lazy registration
272
241
        try:
273
242
            # register the command class, should not fire
274
243
            commands.plugin_cmds.register_lazy('cmd_fake', [],
275
 
                                               'breezy.tests.fake_command')
 
244
                                               'bzrlib.tests.fake_command')
276
245
            self.assertEqual([], hook_calls)
277
246
            # and ask for the object, should fire
278
247
            cmd = commands.get_cmd_object('fake')
288
257
        # ui.
289
258
        commands.install_bzr_command_hooks()
290
259
        hook_calls = []
291
 
 
292
260
        class ACommand(commands.Command):
293
261
            __doc__ = """A sample command."""
294
 
 
295
262
        def get_cmd(cmd_or_None, cmd_name):
296
263
            hook_calls.append(('called', cmd_or_None, cmd_name))
297
264
            if cmd_name in ('foo', 'info'):
316
283
        self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
317
284
 
318
285
 
319
 
class TestCommandNotFound(tests.TestCase):
320
 
 
321
 
    def setUp(self):
322
 
        super(TestCommandNotFound, self).setUp()
323
 
        commands._register_builtin_commands()
324
 
        commands.install_bzr_command_hooks()
325
 
 
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))
330
 
 
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"',
335
 
                         str(e))
336
 
 
337
 
 
338
286
class TestGetMissingCommandHook(tests.TestCase):
339
287
 
340
288
    def hook_missing(self):
341
289
        """Hook get_missing_command for testing."""
342
290
        self.hook_calls = []
343
 
 
344
291
        class ACommand(commands.Command):
345
292
            __doc__ = """A sample command."""
346
 
 
347
293
        def get_missing_cmd(cmd_name):
348
294
            self.hook_calls.append(('called', cmd_name))
349
295
            if cmd_name in ('foo', 'info'):
386
332
        # The list_commands() hook fires when all_command_names() is invoked.
387
333
        hook_calls = []
388
334
        commands.install_bzr_command_hooks()
389
 
 
390
335
        def list_my_commands(cmd_names):
391
336
            hook_calls.append('called')
392
337
            cmd_names.update(['foo', 'bar'])
401
346
        self.assertEqual(['called'], hook_calls)
402
347
        self.assertSubset(['foo', 'bar'], cmds)
403
348
 
404
 
 
405
 
class TestPreAndPostCommandHooks(tests.TestCase):
406
 
    class TestError(Exception):
407
 
        __doc__ = """A test exception."""
408
 
 
409
 
    def test_pre_and_post_hooks(self):
410
 
        hook_calls = []
411
 
 
412
 
        def pre_command(cmd):
413
 
            self.assertEqual([], hook_calls)
414
 
            hook_calls.append('pre')
415
 
 
416
 
        def post_command(cmd):
417
 
            self.assertEqual(['pre', 'run'], hook_calls)
418
 
            hook_calls.append('post')
419
 
 
420
 
        def run(cmd):
421
 
            self.assertEqual(['pre'], hook_calls)
422
 
            hook_calls.append('run')
423
 
 
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)
430
 
 
431
 
        self.assertEqual([], hook_calls)
432
 
        self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
433
 
        self.assertEqual(['pre', 'run', 'post'], hook_calls)
434
 
 
435
 
    def test_post_hook_provided_exception(self):
436
 
        hook_calls = []
437
 
 
438
 
        def post_command(cmd):
439
 
            hook_calls.append('post')
440
 
 
441
 
        def run(cmd):
442
 
            hook_calls.append('run')
443
 
            raise self.TestError()
444
 
 
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)
449
 
 
450
 
        self.assertEqual([], hook_calls)
451
 
        self.assertRaises(self.TestError, commands.run_bzr, [u'rocks'])
452
 
        self.assertEqual(['run', 'post'], hook_calls)
453
 
 
454
 
    def test_pre_command_error(self):
455
 
        """Ensure an BzrCommandError in pre_command aborts the command"""
456
 
 
457
 
        hook_calls = []
458
 
 
459
 
        def pre_command(cmd):
460
 
            hook_calls.append('pre')
461
 
            # verify that all subclasses of BzrCommandError caught too
462
 
            raise commands.BzrOptionError()
463
 
 
464
 
        def post_command(cmd, e):
465
 
            self.fail('post_command should not be called')
466
 
 
467
 
        def run(cmd):
468
 
            self.fail('command should not be called')
469
 
 
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)
476
 
 
477
 
        self.assertEqual([], hook_calls)
478
 
        self.assertRaises(errors.BzrCommandError,
479
 
                          commands.run_bzr, [u'rocks'])
480
 
        self.assertEqual(['pre'], hook_calls)
481
 
 
482
 
 
483
 
class GuessCommandTests(tests.TestCase):
484
 
 
485
 
    def setUp(self):
486
 
        super(GuessCommandTests, self).setUp()
487
 
        commands._register_builtin_commands()
488
 
        commands.install_bzr_command_hooks()
489
 
 
490
 
    def test_guess_override(self):
491
 
        self.assertEqual('ci', commands.guess_command('ic'))
492
 
 
493
 
    def test_guess(self):
494
 
        commands.get_cmd_object('status')
495
 
        self.assertEqual('status', commands.guess_command('statue'))
496
 
 
497
 
    def test_none(self):
498
 
        self.assertIs(None, commands.guess_command('nothingisevenclose'))
 
349
class TestDeprecations(tests.TestCase):
 
350
 
 
351
    def test_shlex_split_unicode_deprecation(self):
 
352
        res = self.applyDeprecated(
 
353
                symbol_versioning.deprecated_in((2, 2, 0)),
 
354
                commands.shlex_split_unicode, 'whatever')