/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Martin Pool
  • Date: 2007-09-03 04:45:38 UTC
  • mto: This revision was merged to the branch mainline in revision 2779.
  • Revision ID: mbp@sourcefrog.net-20070903044538-4iglni6xv5nkk737
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
import re
 
18
 
 
19
from bzrlib import (
 
20
    builtins,
 
21
    bzrdir,
 
22
    commands,
 
23
    errors,
 
24
    option,
 
25
    repository,
 
26
    symbol_versioning,
 
27
    )
 
28
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
 
29
from bzrlib.commands import Command, parse_args
 
30
from bzrlib.tests import TestCase
 
31
from bzrlib.repofmt import knitrepo
 
32
 
 
33
 
 
34
def parse(options, args):
 
35
    parser = option.get_optparser(dict((o.name, o) for o in options))
 
36
    return parser.parse_args(args)
 
37
 
 
38
 
 
39
class OptionTests(TestCase):
 
40
    """Command-line option tests"""
 
41
 
 
42
    def test_parse_args(self):
 
43
        """Option parser"""
 
44
        eq = self.assertEquals
 
45
        eq(parse_args(cmd_commit(), ['--help']),
 
46
           ([], {'fixes': [], 'help': True}))
 
47
        eq(parse_args(cmd_commit(), ['--message=biter']),
 
48
           ([], {'fixes': [], 'message': 'biter'}))
 
49
 
 
50
    def test_no_more_opts(self):
 
51
        """Terminated options"""
 
52
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
 
53
                          (['-file-with-dashes'], {'fixes': []}))
 
54
 
 
55
    def test_option_help(self):
 
56
        """Options have help strings."""
 
57
        out, err = self.run_bzr('commit --help')
 
58
        self.assertContainsRe(out,
 
59
                r'--file(.|\n)*Take commit message from this file\.')
 
60
        self.assertContainsRe(out, r'-h.*--help')
 
61
 
 
62
    def test_option_help_global(self):
 
63
        """Global options have help strings."""
 
64
        out, err = self.run_bzr('help status')
 
65
        self.assertContainsRe(out, r'--show-ids.*Show internal object.')
 
66
 
 
67
    def test_option_arg_help(self):
 
68
        """Help message shows option arguments."""
 
69
        out, err = self.run_bzr('help commit')
 
70
        self.assertEquals(err, '')
 
71
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
 
72
 
 
73
    def test_unknown_short_opt(self):
 
74
        out, err = self.run_bzr('help -r', retcode=3)
 
75
        self.assertContainsRe(err, r'no such option')
 
76
 
 
77
    def test_set_short_name(self):
 
78
        o = option.Option('wiggle')
 
79
        o.set_short_name('w')
 
80
        self.assertEqual(o.short_name(), 'w')
 
81
 
 
82
    def test_allow_dash(self):
 
83
        """Test that we can pass a plain '-' as an argument."""
 
84
        self.assertEqual(
 
85
            (['-'], {'fixes': []}), parse_args(cmd_commit(), ['-']))
 
86
 
 
87
    def parse(self, options, args):
 
88
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
89
        return parser.parse_args(args)
 
90
 
 
91
    def test_conversion(self):
 
92
        options = [option.Option('hello')]
 
93
        opts, args = self.parse(options, ['--no-hello', '--hello'])
 
94
        self.assertEqual(True, opts.hello)
 
95
        opts, args = self.parse(options, [])
 
96
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
 
97
        opts, args = self.parse(options, ['--hello', '--no-hello'])
 
98
        self.assertEqual(False, opts.hello)
 
99
        options = [option.Option('number', type=int)]
 
100
        opts, args = self.parse(options, ['--number', '6'])
 
101
        self.assertEqual(6, opts.number)
 
102
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
103
                          ['--number'])
 
104
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
105
                          ['--no-number'])
 
106
 
 
107
    def test_registry_conversion(self):
 
108
        registry = bzrdir.BzrDirFormatRegistry()
 
109
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
 
110
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
 
111
        registry.register_metadir('hidden', 'RepositoryFormatKnit1',
 
112
            'two help', hidden=True)
 
113
        registry.set_default('one')
 
114
        options = [option.RegistryOption('format', '', registry, str)]
 
115
        opts, args = self.parse(options, ['--format', 'one'])
 
116
        self.assertEqual({'format':'one'}, opts)
 
117
        opts, args = self.parse(options, ['--format', 'two'])
 
118
        self.assertEqual({'format':'two'}, opts)
 
119
        self.assertRaises(errors.BadOptionValue, self.parse, options,
 
120
                          ['--format', 'three'])
 
121
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
122
                          ['--two'])
 
123
        options = [option.RegistryOption('format', '', registry, str,
 
124
                   value_switches=True)]
 
125
        opts, args = self.parse(options, ['--two'])
 
126
        self.assertEqual({'format':'two'}, opts)
 
127
        opts, args = self.parse(options, ['--two', '--one'])
 
128
        self.assertEqual({'format':'one'}, opts)
 
129
        opts, args = self.parse(options, ['--two', '--one',
 
130
                                          '--format', 'two'])
 
131
        self.assertEqual({'format':'two'}, opts)
 
132
        options = [option.RegistryOption('format', '', registry, str,
 
133
                   enum_switch=False)]
 
134
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
135
                          ['--format', 'two'])
 
136
 
 
137
    def test_override(self):
 
138
        options = [option.Option('hello', type=str),
 
139
                   option.Option('hi', type=str, param_name='hello')]
 
140
        opts, args = self.parse(options, ['--hello', 'a', '--hello', 'b'])
 
141
        self.assertEqual('b', opts.hello)
 
142
        opts, args = self.parse(options, ['--hello', 'b', '--hello', 'a'])
 
143
        self.assertEqual('a', opts.hello)
 
144
        opts, args = self.parse(options, ['--hello', 'a', '--hi', 'b'])
 
145
        self.assertEqual('b', opts.hello)
 
146
        opts, args = self.parse(options, ['--hi', 'b', '--hello', 'a'])
 
147
        self.assertEqual('a', opts.hello)
 
148
 
 
149
    def test_registry_converter(self):
 
150
        options = [option.RegistryOption('format', '',
 
151
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
 
152
        opts, args = self.parse(options, ['--format', 'knit'])
 
153
        self.assertIsInstance(opts.format.repository_format,
 
154
                              knitrepo.RepositoryFormatKnit1)
 
155
 
 
156
    def test_from_kwargs(self):
 
157
        my_option = option.RegistryOption.from_kwargs('my-option',
 
158
            help='test option', short='be short', be_long='go long')
 
159
        self.assertEqual(['my-option'],
 
160
            [x[0] for x in my_option.iter_switches()])
 
161
        my_option = option.RegistryOption.from_kwargs('my-option',
 
162
            help='test option', title="My option", short='be short',
 
163
            be_long='go long', value_switches=True)
 
164
        self.assertEqual(['my-option', 'be-long', 'short'],
 
165
            [x[0] for x in my_option.iter_switches()])
 
166
        self.assertEqual('test option', my_option.help)
 
167
 
 
168
    def test_help(self):
 
169
        registry = bzrdir.BzrDirFormatRegistry()
 
170
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
 
171
        registry.register_metadir('two',
 
172
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
 
173
            'two help',
 
174
            )
 
175
        registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
 
176
            hidden=True)
 
177
        registry.set_default('one')
 
178
        options = [option.RegistryOption('format', 'format help', registry,
 
179
                   str, value_switches=True, title='Formats')]
 
180
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
181
        value = parser.format_option_help()
 
182
        self.assertContainsRe(value, 'format.*format help')
 
183
        self.assertContainsRe(value, 'one.*one help')
 
184
        self.assertContainsRe(value, 'Formats:\n *--format')
 
185
        self.assertNotContainsRe(value, 'hidden help')
 
186
 
 
187
    def test_iter_switches(self):
 
188
        opt = option.Option('hello', help='fg')
 
189
        self.assertEqual(list(opt.iter_switches()),
 
190
                         [('hello', None, None, 'fg')])
 
191
        opt = option.Option('hello', help='fg', type=int)
 
192
        self.assertEqual(list(opt.iter_switches()),
 
193
                         [('hello', None, 'ARG', 'fg')])
 
194
        opt = option.Option('hello', help='fg', type=int, argname='gar')
 
195
        self.assertEqual(list(opt.iter_switches()),
 
196
                         [('hello', None, 'GAR', 'fg')])
 
197
        registry = bzrdir.BzrDirFormatRegistry()
 
198
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
 
199
        registry.register_metadir('two',
 
200
                'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
 
201
                'two help',
 
202
                )
 
203
        registry.set_default('one')
 
204
        opt = option.RegistryOption('format', 'format help', registry,
 
205
                                    value_switches=False)
 
206
        self.assertEqual(list(opt.iter_switches()),
 
207
                         [('format', None, 'ARG', 'format help')])
 
208
        opt = option.RegistryOption('format', 'format help', registry,
 
209
                                    value_switches=True)
 
210
        self.assertEqual(list(opt.iter_switches()),
 
211
                         [('format', None, 'ARG', 'format help'),
 
212
                          ('default', None, None, 'one help'),
 
213
                          ('one', None, None, 'one help'),
 
214
                          ('two', None, None, 'two help'),
 
215
                          ])
 
216
 
 
217
 
 
218
class TestListOptions(TestCase):
 
219
    """Tests for ListOption, used to specify lists on the command-line."""
 
220
 
 
221
    def parse(self, options, args):
 
222
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
223
        return parser.parse_args(args)
 
224
 
 
225
    def test_list_option(self):
 
226
        options = [option.ListOption('hello', type=str)]
 
227
        opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
 
228
        self.assertEqual(['world', 'sailor'], opts.hello)
 
229
 
 
230
    def test_list_option_no_arguments(self):
 
231
        options = [option.ListOption('hello', type=str)]
 
232
        opts, args = self.parse(options, [])
 
233
        self.assertEqual([], opts.hello)
 
234
 
 
235
    def test_list_option_with_int_type(self):
 
236
        options = [option.ListOption('hello', type=int)]
 
237
        opts, args = self.parse(options, ['--hello=2', '--hello=3'])
 
238
        self.assertEqual([2, 3], opts.hello)
 
239
 
 
240
    def test_list_option_with_int_type_can_be_reset(self):
 
241
        options = [option.ListOption('hello', type=int)]
 
242
        opts, args = self.parse(options, ['--hello=2', '--hello=3',
 
243
                                          '--hello=-', '--hello=5'])
 
244
        self.assertEqual([5], opts.hello)
 
245
 
 
246
    def test_list_option_can_be_reset(self):
 
247
        """Passing an option of '-' to a list option should reset the list."""
 
248
        options = [option.ListOption('hello', type=str)]
 
249
        opts, args = self.parse(
 
250
            options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
 
251
        self.assertEqual(['c'], opts.hello)
 
252
 
 
253
 
 
254
class TestOptionDefinitions(TestCase):
 
255
    """Tests for options in the Bazaar codebase."""
 
256
 
 
257
    def get_builtin_command_options(self):
 
258
        g = []
 
259
        for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
 
260
            cmd = cmd_class()
 
261
            for opt_name, opt in sorted(cmd.options().items()):
 
262
                g.append((cmd_name, opt))
 
263
        return g
 
264
 
 
265
    def test_global_options_used(self):
 
266
        # In the distant memory, options could only be declared globally.  Now
 
267
        # we prefer to declare them in the command, unless like -r they really
 
268
        # are used very widely with the exact same meaning.  So this checks
 
269
        # for any that should be garbage collected.
 
270
        g = dict(option.Option.OPTIONS.items())
 
271
        used_globals = {}
 
272
        msgs = []
 
273
        for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
 
274
            for option_or_name in sorted(cmd_class.takes_options):
 
275
                if not isinstance(option_or_name, basestring):
 
276
                    self.assertIsInstance(option_or_name, option.Option)
 
277
                elif not option_or_name in g:
 
278
                    msgs.append("apparent reference to undefined "
 
279
                        "global option %r from %r"
 
280
                        % (option_or_name, cmd_class))
 
281
                else:
 
282
                    used_globals.setdefault(option_or_name, []).append(cmd_name)
 
283
        unused_globals = set(g.keys()) - set(used_globals.keys())
 
284
        # not enforced because there might be plugins that use these globals
 
285
        ## for option_name in sorted(unused_globals):
 
286
        ##    msgs.append("unused global option %r" % option_name)
 
287
        ## for option_name, cmds in sorted(used_globals.items()):
 
288
        ##     if len(cmds) <= 1:
 
289
        ##         msgs.append("global option %r is only used by %r"
 
290
        ##                 % (option_name, cmds))
 
291
        if msgs:
 
292
            self.fail("problems with global option definitions:\n"
 
293
                    + '\n'.join(msgs))
 
294
 
 
295
    def test_option_grammar(self):
 
296
        msgs = []
 
297
        # Option help should be written in sentence form, and have a final
 
298
        # period and be all on a single line, because the display code will
 
299
        # wrap it.
 
300
        option_re = re.compile(r'^[A-Z][^\n]+\.$')
 
301
        for scope, option in self.get_builtin_command_options():
 
302
            if not option.help:
 
303
                msgs.append('%-16s %-16s %s' %
 
304
                       ((scope or 'GLOBAL'), option.name, 'NO HELP'))
 
305
            elif not option_re.match(option.help):
 
306
                msgs.append('%-16s %-16s %s' %
 
307
                        ((scope or 'GLOBAL'), option.name, option.help))
 
308
        if msgs:
 
309
            self.fail("The following options don't match the style guide:\n"
 
310
                    + '\n'.join(msgs))
 
311
 
 
312
    def test_is_hidden(self):
 
313
        registry = bzrdir.BzrDirFormatRegistry()
 
314
        registry.register_metadir('hidden', 'HiddenFormat',
 
315
            'hidden help text', hidden=True)
 
316
        registry.register_metadir('visible', 'VisibleFormat',
 
317
            'visible help text', hidden=False)
 
318
        format = option.RegistryOption('format', '', registry, str)
 
319
        self.assertTrue(format.is_hidden('hidden'))
 
320
        self.assertFalse(format.is_hidden('visible'))