161
161
def test_registry_converter(self):
162
162
options = [option.RegistryOption('format', '',
163
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
163
controldir.format_registry, controldir.format_registry.make_bzrdir)]
164
164
opts, args = self.parse(options, ['--format', 'knit'])
165
165
self.assertIsInstance(opts.format.repository_format,
166
166
knitrepo.RepositoryFormatKnit1)
168
168
def test_lazy_registry(self):
169
169
options = [option.RegistryOption('format', '',
170
lazy_registry=('bzrlib.bzrdir','format_registry'),
170
lazy_registry=('bzrlib.controldir','format_registry'),
172
172
opts, args = self.parse(options, ['--format', 'knit'])
173
173
self.assertEqual({'format': 'knit'}, opts)
342
342
def get_builtin_command_options(self):
344
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()):
345
346
cmd = commands.get_cmd_object(cmd_name)
346
347
for opt_name, opt in sorted(cmd.options().items()):
347
348
g.append((cmd_name, opt))
350
def test_global_options_used(self):
351
# In the distant memory, options could only be declared globally. Now
352
# we prefer to declare them in the command, unless like -r they really
353
# are used very widely with the exact same meaning. So this checks
354
# for any that should be garbage collected.
355
g = dict(option.Option.OPTIONS.items())
358
for cmd_name in sorted(commands.all_command_names()):
359
cmd = commands.get_cmd_object(cmd_name)
360
for option_or_name in sorted(cmd.takes_options):
361
if not isinstance(option_or_name, basestring):
362
self.assertIsInstance(option_or_name, option.Option)
363
elif not option_or_name in g:
364
msgs.append("apparent reference to undefined "
365
"global option %r from %r"
366
% (option_or_name, cmd))
368
used_globals.setdefault(option_or_name, []).append(cmd_name)
369
unused_globals = set(g.keys()) - set(used_globals.keys())
370
# not enforced because there might be plugins that use these globals
371
## for option_name in sorted(unused_globals):
372
## msgs.append("unused global option %r" % option_name)
373
## for option_name, cmds in sorted(used_globals.items()):
374
## if len(cmds) <= 1:
375
## msgs.append("global option %r is only used by %r"
376
## % (option_name, cmds))
378
self.fail("problems with global option definitions:\n"
381
352
def test_option_grammar(self):
383
354
# Option help should be written in sentence form, and have a final
384
# period and be all on a single line, because the display code will
386
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]+\))?$')
387
358
for scope, opt in self.get_builtin_command_options():
389
msgs.append('%-16s %-16s %s' %
390
((scope or 'GLOBAL'), opt.name, 'NO HELP'))
391
elif not option_re.match(opt.help):
392
msgs.append('%-16s %-16s %s' %
393
((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))
395
372
self.fail("The following options don't match the style guide:\n"
396
373
+ '\n'.join(msgs))
376
class TestOptionMisc(TestCase):
398
378
def test_is_hidden(self):
399
379
registry = controldir.ControlDirFormatRegistry()
400
380
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',