~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Samuel Bronson
  • Date: 2012-08-30 20:36:18 UTC
  • mto: (6015.57.3 2.4)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: naesten@gmail.com-20120830203618-y2dzw91nqpvpgxvx
Update INSTALL for switch to Python 2.6 and up.

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(
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']))
 
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'}))
52
50
 
53
51
    def test_no_more_opts(self):
54
52
        """Terminated options"""
55
 
        self.assertEqual(
56
 
            (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
57
 
            parse_args(cmd_commit(), ['--', '-file-with-dashes']))
 
53
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
 
54
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
58
55
 
59
56
    def test_option_help(self):
60
57
        """Options have help strings."""
68
65
        out, err = self.run_bzr('help status')
69
66
        self.assertContainsRe(out, r'--show-ids.*Show internal object.')
70
67
 
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
 
 
76
68
    def test_option_arg_help(self):
77
69
        """Help message shows option arguments."""
78
70
        out, err = self.run_bzr('help commit')
341
333
 
342
334
    def get_builtin_command_options(self):
343
335
        g = []
344
 
        commands.install_bzr_command_hooks()
345
 
        for cmd_name in sorted(commands.builtin_command_names()):
 
336
        for cmd_name in sorted(commands.all_command_names()):
346
337
            cmd = commands.get_cmd_object(cmd_name)
347
338
            for opt_name, opt in sorted(cmd.options().items()):
348
339
                g.append((cmd_name, opt))
349
 
        self.assert_(g)
350
340
        return g
351
341
 
 
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
 
352
373
    def test_option_grammar(self):
353
374
        msgs = []
354
375
        # Option help should be written in sentence form, and have a final
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]+\))?$')
 
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]+\.$')
358
379
        for scope, opt in self.get_builtin_command_options():
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))
 
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))
371
386
        if msgs:
372
387
            self.fail("The following options don't match the style guide:\n"
373
388
                    + '\n'.join(msgs))
374
389
 
375
 
 
376
 
class TestOptionMisc(TestCase):
377
 
 
378
390
    def test_is_hidden(self):
379
391
        registry = controldir.ControlDirFormatRegistry()
380
392
        bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',