44
44
# 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']))
47
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True,
49
parse_args(cmd_commit(), ['--help']))
50
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
51
parse_args(cmd_commit(), ['--message=biter']))
51
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter',
53
parse_args(cmd_commit(), ['--message=biter']))
53
55
def test_no_more_opts(self):
54
56
"""Terminated options"""
56
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
58
(['-file-with-dashes'], {
59
'author': [], 'exclude': [], 'fixes': [], 'bugs': []}),
57
60
parse_args(cmd_commit(), ['--', '-file-with-dashes']))
59
62
def test_option_help(self):
60
63
"""Options have help strings."""
61
64
out, err = self.run_bzr('commit --help')
62
65
self.assertContainsRe(out,
63
r'--file(.|\n)*Take commit message from this file\.')
66
r'--file(.|\n)*Take commit message from this file\.')
64
67
self.assertContainsRe(out, r'-h.*--help')
66
69
def test_option_help_global(self):
93
96
self.assertEqual((['-']), parse_args(cmd_commit(), ['-'])[0])
95
98
def parse(self, options, args):
96
parser = option.get_optparser(dict((o.name, o) for o in options))
99
parser = option.get_optparser(options)
97
100
return parser.parse_args(args)
99
102
def test_conversion(self):
119
124
def test_registry_conversion(self):
120
125
registry = controldir.ControlDirFormatRegistry()
121
126
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
122
bzr.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
127
bzr.register_metadir(
128
registry, 'two', 'RepositoryFormatKnit1', 'two help')
123
129
bzr.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
124
'two help', hidden=True)
130
'two help', hidden=True)
125
131
registry.set_default('one')
126
132
options = [option.RegistryOption('format', '', registry, str)]
127
133
opts, args = self.parse(options, ['--format', 'one'])
128
self.assertEqual({'format':'one'}, opts)
134
self.assertEqual({'format': 'one'}, opts)
129
135
opts, args = self.parse(options, ['--format', 'two'])
130
self.assertEqual({'format':'two'}, opts)
136
self.assertEqual({'format': 'two'}, opts)
131
137
self.assertRaises(option.BadOptionValue, self.parse, options,
132
138
['--format', 'three'])
133
139
self.assertRaises(errors.BzrCommandError, self.parse, options,
135
141
options = [option.RegistryOption('format', '', registry, str,
136
value_switches=True)]
142
value_switches=True)]
137
143
opts, args = self.parse(options, ['--two'])
138
self.assertEqual({'format':'two'}, opts)
144
self.assertEqual({'format': 'two'}, opts)
139
145
opts, args = self.parse(options, ['--two', '--one'])
140
self.assertEqual({'format':'one'}, opts)
146
self.assertEqual({'format': 'one'}, opts)
141
147
opts, args = self.parse(options, ['--two', '--one',
142
148
'--format', 'two'])
143
self.assertEqual({'format':'two'}, opts)
149
self.assertEqual({'format': 'two'}, opts)
144
150
options = [option.RegistryOption('format', '', registry, str,
146
152
self.assertRaises(errors.BzrCommandError, self.parse, options,
147
153
['--format', 'two'])
161
167
def test_registry_converter(self):
162
168
options = [option.RegistryOption('format', '',
163
controldir.format_registry, controldir.format_registry.make_controldir)]
169
controldir.format_registry, controldir.format_registry.make_controldir)]
164
170
opts, args = self.parse(options, ['--format', 'knit'])
165
171
self.assertIsInstance(opts.format.repository_format,
166
172
knitrepo.RepositoryFormatKnit1)
168
174
def test_lazy_registry(self):
169
175
options = [option.RegistryOption('format', '',
170
lazy_registry=('breezy.controldir', 'format_registry'),
177
'breezy.controldir', 'format_registry'),
172
179
opts, args = self.parse(options, ['--format', 'knit'])
173
180
self.assertEqual({'format': 'knit'}, opts)
174
181
self.assertRaises(
177
184
def test_from_kwargs(self):
178
185
my_option = option.RegistryOption.from_kwargs('my-option',
179
help='test option', short='be short', be_long='go long')
186
help='test option', short='be short', be_long='go long')
180
187
self.assertEqual(['my-option'],
181
[x[0] for x in my_option.iter_switches()])
188
[x[0] for x in my_option.iter_switches()])
182
189
my_option = option.RegistryOption.from_kwargs('my-option',
183
help='test option', title="My option", short='be short',
184
be_long='go long', value_switches=True)
190
help='test option', title="My option", short='be short',
191
be_long='go long', value_switches=True)
185
192
self.assertEqual(['my-option', 'be-long', 'short'],
186
[x[0] for x in my_option.iter_switches()])
193
[x[0] for x in my_option.iter_switches()])
187
194
self.assertEqual('test option', my_option.help)
189
196
def test_help(self):
190
197
registry = controldir.ControlDirFormatRegistry()
191
198
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
199
bzr.register_metadir(registry, 'two',
193
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
200
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
196
203
bzr.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
198
205
registry.set_default('one')
199
206
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))
207
str, value_switches=True, title='Formats')]
208
parser = option.get_optparser(options)
202
209
value = parser.format_option_help()
203
210
self.assertContainsRe(value, 'format.*format help')
204
211
self.assertContainsRe(value, 'one.*one help')
218
225
registry = controldir.ControlDirFormatRegistry()
219
226
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
227
bzr.register_metadir(registry, 'two',
221
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
228
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
224
231
registry.set_default('one')
225
232
opt = option.RegistryOption('format', 'format help', registry,
226
233
value_switches=False)
238
245
def test_option_callback_bool(self):
239
246
"Test booleans get True and False passed correctly to a callback."""
241
249
def cb(option, name, value, parser):
242
250
cb_calls.append((option, name, value, parser))
243
251
options = [option.Option('hello', custom_callback=cb)]
253
261
def test_option_callback_str(self):
254
262
"""Test callbacks work for string options both long and short."""
256
265
def cb(option, name, value, parser):
257
266
cb_calls.append((option, name, value, parser))
258
267
options = [option.Option('hello', type=str, custom_callback=cb,
260
269
opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
261
270
self.assertEqual(2, len(cb_calls))
262
271
opt, name, value, parser = cb_calls[0]
271
280
"""Tests for ListOption, used to specify lists on the command-line."""
273
282
def parse(self, options, args):
274
parser = option.get_optparser(dict((o.name, o) for o in options))
283
parser = option.get_optparser(options)
275
284
return parser.parse_args(args)
277
286
def test_list_option(self):
311
320
def test_option_callback_list(self):
312
321
"""Test callbacks work for list options."""
314
324
def cb(option, name, value, parser):
315
325
# Note that the value is a reference so copy to keep it
316
326
cb_calls.append((option, name, value[:], parser))
317
327
options = [option.ListOption('hello', type=str, custom_callback=cb)]
318
328
opts, args = self.parse(options, ['--hello=world', '--hello=mars',
320
330
self.assertEqual(3, len(cb_calls))
321
331
opt, name, value, parser = cb_calls[0]
322
332
self.assertEqual('hello', name)
361
371
name = "/".join([opt.name, name])
363
373
msgs.append('%-16s %-16s %s' %
364
((scope or 'GLOBAL'), name, 'NO HELP'))
374
((scope or 'GLOBAL'), name, 'NO HELP'))
365
375
elif not option_re.match(helptxt):
366
376
if name.startswith("format/"):
367
377
# Don't complain about the odd format registry help
369
379
msgs.append('%-16s %-16s %s' %
370
((scope or 'GLOBAL'), name, helptxt))
380
((scope or 'GLOBAL'), name, helptxt))
372
382
self.fail("The following options don't match the style guide:\n"
376
386
class TestOptionMisc(TestCase):
378
388
def test_is_hidden(self):
379
389
registry = controldir.ControlDirFormatRegistry()
380
390
bzr.register_metadir(registry, 'hidden', 'HiddenFormat',
381
'hidden help text', hidden=True)
391
'hidden help text', hidden=True)
382
392
bzr.register_metadir(registry, 'visible', 'VisibleFormat',
383
'visible help text', hidden=False)
393
'visible help text', hidden=False)
384
394
format = option.RegistryOption('format', '', registry, str)
385
395
self.assertTrue(format.is_hidden('hidden'))
386
396
self.assertFalse(format.is_hidden('visible'))
390
400
opt = option.RegistryOption('format', help='', registry=registry)
391
401
self.assertEqual(None, opt.short_name())
392
402
opt = option.RegistryOption('format', short_name='F', help='',
394
404
self.assertEqual('F', opt.short_name())
396
406
def test_option_custom_help(self):
406
416
reg.register('short', 'ShortChoice')
407
417
reg.register('long', 'LongChoice')
408
418
ropt = option.RegistryOption('choice', '', reg, value_switches=True,
409
short_value_switches={'short': 's'})
419
short_value_switches={'short': 's'})
410
420
opts, args = parse([ropt], ['--short'])
411
421
self.assertEqual('ShortChoice', opts.choice)
412
422
opts, args = parse([ropt], ['-s'])
421
431
self.assertEqual(level, option._verbosity_level)
423
433
def test_verbose_quiet_linkage(self):
424
parser = option.get_optparser(option.Option.STD_OPTIONS)
434
parser = option.get_optparser(
435
[v for k, v in sorted(option.Option.STD_OPTIONS.items())])
425
436
self.check(parser, 0, [])
426
437
self.check(parser, 1, ['-v'])
427
438
self.check(parser, 2, ['-v', '-v'])