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

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import inspect
19
19
import sys
20
20
 
21
 
from .. import (
 
21
from brzlib import (
22
22
    builtins,
23
23
    commands,
24
24
    config,
27
27
    tests,
28
28
    trace,
29
29
    )
30
 
from ..commands import display_command
31
 
from . import TestSkipped
 
30
from brzlib.commands import display_command
 
31
from brzlib.tests 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
 
 
53
52
        @display_command
54
53
        def non_thrower():
55
54
            pipe_thrower()
56
55
        non_thrower()
57
 
 
58
56
        @display_command
59
57
        def other_thrower():
60
58
            raise IOError(errno.ESPIPE, "Bogus pipe error")
130
128
 
131
129
    def test_simple(self):
132
130
        my_config = self._get_config("[ALIASES]\n"
133
 
                                     "diff=diff -r -2..-1\n")
 
131
            "diff=diff -r -2..-1\n")
134
132
        self.assertEqual([u'diff', u'-r', u'-2..-1'],
135
 
                         commands.get_alias("diff", config=my_config))
 
133
            commands.get_alias("diff", config=my_config))
136
134
 
137
135
    def test_single_quotes(self):
138
136
        my_config = self._get_config("[ALIASES]\n"
139
 
                                     "diff=diff -r -2..-1 --diff-options "
140
 
                                     "'--strip-trailing-cr -wp'\n")
 
137
            "diff=diff -r -2..-1 --diff-options "
 
138
            "'--strip-trailing-cr -wp'\n")
141
139
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
142
140
                          u'--strip-trailing-cr -wp'],
143
 
                         commands.get_alias("diff", config=my_config))
 
141
                          commands.get_alias("diff", config=my_config))
144
142
 
145
143
    def test_double_quotes(self):
146
144
        my_config = self._get_config("[ALIASES]\n"
147
 
                                     "diff=diff -r -2..-1 --diff-options "
148
 
                                     "\"--strip-trailing-cr -wp\"\n")
 
145
            "diff=diff -r -2..-1 --diff-options "
 
146
            "\"--strip-trailing-cr -wp\"\n")
149
147
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
150
148
                          u'--strip-trailing-cr -wp'],
151
 
                         commands.get_alias("diff", config=my_config))
 
149
                          commands.get_alias("diff", config=my_config))
152
150
 
153
151
    def test_unicode(self):
154
152
        my_config = self._get_config("[ALIASES]\n"
155
 
                                     u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
 
153
            u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
156
154
        self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
157
 
                         commands.get_alias("iam", config=my_config))
 
155
                          commands.get_alias("iam", config=my_config))
158
156
 
159
157
 
160
158
class TestSeeAlso(tests.TestCase):
190
188
        """Additional terms can be supplied and are deduped and sorted."""
191
189
        command = self._get_command_with_see_also(['foo', 'bar'])
192
190
        self.assertEqual(['bar', 'foo', 'gam'],
193
 
                         command.get_see_also(['gam', 'bar', 'gam']))
 
191
            command.get_see_also(['gam', 'bar', 'gam']))
194
192
 
195
193
 
196
194
class TestRegisterLazy(tests.TestCase):
197
195
 
198
196
    def setUp(self):
199
197
        super(TestRegisterLazy, self).setUp()
200
 
        import breezy.tests.fake_command
201
 
        del sys.modules['breezy.tests.fake_command']
 
198
        import brzlib.tests.fake_command
 
199
        del sys.modules['brzlib.tests.fake_command']
202
200
        global lazy_command_imported
203
201
        lazy_command_imported = False
204
202
        commands.install_bzr_command_hooks()
208
206
        commands.plugin_cmds.remove('fake')
209
207
 
210
208
    def assertIsFakeCommand(self, cmd_obj):
211
 
        from breezy.tests.fake_command import cmd_fake
 
209
        from brzlib.tests.fake_command import cmd_fake
212
210
        self.assertIsInstance(cmd_obj, cmd_fake)
213
211
 
214
212
    def test_register_lazy(self):
215
213
        """Ensure lazy registration works"""
216
214
        commands.plugin_cmds.register_lazy('cmd_fake', [],
217
 
                                           'breezy.tests.fake_command')
 
215
                                           'brzlib.tests.fake_command')
218
216
        self.addCleanup(self.remove_fake)
219
217
        self.assertFalse(lazy_command_imported)
220
218
        fake_instance = commands.get_cmd_object('fake')
223
221
 
224
222
    def test_get_unrelated_does_not_import(self):
225
223
        commands.plugin_cmds.register_lazy('cmd_fake', [],
226
 
                                           'breezy.tests.fake_command')
 
224
                                           'brzlib.tests.fake_command')
227
225
        self.addCleanup(self.remove_fake)
228
226
        commands.get_cmd_object('status')
229
227
        self.assertFalse(lazy_command_imported)
230
228
 
231
229
    def test_aliases(self):
232
230
        commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
233
 
                                           'breezy.tests.fake_command')
 
231
                                           'brzlib.tests.fake_command')
234
232
        self.addCleanup(self.remove_fake)
235
233
        fake_instance = commands.get_cmd_object('fake_alias')
236
234
        self.assertIsFakeCommand(fake_instance)
248
246
        commands.Command.hooks.install_named_hook(
249
247
            "extend_command", hook_calls.append, None)
250
248
        # create a command, should not fire
251
 
 
252
249
        class cmd_test_extend_command_hook(commands.Command):
253
250
            __doc__ = """A sample command."""
254
251
        self.assertEqual([], hook_calls)
255
252
        # -- as a builtin
256
253
        # register the command class, should not fire
257
254
        try:
258
 
            commands.builtin_command_registry.register(
259
 
                cmd_test_extend_command_hook)
 
255
            commands.builtin_command_registry.register(cmd_test_extend_command_hook)
260
256
            self.assertEqual([], hook_calls)
261
257
            # and ask for the object, should fire
262
258
            cmd = commands.get_cmd_object('test-extend-command-hook')
266
262
            self.assertSubset([cmd], hook_calls)
267
263
            del hook_calls[:]
268
264
        finally:
269
 
            commands.builtin_command_registry.remove(
270
 
                'test-extend-command-hook')
 
265
            commands.builtin_command_registry.remove('test-extend-command-hook')
271
266
        # -- as a plugin lazy registration
272
267
        try:
273
268
            # register the command class, should not fire
274
269
            commands.plugin_cmds.register_lazy('cmd_fake', [],
275
 
                                               'breezy.tests.fake_command')
 
270
                                               'brzlib.tests.fake_command')
276
271
            self.assertEqual([], hook_calls)
277
272
            # and ask for the object, should fire
278
273
            cmd = commands.get_cmd_object('fake')
288
283
        # ui.
289
284
        commands.install_bzr_command_hooks()
290
285
        hook_calls = []
291
 
 
292
286
        class ACommand(commands.Command):
293
287
            __doc__ = """A sample command."""
294
 
 
295
288
        def get_cmd(cmd_or_None, cmd_name):
296
289
            hook_calls.append(('called', cmd_or_None, cmd_name))
297
290
            if cmd_name in ('foo', 'info'):
316
309
        self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
317
310
 
318
311
 
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
312
class TestGetMissingCommandHook(tests.TestCase):
339
313
 
340
314
    def hook_missing(self):
341
315
        """Hook get_missing_command for testing."""
342
316
        self.hook_calls = []
343
 
 
344
317
        class ACommand(commands.Command):
345
318
            __doc__ = """A sample command."""
346
 
 
347
319
        def get_missing_cmd(cmd_name):
348
320
            self.hook_calls.append(('called', cmd_name))
349
321
            if cmd_name in ('foo', 'info'):
386
358
        # The list_commands() hook fires when all_command_names() is invoked.
387
359
        hook_calls = []
388
360
        commands.install_bzr_command_hooks()
389
 
 
390
361
        def list_my_commands(cmd_names):
391
362
            hook_calls.append('called')
392
363
            cmd_names.update(['foo', 'bar'])
401
372
        self.assertEqual(['called'], hook_calls)
402
373
        self.assertSubset(['foo', 'bar'], cmds)
403
374
 
404
 
 
405
375
class TestPreAndPostCommandHooks(tests.TestCase):
406
 
    class TestError(Exception):
 
376
    class TestError(StandardError):
407
377
        __doc__ = """A test exception."""
408
378
 
409
379
    def test_pre_and_post_hooks(self):
459
429
        def pre_command(cmd):
460
430
            hook_calls.append('pre')
461
431
            # verify that all subclasses of BzrCommandError caught too
462
 
            raise commands.BzrOptionError()
 
432
            raise errors.BzrOptionError()
463
433
 
464
434
        def post_command(cmd, e):
465
435
            self.fail('post_command should not be called')
479
449
                          commands.run_bzr, [u'rocks'])
480
450
        self.assertEqual(['pre'], hook_calls)
481
451
 
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'))