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 (
25
from bzrlib.builtins import cmd_commit
28
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
26
29
from bzrlib.commands import Command, parse_args
27
30
from bzrlib.tests import TestCase
28
31
from bzrlib.repofmt import knitrepo
39
42
def test_parse_args(self):
40
43
"""Option parser"""
41
# XXX: Using cmd_commit makes these tests overly sensitive to changes
42
# to cmd_commit, when they are meant to be about option parsing in
44
self.assertEqual(parse_args(cmd_commit(), ['--help']),
45
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
46
self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
47
([], {'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'}))
49
50
def test_no_more_opts(self):
50
51
"""Terminated options"""
51
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
52
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
53
(['-file-with-dashes'], {'fixes': []}))
54
55
def test_option_help(self):
55
56
"""Options have help strings."""
91
93
opts, args = self.parse(options, ['--no-hello', '--hello'])
92
94
self.assertEqual(True, opts.hello)
93
95
opts, args = self.parse(options, [])
94
self.assertFalse(hasattr(opts, 'hello'))
96
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
95
97
opts, args = self.parse(options, ['--hello', '--no-hello'])
96
98
self.assertEqual(False, opts.hello)
97
99
options = [option.Option('number', type=int)]
136
134
self.assertRaises(errors.BzrCommandError, self.parse, options,
137
135
['--format', 'two'])
139
def test_override(self):
140
options = [option.Option('hello', type=str),
141
option.Option('hi', type=str, param_name='hello')]
142
opts, args = self.parse(options, ['--hello', 'a', '--hello', 'b'])
143
self.assertEqual('b', opts.hello)
144
opts, args = self.parse(options, ['--hello', 'b', '--hello', 'a'])
145
self.assertEqual('a', opts.hello)
146
opts, args = self.parse(options, ['--hello', 'a', '--hi', 'b'])
147
self.assertEqual('b', opts.hello)
148
opts, args = self.parse(options, ['--hi', 'b', '--hello', 'a'])
149
self.assertEqual('a', opts.hello)
151
137
def test_registry_converter(self):
152
138
options = [option.RegistryOption('format', '',
153
139
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
155
141
self.assertIsInstance(opts.format.repository_format,
156
142
knitrepo.RepositoryFormatKnit1)
158
def test_lazy_registry(self):
159
options = [option.RegistryOption('format', '',
160
lazy_registry=('bzrlib.bzrdir','format_registry'),
162
opts, args = self.parse(options, ['--format', 'knit'])
163
self.assertEqual({'format': 'knit'}, opts)
165
errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
167
144
def test_from_kwargs(self):
168
145
my_option = option.RegistryOption.from_kwargs('my-option',
169
146
help='test option', short='be short', be_long='go long')
225
201
('two', None, None, 'two help'),
228
def test_option_callback_bool(self):
229
"Test booleans get True and False passed correctly to a callback."""
231
def cb(option, name, value, parser):
232
cb_calls.append((option,name,value,parser))
233
options = [option.Option('hello', custom_callback=cb)]
234
opts, args = self.parse(options, ['--hello', '--no-hello'])
235
self.assertEqual(2, len(cb_calls))
236
opt,name,value,parser = cb_calls[0]
237
self.assertEqual('hello', name)
238
self.assertTrue(value)
239
opt,name,value,parser = cb_calls[1]
240
self.assertEqual('hello', name)
241
self.assertFalse(value)
243
def test_option_callback_str(self):
244
"""Test callbacks work for string options both long and short."""
246
def cb(option, name, value, parser):
247
cb_calls.append((option,name,value,parser))
248
options = [option.Option('hello', type=str, custom_callback=cb,
250
opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
251
self.assertEqual(2, len(cb_calls))
252
opt,name,value,parser = cb_calls[0]
253
self.assertEqual('hello', name)
254
self.assertEqual('world', value)
255
opt,name,value,parser = cb_calls[1]
256
self.assertEqual('hello', name)
257
self.assertEqual('mars', value)
260
205
class TestListOptions(TestCase):
261
206
"""Tests for ListOption, used to specify lists on the command-line."""
269
214
opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
270
215
self.assertEqual(['world', 'sailor'], opts.hello)
272
def test_list_option_with_dash(self):
273
options = [option.ListOption('with-dash', type=str)]
274
opts, args = self.parse(options, ['--with-dash=world',
275
'--with-dash=sailor'])
276
self.assertEqual(['world', 'sailor'], opts.with_dash)
278
217
def test_list_option_no_arguments(self):
279
218
options = [option.ListOption('hello', type=str)]
280
219
opts, args = self.parse(options, [])
298
237
options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
299
238
self.assertEqual(['c'], opts.hello)
301
def test_option_callback_list(self):
302
"""Test callbacks work for list options."""
304
def cb(option, name, value, parser):
305
# Note that the value is a reference so copy to keep it
306
cb_calls.append((option,name,value[:],parser))
307
options = [option.ListOption('hello', type=str, custom_callback=cb)]
308
opts, args = self.parse(options, ['--hello=world', '--hello=mars',
310
self.assertEqual(3, len(cb_calls))
311
opt,name,value,parser = cb_calls[0]
312
self.assertEqual('hello', name)
313
self.assertEqual(['world'], value)
314
opt,name,value,parser = cb_calls[1]
315
self.assertEqual('hello', name)
316
self.assertEqual(['world', 'mars'], value)
317
opt,name,value,parser = cb_calls[2]
318
self.assertEqual('hello', name)
319
self.assertEqual([], value)
322
241
class TestOptionDefinitions(TestCase):
323
242
"""Tests for options in the Bazaar codebase."""
325
244
def get_builtin_command_options(self):
327
for cmd_name in sorted(commands.all_command_names()):
328
cmd = commands.get_cmd_object(cmd_name)
246
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
329
248
for opt_name, opt in sorted(cmd.options().items()):
330
249
g.append((cmd_name, opt))
338
257
g = dict(option.Option.OPTIONS.items())
339
258
used_globals = {}
341
for cmd_name in sorted(commands.all_command_names()):
342
cmd = commands.get_cmd_object(cmd_name)
343
for option_or_name in sorted(cmd.takes_options):
260
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
261
for option_or_name in sorted(cmd_class.takes_options):
344
262
if not isinstance(option_or_name, basestring):
345
263
self.assertIsInstance(option_or_name, option.Option)
346
264
elif not option_or_name in g:
347
265
msgs.append("apparent reference to undefined "
348
266
"global option %r from %r"
349
% (option_or_name, cmd))
267
% (option_or_name, cmd_class))
351
269
used_globals.setdefault(option_or_name, []).append(cmd_name)
352
270
unused_globals = set(g.keys()) - set(used_globals.keys())
367
285
# period and be all on a single line, because the display code will
369
287
option_re = re.compile(r'^[A-Z][^\n]+\.$')
370
for scope, opt in self.get_builtin_command_options():
372
msgs.append('%-16s %-16s %s' %
373
((scope or 'GLOBAL'), opt.name, 'NO HELP'))
374
elif not option_re.match(opt.help):
375
msgs.append('%-16s %-16s %s' %
376
((scope or 'GLOBAL'), opt.name, opt.help))
288
for scope, option in self.get_builtin_command_options():
290
msgs.append('%-16s %-16s %s' %
291
((scope or 'GLOBAL'), option.name, 'NO HELP'))
292
elif not option_re.match(option.help):
293
msgs.append('%-16s %-16s %s' %
294
((scope or 'GLOBAL'), option.name, option.help))
378
296
self.fail("The following options don't match the style guide:\n"
379
297
+ '\n'.join(msgs))
381
def test_is_hidden(self):
382
registry = bzrdir.BzrDirFormatRegistry()
383
registry.register_metadir('hidden', 'HiddenFormat',
384
'hidden help text', hidden=True)
385
registry.register_metadir('visible', 'VisibleFormat',
386
'visible help text', hidden=False)
387
format = option.RegistryOption('format', '', registry, str)
388
self.assertTrue(format.is_hidden('hidden'))
389
self.assertFalse(format.is_hidden('visible'))
391
def test_option_custom_help(self):
392
the_opt = option.Option.OPTIONS['help']
393
orig_help = the_opt.help[:]
394
my_opt = option.custom_help('help', 'suggest lottery numbers')
395
# Confirm that my_opt has my help and the original is unchanged
396
self.assertEqual('suggest lottery numbers', my_opt.help)
397
self.assertEqual(orig_help, the_opt.help)
400
class TestVerboseQuietLinkage(TestCase):
402
def check(self, parser, level, args):
403
option._verbosity_level = 0
404
opts, args = parser.parse_args(args)
405
self.assertEqual(level, option._verbosity_level)
407
def test_verbose_quiet_linkage(self):
408
parser = option.get_optparser(option.Option.STD_OPTIONS)
409
self.check(parser, 0, [])
410
self.check(parser, 1, ['-v'])
411
self.check(parser, 2, ['-v', '-v'])
412
self.check(parser, -1, ['-q'])
413
self.check(parser, -2, ['-qq'])
414
self.check(parser, -1, ['-v', '-v', '-q'])
415
self.check(parser, 2, ['-q', '-v', '-v'])
416
self.check(parser, 0, ['--no-verbose'])
417
self.check(parser, 0, ['-v', '-q', '--no-quiet'])