~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Vincent Ladeuil
  • Date: 2007-11-24 14:20:59 UTC
  • mto: (3928.1.1 bzr.integration)
  • mto: This revision was merged to the branch mainline in revision 3929.
  • Revision ID: v.ladeuil+lp@free.fr-20071124142059-2114qtsgfdv8g9p1
Ssl files needed for the test https server.

* bzrlib/tests/ssl_certs/create_ssls.py: 
Script to create the ssl keys and certificates.

* bzrlib/tests/ssl_certs/server.crt: 
Server certificate signed by the certificate authority.

* bzrlib/tests/ssl_certs/server.csr: 
Server certificate signing request.

* bzrlib/tests/ssl_certs/server_without_pass.key: 
Server key usable without password.

* bzrlib/tests/ssl_certs/server_with_pass.key: 
Server key.

* bzrlib/tests/ssl_certs/ca.key: 
Certificate authority private key.

* bzrlib/tests/ssl_certs/ca.crt: 
Certificate authority certificate.

* bzrlib/tests/ssl_certs/__init__.py: 
Provide access to ssl files (keys and certificates). 

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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import re
18
18
 
19
19
from bzrlib import (
 
20
    builtins,
20
21
    bzrdir,
21
22
    commands,
22
23
    errors,
23
24
    option,
 
25
    repository,
 
26
    symbol_versioning,
24
27
    )
25
 
from bzrlib.builtins import cmd_commit
 
28
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
26
29
from bzrlib.commands import Command, parse_args
27
30
from bzrlib.tests import TestCase
28
31
from bzrlib.repofmt import knitrepo
38
41
 
39
42
    def test_parse_args(self):
40
43
        """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'}))
 
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'}))
48
49
 
49
50
    def test_no_more_opts(self):
50
51
        """Terminated options"""
51
 
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
 
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
 
52
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
 
53
                          (['-file-with-dashes'], {'fixes': []}))
53
54
 
54
55
    def test_option_help(self):
55
56
        """Options have help strings."""
66
67
    def test_option_arg_help(self):
67
68
        """Help message shows option arguments."""
68
69
        out, err = self.run_bzr('help commit')
69
 
        self.assertEqual(err, '')
 
70
        self.assertEquals(err, '')
70
71
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
71
72
 
72
73
    def test_unknown_short_opt(self):
80
81
 
81
82
    def test_allow_dash(self):
82
83
        """Test that we can pass a plain '-' as an argument."""
83
 
        self.assertEqual((['-']), parse_args(cmd_commit(), ['-'])[0])
 
84
        self.assertEqual(
 
85
            (['-'], {'fixes': []}), parse_args(cmd_commit(), ['-']))
84
86
 
85
87
    def parse(self, options, args):
86
88
        parser = option.get_optparser(dict((o.name, o) for o in options))
102
104
        self.assertRaises(errors.BzrCommandError, self.parse, options,
103
105
                          ['--no-number'])
104
106
 
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
107
    def test_registry_conversion(self):
110
108
        registry = bzrdir.BzrDirFormatRegistry()
111
109
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
155
153
        self.assertIsInstance(opts.format.repository_format,
156
154
                              knitrepo.RepositoryFormatKnit1)
157
155
 
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
156
    def test_from_kwargs(self):
168
157
        my_option = option.RegistryOption.from_kwargs('my-option',
169
158
            help='test option', short='be short', be_long='go long')
269
258
        opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
270
259
        self.assertEqual(['world', 'sailor'], opts.hello)
271
260
 
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
261
    def test_list_option_no_arguments(self):
279
262
        options = [option.ListOption('hello', type=str)]
280
263
        opts, args = self.parse(options, [])
318
301
        self.assertEqual('hello', name)
319
302
        self.assertEqual([], value)
320
303
 
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
304
 
329
305
class TestOptionDefinitions(TestCase):
330
306
    """Tests for options in the Bazaar codebase."""
331
307
 
332
308
    def get_builtin_command_options(self):
333
309
        g = []
334
 
        for cmd_name in sorted(commands.all_command_names()):
335
 
            cmd = commands.get_cmd_object(cmd_name)
 
310
        for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
 
311
            cmd = cmd_class()
336
312
            for opt_name, opt in sorted(cmd.options().items()):
337
313
                g.append((cmd_name, opt))
338
314
        return g
345
321
        g = dict(option.Option.OPTIONS.items())
346
322
        used_globals = {}
347
323
        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):
 
324
        for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
 
325
            for option_or_name in sorted(cmd_class.takes_options):
351
326
                if not isinstance(option_or_name, basestring):
352
327
                    self.assertIsInstance(option_or_name, option.Option)
353
328
                elif not option_or_name in g:
354
329
                    msgs.append("apparent reference to undefined "
355
330
                        "global option %r from %r"
356
 
                        % (option_or_name, cmd))
 
331
                        % (option_or_name, cmd_class))
357
332
                else:
358
333
                    used_globals.setdefault(option_or_name, []).append(cmd_name)
359
334
        unused_globals = set(g.keys()) - set(used_globals.keys())
374
349
        # period and be all on a single line, because the display code will
375
350
        # wrap it.
376
351
        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))
 
352
        for scope, option in self.get_builtin_command_options():
 
353
            if not option.help:
 
354
                msgs.append('%-16s %-16s %s' %
 
355
                       ((scope or 'GLOBAL'), option.name, 'NO HELP'))
 
356
            elif not option_re.match(option.help):
 
357
                msgs.append('%-16s %-16s %s' %
 
358
                        ((scope or 'GLOBAL'), option.name, option.help))
384
359
        if msgs:
385
360
            self.fail("The following options don't match the style guide:\n"
386
361
                    + '\n'.join(msgs))