47
70
def test_unknown_short_opt(self):
48
71
out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
49
self.assertContainsRe(err, r'unknown short option')
72
self.assertContainsRe(err, r'no such option')
74
def test_get_short_name(self):
75
file_opt = option.Option.OPTIONS['file']
76
self.assertEquals(file_opt.short_name(), 'F')
77
force_opt = option.Option.OPTIONS['force']
78
self.assertEquals(force_opt.short_name(), None)
80
def test_set_short_name(self):
81
o = option.Option('wiggle')
83
self.assertEqual(o.short_name(), 'w')
85
def test_old_short_names(self):
86
# test the deprecated method for getting and setting short option
89
"access to SHORT_OPTIONS was deprecated in version 0.14."
90
" Set the short option name when constructing the Option.",
91
DeprecationWarning, 2)
93
def capture_warning(message, category, stacklevel=None):
94
_warnings.append((message, category, stacklevel))
95
old_warning_method = symbol_versioning.warn
97
# an example of the kind of thing plugins might want to do through
98
# the old interface - make a new option and then give it a short
100
symbol_versioning.set_warning_method(capture_warning)
101
example_opt = option.Option('example', help='example option')
102
option.Option.SHORT_OPTIONS['w'] = example_opt
103
self.assertEqual(example_opt.short_name(), 'w')
104
self.assertEqual([expected_warning], _warnings)
105
# now check that it can actually be parsed with the registered
107
opts, args = parse([example_opt], ['-w', 'foo'])
108
self.assertEqual(opts.example, True)
109
self.assertEqual(args, ['foo'])
111
symbol_versioning.set_warning_method(old_warning_method)
113
def test_allow_dash(self):
114
"""Test that we can pass a plain '-' as an argument."""
115
self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
117
def parse(self, options, args):
118
parser = option.get_optparser(dict((o.name, o) for o in options))
119
return parser.parse_args(args)
121
def test_conversion(self):
122
options = [option.Option('hello')]
123
opts, args = self.parse(options, ['--no-hello', '--hello'])
124
self.assertEqual(True, opts.hello)
125
opts, args = self.parse(options, [])
126
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
127
opts, args = self.parse(options, ['--hello', '--no-hello'])
128
self.assertEqual(False, opts.hello)
129
options = [option.Option('number', type=int)]
130
opts, args = self.parse(options, ['--number', '6'])
131
self.assertEqual(6, opts.number)
132
self.assertRaises(errors.BzrCommandError, self.parse, options,
134
self.assertRaises(errors.BzrCommandError, self.parse, options,
137
def test_registry_conversion(self):
138
registry = bzrdir.BzrDirFormatRegistry()
139
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
140
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
141
registry.set_default('one')
142
options = [option.RegistryOption('format', '', registry, str)]
143
opts, args = self.parse(options, ['--format', 'one'])
144
self.assertEqual({'format':'one'}, opts)
145
opts, args = self.parse(options, ['--format', 'two'])
146
self.assertEqual({'format':'two'}, opts)
147
self.assertRaises(errors.BadOptionValue, self.parse, options,
148
['--format', 'three'])
149
self.assertRaises(errors.BzrCommandError, self.parse, options,
151
options = [option.RegistryOption('format', '', registry, str,
152
value_switches=True)]
153
opts, args = self.parse(options, ['--two'])
154
self.assertEqual({'format':'two'}, opts)
155
opts, args = self.parse(options, ['--two', '--one'])
156
self.assertEqual({'format':'one'}, opts)
157
opts, args = self.parse(options, ['--two', '--one',
159
self.assertEqual({'format':'two'}, opts)
160
options = [option.RegistryOption('format', '', registry, str,
162
self.assertRaises(errors.BzrCommandError, self.parse, options,
165
def test_registry_converter(self):
166
options = [option.RegistryOption('format', '',
167
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
168
opts, args = self.parse(options, ['--format', 'knit'])
169
self.assertIsInstance(opts.format.repository_format,
170
knitrepo.RepositoryFormatKnit1)
172
def test_from_kwargs(self):
173
my_option = option.RegistryOption.from_kwargs('my-option',
174
help='test option', short='be short', be_long='go long')
175
self.assertEqual(['my-option'],
176
[x[0] for x in my_option.iter_switches()])
177
my_option = option.RegistryOption.from_kwargs('my-option',
178
help='test option', title="My option", short='be short',
179
be_long='go long', value_switches=True)
180
self.assertEqual(['my-option', 'be-long', 'short'],
181
[x[0] for x in my_option.iter_switches()])
184
registry = bzrdir.BzrDirFormatRegistry()
185
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
186
registry.register_metadir('two',
187
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
190
registry.set_default('one')
191
options = [option.RegistryOption('format', 'format help', registry,
192
str, value_switches=True, title='Formats')]
193
parser = option.get_optparser(dict((o.name, o) for o in options))
194
value = parser.format_option_help()
195
self.assertContainsRe(value, 'format.*format help')
196
self.assertContainsRe(value, 'one.*one help')
197
self.assertContainsRe(value, 'Formats:\n *--format')
199
def test_iter_switches(self):
200
opt = option.Option('hello', help='fg')
201
self.assertEqual(list(opt.iter_switches()),
202
[('hello', None, None, 'fg')])
203
opt = option.Option('hello', help='fg', type=int)
204
self.assertEqual(list(opt.iter_switches()),
205
[('hello', None, 'ARG', 'fg')])
206
opt = option.Option('hello', help='fg', type=int, argname='gar')
207
self.assertEqual(list(opt.iter_switches()),
208
[('hello', None, 'GAR', 'fg')])
209
registry = bzrdir.BzrDirFormatRegistry()
210
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
211
registry.register_metadir('two',
212
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
215
registry.set_default('one')
216
opt = option.RegistryOption('format', 'format help', registry,
217
value_switches=False)
218
self.assertEqual(list(opt.iter_switches()),
219
[('format', None, 'ARG', 'format help')])
220
opt = option.RegistryOption('format', 'format help', registry,
222
self.assertEqual(list(opt.iter_switches()),
223
[('format', None, 'ARG', 'format help'),
224
('default', None, None, 'one help'),
225
('one', None, None, 'one help'),
226
('two', None, None, 'two help'),
52
229
# >>> parse_args('log -r 500'.split())
53
230
# (['log'], {'revision': [<RevisionSpec_int 500>]})