/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 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
22
    controldir,
23
23
    errors,
24
24
    option,
25
25
    registry,
26
26
    )
27
 
from bzrlib.builtins import cmd_commit
28
 
from bzrlib.commands import parse_args
29
 
from bzrlib.tests import TestCase
30
 
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
31
31
 
32
32
 
33
33
def parse(options, args):
34
 
    parser = option.get_optparser(dict((o.name, o) for o in options))
 
34
    parser = option.get_optparser(options)
35
35
    return parser.parse_args(args)
36
36
 
37
37
 
44
44
        # to cmd_commit, when they are meant to be about option parsing in
45
45
        # general.
46
46
        self.assertEqual(
47
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}),
48
 
           parse_args(cmd_commit(), ['--help']))
 
47
            ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True,
 
48
                  'bugs': []}),
 
49
            parse_args(cmd_commit(), ['--help']))
49
50
        self.assertEqual(
50
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}),
51
 
           parse_args(cmd_commit(), ['--message=biter']))
 
51
            ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter',
 
52
                  'bugs': []}),
 
53
            parse_args(cmd_commit(), ['--message=biter']))
52
54
 
53
55
    def test_no_more_opts(self):
54
56
        """Terminated options"""
55
57
        self.assertEqual(
56
 
            (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}),
 
58
            (['-file-with-dashes'], {
 
59
                'author': [], 'exclude': [], 'fixes': [], 'bugs': []}),
57
60
            parse_args(cmd_commit(), ['--', '-file-with-dashes']))
58
61
 
59
62
    def test_option_help(self):
60
63
        """Options have help strings."""
61
64
        out, err = self.run_bzr('commit --help')
62
65
        self.assertContainsRe(out,
63
 
                r'--file(.|\n)*Take commit message from this file\.')
 
66
                              r'--file(.|\n)*Take commit message from this file\.')
64
67
        self.assertContainsRe(out, r'-h.*--help')
65
68
 
66
69
    def test_option_help_global(self):
93
96
        self.assertEqual((['-']), parse_args(cmd_commit(), ['-'])[0])
94
97
 
95
98
    def parse(self, options, args):
96
 
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
99
        parser = option.get_optparser(options)
97
100
        return parser.parse_args(args)
98
101
 
99
102
    def test_conversion(self):
107
110
        options = [option.Option('number', type=int)]
108
111
        opts, args = self.parse(options, ['--number', '6'])
109
112
        self.assertEqual(6, opts.number)
110
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
113
        self.assertRaises(errors.CommandError, self.parse, options,
111
114
                          ['--number'])
112
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
115
        self.assertRaises(errors.CommandError, self.parse, options,
113
116
                          ['--no-number'])
 
117
        self.assertRaises(errors.CommandError, self.parse, options,
 
118
                          ['--number', 'a'])
114
119
 
115
120
    def test_is_hidden(self):
116
121
        self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
118
123
 
119
124
    def test_registry_conversion(self):
120
125
        registry = controldir.ControlDirFormatRegistry()
121
 
        bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
122
 
        bzrdir.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
123
 
        bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
124
 
            'two help', hidden=True)
 
126
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
 
127
        bzr.register_metadir(
 
128
            registry, 'two', 'RepositoryFormatKnit1', 'two help')
 
129
        bzr.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
 
130
                             'two help', hidden=True)
125
131
        registry.set_default('one')
126
132
        options = [option.RegistryOption('format', '', registry, str)]
127
133
        opts, args = self.parse(options, ['--format', 'one'])
128
 
        self.assertEqual({'format':'one'}, opts)
 
134
        self.assertEqual({'format': 'one'}, opts)
129
135
        opts, args = self.parse(options, ['--format', 'two'])
130
 
        self.assertEqual({'format':'two'}, opts)
131
 
        self.assertRaises(errors.BadOptionValue, self.parse, options,
 
136
        self.assertEqual({'format': 'two'}, opts)
 
137
        self.assertRaises(option.BadOptionValue, self.parse, options,
132
138
                          ['--format', 'three'])
133
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
139
        self.assertRaises(errors.CommandError, self.parse, options,
134
140
                          ['--two'])
135
141
        options = [option.RegistryOption('format', '', registry, str,
136
 
                   value_switches=True)]
 
142
                                         value_switches=True)]
137
143
        opts, args = self.parse(options, ['--two'])
138
 
        self.assertEqual({'format':'two'}, opts)
 
144
        self.assertEqual({'format': 'two'}, opts)
139
145
        opts, args = self.parse(options, ['--two', '--one'])
140
 
        self.assertEqual({'format':'one'}, opts)
 
146
        self.assertEqual({'format': 'one'}, opts)
141
147
        opts, args = self.parse(options, ['--two', '--one',
142
148
                                          '--format', 'two'])
143
 
        self.assertEqual({'format':'two'}, opts)
 
149
        self.assertEqual({'format': 'two'}, opts)
144
150
        options = [option.RegistryOption('format', '', registry, str,
145
 
                   enum_switch=False)]
146
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
151
                                         enum_switch=False)]
 
152
        self.assertRaises(errors.CommandError, self.parse, options,
147
153
                          ['--format', 'two'])
148
154
 
149
155
    def test_override(self):
160
166
 
161
167
    def test_registry_converter(self):
162
168
        options = [option.RegistryOption('format', '',
163
 
                   controldir.format_registry, controldir.format_registry.make_bzrdir)]
 
169
                                         controldir.format_registry, controldir.format_registry.make_controldir)]
164
170
        opts, args = self.parse(options, ['--format', 'knit'])
165
171
        self.assertIsInstance(opts.format.repository_format,
166
172
                              knitrepo.RepositoryFormatKnit1)
167
173
 
168
174
    def test_lazy_registry(self):
169
175
        options = [option.RegistryOption('format', '',
170
 
                   lazy_registry=('bzrlib.controldir','format_registry'),
171
 
                   converter=str)]
 
176
                                         lazy_registry=(
 
177
                                             'breezy.controldir', 'format_registry'),
 
178
                                         converter=str)]
172
179
        opts, args = self.parse(options, ['--format', 'knit'])
173
180
        self.assertEqual({'format': 'knit'}, opts)
174
181
        self.assertRaises(
175
 
            errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
 
182
            option.BadOptionValue, self.parse, options, ['--format', 'BAD'])
176
183
 
177
184
    def test_from_kwargs(self):
178
185
        my_option = option.RegistryOption.from_kwargs('my-option',
179
 
            help='test option', short='be short', be_long='go long')
 
186
                                                      help='test option', short='be short', be_long='go long')
180
187
        self.assertEqual(['my-option'],
181
 
            [x[0] for x in my_option.iter_switches()])
 
188
                         [x[0] for x in my_option.iter_switches()])
182
189
        my_option = option.RegistryOption.from_kwargs('my-option',
183
 
            help='test option', title="My option", short='be short',
184
 
            be_long='go long', value_switches=True)
 
190
                                                      help='test option', title="My option", short='be short',
 
191
                                                      be_long='go long', value_switches=True)
185
192
        self.assertEqual(['my-option', 'be-long', 'short'],
186
 
            [x[0] for x in my_option.iter_switches()])
 
193
                         [x[0] for x in my_option.iter_switches()])
187
194
        self.assertEqual('test option', my_option.help)
188
195
 
189
196
    def test_help(self):
190
197
        registry = controldir.ControlDirFormatRegistry()
191
 
        bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
 
        bzrdir.register_metadir(registry, 'two',
193
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
194
 
            'two help',
195
 
            )
196
 
        bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
197
 
            hidden=True)
 
198
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
 
199
        bzr.register_metadir(registry, 'two',
 
200
                             'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
201
                             'two help',
 
202
                             )
 
203
        bzr.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
 
204
                             hidden=True)
198
205
        registry.set_default('one')
199
206
        options = [option.RegistryOption('format', 'format help', registry,
200
 
                   str, value_switches=True, title='Formats')]
201
 
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
207
                                         str, value_switches=True, title='Formats')]
 
208
        parser = option.get_optparser(options)
202
209
        value = parser.format_option_help()
203
210
        self.assertContainsRe(value, 'format.*format help')
204
211
        self.assertContainsRe(value, 'one.*one help')
216
223
        self.assertEqual(list(opt.iter_switches()),
217
224
                         [('hello', None, 'GAR', 'fg')])
218
225
        registry = controldir.ControlDirFormatRegistry()
219
 
        bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
 
        bzrdir.register_metadir(registry, 'two',
221
 
                'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
222
 
                'two help',
223
 
                )
 
226
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
 
227
        bzr.register_metadir(registry, 'two',
 
228
                             'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
229
                             'two help',
 
230
                             )
224
231
        registry.set_default('one')
225
232
        opt = option.RegistryOption('format', 'format help', registry,
226
233
                                    value_switches=False)
238
245
    def test_option_callback_bool(self):
239
246
        "Test booleans get True and False passed correctly to a callback."""
240
247
        cb_calls = []
 
248
 
241
249
        def cb(option, name, value, parser):
242
 
            cb_calls.append((option,name,value,parser))
 
250
            cb_calls.append((option, name, value, parser))
243
251
        options = [option.Option('hello', custom_callback=cb)]
244
252
        opts, args = self.parse(options, ['--hello', '--no-hello'])
245
253
        self.assertEqual(2, len(cb_calls))
246
 
        opt,name,value,parser = cb_calls[0]
 
254
        opt, name, value, parser = cb_calls[0]
247
255
        self.assertEqual('hello', name)
248
256
        self.assertTrue(value)
249
 
        opt,name,value,parser = cb_calls[1]
 
257
        opt, name, value, parser = cb_calls[1]
250
258
        self.assertEqual('hello', name)
251
259
        self.assertFalse(value)
252
260
 
253
261
    def test_option_callback_str(self):
254
262
        """Test callbacks work for string options both long and short."""
255
263
        cb_calls = []
 
264
 
256
265
        def cb(option, name, value, parser):
257
 
            cb_calls.append((option,name,value,parser))
 
266
            cb_calls.append((option, name, value, parser))
258
267
        options = [option.Option('hello', type=str, custom_callback=cb,
259
 
            short_name='h')]
 
268
                                 short_name='h')]
260
269
        opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
261
270
        self.assertEqual(2, len(cb_calls))
262
 
        opt,name,value,parser = cb_calls[0]
 
271
        opt, name, value, parser = cb_calls[0]
263
272
        self.assertEqual('hello', name)
264
273
        self.assertEqual('world', value)
265
 
        opt,name,value,parser = cb_calls[1]
 
274
        opt, name, value, parser = cb_calls[1]
266
275
        self.assertEqual('hello', name)
267
276
        self.assertEqual('mars', value)
268
277
 
271
280
    """Tests for ListOption, used to specify lists on the command-line."""
272
281
 
273
282
    def parse(self, options, args):
274
 
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
283
        parser = option.get_optparser(options)
275
284
        return parser.parse_args(args)
276
285
 
277
286
    def test_list_option(self):
311
320
    def test_option_callback_list(self):
312
321
        """Test callbacks work for list options."""
313
322
        cb_calls = []
 
323
 
314
324
        def cb(option, name, value, parser):
315
325
            # Note that the value is a reference so copy to keep it
316
 
            cb_calls.append((option,name,value[:],parser))
 
326
            cb_calls.append((option, name, value[:], parser))
317
327
        options = [option.ListOption('hello', type=str, custom_callback=cb)]
318
328
        opts, args = self.parse(options, ['--hello=world', '--hello=mars',
319
 
            '--hello=-'])
 
329
                                          '--hello=-'])
320
330
        self.assertEqual(3, len(cb_calls))
321
 
        opt,name,value,parser = cb_calls[0]
 
331
        opt, name, value, parser = cb_calls[0]
322
332
        self.assertEqual('hello', name)
323
333
        self.assertEqual(['world'], value)
324
 
        opt,name,value,parser = cb_calls[1]
 
334
        opt, name, value, parser = cb_calls[1]
325
335
        self.assertEqual('hello', name)
326
336
        self.assertEqual(['world', 'mars'], value)
327
 
        opt,name,value,parser = cb_calls[2]
 
337
        opt, name, value, parser = cb_calls[2]
328
338
        self.assertEqual('hello', name)
329
339
        self.assertEqual([], value)
330
340
 
346
356
            cmd = commands.get_cmd_object(cmd_name)
347
357
            for opt_name, opt in sorted(cmd.options().items()):
348
358
                g.append((cmd_name, opt))
349
 
        self.assert_(g)
 
359
        self.assertTrue(g)
350
360
        return g
351
361
 
352
362
    def test_option_grammar(self):
361
371
                    name = "/".join([opt.name, name])
362
372
                if not helptxt:
363
373
                    msgs.append('%-16s %-16s %s' %
364
 
                           ((scope or 'GLOBAL'), name, 'NO HELP'))
 
374
                                ((scope or 'GLOBAL'), name, 'NO HELP'))
365
375
                elif not option_re.match(helptxt):
366
 
                    if name.startswith("format/"):
367
 
                        # Don't complain about the odd format registry help
368
 
                        continue
369
376
                    msgs.append('%-16s %-16s %s' %
370
 
                            ((scope or 'GLOBAL'), name, helptxt))
 
377
                                ((scope or 'GLOBAL'), name, helptxt))
371
378
        if msgs:
372
379
            self.fail("The following options don't match the style guide:\n"
373
 
                    + '\n'.join(msgs))
 
380
                      + '\n'.join(msgs))
374
381
 
375
382
 
376
383
class TestOptionMisc(TestCase):
377
384
 
378
385
    def test_is_hidden(self):
379
386
        registry = controldir.ControlDirFormatRegistry()
380
 
        bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat',
381
 
            'hidden help text', hidden=True)
382
 
        bzrdir.register_metadir(registry, 'visible', 'VisibleFormat',
383
 
            'visible help text', hidden=False)
 
387
        bzr.register_metadir(registry, 'hidden', 'HiddenFormat',
 
388
                             'hidden help text', hidden=True)
 
389
        bzr.register_metadir(registry, 'visible', 'VisibleFormat',
 
390
                             'visible help text', hidden=False)
384
391
        format = option.RegistryOption('format', '', registry, str)
385
392
        self.assertTrue(format.is_hidden('hidden'))
386
393
        self.assertFalse(format.is_hidden('visible'))
388
395
    def test_short_name(self):
389
396
        registry = controldir.ControlDirFormatRegistry()
390
397
        opt = option.RegistryOption('format', help='', registry=registry)
391
 
        self.assertEquals(None, opt.short_name())
 
398
        self.assertEqual(None, opt.short_name())
392
399
        opt = option.RegistryOption('format', short_name='F', help='',
393
 
            registry=registry)
394
 
        self.assertEquals('F', opt.short_name())
 
400
                                    registry=registry)
 
401
        self.assertEqual('F', opt.short_name())
395
402
 
396
403
    def test_option_custom_help(self):
397
404
        the_opt = option.Option.OPTIONS['help']
406
413
        reg.register('short', 'ShortChoice')
407
414
        reg.register('long', 'LongChoice')
408
415
        ropt = option.RegistryOption('choice', '', reg, value_switches=True,
409
 
            short_value_switches={'short': 's'})
 
416
                                     short_value_switches={'short': 's'})
410
417
        opts, args = parse([ropt], ['--short'])
411
418
        self.assertEqual('ShortChoice', opts.choice)
412
419
        opts, args = parse([ropt], ['-s'])
421
428
        self.assertEqual(level, option._verbosity_level)
422
429
 
423
430
    def test_verbose_quiet_linkage(self):
424
 
        parser = option.get_optparser(option.Option.STD_OPTIONS)
 
431
        parser = option.get_optparser(
 
432
            [v for k, v in sorted(option.Option.STD_OPTIONS.items())])
425
433
        self.check(parser, 0, [])
426
434
        self.check(parser, 1, ['-v'])
427
435
        self.check(parser, 2, ['-v', '-v'])