~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-20 12:19:29 UTC
  • mfrom: (6437.23.11 2.5)
  • mto: (6581.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6582.
  • Revision ID: jelmer@samba.org-20120220121929-7ni2psvjoatm1yp4
Merge bzr/2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        # XXX: Using cmd_commit makes these tests overly sensitive to changes
44
44
        # to cmd_commit, when they are meant to be about option parsing in
45
45
        # general.
46
 
        self.assertEqual(parse_args(cmd_commit(), ['--help']),
47
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
48
 
        self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
49
 
           ([], {'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']))
50
52
 
51
53
    def test_no_more_opts(self):
52
54
        """Terminated options"""
53
 
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
54
 
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
 
55
        self.assertEqual(
 
56
            (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
 
57
            parse_args(cmd_commit(), ['--', '-file-with-dashes']))
55
58
 
56
59
    def test_option_help(self):
57
60
        """Options have help strings."""
65
68
        out, err = self.run_bzr('help status')
66
69
        self.assertContainsRe(out, r'--show-ids.*Show internal object.')
67
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
 
68
76
    def test_option_arg_help(self):
69
77
        """Help message shows option arguments."""
70
78
        out, err = self.run_bzr('help commit')
333
341
 
334
342
    def get_builtin_command_options(self):
335
343
        g = []
336
 
        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()):
337
346
            cmd = commands.get_cmd_object(cmd_name)
338
347
            for opt_name, opt in sorted(cmd.options().items()):
339
348
                g.append((cmd_name, opt))
 
349
        self.assert_(g)
340
350
        return g
341
351
 
342
 
    def test_global_options_used(self):
343
 
        # In the distant memory, options could only be declared globally.  Now
344
 
        # we prefer to declare them in the command, unless like -r they really
345
 
        # are used very widely with the exact same meaning.  So this checks
346
 
        # for any that should be garbage collected.
347
 
        g = dict(option.Option.OPTIONS.items())
348
 
        used_globals = {}
349
 
        msgs = []
350
 
        for cmd_name in sorted(commands.all_command_names()):
351
 
            cmd = commands.get_cmd_object(cmd_name)
352
 
            for option_or_name in sorted(cmd.takes_options):
353
 
                if not isinstance(option_or_name, basestring):
354
 
                    self.assertIsInstance(option_or_name, option.Option)
355
 
                elif not option_or_name in g:
356
 
                    msgs.append("apparent reference to undefined "
357
 
                        "global option %r from %r"
358
 
                        % (option_or_name, cmd))
359
 
                else:
360
 
                    used_globals.setdefault(option_or_name, []).append(cmd_name)
361
 
        unused_globals = set(g.keys()) - set(used_globals.keys())
362
 
        # not enforced because there might be plugins that use these globals
363
 
        ## for option_name in sorted(unused_globals):
364
 
        ##    msgs.append("unused global option %r" % option_name)
365
 
        ## for option_name, cmds in sorted(used_globals.items()):
366
 
        ##     if len(cmds) <= 1:
367
 
        ##         msgs.append("global option %r is only used by %r"
368
 
        ##                 % (option_name, cmds))
369
 
        if msgs:
370
 
            self.fail("problems with global option definitions:\n"
371
 
                    + '\n'.join(msgs))
372
 
 
373
352
    def test_option_grammar(self):
374
353
        msgs = []
375
354
        # Option help should be written in sentence form, and have a final
376
 
        # period and be all on a single line, because the display code will
377
 
        # wrap it.
378
 
        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]+\))?$')
379
358
        for scope, opt in self.get_builtin_command_options():
380
 
            if not opt.help:
381
 
                msgs.append('%-16s %-16s %s' %
382
 
                       ((scope or 'GLOBAL'), opt.name, 'NO HELP'))
383
 
            elif not option_re.match(opt.help):
384
 
                msgs.append('%-16s %-16s %s' %
385
 
                        ((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))
386
371
        if msgs:
387
372
            self.fail("The following options don't match the style guide:\n"
388
373
                    + '\n'.join(msgs))
389
374
 
 
375
 
 
376
class TestOptionMisc(TestCase):
 
377
 
390
378
    def test_is_hidden(self):
391
379
        registry = controldir.ControlDirFormatRegistry()
392
380
        bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',