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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
19
from bzrlib import (
27
from bzrlib.builtins import cmd_commit
28
from bzrlib.commands import parse_args
28
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
29
from bzrlib.commands import Command, parse_args
29
30
from bzrlib.tests import TestCase
30
31
from bzrlib.repofmt import knitrepo
41
42
def test_parse_args(self):
42
43
"""Option parser"""
43
# XXX: Using cmd_commit makes these tests overly sensitive to changes
44
# to cmd_commit, when they are meant to be about option parsing in
46
self.assertEqual(parse_args(cmd_commit(), ['--help']),
47
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
48
self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
49
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}))
44
eq = self.assertEquals
45
eq(parse_args(cmd_commit(), ['--help']),
46
([], {'fixes': [], 'help': True}))
47
eq(parse_args(cmd_commit(), ['--message=biter']),
48
([], {'fixes': [], 'message': 'biter'}))
51
50
def test_no_more_opts(self):
52
51
"""Terminated options"""
53
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
54
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
52
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
53
(['-file-with-dashes'], {'fixes': []}))
56
55
def test_option_help(self):
57
56
"""Options have help strings."""
104
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'))
111
107
def test_registry_conversion(self):
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',
108
registry = bzrdir.BzrDirFormatRegistry()
109
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
110
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
111
registry.register_metadir('hidden', 'RepositoryFormatKnit1',
116
112
'two help', hidden=True)
117
113
registry.set_default('one')
118
114
options = [option.RegistryOption('format', '', registry, str)]
138
134
self.assertRaises(errors.BzrCommandError, self.parse, options,
139
135
['--format', 'two'])
141
def test_override(self):
142
options = [option.Option('hello', type=str),
143
option.Option('hi', type=str, param_name='hello')]
144
opts, args = self.parse(options, ['--hello', 'a', '--hello', 'b'])
145
self.assertEqual('b', opts.hello)
146
opts, args = self.parse(options, ['--hello', 'b', '--hello', 'a'])
147
self.assertEqual('a', opts.hello)
148
opts, args = self.parse(options, ['--hello', 'a', '--hi', 'b'])
149
self.assertEqual('b', opts.hello)
150
opts, args = self.parse(options, ['--hi', 'b', '--hello', 'a'])
151
self.assertEqual('a', opts.hello)
153
137
def test_registry_converter(self):
154
138
options = [option.RegistryOption('format', '',
155
139
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
157
141
self.assertIsInstance(opts.format.repository_format,
158
142
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'])
169
144
def test_from_kwargs(self):
170
145
my_option = option.RegistryOption.from_kwargs('my-option',
171
146
help='test option', short='be short', be_long='go long')
179
154
self.assertEqual('test option', my_option.help)
181
156
def test_help(self):
182
registry = controldir.ControlDirFormatRegistry()
183
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
184
bzrdir.register_metadir(registry, 'two',
157
registry = bzrdir.BzrDirFormatRegistry()
158
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
159
registry.register_metadir('two',
185
160
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
188
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
163
registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
190
165
registry.set_default('one')
191
166
options = [option.RegistryOption('format', 'format help', registry,
207
182
opt = option.Option('hello', help='fg', type=int, argname='gar')
208
183
self.assertEqual(list(opt.iter_switches()),
209
184
[('hello', None, 'GAR', 'fg')])
210
registry = controldir.ControlDirFormatRegistry()
211
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
212
bzrdir.register_metadir(registry, 'two',
185
registry = bzrdir.BzrDirFormatRegistry()
186
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
187
registry.register_metadir('two',
213
188
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
227
202
('two', None, None, 'two help'),
230
def test_option_callback_bool(self):
231
"Test booleans get True and False passed correctly to a callback."""
233
def cb(option, name, value, parser):
234
cb_calls.append((option,name,value,parser))
235
options = [option.Option('hello', custom_callback=cb)]
236
opts, args = self.parse(options, ['--hello', '--no-hello'])
237
self.assertEqual(2, len(cb_calls))
238
opt,name,value,parser = cb_calls[0]
239
self.assertEqual('hello', name)
240
self.assertTrue(value)
241
opt,name,value,parser = cb_calls[1]
242
self.assertEqual('hello', name)
243
self.assertFalse(value)
245
def test_option_callback_str(self):
246
"""Test callbacks work for string options both long and short."""
248
def cb(option, name, value, parser):
249
cb_calls.append((option,name,value,parser))
250
options = [option.Option('hello', type=str, custom_callback=cb,
252
opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
253
self.assertEqual(2, len(cb_calls))
254
opt,name,value,parser = cb_calls[0]
255
self.assertEqual('hello', name)
256
self.assertEqual('world', value)
257
opt,name,value,parser = cb_calls[1]
258
self.assertEqual('hello', name)
259
self.assertEqual('mars', value)
262
206
class TestListOptions(TestCase):
263
207
"""Tests for ListOption, used to specify lists on the command-line."""
300
238
options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
301
239
self.assertEqual(['c'], opts.hello)
303
def test_option_callback_list(self):
304
"""Test callbacks work for list options."""
306
def cb(option, name, value, parser):
307
# Note that the value is a reference so copy to keep it
308
cb_calls.append((option,name,value[:],parser))
309
options = [option.ListOption('hello', type=str, custom_callback=cb)]
310
opts, args = self.parse(options, ['--hello=world', '--hello=mars',
312
self.assertEqual(3, len(cb_calls))
313
opt,name,value,parser = cb_calls[0]
314
self.assertEqual('hello', name)
315
self.assertEqual(['world'], value)
316
opt,name,value,parser = cb_calls[1]
317
self.assertEqual('hello', name)
318
self.assertEqual(['world', 'mars'], value)
319
opt,name,value,parser = cb_calls[2]
320
self.assertEqual('hello', name)
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)
331
242
class TestOptionDefinitions(TestCase):
332
243
"""Tests for options in the Bazaar codebase."""
334
245
def get_builtin_command_options(self):
336
for cmd_name in sorted(commands.all_command_names()):
337
cmd = commands.get_cmd_object(cmd_name)
247
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
338
249
for opt_name, opt in sorted(cmd.options().items()):
339
250
g.append((cmd_name, opt))
347
258
g = dict(option.Option.OPTIONS.items())
348
259
used_globals = {}
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):
261
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
262
for option_or_name in sorted(cmd_class.takes_options):
353
263
if not isinstance(option_or_name, basestring):
354
264
self.assertIsInstance(option_or_name, option.Option)
355
265
elif not option_or_name in g:
356
266
msgs.append("apparent reference to undefined "
357
267
"global option %r from %r"
358
% (option_or_name, cmd))
268
% (option_or_name, cmd_class))
360
270
used_globals.setdefault(option_or_name, []).append(cmd_name)
361
271
unused_globals = set(g.keys()) - set(used_globals.keys())
376
286
# period and be all on a single line, because the display code will
378
288
option_re = re.compile(r'^[A-Z][^\n]+\.$')
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))
289
for scope, option in self.get_builtin_command_options():
291
msgs.append('%-16s %-16s %s' %
292
((scope or 'GLOBAL'), option.name, 'NO HELP'))
293
elif not option_re.match(option.help):
294
msgs.append('%-16s %-16s %s' %
295
((scope or 'GLOBAL'), option.name, option.help))
387
297
self.fail("The following options don't match the style guide:\n"
388
298
+ '\n'.join(msgs))
390
def test_is_hidden(self):
391
registry = controldir.ControlDirFormatRegistry()
392
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
393
'hidden help text', hidden=True)
394
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
395
'visible help text', hidden=False)
396
format = option.RegistryOption('format', '', registry, str)
397
self.assertTrue(format.is_hidden('hidden'))
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())
408
def test_option_custom_help(self):
409
the_opt = option.Option.OPTIONS['help']
410
orig_help = the_opt.help[:]
411
my_opt = option.custom_help('help', 'suggest lottery numbers')
412
# Confirm that my_opt has my help and the original is unchanged
413
self.assertEqual('suggest lottery numbers', my_opt.help)
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)
428
class TestVerboseQuietLinkage(TestCase):
430
def check(self, parser, level, args):
431
option._verbosity_level = 0
432
opts, args = parser.parse_args(args)
433
self.assertEqual(level, option._verbosity_level)
435
def test_verbose_quiet_linkage(self):
436
parser = option.get_optparser(option.Option.STD_OPTIONS)
437
self.check(parser, 0, [])
438
self.check(parser, 1, ['-v'])
439
self.check(parser, 2, ['-v', '-v'])
440
self.check(parser, -1, ['-q'])
441
self.check(parser, -2, ['-qq'])
442
self.check(parser, -1, ['-v', '-v', '-q'])
443
self.check(parser, 2, ['-q', '-v', '-v'])
444
self.check(parser, 0, ['--no-verbose'])
445
self.check(parser, 0, ['-v', '-q', '--no-quiet'])