/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

merge bzr.dev.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import re
18
18
 
19
19
from bzrlib import (
20
 
    builtins,
21
20
    bzrdir,
22
21
    commands,
23
22
    errors,
24
23
    option,
25
 
    repository,
26
 
    symbol_versioning,
27
24
    )
28
 
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
 
25
from bzrlib.builtins import cmd_commit
29
26
from bzrlib.commands import Command, parse_args
30
27
from bzrlib.tests import TestCase
31
28
from bzrlib.repofmt import knitrepo
41
38
 
42
39
    def test_parse_args(self):
43
40
        """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'}))
 
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'}))
49
48
 
50
49
    def test_no_more_opts(self):
51
50
        """Terminated options"""
52
 
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
53
 
                          (['-file-with-dashes'], {'fixes': []}))
 
51
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
 
52
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
54
53
 
55
54
    def test_option_help(self):
56
55
        """Options have help strings."""
67
66
    def test_option_arg_help(self):
68
67
        """Help message shows option arguments."""
69
68
        out, err = self.run_bzr('help commit')
70
 
        self.assertEquals(err, '')
 
69
        self.assertEqual(err, '')
71
70
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
72
71
 
73
72
    def test_unknown_short_opt(self):
81
80
 
82
81
    def test_allow_dash(self):
83
82
        """Test that we can pass a plain '-' as an argument."""
84
 
        self.assertEqual(
85
 
            (['-'], {'fixes': []}), parse_args(cmd_commit(), ['-']))
 
83
        self.assertEqual((['-']), parse_args(cmd_commit(), ['-'])[0])
86
84
 
87
85
    def parse(self, options, args):
88
86
        parser = option.get_optparser(dict((o.name, o) for o in options))
93
91
        opts, args = self.parse(options, ['--no-hello', '--hello'])
94
92
        self.assertEqual(True, opts.hello)
95
93
        opts, args = self.parse(options, [])
96
 
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
 
94
        self.assertFalse(hasattr(opts, 'hello'))
97
95
        opts, args = self.parse(options, ['--hello', '--no-hello'])
98
96
        self.assertEqual(False, opts.hello)
99
97
        options = [option.Option('number', type=int)]
104
102
        self.assertRaises(errors.BzrCommandError, self.parse, options,
105
103
                          ['--no-number'])
106
104
 
 
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
 
107
109
    def test_registry_conversion(self):
108
110
        registry = bzrdir.BzrDirFormatRegistry()
109
111
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
134
136
        self.assertRaises(errors.BzrCommandError, self.parse, options,
135
137
                          ['--format', 'two'])
136
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
 
137
151
    def test_registry_converter(self):
138
152
        options = [option.RegistryOption('format', '',
139
153
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
141
155
        self.assertIsInstance(opts.format.repository_format,
142
156
                              knitrepo.RepositoryFormatKnit1)
143
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
 
144
167
    def test_from_kwargs(self):
145
168
        my_option = option.RegistryOption.from_kwargs('my-option',
146
169
            help='test option', short='be short', be_long='go long')
202
225
                          ('two', None, None, 'two help'),
203
226
                          ])
204
227
 
 
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
 
205
259
 
206
260
class TestListOptions(TestCase):
207
261
    """Tests for ListOption, used to specify lists on the command-line."""
215
269
        opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
216
270
        self.assertEqual(['world', 'sailor'], opts.hello)
217
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
 
218
278
    def test_list_option_no_arguments(self):
219
279
        options = [option.ListOption('hello', type=str)]
220
280
        opts, args = self.parse(options, [])
238
298
            options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
239
299
        self.assertEqual(['c'], opts.hello)
240
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
 
241
321
 
242
322
class TestOptionDefinitions(TestCase):
243
323
    """Tests for options in the Bazaar codebase."""
286
366
        # period and be all on a single line, because the display code will
287
367
        # wrap it.
288
368
        option_re = re.compile(r'^[A-Z][^\n]+\.$')
289
 
        for scope, option in self.get_builtin_command_options():
290
 
            if not option.help:
291
 
                msgs.append('%-16s %-16s %s' %
292
 
                       ((scope or 'GLOBAL'), option.name, 'NO HELP'))
293
 
            elif not option_re.match(option.help):
294
 
                msgs.append('%-16s %-16s %s' %
295
 
                        ((scope or 'GLOBAL'), option.name, option.help))
 
369
        for scope, opt in self.get_builtin_command_options():
 
370
            if not opt.help:
 
371
                msgs.append('%-16s %-16s %s' %
 
372
                       ((scope or 'GLOBAL'), opt.name, 'NO HELP'))
 
373
            elif not option_re.match(opt.help):
 
374
                msgs.append('%-16s %-16s %s' %
 
375
                        ((scope or 'GLOBAL'), opt.name, opt.help))
296
376
        if msgs:
297
377
            self.fail("The following options don't match the style guide:\n"
298
378
                    + '\n'.join(msgs))
 
379
 
 
380
    def test_is_hidden(self):
 
381
        registry = bzrdir.BzrDirFormatRegistry()
 
382
        registry.register_metadir('hidden', 'HiddenFormat',
 
383
            'hidden help text', hidden=True)
 
384
        registry.register_metadir('visible', 'VisibleFormat',
 
385
            'visible help text', hidden=False)
 
386
        format = option.RegistryOption('format', '', registry, str)
 
387
        self.assertTrue(format.is_hidden('hidden'))
 
388
        self.assertFalse(format.is_hidden('visible'))
 
389
 
 
390
    def test_option_custom_help(self):
 
391
        the_opt = option.Option.OPTIONS['help']
 
392
        orig_help = the_opt.help[:]
 
393
        my_opt = option.custom_help('help', 'suggest lottery numbers')
 
394
        # Confirm that my_opt has my help and the original is unchanged
 
395
        self.assertEqual('suggest lottery numbers', my_opt.help)
 
396
        self.assertEqual(orig_help, the_opt.help)
 
397
 
 
398
 
 
399
class TestVerboseQuietLinkage(TestCase):
 
400
 
 
401
    def check(self, parser, level, args):
 
402
        option._verbosity_level = 0
 
403
        opts, args = parser.parse_args(args)
 
404
        self.assertEqual(level, option._verbosity_level)
 
405
 
 
406
    def test_verbose_quiet_linkage(self):
 
407
        parser = option.get_optparser(option.Option.STD_OPTIONS)
 
408
        self.check(parser, 0, [])
 
409
        self.check(parser, 1, ['-v'])
 
410
        self.check(parser, 2, ['-v', '-v'])
 
411
        self.check(parser, -1, ['-q'])
 
412
        self.check(parser, -2, ['-qq'])
 
413
        self.check(parser, -1, ['-v', '-v', '-q'])
 
414
        self.check(parser, 2, ['-q', '-v', '-v'])
 
415
        self.check(parser, 0, ['--no-verbose'])
 
416
        self.check(parser, 0, ['-v', '-q', '--no-quiet'])