1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
28
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
29
from bzrlib.commands import Command, parse_args
30
from bzrlib.tests import TestCase
31
from bzrlib.repofmt import knitrepo
34
def parse(options, args):
35
parser = option.get_optparser(dict((o.name, o) for o in options))
36
return parser.parse_args(args)
39
class OptionTests(TestCase):
40
"""Command-line option tests"""
42
def test_parse_args(self):
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'}))
50
def test_no_more_opts(self):
51
"""Terminated options"""
52
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
53
(['-file-with-dashes'], {'fixes': []}))
55
def test_option_help(self):
56
"""Options have help strings."""
57
out, err = self.run_bzr('commit --help')
58
self.assertContainsRe(out,
59
r'--file(.|\n)*Take commit message from this file\.')
60
self.assertContainsRe(out, r'-h.*--help')
62
def test_option_help_global(self):
63
"""Global options have help strings."""
64
out, err = self.run_bzr('help status')
65
self.assertContainsRe(out, r'--show-ids.*Show internal object.')
67
def test_option_arg_help(self):
68
"""Help message shows option arguments."""
69
out, err = self.run_bzr('help commit')
70
self.assertEquals(err, '')
71
self.assertContainsRe(out, r'--file[ =]MSGFILE')
73
def test_unknown_short_opt(self):
74
out, err = self.run_bzr('help -r', retcode=3)
75
self.assertContainsRe(err, r'no such option')
77
def test_get_short_name(self):
78
file_opt = option.Option.OPTIONS['file']
79
self.assertEquals(file_opt.short_name(), 'F')
80
force_opt = option.Option.OPTIONS['force']
81
self.assertEquals(force_opt.short_name(), None)
83
def test_set_short_name(self):
84
o = option.Option('wiggle')
86
self.assertEqual(o.short_name(), 'w')
88
def test_old_short_names(self):
89
# test the deprecated method for getting and setting short option
92
"access to SHORT_OPTIONS was deprecated in version 0.14."
93
" Set the short option name when constructing the Option.",
94
DeprecationWarning, 2)
96
def capture_warning(message, category, stacklevel=None):
97
_warnings.append((message, category, stacklevel))
98
old_warning_method = symbol_versioning.warn
100
# an example of the kind of thing plugins might want to do through
101
# the old interface - make a new option and then give it a short
103
symbol_versioning.set_warning_method(capture_warning)
104
example_opt = option.Option('example', help='example option')
105
option.Option.SHORT_OPTIONS['w'] = example_opt
106
self.assertEqual(example_opt.short_name(), 'w')
107
self.assertEqual([expected_warning], _warnings)
108
# now check that it can actually be parsed with the registered
110
opts, args = parse([example_opt], ['-w', 'foo'])
111
self.assertEqual(opts.example, True)
112
self.assertEqual(args, ['foo'])
114
symbol_versioning.set_warning_method(old_warning_method)
116
def test_allow_dash(self):
117
"""Test that we can pass a plain '-' as an argument."""
119
(['-'], {'fixes': []}), parse_args(cmd_commit(), ['-']))
121
def parse(self, options, args):
122
parser = option.get_optparser(dict((o.name, o) for o in options))
123
return parser.parse_args(args)
125
def test_conversion(self):
126
options = [option.Option('hello')]
127
opts, args = self.parse(options, ['--no-hello', '--hello'])
128
self.assertEqual(True, opts.hello)
129
opts, args = self.parse(options, [])
130
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
131
opts, args = self.parse(options, ['--hello', '--no-hello'])
132
self.assertEqual(False, opts.hello)
133
options = [option.Option('number', type=int)]
134
opts, args = self.parse(options, ['--number', '6'])
135
self.assertEqual(6, opts.number)
136
self.assertRaises(errors.BzrCommandError, self.parse, options,
138
self.assertRaises(errors.BzrCommandError, self.parse, options,
141
def test_registry_conversion(self):
142
registry = bzrdir.BzrDirFormatRegistry()
143
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
144
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
145
registry.register_metadir('hidden', 'RepositoryFormatKnit1',
146
'two help', hidden=True)
147
registry.set_default('one')
148
options = [option.RegistryOption('format', '', registry, str)]
149
opts, args = self.parse(options, ['--format', 'one'])
150
self.assertEqual({'format':'one'}, opts)
151
opts, args = self.parse(options, ['--format', 'two'])
152
self.assertEqual({'format':'two'}, opts)
153
self.assertRaises(errors.BadOptionValue, self.parse, options,
154
['--format', 'three'])
155
self.assertRaises(errors.BzrCommandError, self.parse, options,
157
options = [option.RegistryOption('format', '', registry, str,
158
value_switches=True)]
159
opts, args = self.parse(options, ['--two'])
160
self.assertEqual({'format':'two'}, opts)
161
opts, args = self.parse(options, ['--two', '--one'])
162
self.assertEqual({'format':'one'}, opts)
163
opts, args = self.parse(options, ['--two', '--one',
165
self.assertEqual({'format':'two'}, opts)
166
options = [option.RegistryOption('format', '', registry, str,
168
self.assertRaises(errors.BzrCommandError, self.parse, options,
171
def test_registry_converter(self):
172
options = [option.RegistryOption('format', '',
173
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
174
opts, args = self.parse(options, ['--format', 'knit'])
175
self.assertIsInstance(opts.format.repository_format,
176
knitrepo.RepositoryFormatKnit1)
178
def test_from_kwargs(self):
179
my_option = option.RegistryOption.from_kwargs('my-option',
180
help='test option', short='be short', be_long='go long')
181
self.assertEqual(['my-option'],
182
[x[0] for x in my_option.iter_switches()])
183
my_option = option.RegistryOption.from_kwargs('my-option',
184
help='test option', title="My option", short='be short',
185
be_long='go long', value_switches=True)
186
self.assertEqual(['my-option', 'be-long', 'short'],
187
[x[0] for x in my_option.iter_switches()])
190
registry = bzrdir.BzrDirFormatRegistry()
191
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
192
registry.register_metadir('two',
193
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
196
registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
198
registry.set_default('one')
199
options = [option.RegistryOption('format', 'format help', registry,
200
str, value_switches=True, title='Formats')]
201
parser = option.get_optparser(dict((o.name, o) for o in options))
202
value = parser.format_option_help()
203
self.assertContainsRe(value, 'format.*format help')
204
self.assertContainsRe(value, 'one.*one help')
205
self.assertContainsRe(value, 'Formats:\n *--format')
206
self.assertNotContainsRe(value, 'hidden help')
208
def test_iter_switches(self):
209
opt = option.Option('hello', help='fg')
210
self.assertEqual(list(opt.iter_switches()),
211
[('hello', None, None, 'fg')])
212
opt = option.Option('hello', help='fg', type=int)
213
self.assertEqual(list(opt.iter_switches()),
214
[('hello', None, 'ARG', 'fg')])
215
opt = option.Option('hello', help='fg', type=int, argname='gar')
216
self.assertEqual(list(opt.iter_switches()),
217
[('hello', None, 'GAR', 'fg')])
218
registry = bzrdir.BzrDirFormatRegistry()
219
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
220
registry.register_metadir('two',
221
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
224
registry.set_default('one')
225
opt = option.RegistryOption('format', 'format help', registry,
226
value_switches=False)
227
self.assertEqual(list(opt.iter_switches()),
228
[('format', None, 'ARG', 'format help')])
229
opt = option.RegistryOption('format', 'format help', registry,
231
self.assertEqual(list(opt.iter_switches()),
232
[('format', None, 'ARG', 'format help'),
233
('default', None, None, 'one help'),
234
('one', None, None, 'one help'),
235
('two', None, None, 'two help'),
239
class TestListOptions(TestCase):
240
"""Tests for ListOption, used to specify lists on the command-line."""
242
def parse(self, options, args):
243
parser = option.get_optparser(dict((o.name, o) for o in options))
244
return parser.parse_args(args)
246
def test_list_option(self):
247
options = [option.ListOption('hello', type=str)]
248
opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
249
self.assertEqual(['world', 'sailor'], opts.hello)
251
def test_list_option_no_arguments(self):
252
options = [option.ListOption('hello', type=str)]
253
opts, args = self.parse(options, [])
254
self.assertEqual([], opts.hello)
256
def test_list_option_with_int_type(self):
257
options = [option.ListOption('hello', type=int)]
258
opts, args = self.parse(options, ['--hello=2', '--hello=3'])
259
self.assertEqual([2, 3], opts.hello)
261
def test_list_option_with_int_type_can_be_reset(self):
262
options = [option.ListOption('hello', type=int)]
263
opts, args = self.parse(options, ['--hello=2', '--hello=3',
264
'--hello=-', '--hello=5'])
265
self.assertEqual([5], opts.hello)
267
def test_list_option_can_be_reset(self):
268
"""Passing an option of '-' to a list option should reset the list."""
269
options = [option.ListOption('hello', type=str)]
270
opts, args = self.parse(
271
options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
272
self.assertEqual(['c'], opts.hello)
275
class TestOptionDefinitions(TestCase):
276
"""Tests for options in the Bazaar codebase."""
278
def get_all_options(self):
279
"""Return a list of all options used by Bazaar, both global and command.
281
The list returned contains elements of (scope, option) where 'scope'
282
is either None for global options, or a command name.
284
This includes options provided by plugins.
286
g = [(None, opt) for name, opt
287
in sorted(option.Option.OPTIONS.items())]
288
for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
290
for opt_name, opt in sorted(cmd.options().items()):
291
g.append((cmd_name, opt))
294
def test_get_all_options(self):
295
all = self.get_all_options()
296
self.assertTrue(len(all) > 100,
297
"too few options found: %r" % all)
299
def test_option_grammar(self):
301
# Option help should be written in sentence form, and have a final
302
# period and be all on a single line, because the display code will
304
option_re = re.compile(r'^[A-Z][^\n]+\.$')
305
for scope, option in self.get_all_options():
307
# TODO: Also complain about options that have no help message?
309
if not option_re.match(option.help):
310
msgs.append('%-16s %-16s %s' %
311
((scope or 'GLOBAL'), option.name, option.help))
313
self.fail("The following options don't match the style guide:\n"
316
# TODO: Scan for global options that aren't used by any command?
318
# TODO: Check that there are two spaces between sentences.