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
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'}))
47
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}),
48
parse_args(cmd_commit(), ['--help']))
50
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
51
parse_args(cmd_commit(), ['--message=biter']))
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': []}))
56
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
57
parse_args(cmd_commit(), ['--', '-file-with-dashes']))
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.')
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')
67
76
def test_option_arg_help(self):
68
77
"""Help message shows option arguments."""
69
78
out, err = self.run_bzr('help commit')
333
342
def get_builtin_command_options(self):
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))
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())
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))
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))
369
self.fail("problems with global option definitions:\n"
372
352
def test_option_grammar(self):
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
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():
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():
361
name = "/".join([opt.name, name])
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
369
msgs.append('%-16s %-16s %s' %
370
((scope or 'GLOBAL'), name, helptxt))
386
372
self.fail("The following options don't match the style guide:\n"
387
373
+ '\n'.join(msgs))
376
class TestOptionMisc(TestCase):
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'))
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='',
394
self.assertEquals('F', opt.short_name())
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)
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)
408
416
class TestVerboseQuietLinkage(TestCase):