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 (
28
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
25
from bzrlib.builtins import cmd_commit
29
26
from bzrlib.commands import Command, parse_args
30
27
from bzrlib.tests import TestCase
31
28
from bzrlib.repofmt import knitrepo
42
39
def test_parse_args(self):
43
40
"""Option parser"""
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'}))
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'}))
50
49
def test_no_more_opts(self):
51
50
"""Terminated options"""
52
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
53
(['-file-with-dashes'], {'fixes': []}))
51
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
55
54
def test_option_help(self):
56
55
"""Options have help strings."""
67
66
def test_option_arg_help(self):
68
67
"""Help message shows option arguments."""
69
68
out, err = self.run_bzr('help commit')
70
self.assertEquals(err, '')
69
self.assertEqual(err, '')
71
70
self.assertContainsRe(out, r'--file[ =]MSGFILE')
73
72
def test_unknown_short_opt(self):
82
81
def test_allow_dash(self):
83
82
"""Test that we can pass a plain '-' as an argument."""
85
(['-'], {'fixes': []}), parse_args(cmd_commit(), ['-']))
83
self.assertEqual((['-']), parse_args(cmd_commit(), ['-'])[0])
87
85
def parse(self, options, args):
88
86
parser = option.get_optparser(dict((o.name, o) for o in options))
104
102
self.assertRaises(errors.BzrCommandError, self.parse, options,
105
def test_is_hidden(self):
106
self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
107
self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
107
109
def test_registry_conversion(self):
108
110
registry = bzrdir.BzrDirFormatRegistry()
109
111
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
153
155
self.assertIsInstance(opts.format.repository_format,
154
156
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'])
156
167
def test_from_kwargs(self):
157
168
my_option = option.RegistryOption.from_kwargs('my-option',
158
169
help='test option', short='be short', be_long='go long')
258
269
opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
259
270
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)
261
278
def test_list_option_no_arguments(self):
262
279
options = [option.ListOption('hello', type=str)]
263
280
opts, args = self.parse(options, [])
308
325
def get_builtin_command_options(self):
310
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
327
for cmd_name in sorted(commands.all_command_names()):
328
cmd = commands.get_cmd_object(cmd_name)
312
329
for opt_name, opt in sorted(cmd.options().items()):
313
330
g.append((cmd_name, opt))
321
338
g = dict(option.Option.OPTIONS.items())
322
339
used_globals = {}
324
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
325
for option_or_name in sorted(cmd_class.takes_options):
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):
326
344
if not isinstance(option_or_name, basestring):
327
345
self.assertIsInstance(option_or_name, option.Option)
328
346
elif not option_or_name in g:
329
347
msgs.append("apparent reference to undefined "
330
348
"global option %r from %r"
331
% (option_or_name, cmd_class))
349
% (option_or_name, cmd))
333
351
used_globals.setdefault(option_or_name, []).append(cmd_name)
334
352
unused_globals = set(g.keys()) - set(used_globals.keys())
349
367
# period and be all on a single line, because the display code will
351
369
option_re = re.compile(r'^[A-Z][^\n]+\.$')
352
for scope, option in self.get_builtin_command_options():
354
msgs.append('%-16s %-16s %s' %
355
((scope or 'GLOBAL'), option.name, 'NO HELP'))
356
elif not option_re.match(option.help):
357
msgs.append('%-16s %-16s %s' %
358
((scope or 'GLOBAL'), option.name, option.help))
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))
360
378
self.fail("The following options don't match the style guide:\n"
361
379
+ '\n'.join(msgs))