/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 breezy/tests/test_options.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
import re
18
18
 
19
 
from bzrlib import (
20
 
    bzrdir,
 
19
from .. import (
 
20
    bzr,
21
21
    commands,
 
22
    controldir,
22
23
    errors,
23
24
    option,
 
25
    registry,
24
26
    )
25
 
from bzrlib.builtins import cmd_commit
26
 
from bzrlib.commands import Command, parse_args
27
 
from bzrlib.tests import TestCase
28
 
from bzrlib.repofmt import knitrepo
 
27
from ..builtins import cmd_commit
 
28
from ..commands import parse_args
 
29
from . import TestCase
 
30
from ..bzr import knitrepo
29
31
 
30
32
 
31
33
def parse(options, args):
41
43
        # XXX: Using cmd_commit makes these tests overly sensitive to changes
42
44
        # to cmd_commit, when they are meant to be about option parsing in
43
45
        # 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'}))
 
46
        self.assertEqual(
 
47
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}),
 
48
           parse_args(cmd_commit(), ['--help']))
 
49
        self.assertEqual(
 
50
           ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
 
51
           parse_args(cmd_commit(), ['--message=biter']))
48
52
 
49
53
    def test_no_more_opts(self):
50
54
        """Terminated options"""
51
 
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
 
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
 
55
        self.assertEqual(
 
56
            (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
 
57
            parse_args(cmd_commit(), ['--', '-file-with-dashes']))
53
58
 
54
59
    def test_option_help(self):
55
60
        """Options have help strings."""
63
68
        out, err = self.run_bzr('help status')
64
69
        self.assertContainsRe(out, r'--show-ids.*Show internal object.')
65
70
 
 
71
    def test_option_help_global_hidden(self):
 
72
        """Hidden global options have no help strings."""
 
73
        out, err = self.run_bzr('help log')
 
74
        self.assertNotContainsRe(out, br'--message')
 
75
 
66
76
    def test_option_arg_help(self):
67
77
        """Help message shows option arguments."""
68
78
        out, err = self.run_bzr('help commit')
71
81
 
72
82
    def test_unknown_short_opt(self):
73
83
        out, err = self.run_bzr('help -r', retcode=3)
74
 
        self.assertContainsRe(err, r'no such option')
 
84
        self.assertContainsRe(err, br'no such option')
75
85
 
76
86
    def test_set_short_name(self):
77
87
        o = option.Option('wiggle')
107
117
        self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
108
118
 
109
119
    def test_registry_conversion(self):
110
 
        registry = bzrdir.BzrDirFormatRegistry()
111
 
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
112
 
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
113
 
        registry.register_metadir('hidden', 'RepositoryFormatKnit1',
 
120
        registry = controldir.ControlDirFormatRegistry()
 
121
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
 
122
        bzr.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
 
123
        bzr.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
114
124
            'two help', hidden=True)
115
125
        registry.set_default('one')
116
126
        options = [option.RegistryOption('format', '', registry, str)]
118
128
        self.assertEqual({'format':'one'}, opts)
119
129
        opts, args = self.parse(options, ['--format', 'two'])
120
130
        self.assertEqual({'format':'two'}, opts)
121
 
        self.assertRaises(errors.BadOptionValue, self.parse, options,
 
131
        self.assertRaises(option.BadOptionValue, self.parse, options,
122
132
                          ['--format', 'three'])
123
133
        self.assertRaises(errors.BzrCommandError, self.parse, options,
124
134
                          ['--two'])
150
160
 
151
161
    def test_registry_converter(self):
152
162
        options = [option.RegistryOption('format', '',
153
 
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
 
163
                   controldir.format_registry, controldir.format_registry.make_controldir)]
154
164
        opts, args = self.parse(options, ['--format', 'knit'])
155
165
        self.assertIsInstance(opts.format.repository_format,
156
166
                              knitrepo.RepositoryFormatKnit1)
157
167
 
158
168
    def test_lazy_registry(self):
159
169
        options = [option.RegistryOption('format', '',
160
 
                   lazy_registry=('bzrlib.bzrdir','format_registry'),
 
170
                   lazy_registry=('breezy.controldir', 'format_registry'),
161
171
                   converter=str)]
162
172
        opts, args = self.parse(options, ['--format', 'knit'])
163
173
        self.assertEqual({'format': 'knit'}, opts)
164
174
        self.assertRaises(
165
 
            errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
 
175
            option.BadOptionValue, self.parse, options, ['--format', 'BAD'])
166
176
 
167
177
    def test_from_kwargs(self):
168
178
        my_option = option.RegistryOption.from_kwargs('my-option',
177
187
        self.assertEqual('test option', my_option.help)
178
188
 
179
189
    def test_help(self):
180
 
        registry = bzrdir.BzrDirFormatRegistry()
181
 
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
182
 
        registry.register_metadir('two',
183
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
 
190
        registry = controldir.ControlDirFormatRegistry()
 
191
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
 
192
        bzr.register_metadir(registry, 'two',
 
193
            'breezy.bzr.knitrepo.RepositoryFormatKnit1',
184
194
            'two help',
185
195
            )
186
 
        registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help',
 
196
        bzr.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
187
197
            hidden=True)
188
198
        registry.set_default('one')
189
199
        options = [option.RegistryOption('format', 'format help', registry,
205
215
        opt = option.Option('hello', help='fg', type=int, argname='gar')
206
216
        self.assertEqual(list(opt.iter_switches()),
207
217
                         [('hello', None, 'GAR', 'fg')])
208
 
        registry = bzrdir.BzrDirFormatRegistry()
209
 
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
210
 
        registry.register_metadir('two',
211
 
                'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
 
218
        registry = controldir.ControlDirFormatRegistry()
 
219
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
 
220
        bzr.register_metadir(registry, 'two',
 
221
                'breezy.bzr.knitrepo.RepositoryFormatKnit1',
212
222
                'two help',
213
223
                )
214
224
        registry.set_default('one')
229
239
        "Test booleans get True and False passed correctly to a callback."""
230
240
        cb_calls = []
231
241
        def cb(option, name, value, parser):
232
 
            cb_calls.append((option,name,value,parser))
 
242
            cb_calls.append((option, name, value, parser))
233
243
        options = [option.Option('hello', custom_callback=cb)]
234
244
        opts, args = self.parse(options, ['--hello', '--no-hello'])
235
245
        self.assertEqual(2, len(cb_calls))
236
 
        opt,name,value,parser = cb_calls[0]
 
246
        opt, name, value, parser = cb_calls[0]
237
247
        self.assertEqual('hello', name)
238
248
        self.assertTrue(value)
239
 
        opt,name,value,parser = cb_calls[1]
 
249
        opt, name, value, parser = cb_calls[1]
240
250
        self.assertEqual('hello', name)
241
251
        self.assertFalse(value)
242
252
 
244
254
        """Test callbacks work for string options both long and short."""
245
255
        cb_calls = []
246
256
        def cb(option, name, value, parser):
247
 
            cb_calls.append((option,name,value,parser))
 
257
            cb_calls.append((option, name, value, parser))
248
258
        options = [option.Option('hello', type=str, custom_callback=cb,
249
259
            short_name='h')]
250
260
        opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
251
261
        self.assertEqual(2, len(cb_calls))
252
 
        opt,name,value,parser = cb_calls[0]
 
262
        opt, name, value, parser = cb_calls[0]
253
263
        self.assertEqual('hello', name)
254
264
        self.assertEqual('world', value)
255
 
        opt,name,value,parser = cb_calls[1]
 
265
        opt, name, value, parser = cb_calls[1]
256
266
        self.assertEqual('hello', name)
257
267
        self.assertEqual('mars', value)
258
268
 
303
313
        cb_calls = []
304
314
        def cb(option, name, value, parser):
305
315
            # Note that the value is a reference so copy to keep it
306
 
            cb_calls.append((option,name,value[:],parser))
 
316
            cb_calls.append((option, name, value[:], parser))
307
317
        options = [option.ListOption('hello', type=str, custom_callback=cb)]
308
318
        opts, args = self.parse(options, ['--hello=world', '--hello=mars',
309
319
            '--hello=-'])
310
320
        self.assertEqual(3, len(cb_calls))
311
 
        opt,name,value,parser = cb_calls[0]
 
321
        opt, name, value, parser = cb_calls[0]
312
322
        self.assertEqual('hello', name)
313
323
        self.assertEqual(['world'], value)
314
 
        opt,name,value,parser = cb_calls[1]
 
324
        opt, name, value, parser = cb_calls[1]
315
325
        self.assertEqual('hello', name)
316
326
        self.assertEqual(['world', 'mars'], value)
317
 
        opt,name,value,parser = cb_calls[2]
 
327
        opt, name, value, parser = cb_calls[2]
318
328
        self.assertEqual('hello', name)
319
329
        self.assertEqual([], value)
320
330
 
331
341
 
332
342
    def get_builtin_command_options(self):
333
343
        g = []
334
 
        for cmd_name in sorted(commands.all_command_names()):
 
344
        commands.install_bzr_command_hooks()
 
345
        for cmd_name in sorted(commands.builtin_command_names()):
335
346
            cmd = commands.get_cmd_object(cmd_name)
336
347
            for opt_name, opt in sorted(cmd.options().items()):
337
348
                g.append((cmd_name, opt))
 
349
        self.assertTrue(g)
338
350
        return g
339
351
 
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
352
    def test_option_grammar(self):
372
353
        msgs = []
373
354
        # 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]+\.$')
 
355
        # period with an optional bracketed suffix. All the text should be on
 
356
        # one line, because the display code will wrap it.
 
357
        option_re = re.compile(r'^[A-Z][^\n]+\.(?: \([^\n]+\))?$')
377
358
        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))
 
359
            for name, _, _, helptxt in opt.iter_switches():
 
360
                if name != opt.name:
 
361
                    name = "/".join([opt.name, name])
 
362
                if not helptxt:
 
363
                    msgs.append('%-16s %-16s %s' %
 
364
                           ((scope or 'GLOBAL'), name, 'NO HELP'))
 
365
                elif not option_re.match(helptxt):
 
366
                    if name.startswith("format/"):
 
367
                        # Don't complain about the odd format registry help
 
368
                        continue
 
369
                    msgs.append('%-16s %-16s %s' %
 
370
                            ((scope or 'GLOBAL'), name, helptxt))
384
371
        if msgs:
385
372
            self.fail("The following options don't match the style guide:\n"
386
373
                    + '\n'.join(msgs))
387
374
 
 
375
 
 
376
class TestOptionMisc(TestCase):
 
377
 
388
378
    def test_is_hidden(self):
389
 
        registry = bzrdir.BzrDirFormatRegistry()
390
 
        registry.register_metadir('hidden', 'HiddenFormat',
 
379
        registry = controldir.ControlDirFormatRegistry()
 
380
        bzr.register_metadir(registry, 'hidden', 'HiddenFormat',
391
381
            'hidden help text', hidden=True)
392
 
        registry.register_metadir('visible', 'VisibleFormat',
 
382
        bzr.register_metadir(registry, 'visible', 'VisibleFormat',
393
383
            'visible help text', hidden=False)
394
384
        format = option.RegistryOption('format', '', registry, str)
395
385
        self.assertTrue(format.is_hidden('hidden'))
396
386
        self.assertFalse(format.is_hidden('visible'))
397
387
 
 
388
    def test_short_name(self):
 
389
        registry = controldir.ControlDirFormatRegistry()
 
390
        opt = option.RegistryOption('format', help='', registry=registry)
 
391
        self.assertEqual(None, opt.short_name())
 
392
        opt = option.RegistryOption('format', short_name='F', help='',
 
393
            registry=registry)
 
394
        self.assertEqual('F', opt.short_name())
 
395
 
398
396
    def test_option_custom_help(self):
399
397
        the_opt = option.Option.OPTIONS['help']
400
398
        orig_help = the_opt.help[:]
403
401
        self.assertEqual('suggest lottery numbers', my_opt.help)
404
402
        self.assertEqual(orig_help, the_opt.help)
405
403
 
 
404
    def test_short_value_switches(self):
 
405
        reg = registry.Registry()
 
406
        reg.register('short', 'ShortChoice')
 
407
        reg.register('long', 'LongChoice')
 
408
        ropt = option.RegistryOption('choice', '', reg, value_switches=True,
 
409
            short_value_switches={'short': 's'})
 
410
        opts, args = parse([ropt], ['--short'])
 
411
        self.assertEqual('ShortChoice', opts.choice)
 
412
        opts, args = parse([ropt], ['-s'])
 
413
        self.assertEqual('ShortChoice', opts.choice)
 
414
 
406
415
 
407
416
class TestVerboseQuietLinkage(TestCase):
408
417