13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
from bzrlib import (
25
27
from bzrlib.builtins import cmd_commit
26
from bzrlib.commands import Command, parse_args
28
from bzrlib.commands import parse_args
27
29
from bzrlib.tests import TestCase
28
30
from bzrlib.repofmt import knitrepo
42
44
# to cmd_commit, when they are meant to be about option parsing in
44
46
self.assertEqual(parse_args(cmd_commit(), ['--help']),
45
([], {'exclude': [], 'fixes': [], 'help': True}))
47
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
46
48
self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
47
([], {'exclude': [], 'fixes': [], 'message': 'biter'}))
49
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}))
49
51
def test_no_more_opts(self):
50
52
"""Terminated options"""
51
53
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
(['-file-with-dashes'], {'exclude': [], 'fixes': []}))
54
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
54
56
def test_option_help(self):
55
57
"""Options have help strings."""
102
104
self.assertRaises(errors.BzrCommandError, self.parse, options,
107
def test_is_hidden(self):
108
self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
109
self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
105
111
def test_registry_conversion(self):
106
registry = bzrdir.BzrDirFormatRegistry()
107
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
108
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
109
registry.register_metadir('hidden', 'RepositoryFormatKnit1',
112
registry = controldir.ControlDirFormatRegistry()
113
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
114
bzrdir.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
115
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
110
116
'two help', hidden=True)
111
117
registry.set_default('one')
112
118
options = [option.RegistryOption('format', '', registry, str)]
151
157
self.assertIsInstance(opts.format.repository_format,
152
158
knitrepo.RepositoryFormatKnit1)
160
def test_lazy_registry(self):
161
options = [option.RegistryOption('format', '',
162
lazy_registry=('bzrlib.bzrdir','format_registry'),
164
opts, args = self.parse(options, ['--format', 'knit'])
165
self.assertEqual({'format': 'knit'}, opts)
167
errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
154
169
def test_from_kwargs(self):
155
170
my_option = option.RegistryOption.from_kwargs('my-option',
156
171
help='test option', short='be short', be_long='go long')
164
179
self.assertEqual('test option', my_option.help)
166
181
def test_help(self):
167
registry = bzrdir.BzrDirFormatRegistry()
168
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
169
registry.register_metadir('two',
182
registry = controldir.ControlDirFormatRegistry()
183
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
184
bzrdir.register_metadir(registry, 'two',
170
185
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
173
registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
188
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
175
190
registry.set_default('one')
176
191
options = [option.RegistryOption('format', 'format help', registry,
192
207
opt = option.Option('hello', help='fg', type=int, argname='gar')
193
208
self.assertEqual(list(opt.iter_switches()),
194
209
[('hello', None, 'GAR', 'fg')])
195
registry = bzrdir.BzrDirFormatRegistry()
196
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
197
registry.register_metadir('two',
210
registry = controldir.ControlDirFormatRegistry()
211
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
212
bzrdir.register_metadir(registry, 'two',
198
213
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
305
320
self.assertEqual('hello', name)
306
321
self.assertEqual([], value)
323
def test_list_option_param_name(self):
324
"""Test list options can have their param_name set."""
325
options = [option.ListOption('hello', type=str, param_name='greeting')]
326
opts, args = self.parse(
327
options, ['--hello=world', '--hello=sailor'])
328
self.assertEqual(['world', 'sailor'], opts.greeting)
309
331
class TestOptionDefinitions(TestCase):
310
332
"""Tests for options in the Bazaar codebase."""
312
334
def get_builtin_command_options(self):
314
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
336
for cmd_name in sorted(commands.all_command_names()):
337
cmd = commands.get_cmd_object(cmd_name)
316
338
for opt_name, opt in sorted(cmd.options().items()):
317
339
g.append((cmd_name, opt))
325
347
g = dict(option.Option.OPTIONS.items())
326
348
used_globals = {}
328
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
329
for option_or_name in sorted(cmd_class.takes_options):
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):
330
353
if not isinstance(option_or_name, basestring):
331
354
self.assertIsInstance(option_or_name, option.Option)
332
355
elif not option_or_name in g:
333
356
msgs.append("apparent reference to undefined "
334
357
"global option %r from %r"
335
% (option_or_name, cmd_class))
358
% (option_or_name, cmd))
337
360
used_globals.setdefault(option_or_name, []).append(cmd_name)
338
361
unused_globals = set(g.keys()) - set(used_globals.keys())
353
376
# period and be all on a single line, because the display code will
355
378
option_re = re.compile(r'^[A-Z][^\n]+\.$')
356
for scope, option in self.get_builtin_command_options():
358
msgs.append('%-16s %-16s %s' %
359
((scope or 'GLOBAL'), option.name, 'NO HELP'))
360
elif not option_re.match(option.help):
361
msgs.append('%-16s %-16s %s' %
362
((scope or 'GLOBAL'), option.name, option.help))
379
for scope, opt in self.get_builtin_command_options():
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))
364
387
self.fail("The following options don't match the style guide:\n"
365
388
+ '\n'.join(msgs))
367
390
def test_is_hidden(self):
368
registry = bzrdir.BzrDirFormatRegistry()
369
registry.register_metadir('hidden', 'HiddenFormat',
391
registry = controldir.ControlDirFormatRegistry()
392
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
370
393
'hidden help text', hidden=True)
371
registry.register_metadir('visible', 'VisibleFormat',
394
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
372
395
'visible help text', hidden=False)
373
396
format = option.RegistryOption('format', '', registry, str)
374
397
self.assertTrue(format.is_hidden('hidden'))
375
398
self.assertFalse(format.is_hidden('visible'))
400
def test_short_name(self):
401
registry = controldir.ControlDirFormatRegistry()
402
opt = option.RegistryOption('format', help='', registry=registry)
403
self.assertEquals(None, opt.short_name())
404
opt = option.RegistryOption('format', short_name='F', help='',
406
self.assertEquals('F', opt.short_name())
377
408
def test_option_custom_help(self):
378
409
the_opt = option.Option.OPTIONS['help']
379
410
orig_help = the_opt.help[:]
382
413
self.assertEqual('suggest lottery numbers', my_opt.help)
383
414
self.assertEqual(orig_help, the_opt.help)
416
def test_short_value_switches(self):
417
reg = registry.Registry()
418
reg.register('short', 'ShortChoice')
419
reg.register('long', 'LongChoice')
420
ropt = option.RegistryOption('choice', '', reg, value_switches=True,
421
short_value_switches={'short': 's'})
422
opts, args = parse([ropt], ['--short'])
423
self.assertEqual('ShortChoice', opts.choice)
424
opts, args = parse([ropt], ['-s'])
425
self.assertEqual('ShortChoice', opts.choice)
386
428
class TestVerboseQuietLinkage(TestCase):