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):
107
110
options = [option.Option('number', type=int)]
108
111
opts, args = self.parse(options, ['--number', '6'])
109
112
self.assertEqual(6, opts.number)
110
self.assertRaises(errors.BzrCommandError, self.parse, options,
113
self.assertRaises(errors.CommandError, self.parse, options,
112
self.assertRaises(errors.BzrCommandError, self.parse, options,
115
self.assertRaises(errors.CommandError, self.parse, options,
117
self.assertRaises(errors.CommandError, self.parse, options,
115
120
def test_is_hidden(self):
116
121
self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
119
124
def test_registry_conversion(self):
120
125
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',
124
'two help', hidden=True)
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)
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)
131
self.assertRaises(errors.BadOptionValue, self.parse, options,
136
self.assertEqual({'format': 'two'}, opts)
137
self.assertRaises(option.BadOptionValue, self.parse, options,
132
138
['--format', 'three'])
133
self.assertRaises(errors.BzrCommandError, self.parse, options,
139
self.assertRaises(errors.CommandError, 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
self.assertRaises(errors.BzrCommandError, self.parse, options,
152
self.assertRaises(errors.CommandError, self.parse, options,
147
153
['--format', 'two'])
149
155
def test_override(self):
161
167
def test_registry_converter(self):
162
168
options = [option.RegistryOption('format', '',
163
controldir.format_registry, controldir.format_registry.make_bzrdir)]
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=('bzrlib.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(
175
errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
182
option.BadOptionValue, self.parse, options, ['--format', 'BAD'])
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
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
bzrdir.register_metadir(registry, 'two',
193
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
196
bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
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',
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')
216
223
self.assertEqual(list(opt.iter_switches()),
217
224
[('hello', None, 'GAR', 'fg')])
218
225
registry = controldir.ControlDirFormatRegistry()
219
bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
bzrdir.register_metadir(registry, 'two',
221
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
226
bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
227
bzr.register_metadir(registry, 'two',
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
cb_calls.append((option,name,value,parser))
250
cb_calls.append((option, name, value, parser))
243
251
options = [option.Option('hello', custom_callback=cb)]
244
252
opts, args = self.parse(options, ['--hello', '--no-hello'])
245
253
self.assertEqual(2, len(cb_calls))
246
opt,name,value,parser = cb_calls[0]
254
opt, name, value, parser = cb_calls[0]
247
255
self.assertEqual('hello', name)
248
256
self.assertTrue(value)
249
opt,name,value,parser = cb_calls[1]
257
opt, name, value, parser = cb_calls[1]
250
258
self.assertEqual('hello', name)
251
259
self.assertFalse(value)
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
cb_calls.append((option,name,value,parser))
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
opt,name,value,parser = cb_calls[0]
271
opt, name, value, parser = cb_calls[0]
263
272
self.assertEqual('hello', name)
264
273
self.assertEqual('world', value)
265
opt,name,value,parser = cb_calls[1]
274
opt, name, value, parser = cb_calls[1]
266
275
self.assertEqual('hello', name)
267
276
self.assertEqual('mars', value)
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
cb_calls.append((option,name,value[:],parser))
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
opt,name,value,parser = cb_calls[0]
331
opt, name, value, parser = cb_calls[0]
322
332
self.assertEqual('hello', name)
323
333
self.assertEqual(['world'], value)
324
opt,name,value,parser = cb_calls[1]
334
opt, name, value, parser = cb_calls[1]
325
335
self.assertEqual('hello', name)
326
336
self.assertEqual(['world', 'mars'], value)
327
opt,name,value,parser = cb_calls[2]
337
opt, name, value, parser = cb_calls[2]
328
338
self.assertEqual('hello', name)
329
339
self.assertEqual([], value)
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
if name.startswith("format/"):
367
# Don't complain about the odd format registry help
369
376
msgs.append('%-16s %-16s %s' %
370
((scope or 'GLOBAL'), name, helptxt))
377
((scope or 'GLOBAL'), name, helptxt))
372
379
self.fail("The following options don't match the style guide:\n"
376
383
class TestOptionMisc(TestCase):
378
385
def test_is_hidden(self):
379
386
registry = controldir.ControlDirFormatRegistry()
380
bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
381
'hidden help text', hidden=True)
382
bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
383
'visible help text', hidden=False)
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)
384
391
format = option.RegistryOption('format', '', registry, str)
385
392
self.assertTrue(format.is_hidden('hidden'))
386
393
self.assertFalse(format.is_hidden('visible'))
388
395
def test_short_name(self):
389
396
registry = controldir.ControlDirFormatRegistry()
390
397
opt = option.RegistryOption('format', help='', registry=registry)
391
self.assertEquals(None, opt.short_name())
398
self.assertEqual(None, opt.short_name())
392
399
opt = option.RegistryOption('format', short_name='F', help='',
394
self.assertEquals('F', opt.short_name())
401
self.assertEqual('F', opt.short_name())
396
403
def test_option_custom_help(self):
397
404
the_opt = option.Option.OPTIONS['help']