1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
26
from bzrlib.commands import Command, parse_args
27
from bzrlib.tests import TestCase
29
def parse(options, args):
30
parser = option.get_optparser(dict((o.name, o) for o in options))
31
return parser.parse_args(args)
33
class OptionTests(TestCase):
34
"""Command-line option tests"""
36
def test_parse_args(self):
38
eq = self.assertEquals
39
eq(parse_args(cmd_commit(), ['--help']),
41
eq(parse_args(cmd_commit(), ['--message=biter']),
42
([], {'message': 'biter'}))
43
## eq(parse_args(cmd_log(), '-r 500'.split()),
44
## ([], {'revision': RevisionSpec_int(500)}))
46
def test_no_more_opts(self):
47
"""Terminated options"""
48
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
49
(['-file-with-dashes'], {}))
51
def test_option_help(self):
52
"""Options have help strings."""
53
out, err = self.run_bzr_captured(['commit', '--help'])
54
self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
56
self.assertContainsRe(out, r'-h.*--help')
58
def test_option_help_global(self):
59
"""Global options have help strings."""
60
out, err = self.run_bzr_captured(['help', 'status'])
61
self.assertContainsRe(out, r'--show-ids.*show internal object')
63
def test_option_arg_help(self):
64
"""Help message shows option arguments."""
65
out, err = self.run_bzr_captured(['help', 'commit'])
66
self.assertEquals(err, '')
67
self.assertContainsRe(out, r'--file[ =]MSGFILE')
69
def test_unknown_short_opt(self):
70
out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
71
self.assertContainsRe(err, r'no such option')
73
def test_get_short_name(self):
74
file_opt = option.Option.OPTIONS['file']
75
self.assertEquals(file_opt.short_name(), 'F')
76
force_opt = option.Option.OPTIONS['force']
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)
112
def test_allow_dash(self):
113
"""Test that we can pass a plain '-' as an argument."""
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)
120
def test_conversion(self):
121
options = [option.Option('hello')]
122
opts, args = self.parse(options, ['--no-hello', '--hello'])
123
self.assertEqual(True, opts.hello)
124
opts, args = self.parse(options, [])
125
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
126
opts, args = self.parse(options, ['--hello', '--no-hello'])
127
self.assertEqual(False, opts.hello)
128
options = [option.Option('number', type=int)]
129
opts, args = self.parse(options, ['--number', '6'])
130
self.assertEqual(6, opts.number)
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, builtins.get_format_type)]
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')
179
def test_iter_switches(self):
180
opt = option.Option('hello', help='fg')
181
self.assertEqual(list(opt.iter_switches()),
182
[('hello', None, None, 'fg')])
183
opt = option.Option('hello', help='fg', type=int)
184
self.assertEqual(list(opt.iter_switches()),
185
[('hello', None, 'ARG', 'fg')])
186
opt = option.Option('hello', help='fg', type=int, argname='gar')
187
self.assertEqual(list(opt.iter_switches()),
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'),
206
# >>> parse_args('log -r 500'.split())
207
# (['log'], {'revision': [<RevisionSpec_int 500>]})
208
# >>> parse_args('log -r500..600'.split())
209
# (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
210
# >>> parse_args('log -vr500..600'.split())
211
# (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
212
# >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
213
# (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})