~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-01-24 17:06:02 UTC
  • mfrom: (2193.4.3 miss.last-file-location)
  • Revision ID: pqm@pqm.ubuntu.com-20070124170602-5f008e922b3dd800
(bialix) 'bzr missing' show remembered location unescaped,and show
 file URL as filepath not a URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
import re
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
16
 
19
17
from bzrlib import (
 
18
    builtins,
20
19
    bzrdir,
21
 
    commands,
22
20
    errors,
23
21
    option,
 
22
    repository,
 
23
    symbol_versioning,
24
24
    )
25
 
from bzrlib.builtins import cmd_commit
 
25
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
26
26
from bzrlib.commands import Command, parse_args
27
27
from bzrlib.tests import TestCase
28
 
from bzrlib.repofmt import knitrepo
29
 
 
30
28
 
31
29
def parse(options, args):
32
30
    parser = option.get_optparser(dict((o.name, o) for o in options))
33
31
    return parser.parse_args(args)
34
32
 
35
 
 
36
33
class OptionTests(TestCase):
37
34
    """Command-line option tests"""
38
35
 
39
36
    def test_parse_args(self):
40
37
        """Option parser"""
41
 
        # XXX: Using cmd_commit makes these tests overly sensitive to changes
42
 
        # to cmd_commit, when they are meant to be about option parsing in
43
 
        # general.
44
 
        self.assertEqual(parse_args(cmd_commit(), ['--help']),
45
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
46
 
        self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
47
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}))
 
38
        eq = self.assertEquals
 
39
        eq(parse_args(cmd_commit(), ['--help']),
 
40
           ([], {'help': True}))
 
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)}))
48
45
 
49
46
    def test_no_more_opts(self):
50
47
        """Terminated options"""
51
 
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
 
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
 
48
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
 
49
                          (['-file-with-dashes'], {}))
53
50
 
54
51
    def test_option_help(self):
55
52
        """Options have help strings."""
56
 
        out, err = self.run_bzr('commit --help')
57
 
        self.assertContainsRe(out,
58
 
                r'--file(.|\n)*Take commit message from this file\.')
 
53
        out, err = self.run_bzr_captured(['commit', '--help'])
 
54
        self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
 
55
                                   ' message')
59
56
        self.assertContainsRe(out, r'-h.*--help')
60
57
 
61
58
    def test_option_help_global(self):
62
59
        """Global options have help strings."""
63
 
        out, err = self.run_bzr('help status')
64
 
        self.assertContainsRe(out, r'--show-ids.*Show internal object.')
 
60
        out, err = self.run_bzr_captured(['help', 'status'])
 
61
        self.assertContainsRe(out, r'--show-ids.*show internal object')
65
62
 
66
63
    def test_option_arg_help(self):
67
64
        """Help message shows option arguments."""
68
 
        out, err = self.run_bzr('help commit')
69
 
        self.assertEqual(err, '')
 
65
        out, err = self.run_bzr_captured(['help', 'commit'])
 
66
        self.assertEquals(err, '')
70
67
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
71
68
 
72
69
    def test_unknown_short_opt(self):
73
 
        out, err = self.run_bzr('help -r', retcode=3)
 
70
        out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
74
71
        self.assertContainsRe(err, r'no such option')
75
72
 
 
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)
 
78
 
76
79
    def test_set_short_name(self):
77
80
        o = option.Option('wiggle')
78
81
        o.set_short_name('w')
79
82
        self.assertEqual(o.short_name(), 'w')
80
83
 
 
84
    def test_old_short_names(self):
 
85
        # test the deprecated method for getting and setting short option
 
86
        # names
 
87
        expected_warning = (
 
88
            "access to SHORT_OPTIONS was deprecated in version 0.14."
 
89
            " Set the short option name when constructing the Option.",
 
90
            DeprecationWarning, 2)
 
91
        _warnings = []
 
92
        def capture_warning(message, category, stacklevel=None):
 
93
            _warnings.append((message, category, stacklevel))
 
94
        old_warning_method = symbol_versioning.warn
 
95
        try:
 
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
 
98
            # name.
 
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
 
105
            # value
 
106
            opts, args = parse([example_opt], ['-w', 'foo'])
 
107
            self.assertEqual(opts.example, True)
 
108
            self.assertEqual(args, ['foo'])
 
109
        finally:
 
110
            symbol_versioning.set_warning_method(old_warning_method)
 
111
 
81
112
    def test_allow_dash(self):
82
113
        """Test that we can pass a plain '-' as an argument."""
83
 
        self.assertEqual((['-']), parse_args(cmd_commit(), ['-'])[0])
 
114
        self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
84
115
 
85
116
    def parse(self, options, args):
86
117
        parser = option.get_optparser(dict((o.name, o) for o in options))
91
122
        opts, args = self.parse(options, ['--no-hello', '--hello'])
92
123
        self.assertEqual(True, opts.hello)
93
124
        opts, args = self.parse(options, [])
94
 
        self.assertFalse(hasattr(opts, 'hello'))
 
125
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
95
126
        opts, args = self.parse(options, ['--hello', '--no-hello'])
96
127
        self.assertEqual(False, opts.hello)
97
128
        options = [option.Option('number', type=int)]
102
133
        self.assertRaises(errors.BzrCommandError, self.parse, options,
103
134
                          ['--no-number'])
104
135
 
105
 
    def test_is_hidden(self):
106
 
        self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
107
 
        self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
108
 
 
109
136
    def test_registry_conversion(self):
110
137
        registry = bzrdir.BzrDirFormatRegistry()
111
138
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
112
139
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
113
 
        registry.register_metadir('hidden', 'RepositoryFormatKnit1',
114
 
            'two help', hidden=True)
115
140
        registry.set_default('one')
116
141
        options = [option.RegistryOption('format', '', registry, str)]
117
142
        opts, args = self.parse(options, ['--format', 'one'])
131
156
        opts, args = self.parse(options, ['--two', '--one',
132
157
                                          '--format', 'two'])
133
158
        self.assertEqual({'format':'two'}, opts)
134
 
        options = [option.RegistryOption('format', '', registry, str,
135
 
                   enum_switch=False)]
136
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
137
 
                          ['--format', 'two'])
138
 
 
139
 
    def test_override(self):
140
 
        options = [option.Option('hello', type=str),
141
 
                   option.Option('hi', type=str, param_name='hello')]
142
 
        opts, args = self.parse(options, ['--hello', 'a', '--hello', 'b'])
143
 
        self.assertEqual('b', opts.hello)
144
 
        opts, args = self.parse(options, ['--hello', 'b', '--hello', 'a'])
145
 
        self.assertEqual('a', opts.hello)
146
 
        opts, args = self.parse(options, ['--hello', 'a', '--hi', 'b'])
147
 
        self.assertEqual('b', opts.hello)
148
 
        opts, args = self.parse(options, ['--hi', 'b', '--hello', 'a'])
149
 
        self.assertEqual('a', opts.hello)
150
159
 
151
160
    def test_registry_converter(self):
152
161
        options = [option.RegistryOption('format', '',
153
 
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
 
162
                   bzrdir.format_registry, builtins.get_format_type)]
154
163
        opts, args = self.parse(options, ['--format', 'knit'])
155
164
        self.assertIsInstance(opts.format.repository_format,
156
 
                              knitrepo.RepositoryFormatKnit1)
157
 
 
158
 
    def test_lazy_registry(self):
159
 
        options = [option.RegistryOption('format', '',
160
 
                   lazy_registry=('bzrlib.bzrdir','format_registry'),
161
 
                   converter=str)]
162
 
        opts, args = self.parse(options, ['--format', 'knit'])
163
 
        self.assertEqual({'format': 'knit'}, opts)
164
 
        self.assertRaises(
165
 
            errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
166
 
 
167
 
    def test_from_kwargs(self):
168
 
        my_option = option.RegistryOption.from_kwargs('my-option',
169
 
            help='test option', short='be short', be_long='go long')
170
 
        self.assertEqual(['my-option'],
171
 
            [x[0] for x in my_option.iter_switches()])
172
 
        my_option = option.RegistryOption.from_kwargs('my-option',
173
 
            help='test option', title="My option", short='be short',
174
 
            be_long='go long', value_switches=True)
175
 
        self.assertEqual(['my-option', 'be-long', 'short'],
176
 
            [x[0] for x in my_option.iter_switches()])
177
 
        self.assertEqual('test option', my_option.help)
 
165
                              repository.RepositoryFormatKnit1)
178
166
 
179
167
    def test_help(self):
180
168
        registry = bzrdir.BzrDirFormatRegistry()
181
169
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
182
 
        registry.register_metadir('two',
183
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
184
 
            'two help',
185
 
            )
186
 
        registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
187
 
            hidden=True)
 
170
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
188
171
        registry.set_default('one')
189
172
        options = [option.RegistryOption('format', 'format help', registry,
190
 
                   str, value_switches=True, title='Formats')]
 
173
                   str, value_switches=True)]
191
174
        parser = option.get_optparser(dict((o.name, o) for o in options))
192
175
        value = parser.format_option_help()
193
176
        self.assertContainsRe(value, 'format.*format help')
194
177
        self.assertContainsRe(value, 'one.*one help')
195
 
        self.assertContainsRe(value, 'Formats:\n *--format')
196
 
        self.assertNotContainsRe(value, 'hidden help')
197
178
 
198
179
    def test_iter_switches(self):
199
180
        opt = option.Option('hello', help='fg')
207
188
                         [('hello', None, 'GAR', 'fg')])
208
189
        registry = bzrdir.BzrDirFormatRegistry()
209
190
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
210
 
        registry.register_metadir('two',
211
 
                'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
212
 
                'two help',
213
 
                )
 
191
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
214
192
        registry.set_default('one')
215
193
        opt = option.RegistryOption('format', 'format help', registry,
216
194
                                    value_switches=False)
225
203
                          ('two', None, None, 'two help'),
226
204
                          ])
227
205
 
228
 
    def test_option_callback_bool(self):
229
 
        "Test booleans get True and False passed correctly to a callback."""
230
 
        cb_calls = []
231
 
        def cb(option, name, value, parser):
232
 
            cb_calls.append((option,name,value,parser))
233
 
        options = [option.Option('hello', custom_callback=cb)]
234
 
        opts, args = self.parse(options, ['--hello', '--no-hello'])
235
 
        self.assertEqual(2, len(cb_calls))
236
 
        opt,name,value,parser = cb_calls[0]
237
 
        self.assertEqual('hello', name)
238
 
        self.assertTrue(value)
239
 
        opt,name,value,parser = cb_calls[1]
240
 
        self.assertEqual('hello', name)
241
 
        self.assertFalse(value)
242
 
 
243
 
    def test_option_callback_str(self):
244
 
        """Test callbacks work for string options both long and short."""
245
 
        cb_calls = []
246
 
        def cb(option, name, value, parser):
247
 
            cb_calls.append((option,name,value,parser))
248
 
        options = [option.Option('hello', type=str, custom_callback=cb,
249
 
            short_name='h')]
250
 
        opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
251
 
        self.assertEqual(2, len(cb_calls))
252
 
        opt,name,value,parser = cb_calls[0]
253
 
        self.assertEqual('hello', name)
254
 
        self.assertEqual('world', value)
255
 
        opt,name,value,parser = cb_calls[1]
256
 
        self.assertEqual('hello', name)
257
 
        self.assertEqual('mars', value)
258
 
 
259
 
 
260
 
class TestListOptions(TestCase):
261
 
    """Tests for ListOption, used to specify lists on the command-line."""
262
 
 
263
 
    def parse(self, options, args):
264
 
        parser = option.get_optparser(dict((o.name, o) for o in options))
265
 
        return parser.parse_args(args)
266
 
 
267
 
    def test_list_option(self):
268
 
        options = [option.ListOption('hello', type=str)]
269
 
        opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
270
 
        self.assertEqual(['world', 'sailor'], opts.hello)
271
 
 
272
 
    def test_list_option_with_dash(self):
273
 
        options = [option.ListOption('with-dash', type=str)]
274
 
        opts, args = self.parse(options, ['--with-dash=world',
275
 
                                          '--with-dash=sailor'])
276
 
        self.assertEqual(['world', 'sailor'], opts.with_dash)
277
 
 
278
 
    def test_list_option_no_arguments(self):
279
 
        options = [option.ListOption('hello', type=str)]
280
 
        opts, args = self.parse(options, [])
281
 
        self.assertEqual([], opts.hello)
282
 
 
283
 
    def test_list_option_with_int_type(self):
284
 
        options = [option.ListOption('hello', type=int)]
285
 
        opts, args = self.parse(options, ['--hello=2', '--hello=3'])
286
 
        self.assertEqual([2, 3], opts.hello)
287
 
 
288
 
    def test_list_option_with_int_type_can_be_reset(self):
289
 
        options = [option.ListOption('hello', type=int)]
290
 
        opts, args = self.parse(options, ['--hello=2', '--hello=3',
291
 
                                          '--hello=-', '--hello=5'])
292
 
        self.assertEqual([5], opts.hello)
293
 
 
294
 
    def test_list_option_can_be_reset(self):
295
 
        """Passing an option of '-' to a list option should reset the list."""
296
 
        options = [option.ListOption('hello', type=str)]
297
 
        opts, args = self.parse(
298
 
            options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
299
 
        self.assertEqual(['c'], opts.hello)
300
 
 
301
 
    def test_option_callback_list(self):
302
 
        """Test callbacks work for list options."""
303
 
        cb_calls = []
304
 
        def cb(option, name, value, parser):
305
 
            # Note that the value is a reference so copy to keep it
306
 
            cb_calls.append((option,name,value[:],parser))
307
 
        options = [option.ListOption('hello', type=str, custom_callback=cb)]
308
 
        opts, args = self.parse(options, ['--hello=world', '--hello=mars',
309
 
            '--hello=-'])
310
 
        self.assertEqual(3, len(cb_calls))
311
 
        opt,name,value,parser = cb_calls[0]
312
 
        self.assertEqual('hello', name)
313
 
        self.assertEqual(['world'], value)
314
 
        opt,name,value,parser = cb_calls[1]
315
 
        self.assertEqual('hello', name)
316
 
        self.assertEqual(['world', 'mars'], value)
317
 
        opt,name,value,parser = cb_calls[2]
318
 
        self.assertEqual('hello', name)
319
 
        self.assertEqual([], value)
320
 
 
321
 
    def test_list_option_param_name(self):
322
 
        """Test list options can have their param_name set."""
323
 
        options = [option.ListOption('hello', type=str, param_name='greeting')]
324
 
        opts, args = self.parse(
325
 
            options, ['--hello=world', '--hello=sailor'])
326
 
        self.assertEqual(['world', 'sailor'], opts.greeting)
327
 
 
328
 
 
329
 
class TestOptionDefinitions(TestCase):
330
 
    """Tests for options in the Bazaar codebase."""
331
 
 
332
 
    def get_builtin_command_options(self):
333
 
        g = []
334
 
        for cmd_name in sorted(commands.all_command_names()):
335
 
            cmd = commands.get_cmd_object(cmd_name)
336
 
            for opt_name, opt in sorted(cmd.options().items()):
337
 
                g.append((cmd_name, opt))
338
 
        return g
339
 
 
340
 
    def test_global_options_used(self):
341
 
        # In the distant memory, options could only be declared globally.  Now
342
 
        # we prefer to declare them in the command, unless like -r they really
343
 
        # are used very widely with the exact same meaning.  So this checks
344
 
        # for any that should be garbage collected.
345
 
        g = dict(option.Option.OPTIONS.items())
346
 
        used_globals = {}
347
 
        msgs = []
348
 
        for cmd_name in sorted(commands.all_command_names()):
349
 
            cmd = commands.get_cmd_object(cmd_name)
350
 
            for option_or_name in sorted(cmd.takes_options):
351
 
                if not isinstance(option_or_name, basestring):
352
 
                    self.assertIsInstance(option_or_name, option.Option)
353
 
                elif not option_or_name in g:
354
 
                    msgs.append("apparent reference to undefined "
355
 
                        "global option %r from %r"
356
 
                        % (option_or_name, cmd))
357
 
                else:
358
 
                    used_globals.setdefault(option_or_name, []).append(cmd_name)
359
 
        unused_globals = set(g.keys()) - set(used_globals.keys())
360
 
        # not enforced because there might be plugins that use these globals
361
 
        ## for option_name in sorted(unused_globals):
362
 
        ##    msgs.append("unused global option %r" % option_name)
363
 
        ## for option_name, cmds in sorted(used_globals.items()):
364
 
        ##     if len(cmds) <= 1:
365
 
        ##         msgs.append("global option %r is only used by %r"
366
 
        ##                 % (option_name, cmds))
367
 
        if msgs:
368
 
            self.fail("problems with global option definitions:\n"
369
 
                    + '\n'.join(msgs))
370
 
 
371
 
    def test_option_grammar(self):
372
 
        msgs = []
373
 
        # Option help should be written in sentence form, and have a final
374
 
        # period and be all on a single line, because the display code will
375
 
        # wrap it.
376
 
        option_re = re.compile(r'^[A-Z][^\n]+\.$')
377
 
        for scope, opt in self.get_builtin_command_options():
378
 
            if not opt.help:
379
 
                msgs.append('%-16s %-16s %s' %
380
 
                       ((scope or 'GLOBAL'), opt.name, 'NO HELP'))
381
 
            elif not option_re.match(opt.help):
382
 
                msgs.append('%-16s %-16s %s' %
383
 
                        ((scope or 'GLOBAL'), opt.name, opt.help))
384
 
        if msgs:
385
 
            self.fail("The following options don't match the style guide:\n"
386
 
                    + '\n'.join(msgs))
387
 
 
388
 
    def test_is_hidden(self):
389
 
        registry = bzrdir.BzrDirFormatRegistry()
390
 
        registry.register_metadir('hidden', 'HiddenFormat',
391
 
            'hidden help text', hidden=True)
392
 
        registry.register_metadir('visible', 'VisibleFormat',
393
 
            'visible help text', hidden=False)
394
 
        format = option.RegistryOption('format', '', registry, str)
395
 
        self.assertTrue(format.is_hidden('hidden'))
396
 
        self.assertFalse(format.is_hidden('visible'))
397
 
 
398
 
    def test_option_custom_help(self):
399
 
        the_opt = option.Option.OPTIONS['help']
400
 
        orig_help = the_opt.help[:]
401
 
        my_opt = option.custom_help('help', 'suggest lottery numbers')
402
 
        # Confirm that my_opt has my help and the original is unchanged
403
 
        self.assertEqual('suggest lottery numbers', my_opt.help)
404
 
        self.assertEqual(orig_help, the_opt.help)
405
 
 
406
 
 
407
 
class TestVerboseQuietLinkage(TestCase):
408
 
 
409
 
    def check(self, parser, level, args):
410
 
        option._verbosity_level = 0
411
 
        opts, args = parser.parse_args(args)
412
 
        self.assertEqual(level, option._verbosity_level)
413
 
 
414
 
    def test_verbose_quiet_linkage(self):
415
 
        parser = option.get_optparser(option.Option.STD_OPTIONS)
416
 
        self.check(parser, 0, [])
417
 
        self.check(parser, 1, ['-v'])
418
 
        self.check(parser, 2, ['-v', '-v'])
419
 
        self.check(parser, -1, ['-q'])
420
 
        self.check(parser, -2, ['-qq'])
421
 
        self.check(parser, -1, ['-v', '-v', '-q'])
422
 
        self.check(parser, 2, ['-q', '-v', '-v'])
423
 
        self.check(parser, 0, ['--no-verbose'])
424
 
        self.check(parser, 0, ['-v', '-q', '--no-quiet'])
 
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>]})