44
46
# 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']))
49
([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}),
50
parse_args(cmd_commit(), ['--help']))
51
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter',
53
parse_args(cmd_commit(), ['--message=biter']))
52
([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
53
parse_args(cmd_commit(), ['--message=biter']))
55
55
def test_no_more_opts(self):
56
56
"""Terminated options"""
58
(['-file-with-dashes'], {
59
'author': [], 'exclude': [], 'fixes': [], 'bugs': []}),
58
(['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
60
59
parse_args(cmd_commit(), ['--', '-file-with-dashes']))
62
61
def test_option_help(self):
63
62
"""Options have help strings."""
64
63
out, err = self.run_bzr('commit --help')
65
64
self.assertContainsRe(out,
66
r'--file(.|\n)*Take commit message from this file\.')
65
r'--file(.|\n)*Take commit message from this file\.')
67
66
self.assertContainsRe(out, r'-h.*--help')
69
68
def test_option_help_global(self):
110
109
options = [option.Option('number', type=int)]
111
110
opts, args = self.parse(options, ['--number', '6'])
112
111
self.assertEqual(6, opts.number)
113
self.assertRaises(errors.CommandError, self.parse, options,
112
self.assertRaises(errors.BzrCommandError, self.parse, options,
115
self.assertRaises(errors.CommandError, self.parse, options,
114
self.assertRaises(errors.BzrCommandError, self.parse, options,
117
self.assertRaises(errors.CommandError, self.parse, options,
120
117
def test_is_hidden(self):
121
118
self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
124
121
def test_registry_conversion(self):
125
122
registry = controldir.ControlDirFormatRegistry()
126
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
127
bzr.register_metadir(
128
registry, 'two', 'RepositoryFormatKnit1', 'two help')
129
bzr.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
130
'two help', hidden=True)
123
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
124
bzrdir.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
125
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
126
'two help', hidden=True)
131
127
registry.set_default('one')
132
128
options = [option.RegistryOption('format', '', registry, str)]
133
129
opts, args = self.parse(options, ['--format', 'one'])
134
self.assertEqual({'format': 'one'}, opts)
130
self.assertEqual({'format':'one'}, opts)
135
131
opts, args = self.parse(options, ['--format', 'two'])
136
self.assertEqual({'format': 'two'}, opts)
137
self.assertRaises(option.BadOptionValue, self.parse, options,
132
self.assertEqual({'format':'two'}, opts)
133
self.assertRaises(errors.BadOptionValue, self.parse, options,
138
134
['--format', 'three'])
139
self.assertRaises(errors.CommandError, self.parse, options,
135
self.assertRaises(errors.BzrCommandError, self.parse, options,
141
137
options = [option.RegistryOption('format', '', registry, str,
142
value_switches=True)]
138
value_switches=True)]
143
139
opts, args = self.parse(options, ['--two'])
144
self.assertEqual({'format': 'two'}, opts)
140
self.assertEqual({'format':'two'}, opts)
145
141
opts, args = self.parse(options, ['--two', '--one'])
146
self.assertEqual({'format': 'one'}, opts)
142
self.assertEqual({'format':'one'}, opts)
147
143
opts, args = self.parse(options, ['--two', '--one',
148
144
'--format', 'two'])
149
self.assertEqual({'format': 'two'}, opts)
145
self.assertEqual({'format':'two'}, opts)
150
146
options = [option.RegistryOption('format', '', registry, str,
152
self.assertRaises(errors.CommandError, self.parse, options,
148
self.assertRaises(errors.BzrCommandError, self.parse, options,
153
149
['--format', 'two'])
155
151
def test_override(self):
167
163
def test_registry_converter(self):
168
164
options = [option.RegistryOption('format', '',
169
controldir.format_registry, controldir.format_registry.make_controldir)]
165
controldir.format_registry, controldir.format_registry.make_bzrdir)]
170
166
opts, args = self.parse(options, ['--format', 'knit'])
171
167
self.assertIsInstance(opts.format.repository_format,
172
168
knitrepo.RepositoryFormatKnit1)
174
170
def test_lazy_registry(self):
175
171
options = [option.RegistryOption('format', '',
177
'breezy.controldir', 'format_registry'),
172
lazy_registry=('breezy.controldir','format_registry'),
179
174
opts, args = self.parse(options, ['--format', 'knit'])
180
175
self.assertEqual({'format': 'knit'}, opts)
181
176
self.assertRaises(
182
option.BadOptionValue, self.parse, options, ['--format', 'BAD'])
177
errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
184
179
def test_from_kwargs(self):
185
180
my_option = option.RegistryOption.from_kwargs('my-option',
186
help='test option', short='be short', be_long='go long')
181
help='test option', short='be short', be_long='go long')
187
182
self.assertEqual(['my-option'],
188
[x[0] for x in my_option.iter_switches()])
183
[x[0] for x in my_option.iter_switches()])
189
184
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)
185
help='test option', title="My option", short='be short',
186
be_long='go long', value_switches=True)
192
187
self.assertEqual(['my-option', 'be-long', 'short'],
193
[x[0] for x in my_option.iter_switches()])
188
[x[0] for x in my_option.iter_switches()])
194
189
self.assertEqual('test option', my_option.help)
196
191
def test_help(self):
197
192
registry = controldir.ControlDirFormatRegistry()
198
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
199
bzr.register_metadir(registry, 'two',
200
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
203
bzr.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
193
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
194
bzrdir.register_metadir(registry, 'two',
195
'breezy.repofmt.knitrepo.RepositoryFormatKnit1',
198
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
205
200
registry.set_default('one')
206
201
options = [option.RegistryOption('format', 'format help', registry,
207
str, value_switches=True, title='Formats')]
208
parser = option.get_optparser(options)
202
str, value_switches=True, title='Formats')]
203
parser = option.get_optparser(dict((o.name, o) for o in options))
209
204
value = parser.format_option_help()
210
205
self.assertContainsRe(value, 'format.*format help')
211
206
self.assertContainsRe(value, 'one.*one help')
223
218
self.assertEqual(list(opt.iter_switches()),
224
219
[('hello', None, 'GAR', 'fg')])
225
220
registry = controldir.ControlDirFormatRegistry()
226
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
227
bzr.register_metadir(registry, 'two',
228
'breezy.bzr.knitrepo.RepositoryFormatKnit1',
221
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
222
bzrdir.register_metadir(registry, 'two',
223
'breezy.repofmt.knitrepo.RepositoryFormatKnit1',
231
226
registry.set_default('one')
232
227
opt = option.RegistryOption('format', 'format help', registry,
233
228
value_switches=False)
245
240
def test_option_callback_bool(self):
246
241
"Test booleans get True and False passed correctly to a callback."""
249
243
def cb(option, name, value, parser):
250
cb_calls.append((option, name, value, parser))
244
cb_calls.append((option,name,value,parser))
251
245
options = [option.Option('hello', custom_callback=cb)]
252
246
opts, args = self.parse(options, ['--hello', '--no-hello'])
253
247
self.assertEqual(2, len(cb_calls))
254
opt, name, value, parser = cb_calls[0]
248
opt,name,value,parser = cb_calls[0]
255
249
self.assertEqual('hello', name)
256
250
self.assertTrue(value)
257
opt, name, value, parser = cb_calls[1]
251
opt,name,value,parser = cb_calls[1]
258
252
self.assertEqual('hello', name)
259
253
self.assertFalse(value)
261
255
def test_option_callback_str(self):
262
256
"""Test callbacks work for string options both long and short."""
265
258
def cb(option, name, value, parser):
266
cb_calls.append((option, name, value, parser))
259
cb_calls.append((option,name,value,parser))
267
260
options = [option.Option('hello', type=str, custom_callback=cb,
269
262
opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
270
263
self.assertEqual(2, len(cb_calls))
271
opt, name, value, parser = cb_calls[0]
264
opt,name,value,parser = cb_calls[0]
272
265
self.assertEqual('hello', name)
273
266
self.assertEqual('world', value)
274
opt, name, value, parser = cb_calls[1]
267
opt,name,value,parser = cb_calls[1]
275
268
self.assertEqual('hello', name)
276
269
self.assertEqual('mars', value)
320
313
def test_option_callback_list(self):
321
314
"""Test callbacks work for list options."""
324
316
def cb(option, name, value, parser):
325
317
# Note that the value is a reference so copy to keep it
326
cb_calls.append((option, name, value[:], parser))
318
cb_calls.append((option,name,value[:],parser))
327
319
options = [option.ListOption('hello', type=str, custom_callback=cb)]
328
320
opts, args = self.parse(options, ['--hello=world', '--hello=mars',
330
322
self.assertEqual(3, len(cb_calls))
331
opt, name, value, parser = cb_calls[0]
323
opt,name,value,parser = cb_calls[0]
332
324
self.assertEqual('hello', name)
333
325
self.assertEqual(['world'], value)
334
opt, name, value, parser = cb_calls[1]
326
opt,name,value,parser = cb_calls[1]
335
327
self.assertEqual('hello', name)
336
328
self.assertEqual(['world', 'mars'], value)
337
opt, name, value, parser = cb_calls[2]
329
opt,name,value,parser = cb_calls[2]
338
330
self.assertEqual('hello', name)
339
331
self.assertEqual([], value)
371
363
name = "/".join([opt.name, name])
373
365
msgs.append('%-16s %-16s %s' %
374
((scope or 'GLOBAL'), name, 'NO HELP'))
366
((scope or 'GLOBAL'), name, 'NO HELP'))
375
367
elif not option_re.match(helptxt):
368
if name.startswith("format/"):
369
# Don't complain about the odd format registry help
376
371
msgs.append('%-16s %-16s %s' %
377
((scope or 'GLOBAL'), name, helptxt))
372
((scope or 'GLOBAL'), name, helptxt))
379
374
self.fail("The following options don't match the style guide:\n"
383
378
class TestOptionMisc(TestCase):
385
380
def test_is_hidden(self):
386
381
registry = controldir.ControlDirFormatRegistry()
387
bzr.register_metadir(registry, 'hidden', 'HiddenFormat',
388
'hidden help text', hidden=True)
389
bzr.register_metadir(registry, 'visible', 'VisibleFormat',
390
'visible help text', hidden=False)
382
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
383
'hidden help text', hidden=True)
384
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
385
'visible help text', hidden=False)
391
386
format = option.RegistryOption('format', '', registry, str)
392
387
self.assertTrue(format.is_hidden('hidden'))
393
388
self.assertFalse(format.is_hidden('visible'))