1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
1
# Copyright (C) 2005-2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
27
from ..builtins import cmd_commit
28
from ..commands import parse_args
29
from . import TestCase
30
from ..bzr import knitrepo
27
from bzrlib.builtins import cmd_commit
28
from bzrlib.commands import parse_args
29
from bzrlib.tests import TestCase
30
from bzrlib.repofmt import knitrepo
33
33
def parse(options, args):
119
119
def test_registry_conversion(self):
120
120
registry = controldir.ControlDirFormatRegistry()
121
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
122
bzr.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
123
bzr.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
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',
124
124
'two help', hidden=True)
125
125
registry.set_default('one')
126
126
options = [option.RegistryOption('format', '', registry, str)]
128
128
self.assertEqual({'format':'one'}, opts)
129
129
opts, args = self.parse(options, ['--format', 'two'])
130
130
self.assertEqual({'format':'two'}, opts)
131
self.assertRaises(option.BadOptionValue, self.parse, options,
131
self.assertRaises(errors.BadOptionValue, self.parse, options,
132
132
['--format', 'three'])
133
133
self.assertRaises(errors.BzrCommandError, self.parse, options,
161
161
def test_registry_converter(self):
162
162
options = [option.RegistryOption('format', '',
163
controldir.format_registry, controldir.format_registry.make_controldir)]
163
controldir.format_registry, controldir.format_registry.make_bzrdir)]
164
164
opts, args = self.parse(options, ['--format', 'knit'])
165
165
self.assertIsInstance(opts.format.repository_format,
166
166
knitrepo.RepositoryFormatKnit1)
168
168
def test_lazy_registry(self):
169
169
options = [option.RegistryOption('format', '',
170
lazy_registry=('breezy.controldir', 'format_registry'),
170
lazy_registry=('bzrlib.controldir','format_registry'),
172
172
opts, args = self.parse(options, ['--format', 'knit'])
173
173
self.assertEqual({'format': 'knit'}, opts)
174
174
self.assertRaises(
175
option.BadOptionValue, self.parse, options, ['--format', 'BAD'])
175
errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
177
177
def test_from_kwargs(self):
178
178
my_option = option.RegistryOption.from_kwargs('my-option',
189
189
def test_help(self):
190
190
registry = controldir.ControlDirFormatRegistry()
191
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
bzr.register_metadir(registry, 'two',
193
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
191
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
bzrdir.register_metadir(registry, 'two',
193
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
196
bzr.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
196
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
198
198
registry.set_default('one')
199
199
options = [option.RegistryOption('format', 'format help', registry,
216
216
self.assertEqual(list(opt.iter_switches()),
217
217
[('hello', None, 'GAR', 'fg')])
218
218
registry = controldir.ControlDirFormatRegistry()
219
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
bzr.register_metadir(registry, 'two',
221
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
219
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
bzrdir.register_metadir(registry, 'two',
221
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
224
224
registry.set_default('one')
239
239
"Test booleans get True and False passed correctly to a callback."""
241
241
def cb(option, name, value, parser):
242
cb_calls.append((option, name, value, parser))
242
cb_calls.append((option,name,value,parser))
243
243
options = [option.Option('hello', custom_callback=cb)]
244
244
opts, args = self.parse(options, ['--hello', '--no-hello'])
245
245
self.assertEqual(2, len(cb_calls))
246
opt, name, value, parser = cb_calls[0]
246
opt,name,value,parser = cb_calls[0]
247
247
self.assertEqual('hello', name)
248
248
self.assertTrue(value)
249
opt, name, value, parser = cb_calls[1]
249
opt,name,value,parser = cb_calls[1]
250
250
self.assertEqual('hello', name)
251
251
self.assertFalse(value)
254
254
"""Test callbacks work for string options both long and short."""
256
256
def cb(option, name, value, parser):
257
cb_calls.append((option, name, value, parser))
257
cb_calls.append((option,name,value,parser))
258
258
options = [option.Option('hello', type=str, custom_callback=cb,
260
260
opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
261
261
self.assertEqual(2, len(cb_calls))
262
opt, name, value, parser = cb_calls[0]
262
opt,name,value,parser = cb_calls[0]
263
263
self.assertEqual('hello', name)
264
264
self.assertEqual('world', value)
265
opt, name, value, parser = cb_calls[1]
265
opt,name,value,parser = cb_calls[1]
266
266
self.assertEqual('hello', name)
267
267
self.assertEqual('mars', value)
314
314
def cb(option, name, value, parser):
315
315
# Note that the value is a reference so copy to keep it
316
cb_calls.append((option, name, value[:], parser))
316
cb_calls.append((option,name,value[:],parser))
317
317
options = [option.ListOption('hello', type=str, custom_callback=cb)]
318
318
opts, args = self.parse(options, ['--hello=world', '--hello=mars',
320
320
self.assertEqual(3, len(cb_calls))
321
opt, name, value, parser = cb_calls[0]
321
opt,name,value,parser = cb_calls[0]
322
322
self.assertEqual('hello', name)
323
323
self.assertEqual(['world'], value)
324
opt, name, value, parser = cb_calls[1]
324
opt,name,value,parser = cb_calls[1]
325
325
self.assertEqual('hello', name)
326
326
self.assertEqual(['world', 'mars'], value)
327
opt, name, value, parser = cb_calls[2]
327
opt,name,value,parser = cb_calls[2]
328
328
self.assertEqual('hello', name)
329
329
self.assertEqual([], value)
346
346
cmd = commands.get_cmd_object(cmd_name)
347
347
for opt_name, opt in sorted(cmd.options().items()):
348
348
g.append((cmd_name, opt))
352
352
def test_option_grammar(self):
378
378
def test_is_hidden(self):
379
379
registry = controldir.ControlDirFormatRegistry()
380
bzr.register_metadir(registry, 'hidden', 'HiddenFormat',
380
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
381
381
'hidden help text', hidden=True)
382
bzr.register_metadir(registry, 'visible', 'VisibleFormat',
382
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
383
383
'visible help text', hidden=False)
384
384
format = option.RegistryOption('format', '', registry, str)
385
385
self.assertTrue(format.is_hidden('hidden'))
388
388
def test_short_name(self):
389
389
registry = controldir.ControlDirFormatRegistry()
390
390
opt = option.RegistryOption('format', help='', registry=registry)
391
self.assertEqual(None, opt.short_name())
391
self.assertEquals(None, opt.short_name())
392
392
opt = option.RegistryOption('format', short_name='F', help='',
393
393
registry=registry)
394
self.assertEqual('F', opt.short_name())
394
self.assertEquals('F', opt.short_name())
396
396
def test_option_custom_help(self):
397
397
the_opt = option.Option.OPTIONS['help']