/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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):
111
114
                          ['--number'])
112
115
        self.assertRaises(errors.BzrCommandError, self.parse, options,
113
116
                          ['--no-number'])
 
117
        self.assertRaises(errors.BzrCommandError, 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'))
119
124
    def test_registry_conversion(self):
120
125
        registry = controldir.ControlDirFormatRegistry()
121
126
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
122
 
        bzr.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help')
 
127
        bzr.register_metadir(
 
128
            registry, 'two', 'RepositoryFormatKnit1', 'two help')
123
129
        bzr.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1',
124
 
            'two help', hidden=True)
 
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)
 
136
        self.assertEqual({'format': 'two'}, opts)
131
137
        self.assertRaises(option.BadOptionValue, self.parse, options,
132
138
                          ['--format', 'three'])
133
139
        self.assertRaises(errors.BzrCommandError, 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)]
 
151
                                         enum_switch=False)]
146
152
        self.assertRaises(errors.BzrCommandError, self.parse, options,
147
153
                          ['--format', 'two'])
148
154
 
160
166
 
161
167
    def test_registry_converter(self):
162
168
        options = [option.RegistryOption('format', '',
163
 
                   controldir.format_registry, controldir.format_registry.make_controldir)]
 
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=('breezy.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(
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
198
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
192
199
        bzr.register_metadir(registry, 'two',
193
 
            'breezy.bzr.knitrepo.RepositoryFormatKnit1',
194
 
            'two help',
195
 
            )
 
200
                             'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
201
                             'two help',
 
202
                             )
196
203
        bzr.register_metadir(registry, 'hidden', 'RepositoryFormat7', 'hidden help',
197
 
            hidden=True)
 
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')
218
225
        registry = controldir.ControlDirFormatRegistry()
219
226
        bzr.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help')
220
227
        bzr.register_metadir(registry, 'two',
221
 
                'breezy.bzr.knitrepo.RepositoryFormatKnit1',
222
 
                'two help',
223
 
                )
 
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
250
            cb_calls.append((option, name, value, parser))
243
251
        options = [option.Option('hello', custom_callback=cb)]
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
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
271
        opt, name, value, parser = cb_calls[0]
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
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
331
        opt, name, value, parser = cb_calls[0]
322
332
        self.assertEqual('hello', name)
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
376
                    if name.startswith("format/"):
367
377
                        # Don't complain about the odd format registry help
368
378
                        continue
369
379
                    msgs.append('%-16s %-16s %s' %
370
 
                            ((scope or 'GLOBAL'), name, helptxt))
 
380
                                ((scope or 'GLOBAL'), name, helptxt))
371
381
        if msgs:
372
382
            self.fail("The following options don't match the style guide:\n"
373
 
                    + '\n'.join(msgs))
 
383
                      + '\n'.join(msgs))
374
384
 
375
385
 
376
386
class TestOptionMisc(TestCase):
378
388
    def test_is_hidden(self):
379
389
        registry = controldir.ControlDirFormatRegistry()
380
390
        bzr.register_metadir(registry, 'hidden', 'HiddenFormat',
381
 
            'hidden help text', hidden=True)
 
391
                             'hidden help text', hidden=True)
382
392
        bzr.register_metadir(registry, 'visible', 'VisibleFormat',
383
 
            'visible help text', hidden=False)
 
393
                             'visible help text', hidden=False)
384
394
        format = option.RegistryOption('format', '', registry, str)
385
395
        self.assertTrue(format.is_hidden('hidden'))
386
396
        self.assertFalse(format.is_hidden('visible'))
390
400
        opt = option.RegistryOption('format', help='', registry=registry)
391
401
        self.assertEqual(None, opt.short_name())
392
402
        opt = option.RegistryOption('format', short_name='F', help='',
393
 
            registry=registry)
 
403
                                    registry=registry)
394
404
        self.assertEqual('F', opt.short_name())
395
405
 
396
406
    def test_option_custom_help(self):
406
416
        reg.register('short', 'ShortChoice')
407
417
        reg.register('long', 'LongChoice')
408
418
        ropt = option.RegistryOption('choice', '', reg, value_switches=True,
409
 
            short_value_switches={'short': 's'})
 
419
                                     short_value_switches={'short': 's'})
410
420
        opts, args = parse([ropt], ['--short'])
411
421
        self.assertEqual('ShortChoice', opts.choice)
412
422
        opts, args = parse([ropt], ['-s'])
421
431
        self.assertEqual(level, option._verbosity_level)
422
432
 
423
433
    def test_verbose_quiet_linkage(self):
424
 
        parser = option.get_optparser(option.Option.STD_OPTIONS)
 
434
        parser = option.get_optparser(
 
435
            [v for k, v in sorted(option.Option.STD_OPTIONS.items())])
425
436
        self.check(parser, 0, [])
426
437
        self.check(parser, 1, ['-v'])
427
438
        self.check(parser, 2, ['-v', '-v'])