74
74
out, err = self.run_bzr('help -r', retcode=3)
75
75
self.assertContainsRe(err, r'no such option')
77
def test_get_short_name(self):
78
file_opt = option.Option.OPTIONS['file']
79
self.assertEquals(file_opt.short_name(), 'F')
80
force_opt = option.Option.OPTIONS['force']
81
self.assertEquals(force_opt.short_name(), None)
83
77
def test_set_short_name(self):
84
78
o = option.Option('wiggle')
85
79
o.set_short_name('w')
86
80
self.assertEqual(o.short_name(), 'w')
88
def test_old_short_names(self):
89
# test the deprecated method for getting and setting short option
92
"access to SHORT_OPTIONS was deprecated in version 0.14."
93
" Set the short option name when constructing the Option.",
94
DeprecationWarning, 2)
96
def capture_warning(message, category, stacklevel=None):
97
_warnings.append((message, category, stacklevel))
98
old_warning_method = symbol_versioning.warn
100
# an example of the kind of thing plugins might want to do through
101
# the old interface - make a new option and then give it a short
103
symbol_versioning.set_warning_method(capture_warning)
104
example_opt = option.Option('example', help='example option')
105
option.Option.SHORT_OPTIONS['w'] = example_opt
106
self.assertEqual(example_opt.short_name(), 'w')
107
self.assertEqual([expected_warning], _warnings)
108
# now check that it can actually be parsed with the registered
110
opts, args = parse([example_opt], ['-w', 'foo'])
111
self.assertEqual(opts.example, True)
112
self.assertEqual(args, ['foo'])
114
symbol_versioning.set_warning_method(old_warning_method)
116
82
def test_allow_dash(self):
117
83
"""Test that we can pass a plain '-' as an argument."""
296
262
self.assertTrue(len(all) > 100,
297
263
"too few options found: %r" % all)
265
def test_global_options_used(self):
266
# In the distant memory, options could only be declared globally. Now
267
# we prefer to declare them in the command, unless like -r they really
268
# are used very widely with the exact same meaning. So this checks
269
# for any that should be garbage collected.
270
g = dict(option.Option.OPTIONS.items())
273
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
274
for option_or_name in sorted(cmd_class.takes_options):
275
if not isinstance(option_or_name, basestring):
276
self.assertIsInstance(option_or_name, option.Option)
277
elif not option_or_name in g:
278
msgs.append("apparent reference to undefined "
279
"global option %r from %r"
280
% (option_or_name, cmd_class))
282
used_globals.setdefault(option_or_name, []).append(cmd_name)
283
unused_globals = set(g.keys()) - set(used_globals.keys())
284
for option_name in sorted(unused_globals):
285
msgs.append("unused global option %r" % option_name)
286
for option_name, cmds in sorted(used_globals.items()):
288
msgs.append("global option %r is only used by %r"
289
% (option_name, cmds))
291
self.fail("problems with global option definitions:\n"
299
294
def test_option_grammar(self):
301
296
# Option help should be written in sentence form, and have a final
304
299
option_re = re.compile(r'^[A-Z][^\n]+\.$')
305
300
for scope, option in self.get_all_options():
306
301
if not option.help:
307
# TODO: Also complain about options that have no help message?
309
if not option_re.match(option.help):
302
msgs.append('%-16s %-16s %s' %
303
((scope or 'GLOBAL'), option.name, 'NO HELP'))
304
elif not option_re.match(option.help):
310
305
msgs.append('%-16s %-16s %s' %
311
306
((scope or 'GLOBAL'), option.name, option.help))