~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
22
22
    controldir,
23
23
    errors,
24
24
    option,
 
25
    registry,
25
26
    )
26
27
from bzrlib.builtins import cmd_commit
27
 
from bzrlib.commands import Command, parse_args
 
28
from bzrlib.commands import parse_args
28
29
from bzrlib.tests import TestCase
29
30
from bzrlib.repofmt import knitrepo
30
31
 
42
43
        # XXX: Using cmd_commit makes these tests overly sensitive to changes
43
44
        # to cmd_commit, when they are meant to be about option parsing in
44
45
        # general.
45
 
        self.assertEqual(parse_args(cmd_commit(), ['--help']),
46
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
47
 
        self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
48
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}))
 
46
        self.assertEqual(
 
47
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}),
 
48
           parse_args(cmd_commit(), ['--help']))
 
49
        self.assertEqual(
 
50
           ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
 
51
           parse_args(cmd_commit(), ['--message=biter']))
49
52
 
50
53
    def test_no_more_opts(self):
51
54
        """Terminated options"""
52
 
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
53
 
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
 
55
        self.assertEqual(
 
56
            (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
 
57
            parse_args(cmd_commit(), ['--', '-file-with-dashes']))
54
58
 
55
59
    def test_option_help(self):
56
60
        """Options have help strings."""
64
68
        out, err = self.run_bzr('help status')
65
69
        self.assertContainsRe(out, r'--show-ids.*Show internal object.')
66
70
 
 
71
    def test_option_help_global_hidden(self):
 
72
        """Hidden global options have no help strings."""
 
73
        out, err = self.run_bzr('help log')
 
74
        self.assertNotContainsRe(out, r'--message')
 
75
 
67
76
    def test_option_arg_help(self):
68
77
        """Help message shows option arguments."""
69
78
        out, err = self.run_bzr('help commit')
151
160
 
152
161
    def test_registry_converter(self):
153
162
        options = [option.RegistryOption('format', '',
154
 
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
 
163
                   controldir.format_registry, controldir.format_registry.make_bzrdir)]
155
164
        opts, args = self.parse(options, ['--format', 'knit'])
156
165
        self.assertIsInstance(opts.format.repository_format,
157
166
                              knitrepo.RepositoryFormatKnit1)
158
167
 
159
168
    def test_lazy_registry(self):
160
169
        options = [option.RegistryOption('format', '',
161
 
                   lazy_registry=('bzrlib.bzrdir','format_registry'),
 
170
                   lazy_registry=('bzrlib.controldir','format_registry'),
162
171
                   converter=str)]
163
172
        opts, args = self.parse(options, ['--format', 'knit'])
164
173
        self.assertEqual({'format': 'knit'}, opts)
332
341
 
333
342
    def get_builtin_command_options(self):
334
343
        g = []
335
 
        for cmd_name in sorted(commands.all_command_names()):
 
344
        commands.install_bzr_command_hooks()
 
345
        for cmd_name in sorted(commands.builtin_command_names()):
336
346
            cmd = commands.get_cmd_object(cmd_name)
337
347
            for opt_name, opt in sorted(cmd.options().items()):
338
348
                g.append((cmd_name, opt))
 
349
        self.assert_(g)
339
350
        return g
340
351
 
341
 
    def test_global_options_used(self):
342
 
        # In the distant memory, options could only be declared globally.  Now
343
 
        # we prefer to declare them in the command, unless like -r they really
344
 
        # are used very widely with the exact same meaning.  So this checks
345
 
        # for any that should be garbage collected.
346
 
        g = dict(option.Option.OPTIONS.items())
347
 
        used_globals = {}
348
 
        msgs = []
349
 
        for cmd_name in sorted(commands.all_command_names()):
350
 
            cmd = commands.get_cmd_object(cmd_name)
351
 
            for option_or_name in sorted(cmd.takes_options):
352
 
                if not isinstance(option_or_name, basestring):
353
 
                    self.assertIsInstance(option_or_name, option.Option)
354
 
                elif not option_or_name in g:
355
 
                    msgs.append("apparent reference to undefined "
356
 
                        "global option %r from %r"
357
 
                        % (option_or_name, cmd))
358
 
                else:
359
 
                    used_globals.setdefault(option_or_name, []).append(cmd_name)
360
 
        unused_globals = set(g.keys()) - set(used_globals.keys())
361
 
        # not enforced because there might be plugins that use these globals
362
 
        ## for option_name in sorted(unused_globals):
363
 
        ##    msgs.append("unused global option %r" % option_name)
364
 
        ## for option_name, cmds in sorted(used_globals.items()):
365
 
        ##     if len(cmds) <= 1:
366
 
        ##         msgs.append("global option %r is only used by %r"
367
 
        ##                 % (option_name, cmds))
368
 
        if msgs:
369
 
            self.fail("problems with global option definitions:\n"
370
 
                    + '\n'.join(msgs))
371
 
 
372
352
    def test_option_grammar(self):
373
353
        msgs = []
374
354
        # Option help should be written in sentence form, and have a final
375
 
        # period and be all on a single line, because the display code will
376
 
        # wrap it.
377
 
        option_re = re.compile(r'^[A-Z][^\n]+\.$')
 
355
        # period with an optional bracketed suffix. All the text should be on
 
356
        # one line, because the display code will wrap it.
 
357
        option_re = re.compile(r'^[A-Z][^\n]+\.(?: \([^\n]+\))?$')
378
358
        for scope, opt in self.get_builtin_command_options():
379
 
            if not opt.help:
380
 
                msgs.append('%-16s %-16s %s' %
381
 
                       ((scope or 'GLOBAL'), opt.name, 'NO HELP'))
382
 
            elif not option_re.match(opt.help):
383
 
                msgs.append('%-16s %-16s %s' %
384
 
                        ((scope or 'GLOBAL'), opt.name, opt.help))
 
359
            for name, _, _, helptxt in opt.iter_switches():
 
360
                if name != opt.name:
 
361
                    name = "/".join([opt.name, name])
 
362
                if not helptxt:
 
363
                    msgs.append('%-16s %-16s %s' %
 
364
                           ((scope or 'GLOBAL'), name, 'NO HELP'))
 
365
                elif not option_re.match(helptxt):
 
366
                    if name.startswith("format/"):
 
367
                        # Don't complain about the odd format registry help
 
368
                        continue
 
369
                    msgs.append('%-16s %-16s %s' %
 
370
                            ((scope or 'GLOBAL'), name, helptxt))
385
371
        if msgs:
386
372
            self.fail("The following options don't match the style guide:\n"
387
373
                    + '\n'.join(msgs))
388
374
 
 
375
 
 
376
class TestOptionMisc(TestCase):
 
377
 
389
378
    def test_is_hidden(self):
390
379
        registry = controldir.ControlDirFormatRegistry()
391
380
        bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
396
385
        self.assertTrue(format.is_hidden('hidden'))
397
386
        self.assertFalse(format.is_hidden('visible'))
398
387
 
 
388
    def test_short_name(self):
 
389
        registry = controldir.ControlDirFormatRegistry()
 
390
        opt = option.RegistryOption('format', help='', registry=registry)
 
391
        self.assertEquals(None, opt.short_name())
 
392
        opt = option.RegistryOption('format', short_name='F', help='',
 
393
            registry=registry)
 
394
        self.assertEquals('F', opt.short_name())
 
395
 
399
396
    def test_option_custom_help(self):
400
397
        the_opt = option.Option.OPTIONS['help']
401
398
        orig_help = the_opt.help[:]
404
401
        self.assertEqual('suggest lottery numbers', my_opt.help)
405
402
        self.assertEqual(orig_help, the_opt.help)
406
403
 
 
404
    def test_short_value_switches(self):
 
405
        reg = registry.Registry()
 
406
        reg.register('short', 'ShortChoice')
 
407
        reg.register('long', 'LongChoice')
 
408
        ropt = option.RegistryOption('choice', '', reg, value_switches=True,
 
409
            short_value_switches={'short': 's'})
 
410
        opts, args = parse([ropt], ['--short'])
 
411
        self.assertEqual('ShortChoice', opts.choice)
 
412
        opts, args = parse([ropt], ['-s'])
 
413
        self.assertEqual('ShortChoice', opts.choice)
 
414
 
407
415
 
408
416
class TestVerboseQuietLinkage(TestCase):
409
417