1
# Copyright (C) 2005, 2006, 2007 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
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
41
43
# XXX: Using cmd_commit makes these tests overly sensitive to changes
42
44
# 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'}))
47
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}),
48
parse_args(cmd_commit(), ['--help']))
50
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
51
parse_args(cmd_commit(), ['--message=biter']))
49
53
def test_no_more_opts(self):
50
54
"""Terminated options"""
51
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
56
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
57
parse_args(cmd_commit(), ['--', '-file-with-dashes']))
54
59
def test_option_help(self):
55
60
"""Options have help strings."""
63
68
out, err = self.run_bzr('help status')
64
69
self.assertContainsRe(out, r'--show-ids.*Show internal object.')
71
def test_option_help_global_hidden(self):
72
"""Hidden global options have no help strings."""
73
out, err = self.run_bzr('help log')
74
self.assertNotContainsRe(out, r'--message')
66
76
def test_option_arg_help(self):
67
77
"""Help message shows option arguments."""
68
78
out, err = self.run_bzr('help commit')
107
117
self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
109
119
def test_registry_conversion(self):
110
registry = bzrdir.BzrDirFormatRegistry()
111
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
112
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
113
registry.register_metadir('hidden', 'RepositoryFormatKnit1',
120
registry = controldir.ControlDirFormatRegistry()
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',
114
124
'two help', hidden=True)
115
125
registry.set_default('one')
116
126
options = [option.RegistryOption('format', '', registry, str)]
177
187
self.assertEqual('test option', my_option.help)
179
189
def test_help(self):
180
registry = bzrdir.BzrDirFormatRegistry()
181
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
182
registry.register_metadir('two',
190
registry = controldir.ControlDirFormatRegistry()
191
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
bzrdir.register_metadir(registry, 'two',
183
193
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
186
registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
196
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
188
198
registry.set_default('one')
189
199
options = [option.RegistryOption('format', 'format help', registry,
205
215
opt = option.Option('hello', help='fg', type=int, argname='gar')
206
216
self.assertEqual(list(opt.iter_switches()),
207
217
[('hello', None, 'GAR', 'fg')])
208
registry = bzrdir.BzrDirFormatRegistry()
209
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
210
registry.register_metadir('two',
218
registry = controldir.ControlDirFormatRegistry()
219
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
bzrdir.register_metadir(registry, 'two',
211
221
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
318
328
self.assertEqual('hello', name)
319
329
self.assertEqual([], value)
331
def test_list_option_param_name(self):
332
"""Test list options can have their param_name set."""
333
options = [option.ListOption('hello', type=str, param_name='greeting')]
334
opts, args = self.parse(
335
options, ['--hello=world', '--hello=sailor'])
336
self.assertEqual(['world', 'sailor'], opts.greeting)
322
339
class TestOptionDefinitions(TestCase):
323
340
"""Tests for options in the Bazaar codebase."""
379
396
+ '\n'.join(msgs))
381
398
def test_is_hidden(self):
382
registry = bzrdir.BzrDirFormatRegistry()
383
registry.register_metadir('hidden', 'HiddenFormat',
399
registry = controldir.ControlDirFormatRegistry()
400
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
384
401
'hidden help text', hidden=True)
385
registry.register_metadir('visible', 'VisibleFormat',
402
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
386
403
'visible help text', hidden=False)
387
404
format = option.RegistryOption('format', '', registry, str)
388
405
self.assertTrue(format.is_hidden('hidden'))
389
406
self.assertFalse(format.is_hidden('visible'))
408
def test_short_name(self):
409
registry = controldir.ControlDirFormatRegistry()
410
opt = option.RegistryOption('format', help='', registry=registry)
411
self.assertEquals(None, opt.short_name())
412
opt = option.RegistryOption('format', short_name='F', help='',
414
self.assertEquals('F', opt.short_name())
391
416
def test_option_custom_help(self):
392
417
the_opt = option.Option.OPTIONS['help']
393
418
orig_help = the_opt.help[:]
396
421
self.assertEqual('suggest lottery numbers', my_opt.help)
397
422
self.assertEqual(orig_help, the_opt.help)
424
def test_short_value_switches(self):
425
reg = registry.Registry()
426
reg.register('short', 'ShortChoice')
427
reg.register('long', 'LongChoice')
428
ropt = option.RegistryOption('choice', '', reg, value_switches=True,
429
short_value_switches={'short': 's'})
430
opts, args = parse([ropt], ['--short'])
431
self.assertEqual('ShortChoice', opts.choice)
432
opts, args = parse([ropt], ['-s'])
433
self.assertEqual('ShortChoice', opts.choice)
400
436
class TestVerboseQuietLinkage(TestCase):