43
41
# XXX: Using cmd_commit makes these tests overly sensitive to changes
44
42
# 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']))
44
self.assertEqual(parse_args(cmd_commit(), ['--help']),
45
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
46
self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
47
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}))
53
49
def test_no_more_opts(self):
54
50
"""Terminated options"""
56
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
57
parse_args(cmd_commit(), ['--', '-file-with-dashes']))
51
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
59
54
def test_option_help(self):
60
55
"""Options have help strings."""
117
107
self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
119
109
def test_registry_conversion(self):
120
registry = controldir.ControlDirFormatRegistry()
121
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
122
bzrdir.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
123
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
110
registry = bzrdir.BzrDirFormatRegistry()
111
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
112
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
113
registry.register_metadir('hidden', 'RepositoryFormatKnit1',
124
114
'two help', hidden=True)
125
115
registry.set_default('one')
126
116
options = [option.RegistryOption('format', '', registry, str)]
161
151
def test_registry_converter(self):
162
152
options = [option.RegistryOption('format', '',
163
controldir.format_registry, controldir.format_registry.make_bzrdir)]
153
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
164
154
opts, args = self.parse(options, ['--format', 'knit'])
165
155
self.assertIsInstance(opts.format.repository_format,
166
156
knitrepo.RepositoryFormatKnit1)
168
158
def test_lazy_registry(self):
169
159
options = [option.RegistryOption('format', '',
170
lazy_registry=('bzrlib.controldir','format_registry'),
160
lazy_registry=('bzrlib.bzrdir','format_registry'),
172
162
opts, args = self.parse(options, ['--format', 'knit'])
173
163
self.assertEqual({'format': 'knit'}, opts)
187
177
self.assertEqual('test option', my_option.help)
189
179
def test_help(self):
190
registry = controldir.ControlDirFormatRegistry()
191
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
bzrdir.register_metadir(registry, 'two',
180
registry = bzrdir.BzrDirFormatRegistry()
181
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
182
registry.register_metadir('two',
193
183
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
196
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
186
registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
198
188
registry.set_default('one')
199
189
options = [option.RegistryOption('format', 'format help', registry,
328
318
self.assertEqual('hello', name)
329
319
self.assertEqual([], value)
331
def test_list_option_param_name(self):
332
"""Test list options can have their param_name set."""
333
options = [option.ListOption('hello', type=str, param_name='greeting')]
334
opts, args = self.parse(
335
options, ['--hello=world', '--hello=sailor'])
336
self.assertEqual(['world', 'sailor'], opts.greeting)
339
322
class TestOptionDefinitions(TestCase):
340
323
"""Tests for options in the Bazaar codebase."""
342
325
def get_builtin_command_options(self):
344
commands.install_bzr_command_hooks()
345
for cmd_name in sorted(commands.builtin_command_names()):
327
for cmd_name in sorted(commands.all_command_names()):
346
328
cmd = commands.get_cmd_object(cmd_name)
347
329
for opt_name, opt in sorted(cmd.options().items()):
348
330
g.append((cmd_name, opt))
333
def test_global_options_used(self):
334
# In the distant memory, options could only be declared globally. Now
335
# we prefer to declare them in the command, unless like -r they really
336
# are used very widely with the exact same meaning. So this checks
337
# for any that should be garbage collected.
338
g = dict(option.Option.OPTIONS.items())
341
for cmd_name in sorted(commands.all_command_names()):
342
cmd = commands.get_cmd_object(cmd_name)
343
for option_or_name in sorted(cmd.takes_options):
344
if not isinstance(option_or_name, basestring):
345
self.assertIsInstance(option_or_name, option.Option)
346
elif not option_or_name in g:
347
msgs.append("apparent reference to undefined "
348
"global option %r from %r"
349
% (option_or_name, cmd))
351
used_globals.setdefault(option_or_name, []).append(cmd_name)
352
unused_globals = set(g.keys()) - set(used_globals.keys())
353
# not enforced because there might be plugins that use these globals
354
## for option_name in sorted(unused_globals):
355
## msgs.append("unused global option %r" % option_name)
356
## for option_name, cmds in sorted(used_globals.items()):
357
## if len(cmds) <= 1:
358
## msgs.append("global option %r is only used by %r"
359
## % (option_name, cmds))
361
self.fail("problems with global option definitions:\n"
352
364
def test_option_grammar(self):
354
366
# 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]+\))?$')
367
# period and be all on a single line, because the display code will
369
option_re = re.compile(r'^[A-Z][^\n]+\.$')
358
370
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))
372
msgs.append('%-16s %-16s %s' %
373
((scope or 'GLOBAL'), opt.name, 'NO HELP'))
374
elif not option_re.match(opt.help):
375
msgs.append('%-16s %-16s %s' %
376
((scope or 'GLOBAL'), opt.name, opt.help))
372
378
self.fail("The following options don't match the style guide:\n"
373
379
+ '\n'.join(msgs))
376
class TestOptionMisc(TestCase):
378
381
def test_is_hidden(self):
379
registry = controldir.ControlDirFormatRegistry()
380
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
382
registry = bzrdir.BzrDirFormatRegistry()
383
registry.register_metadir('hidden', 'HiddenFormat',
381
384
'hidden help text', hidden=True)
382
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
385
registry.register_metadir('visible', 'VisibleFormat',
383
386
'visible help text', hidden=False)
384
387
format = option.RegistryOption('format', '', registry, str)
385
388
self.assertTrue(format.is_hidden('hidden'))
386
389
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.assertEqual(None, opt.short_name())
392
opt = option.RegistryOption('format', short_name='F', help='',
394
self.assertEqual('F', opt.short_name())
396
391
def test_option_custom_help(self):
397
392
the_opt = option.Option.OPTIONS['help']
398
393
orig_help = the_opt.help[:]
401
396
self.assertEqual('suggest lottery numbers', my_opt.help)
402
397
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)
416
400
class TestVerboseQuietLinkage(TestCase):