52
55
def test_option_help(self):
53
56
"""Options have help strings."""
54
out, err = self.run_bzr_captured(['commit', '--help'])
55
self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
57
out, err = self.run_bzr('commit --help')
58
self.assertContainsRe(out,
59
r'--file(.|\n)*Take commit message from this file\.')
57
60
self.assertContainsRe(out, r'-h.*--help')
59
62
def test_option_help_global(self):
60
63
"""Global options have help strings."""
61
out, err = self.run_bzr_captured(['help', 'status'])
62
self.assertContainsRe(out, r'--show-ids.*show internal object')
64
out, err = self.run_bzr('help status')
65
self.assertContainsRe(out, r'--show-ids.*Show internal object.')
64
67
def test_option_arg_help(self):
65
68
"""Help message shows option arguments."""
66
out, err = self.run_bzr_captured(['help', 'commit'])
69
out, err = self.run_bzr('help commit')
67
70
self.assertEquals(err, '')
68
71
self.assertContainsRe(out, r'--file[ =]MSGFILE')
70
73
def test_unknown_short_opt(self):
71
out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
74
out, err = self.run_bzr('help -r', retcode=3)
72
75
self.assertContainsRe(err, r'no such option')
74
def test_get_short_name(self):
75
file_opt = option.Option.OPTIONS['file']
76
self.assertEquals(file_opt.short_name(), 'F')
77
force_opt = option.Option.OPTIONS['force']
78
self.assertEquals(force_opt.short_name(), None)
80
77
def test_set_short_name(self):
81
78
o = option.Option('wiggle')
82
79
o.set_short_name('w')
83
80
self.assertEqual(o.short_name(), 'w')
85
def test_old_short_names(self):
86
# test the deprecated method for getting and setting short option
89
"access to SHORT_OPTIONS was deprecated in version 0.14."
90
" Set the short option name when constructing the Option.",
91
DeprecationWarning, 2)
93
def capture_warning(message, category, stacklevel=None):
94
_warnings.append((message, category, stacklevel))
95
old_warning_method = symbol_versioning.warn
97
# an example of the kind of thing plugins might want to do through
98
# the old interface - make a new option and then give it a short
100
symbol_versioning.set_warning_method(capture_warning)
101
example_opt = option.Option('example', help='example option')
102
option.Option.SHORT_OPTIONS['w'] = example_opt
103
self.assertEqual(example_opt.short_name(), 'w')
104
self.assertEqual([expected_warning], _warnings)
105
# now check that it can actually be parsed with the registered
107
opts, args = parse([example_opt], ['-w', 'foo'])
108
self.assertEqual(opts.example, True)
109
self.assertEqual(args, ['foo'])
111
symbol_versioning.set_warning_method(old_warning_method)
113
82
def test_allow_dash(self):
114
83
"""Test that we can pass a plain '-' as an argument."""
165
134
self.assertRaises(errors.BzrCommandError, self.parse, options,
166
135
['--format', 'two'])
137
def test_override(self):
138
options = [option.Option('hello', type=str),
139
option.Option('hi', type=str, param_name='hello')]
140
opts, args = self.parse(options, ['--hello', 'a', '--hello', 'b'])
141
self.assertEqual('b', opts.hello)
142
opts, args = self.parse(options, ['--hello', 'b', '--hello', 'a'])
143
self.assertEqual('a', opts.hello)
144
opts, args = self.parse(options, ['--hello', 'a', '--hi', 'b'])
145
self.assertEqual('b', opts.hello)
146
opts, args = self.parse(options, ['--hi', 'b', '--hello', 'a'])
147
self.assertEqual('a', opts.hello)
168
149
def test_registry_converter(self):
169
150
options = [option.RegistryOption('format', '',
170
151
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
232
214
('two', None, None, 'two help'),
217
def test_option_callback_bool(self):
218
"Test booleans get True and False passed correctly to a callback."""
220
def cb(option, name, value, parser):
221
cb_calls.append((option,name,value,parser))
222
options = [option.Option('hello', custom_callback=cb)]
223
opts, args = self.parse(options, ['--hello', '--no-hello'])
224
self.assertEqual(2, len(cb_calls))
225
opt,name,value,parser = cb_calls[0]
226
self.assertEqual('hello', name)
227
self.assertTrue(value)
228
opt,name,value,parser = cb_calls[1]
229
self.assertEqual('hello', name)
230
self.assertFalse(value)
232
def test_option_callback_str(self):
233
"""Test callbacks work for string options both long and short."""
235
def cb(option, name, value, parser):
236
cb_calls.append((option,name,value,parser))
237
options = [option.Option('hello', type=str, custom_callback=cb,
239
opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
240
self.assertEqual(2, len(cb_calls))
241
opt,name,value,parser = cb_calls[0]
242
self.assertEqual('hello', name)
243
self.assertEqual('world', value)
244
opt,name,value,parser = cb_calls[1]
245
self.assertEqual('hello', name)
246
self.assertEqual('mars', value)
236
249
class TestListOptions(TestCase):
237
250
"""Tests for ListOption, used to specify lists on the command-line."""
267
280
opts, args = self.parse(
268
281
options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
269
282
self.assertEqual(['c'], opts.hello)
284
def test_option_callback_list(self):
285
"""Test callbacks work for list options."""
287
def cb(option, name, value, parser):
288
# Note that the value is a reference so copy to keep it
289
cb_calls.append((option,name,value[:],parser))
290
options = [option.ListOption('hello', type=str, custom_callback=cb)]
291
opts, args = self.parse(options, ['--hello=world', '--hello=mars',
293
self.assertEqual(3, len(cb_calls))
294
opt,name,value,parser = cb_calls[0]
295
self.assertEqual('hello', name)
296
self.assertEqual(['world'], value)
297
opt,name,value,parser = cb_calls[1]
298
self.assertEqual('hello', name)
299
self.assertEqual(['world', 'mars'], value)
300
opt,name,value,parser = cb_calls[2]
301
self.assertEqual('hello', name)
302
self.assertEqual([], value)
305
class TestOptionDefinitions(TestCase):
306
"""Tests for options in the Bazaar codebase."""
308
def get_builtin_command_options(self):
310
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
312
for opt_name, opt in sorted(cmd.options().items()):
313
g.append((cmd_name, opt))
316
def test_global_options_used(self):
317
# In the distant memory, options could only be declared globally. Now
318
# we prefer to declare them in the command, unless like -r they really
319
# are used very widely with the exact same meaning. So this checks
320
# for any that should be garbage collected.
321
g = dict(option.Option.OPTIONS.items())
324
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
325
for option_or_name in sorted(cmd_class.takes_options):
326
if not isinstance(option_or_name, basestring):
327
self.assertIsInstance(option_or_name, option.Option)
328
elif not option_or_name in g:
329
msgs.append("apparent reference to undefined "
330
"global option %r from %r"
331
% (option_or_name, cmd_class))
333
used_globals.setdefault(option_or_name, []).append(cmd_name)
334
unused_globals = set(g.keys()) - set(used_globals.keys())
335
# not enforced because there might be plugins that use these globals
336
## for option_name in sorted(unused_globals):
337
## msgs.append("unused global option %r" % option_name)
338
## for option_name, cmds in sorted(used_globals.items()):
339
## if len(cmds) <= 1:
340
## msgs.append("global option %r is only used by %r"
341
## % (option_name, cmds))
343
self.fail("problems with global option definitions:\n"
346
def test_option_grammar(self):
348
# Option help should be written in sentence form, and have a final
349
# period and be all on a single line, because the display code will
351
option_re = re.compile(r'^[A-Z][^\n]+\.$')
352
for scope, option in self.get_builtin_command_options():
354
msgs.append('%-16s %-16s %s' %
355
((scope or 'GLOBAL'), option.name, 'NO HELP'))
356
elif not option_re.match(option.help):
357
msgs.append('%-16s %-16s %s' %
358
((scope or 'GLOBAL'), option.name, option.help))
360
self.fail("The following options don't match the style guide:\n"
363
def test_is_hidden(self):
364
registry = bzrdir.BzrDirFormatRegistry()
365
registry.register_metadir('hidden', 'HiddenFormat',
366
'hidden help text', hidden=True)
367
registry.register_metadir('visible', 'VisibleFormat',
368
'visible help text', hidden=False)
369
format = option.RegistryOption('format', '', registry, str)
370
self.assertTrue(format.is_hidden('hidden'))
371
self.assertFalse(format.is_hidden('visible'))
373
def test_option_custom_help(self):
374
the_opt = option.Option.OPTIONS['help']
375
orig_help = the_opt.help[:]
376
my_opt = option.custom_help('help', 'suggest lottery numbers')
377
# Confirm that my_opt has my help and the original is unchanged
378
self.assertEqual('suggest lottery numbers', my_opt.help)
379
self.assertEqual(orig_help, the_opt.help)
382
class TestVerboseQuietLinkage(TestCase):
384
def check(self, parser, level, args):
385
option._verbosity_level = 0
386
opts, args = parser.parse_args(args)
387
self.assertEqual(level, option._verbosity_level)
389
def test_verbose_quiet_linkage(self):
390
parser = option.get_optparser(option.Option.STD_OPTIONS)
391
self.check(parser, 0, [])
392
self.check(parser, 1, ['-v'])
393
self.check(parser, 2, ['-v', '-v'])
394
self.check(parser, -1, ['-q'])
395
self.check(parser, -2, ['-qq'])
396
self.check(parser, -1, ['-v', '-v', '-q'])
397
self.check(parser, 2, ['-q', '-v', '-v'])
398
self.check(parser, 0, ['--no-verbose'])
399
self.check(parser, 0, ['-v', '-q', '--no-quiet'])