66
73
def test_get_short_name(self):
67
74
file_opt = option.Option.OPTIONS['file']
68
self.assertEquals(file_opt.short_name, 'F')
75
self.assertEquals(file_opt.short_name(), 'F')
69
76
force_opt = option.Option.OPTIONS['force']
70
self.assertEquals(force_opt.short_name, None)
77
self.assertEquals(force_opt.short_name(), None)
79
def test_set_short_name(self):
80
o = option.Option('wiggle')
82
self.assertEqual(o.short_name(), 'w')
84
def test_old_short_names(self):
85
# test the deprecated method for getting and setting short option
88
"access to SHORT_OPTIONS was deprecated in version 0.14."
89
" Set the short option name when constructing the Option.",
90
DeprecationWarning, 2)
92
def capture_warning(message, category, stacklevel=None):
93
_warnings.append((message, category, stacklevel))
94
old_warning_method = symbol_versioning.warn
96
# an example of the kind of thing plugins might want to do through
97
# the old interface - make a new option and then give it a short
99
symbol_versioning.set_warning_method(capture_warning)
100
example_opt = option.Option('example', help='example option')
101
option.Option.SHORT_OPTIONS['w'] = example_opt
102
self.assertEqual(example_opt.short_name(), 'w')
103
self.assertEqual([expected_warning], _warnings)
104
# now check that it can actually be parsed with the registered
106
opts, args = parse([example_opt], ['-w', 'foo'])
107
self.assertEqual(opts.example, True)
108
self.assertEqual(args, ['foo'])
110
symbol_versioning.set_warning_method(old_warning_method)
72
112
def test_allow_dash(self):
73
113
"""Test that we can pass a plain '-' as an argument."""
74
114
self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
116
def parse(self, options, args):
117
parser = option.get_optparser(dict((o.name, o) for o in options))
118
return parser.parse_args(args)
76
120
def test_conversion(self):
77
def parse(options, args):
78
parser = option.get_optparser(dict((o.name, o) for o in options))
79
return parser.parse_args(args)
80
121
options = [option.Option('hello')]
81
opts, args = parse(options, ['--no-hello', '--hello'])
122
opts, args = self.parse(options, ['--no-hello', '--hello'])
82
123
self.assertEqual(True, opts.hello)
83
opts, args = parse(options, [])
124
opts, args = self.parse(options, [])
84
125
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
85
opts, args = parse(options, ['--hello', '--no-hello'])
126
opts, args = self.parse(options, ['--hello', '--no-hello'])
86
127
self.assertEqual(False, opts.hello)
87
128
options = [option.Option('number', type=int)]
88
opts, args = parse(options, ['--number', '6'])
129
opts, args = self.parse(options, ['--number', '6'])
89
130
self.assertEqual(6, opts.number)
90
self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
91
self.assertRaises(errors.BzrCommandError, parse, options,
131
self.assertRaises(errors.BzrCommandError, self.parse, options,
133
self.assertRaises(errors.BzrCommandError, self.parse, options,
136
def test_registry_conversion(self):
137
registry = bzrdir.BzrDirFormatRegistry()
138
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
139
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
140
registry.set_default('one')
141
options = [option.RegistryOption('format', '', registry, str)]
142
opts, args = self.parse(options, ['--format', 'one'])
143
self.assertEqual({'format':'one'}, opts)
144
opts, args = self.parse(options, ['--format', 'two'])
145
self.assertEqual({'format':'two'}, opts)
146
self.assertRaises(errors.BadOptionValue, self.parse, options,
147
['--format', 'three'])
148
self.assertRaises(errors.BzrCommandError, self.parse, options,
150
options = [option.RegistryOption('format', '', registry, str,
151
value_switches=True)]
152
opts, args = self.parse(options, ['--two'])
153
self.assertEqual({'format':'two'}, opts)
154
opts, args = self.parse(options, ['--two', '--one'])
155
self.assertEqual({'format':'one'}, opts)
156
opts, args = self.parse(options, ['--two', '--one',
158
self.assertEqual({'format':'two'}, opts)
160
def test_registry_converter(self):
161
options = [option.RegistryOption('format', '',
162
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
163
opts, args = self.parse(options, ['--format', 'knit'])
164
self.assertIsInstance(opts.format.repository_format,
165
repository.RepositoryFormatKnit1)
168
registry = bzrdir.BzrDirFormatRegistry()
169
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
170
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
171
registry.set_default('one')
172
options = [option.RegistryOption('format', 'format help', registry,
173
str, value_switches=True)]
174
parser = option.get_optparser(dict((o.name, o) for o in options))
175
value = parser.format_option_help()
176
self.assertContainsRe(value, 'format.*format help')
177
self.assertContainsRe(value, 'one.*one help')
94
179
def test_iter_switches(self):
95
180
opt = option.Option('hello', help='fg')
96
181
self.assertEqual(list(opt.iter_switches()),
101
186
opt = option.Option('hello', help='fg', type=int, argname='gar')
102
187
self.assertEqual(list(opt.iter_switches()),
103
188
[('hello', None, 'GAR', 'fg')])
189
registry = bzrdir.BzrDirFormatRegistry()
190
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
191
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
192
registry.set_default('one')
193
opt = option.RegistryOption('format', 'format help', registry,
194
value_switches=False)
195
self.assertEqual(list(opt.iter_switches()),
196
[('format', None, 'ARG', 'format help')])
197
opt = option.RegistryOption('format', 'format help', registry,
199
self.assertEqual(list(opt.iter_switches()),
200
[('format', None, 'ARG', 'format help'),
201
('default', None, None, 'one help'),
202
('one', None, None, 'one help'),
203
('two', None, None, 'two help'),
105
206
# >>> parse_args('log -r 500'.split())
106
207
# (['log'], {'revision': [<RevisionSpec_int 500>]})