1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 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 (
27
25
from bzrlib.builtins import cmd_commit
28
from bzrlib.commands import parse_args
26
from bzrlib.commands import Command, parse_args
29
27
from bzrlib.tests import TestCase
30
28
from bzrlib.repofmt import knitrepo
43
41
# XXX: Using cmd_commit makes these tests overly sensitive to changes
44
42
# to cmd_commit, when they are meant to be about option parsing in
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']))
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'}))
53
49
def test_no_more_opts(self):
54
50
"""Terminated options"""
56
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
57
parse_args(cmd_commit(), ['--', '-file-with-dashes']))
51
self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
59
54
def test_option_help(self):
60
55
"""Options have help strings."""
68
63
out, err = self.run_bzr('help status')
69
64
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')
76
66
def test_option_arg_help(self):
77
67
"""Help message shows option arguments."""
78
68
out, err = self.run_bzr('help commit')
117
107
self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
119
109
def test_registry_conversion(self):
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',
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',
124
114
'two help', hidden=True)
125
115
registry.set_default('one')
126
116
options = [option.RegistryOption('format', '', registry, str)]
187
177
self.assertEqual('test option', my_option.help)
189
179
def test_help(self):
190
registry = controldir.ControlDirFormatRegistry()
191
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
bzrdir.register_metadir(registry, 'two',
180
registry = bzrdir.BzrDirFormatRegistry()
181
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
182
registry.register_metadir('two',
193
183
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
196
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
186
registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
198
188
registry.set_default('one')
199
189
options = [option.RegistryOption('format', 'format help', registry,
215
205
opt = option.Option('hello', help='fg', type=int, argname='gar')
216
206
self.assertEqual(list(opt.iter_switches()),
217
207
[('hello', None, 'GAR', 'fg')])
218
registry = controldir.ControlDirFormatRegistry()
219
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
bzrdir.register_metadir(registry, 'two',
208
registry = bzrdir.BzrDirFormatRegistry()
209
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
210
registry.register_metadir('two',
221
211
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
328
318
self.assertEqual('hello', name)
329
319
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)
339
322
class TestOptionDefinitions(TestCase):
340
323
"""Tests for options in the Bazaar codebase."""
396
379
+ '\n'.join(msgs))
398
381
def test_is_hidden(self):
399
registry = controldir.ControlDirFormatRegistry()
400
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
382
registry = bzrdir.BzrDirFormatRegistry()
383
registry.register_metadir('hidden', 'HiddenFormat',
401
384
'hidden help text', hidden=True)
402
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
385
registry.register_metadir('visible', 'VisibleFormat',
403
386
'visible help text', hidden=False)
404
387
format = option.RegistryOption('format', '', registry, str)
405
388
self.assertTrue(format.is_hidden('hidden'))
406
389
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())
416
391
def test_option_custom_help(self):
417
392
the_opt = option.Option.OPTIONS['help']
418
393
orig_help = the_opt.help[:]
421
396
self.assertEqual('suggest lottery numbers', my_opt.help)
422
397
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)
436
400
class TestVerboseQuietLinkage(TestCase):