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
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']))
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'}))
53
51
def test_no_more_opts(self):
54
52
"""Terminated options"""
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': []}))
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.')
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')
76
68
def test_option_arg_help(self):
77
69
"""Help message shows option arguments."""
78
70
out, err = self.run_bzr('help commit')
161
153
def test_registry_converter(self):
162
154
options = [option.RegistryOption('format', '',
163
controldir.format_registry, controldir.format_registry.make_bzrdir)]
155
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
164
156
opts, args = self.parse(options, ['--format', 'knit'])
165
157
self.assertIsInstance(opts.format.repository_format,
166
158
knitrepo.RepositoryFormatKnit1)
168
160
def test_lazy_registry(self):
169
161
options = [option.RegistryOption('format', '',
170
lazy_registry=('bzrlib.controldir','format_registry'),
162
lazy_registry=('bzrlib.bzrdir','format_registry'),
172
164
opts, args = self.parse(options, ['--format', 'knit'])
173
165
self.assertEqual({'format': 'knit'}, opts)
342
334
def get_builtin_command_options(self):
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))
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())
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))
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))
370
self.fail("problems with global option definitions:\n"
352
373
def test_option_grammar(self):
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
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():
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))
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))
372
387
self.fail("The following options don't match the style guide:\n"
373
388
+ '\n'.join(msgs))
376
class TestOptionMisc(TestCase):
378
390
def test_is_hidden(self):
379
391
registry = controldir.ControlDirFormatRegistry()
380
392
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',