~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: John Arbash Meinel
  • Date: 2007-01-24 20:40:20 UTC
  • mfrom: (2242 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2245.
  • Revision ID: john@arbash-meinel.com-20070124204020-szyxbjpn9mzbsks7
[merge] bzr.dev 2242

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from bzrlib import (
 
18
    builtins,
 
19
    bzrdir,
 
20
    errors,
 
21
    option,
 
22
    repository,
 
23
    symbol_versioning,
 
24
    )
17
25
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
18
26
from bzrlib.commands import Command, parse_args
19
 
from bzrlib import errors
20
 
from bzrlib import option
21
27
from bzrlib.tests import TestCase
22
28
 
23
 
# TODO: might be nice to just parse them into a structured form and test
24
 
# against that, rather than running the whole command.
 
29
def parse(options, args):
 
30
    parser = option.get_optparser(dict((o.name, o) for o in options))
 
31
    return parser.parse_args(args)
25
32
 
26
33
class OptionTests(TestCase):
27
34
    """Command-line option tests"""
69
76
        force_opt = option.Option.OPTIONS['force']
70
77
        self.assertEquals(force_opt.short_name(), None)
71
78
 
 
79
    def test_set_short_name(self):
 
80
        o = option.Option('wiggle')
 
81
        o.set_short_name('w')
 
82
        self.assertEqual(o.short_name(), 'w')
 
83
 
 
84
    def test_old_short_names(self):
 
85
        # test the deprecated method for getting and setting short option
 
86
        # names
 
87
        expected_warning = (
 
88
            "access to SHORT_OPTIONS was deprecated in version 0.14."
 
89
            " Set the short option name when constructing the Option.",
 
90
            DeprecationWarning, 2)
 
91
        _warnings = []
 
92
        def capture_warning(message, category, stacklevel=None):
 
93
            _warnings.append((message, category, stacklevel))
 
94
        old_warning_method = symbol_versioning.warn
 
95
        try:
 
96
            # an example of the kind of thing plugins might want to do through
 
97
            # the old interface - make a new option and then give it a short
 
98
            # name.
 
99
            symbol_versioning.set_warning_method(capture_warning)
 
100
            example_opt = option.Option('example', help='example option')
 
101
            option.Option.SHORT_OPTIONS['w'] = example_opt
 
102
            self.assertEqual(example_opt.short_name(), 'w')
 
103
            self.assertEqual([expected_warning], _warnings)
 
104
            # now check that it can actually be parsed with the registered
 
105
            # value
 
106
            opts, args = parse([example_opt], ['-w', 'foo'])
 
107
            self.assertEqual(opts.example, True)
 
108
            self.assertEqual(args, ['foo'])
 
109
        finally:
 
110
            symbol_versioning.set_warning_method(old_warning_method)
 
111
 
72
112
    def test_allow_dash(self):
73
113
        """Test that we can pass a plain '-' as an argument."""
74
114
        self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
75
115
 
 
116
    def parse(self, options, args):
 
117
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
118
        return parser.parse_args(args)
 
119
 
76
120
    def test_conversion(self):
77
 
        def parse(options, args):
78
 
            parser = option.get_optparser(dict((o.name, o) for o in options))
79
 
            return parser.parse_args(args)
80
121
        options = [option.Option('hello')]
81
 
        opts, args = parse(options, ['--no-hello', '--hello'])
 
122
        opts, args = self.parse(options, ['--no-hello', '--hello'])
82
123
        self.assertEqual(True, opts.hello)
83
 
        opts, args = parse(options, [])
 
124
        opts, args = self.parse(options, [])
84
125
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
85
 
        opts, args = parse(options, ['--hello', '--no-hello'])
 
126
        opts, args = self.parse(options, ['--hello', '--no-hello'])
86
127
        self.assertEqual(False, opts.hello)
87
128
        options = [option.Option('number', type=int)]
88
 
        opts, args = parse(options, ['--number', '6'])
 
129
        opts, args = self.parse(options, ['--number', '6'])
89
130
        self.assertEqual(6, opts.number)
90
 
        self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
91
 
        self.assertRaises(errors.BzrCommandError, parse, options, 
 
131
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
132
                          ['--number'])
 
133
        self.assertRaises(errors.BzrCommandError, self.parse, options,
92
134
                          ['--no-number'])
93
135
 
 
136
    def test_registry_conversion(self):
 
137
        registry = bzrdir.BzrDirFormatRegistry()
 
138
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
 
139
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
 
140
        registry.set_default('one')
 
141
        options = [option.RegistryOption('format', '', registry, str)]
 
142
        opts, args = self.parse(options, ['--format', 'one'])
 
143
        self.assertEqual({'format':'one'}, opts)
 
144
        opts, args = self.parse(options, ['--format', 'two'])
 
145
        self.assertEqual({'format':'two'}, opts)
 
146
        self.assertRaises(errors.BadOptionValue, self.parse, options,
 
147
                          ['--format', 'three'])
 
148
        self.assertRaises(errors.BzrCommandError, self.parse, options,
 
149
                          ['--two'])
 
150
        options = [option.RegistryOption('format', '', registry, str,
 
151
                   value_switches=True)]
 
152
        opts, args = self.parse(options, ['--two'])
 
153
        self.assertEqual({'format':'two'}, opts)
 
154
        opts, args = self.parse(options, ['--two', '--one'])
 
155
        self.assertEqual({'format':'one'}, opts)
 
156
        opts, args = self.parse(options, ['--two', '--one',
 
157
                                          '--format', 'two'])
 
158
        self.assertEqual({'format':'two'}, opts)
 
159
 
 
160
    def test_registry_converter(self):
 
161
        options = [option.RegistryOption('format', '',
 
162
                   bzrdir.format_registry, builtins.get_format_type)]
 
163
        opts, args = self.parse(options, ['--format', 'knit'])
 
164
        self.assertIsInstance(opts.format.repository_format,
 
165
                              repository.RepositoryFormatKnit1)
 
166
 
 
167
    def test_help(self):
 
168
        registry = bzrdir.BzrDirFormatRegistry()
 
169
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
 
170
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
 
171
        registry.set_default('one')
 
172
        options = [option.RegistryOption('format', 'format help', registry,
 
173
                   str, value_switches=True)]
 
174
        parser = option.get_optparser(dict((o.name, o) for o in options))
 
175
        value = parser.format_option_help()
 
176
        self.assertContainsRe(value, 'format.*format help')
 
177
        self.assertContainsRe(value, 'one.*one help')
 
178
 
94
179
    def test_iter_switches(self):
95
180
        opt = option.Option('hello', help='fg')
96
181
        self.assertEqual(list(opt.iter_switches()),
101
186
        opt = option.Option('hello', help='fg', type=int, argname='gar')
102
187
        self.assertEqual(list(opt.iter_switches()),
103
188
                         [('hello', None, 'GAR', 'fg')])
 
189
        registry = bzrdir.BzrDirFormatRegistry()
 
190
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
 
191
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
 
192
        registry.set_default('one')
 
193
        opt = option.RegistryOption('format', 'format help', registry,
 
194
                                    value_switches=False)
 
195
        self.assertEqual(list(opt.iter_switches()),
 
196
                         [('format', None, 'ARG', 'format help')])
 
197
        opt = option.RegistryOption('format', 'format help', registry,
 
198
                                    value_switches=True)
 
199
        self.assertEqual(list(opt.iter_switches()),
 
200
                         [('format', None, 'ARG', 'format help'),
 
201
                          ('default', None, None, 'one help'),
 
202
                          ('one', None, None, 'one help'),
 
203
                          ('two', None, None, 'two help'),
 
204
                          ])
104
205
 
105
206
#     >>> parse_args('log -r 500'.split())
106
207
#     (['log'], {'revision': [<RevisionSpec_int 500>]})