1
# Copyright (C) 2005, 2006 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
24
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
25
from bzrlib.commands import Command, parse_args
26
from bzrlib.tests import TestCase
28
# TODO: might be nice to just parse them into a structured form and test
29
# against that, rather than running the whole command.
31
class OptionTests(TestCase):
32
"""Command-line option tests"""
34
def test_parse_args(self):
36
eq = self.assertEquals
37
eq(parse_args(cmd_commit(), ['--help']),
39
eq(parse_args(cmd_commit(), ['--message=biter']),
40
([], {'message': 'biter'}))
41
## eq(parse_args(cmd_log(), '-r 500'.split()),
42
## ([], {'revision': RevisionSpec_int(500)}))
44
def test_no_more_opts(self):
45
"""Terminated options"""
46
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
47
(['-file-with-dashes'], {}))
49
def test_option_help(self):
50
"""Options have help strings."""
51
out, err = self.run_bzr_captured(['commit', '--help'])
52
self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
54
self.assertContainsRe(out, r'-h.*--help')
56
def test_option_help_global(self):
57
"""Global options have help strings."""
58
out, err = self.run_bzr_captured(['help', 'status'])
59
self.assertContainsRe(out, r'--show-ids.*show internal object')
61
def test_option_arg_help(self):
62
"""Help message shows option arguments."""
63
out, err = self.run_bzr_captured(['help', 'commit'])
64
self.assertEquals(err, '')
65
self.assertContainsRe(out, r'--file[ =]MSGFILE')
67
def test_unknown_short_opt(self):
68
out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
69
self.assertContainsRe(err, r'no such option')
71
def test_get_short_name(self):
72
file_opt = option.Option.OPTIONS['file']
73
self.assertEquals(file_opt.short_name, 'F')
74
force_opt = option.Option.OPTIONS['force']
75
self.assertEquals(force_opt.short_name, None)
77
def test_allow_dash(self):
78
"""Test that we can pass a plain '-' as an argument."""
79
self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
81
def parse(self, options, args):
82
parser = option.get_optparser(dict((o.name, o) for o in options))
83
return parser.parse_args(args)
85
def test_conversion(self):
86
options = [option.Option('hello')]
87
opts, args = self.parse(options, ['--no-hello', '--hello'])
88
self.assertEqual(True, opts.hello)
89
opts, args = self.parse(options, [])
90
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
91
opts, args = self.parse(options, ['--hello', '--no-hello'])
92
self.assertEqual(False, opts.hello)
93
options = [option.Option('number', type=int)]
94
opts, args = self.parse(options, ['--number', '6'])
95
self.assertEqual(6, opts.number)
96
self.assertRaises(errors.BzrCommandError, self.parse, options,
98
self.assertRaises(errors.BzrCommandError, self.parse, options,
101
def test_registry_conversion(self):
102
registry = bzrdir.BzrDirFormatRegistry()
103
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
104
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
105
registry.set_default('one')
106
options = [option.RegistryOption('format', '', registry, str)]
107
opts, args = self.parse(options, ['--format', 'one'])
108
self.assertEqual({'format':'one'}, opts)
109
opts, args = self.parse(options, ['--format', 'two'])
110
self.assertEqual({'format':'two'}, opts)
111
self.assertRaises(errors.BadOptionValue, self.parse, options,
112
['--format', 'three'])
113
self.assertRaises(errors.BzrCommandError, self.parse, options,
115
options = [option.RegistryOption('format', '', registry, str,
116
value_switches=True)]
117
opts, args = self.parse(options, ['--two'])
118
self.assertEqual({'format':'two'}, opts)
119
opts, args = self.parse(options, ['--two', '--one'])
120
self.assertEqual({'format':'one'}, opts)
121
opts, args = self.parse(options, ['--two', '--one',
123
self.assertEqual({'format':'two'}, opts)
125
def test_registry_converter(self):
126
options = [option.RegistryOption('format', '',
127
bzrdir.format_registry, builtins.get_format_type)]
128
opts, args = self.parse(options, ['--format', 'knit'])
129
self.assertIsInstance(opts.format.repository_format,
130
repository.RepositoryFormatKnit1)
133
registry = bzrdir.BzrDirFormatRegistry()
134
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
135
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
136
registry.set_default('one')
137
options = [option.RegistryOption('format', 'format help', registry,
138
str, value_switches=True)]
139
parser = option.get_optparser(dict((o.name, o) for o in options))
140
value = parser.format_option_help()
141
self.assertContainsRe(value, 'format.*format help')
142
self.assertContainsRe(value, 'one.*one help')
144
def test_iter_switches(self):
145
opt = option.Option('hello', help='fg')
146
self.assertEqual(list(opt.iter_switches()),
147
[('hello', None, None, 'fg')])
148
opt = option.Option('hello', help='fg', type=int)
149
self.assertEqual(list(opt.iter_switches()),
150
[('hello', None, 'ARG', 'fg')])
151
opt = option.Option('hello', help='fg', type=int, argname='gar')
152
self.assertEqual(list(opt.iter_switches()),
153
[('hello', None, 'GAR', 'fg')])
154
registry = bzrdir.BzrDirFormatRegistry()
155
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
156
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
157
registry.set_default('one')
158
opt = option.RegistryOption('format', 'format help', registry,
159
value_switches=False)
160
self.assertEqual(list(opt.iter_switches()),
161
[('format', None, 'ARG', 'format help')])
162
opt = option.RegistryOption('format', 'format help', registry,
164
self.assertEqual(list(opt.iter_switches()),
165
[('format', None, 'ARG', 'format help'),
166
('default', None, None, 'one help'),
167
('one', None, None, 'one help'),
168
('two', None, None, 'two help'),
171
# >>> parse_args('log -r 500'.split())
172
# (['log'], {'revision': [<RevisionSpec_int 500>]})
173
# >>> parse_args('log -r500..600'.split())
174
# (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
175
# >>> parse_args('log -vr500..600'.split())
176
# (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
177
# >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
178
# (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})