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