/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 breezy/tests/test_commands.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 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
18
18
import inspect
19
19
import sys
20
20
 
21
 
from bzrlib import (
 
21
from .. import (
22
22
    builtins,
23
23
    commands,
24
24
    config,
27
27
    tests,
28
28
    trace,
29
29
    )
30
 
from bzrlib.commands import display_command
31
 
from bzrlib.tests import TestSkipped
 
30
from ..commands import display_command
 
31
from . import TestSkipped
32
32
 
33
33
 
34
34
class TestCommands(tests.TestCase):
49
49
        def pipe_thrower():
50
50
            raise IOError(errno.EPIPE, "Bogus pipe error")
51
51
        self.assertRaises(IOError, pipe_thrower)
 
52
 
52
53
        @display_command
53
54
        def non_thrower():
54
55
            pipe_thrower()
55
56
        non_thrower()
 
57
 
56
58
        @display_command
57
59
        def other_thrower():
58
60
            raise IOError(errno.ESPIPE, "Bogus pipe error")
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'])
66
68
 
67
69
    def test_unicode_option(self):
70
72
        import optparse
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'])
75
77
 
76
78
    @staticmethod
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'))
109
111
 
110
112
 
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')
121
123
 
122
124
 
123
125
class TestGetAlias(tests.TestCase):
128
130
 
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))
134
136
 
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))
142
144
 
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))
150
152
 
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))
156
158
 
157
159
 
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']))
192
194
 
193
195
 
194
196
class TestRegisterLazy(tests.TestCase):
195
197
 
196
198
    def setUp(self):
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')
207
209
 
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)
211
213
 
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')
221
223
 
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)
228
230
 
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
 
251
 
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
254
257
        try:
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[:]
264
268
        finally:
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
267
272
        try:
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')
283
288
        # ui.
284
289
        commands.install_bzr_command_hooks()
285
290
        hook_calls = []
 
291
 
286
292
        class ACommand(commands.Command):
287
293
            __doc__ = """A sample command."""
 
294
 
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)
310
317
 
311
318
 
 
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.CommandError,
 
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.CommandError,
 
333
                              commands.get_cmd_object, 'statue')
 
334
        self.assertEqual('unknown command "statue". Perhaps you meant "status"',
 
335
                         str(e))
 
336
 
 
337
 
312
338
class TestGetMissingCommandHook(tests.TestCase):
313
339
 
314
340
    def hook_missing(self):
315
341
        """Hook get_missing_command for testing."""
316
342
        self.hook_calls = []
 
343
 
317
344
        class ACommand(commands.Command):
318
345
            __doc__ = """A sample command."""
 
346
 
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.
359
387
        hook_calls = []
360
388
        commands.install_bzr_command_hooks()
 
389
 
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)
374
403
 
 
404
 
375
405
class TestPreAndPostCommandHooks(tests.TestCase):
376
 
    class TestError(StandardError):
 
406
    class TestError(Exception):
377
407
        __doc__ = """A test exception."""
378
408
 
379
409
    def test_pre_and_post_hooks(self):
422
452
        self.assertEqual(['run', 'post'], hook_calls)
423
453
 
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"""
426
456
 
427
457
        hook_calls = []
428
458
 
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()
433
463
 
434
464
        def post_command(cmd, e):
435
465
            self.fail('post_command should not be called')
445
475
            "post_command", post_command, None)
446
476
 
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)
451
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'))