~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testoptions.py

- improved handling of non-ascii branch names and test
  patch from Joel Rosdahl

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
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
 
1
# (C) 2005 Canonical
16
2
 
17
 
from bzrlib import (
18
 
    builtins,
19
 
    bzrdir,
20
 
    errors,
21
 
    option,
22
 
    repository,
23
 
    symbol_versioning,
24
 
    )
 
3
from bzrlib.selftest import TestCase
 
4
from bzrlib.commands import Command, parse_args
25
5
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
26
 
from bzrlib.commands import Command, parse_args
27
 
from bzrlib.tests import TestCase
28
 
from bzrlib.repofmt import knitrepo
29
6
 
30
 
def parse(options, args):
31
 
    parser = option.get_optparser(dict((o.name, o) for o in options))
32
 
    return parser.parse_args(args)
33
7
 
34
8
class OptionTests(TestCase):
35
9
    """Command-line option tests"""
39
13
        eq = self.assertEquals
40
14
        eq(parse_args(cmd_commit(), ['--help']),
41
15
           ([], {'help': True}))
 
16
        eq(parse_args(cmd_status(), ['--all']),
 
17
           ([], {'all': True}))
42
18
        eq(parse_args(cmd_commit(), ['--message=biter']),
43
19
           ([], {'message': 'biter'}))
44
20
        ## eq(parse_args(cmd_log(),  '-r 500'.split()),
52
28
    def test_option_help(self):
53
29
        """Options have help strings."""
54
30
        out, err = self.run_bzr_captured(['commit', '--help'])
55
 
        self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
56
 
                                   ' message')
57
 
        self.assertContainsRe(out, r'-h.*--help')
 
31
        self.assertContainsRe(out, r'--file.*file containing commit message')
58
32
 
59
33
    def test_option_help_global(self):
60
34
        """Global options have help strings."""
67
41
        self.assertEquals(err, '')
68
42
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
69
43
 
70
 
    def test_unknown_short_opt(self):
71
 
        out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
72
 
        self.assertContainsRe(err, r'no such option')
73
 
 
74
 
    def test_get_short_name(self):
75
 
        file_opt = option.Option.OPTIONS['file']
76
 
        self.assertEquals(file_opt.short_name(), 'F')
77
 
        force_opt = option.Option.OPTIONS['force']
78
 
        self.assertEquals(force_opt.short_name(), None)
79
 
 
80
 
    def test_set_short_name(self):
81
 
        o = option.Option('wiggle')
82
 
        o.set_short_name('w')
83
 
        self.assertEqual(o.short_name(), 'w')
84
 
 
85
 
    def test_old_short_names(self):
86
 
        # test the deprecated method for getting and setting short option
87
 
        # names
88
 
        expected_warning = (
89
 
            "access to SHORT_OPTIONS was deprecated in version 0.14."
90
 
            " Set the short option name when constructing the Option.",
91
 
            DeprecationWarning, 2)
92
 
        _warnings = []
93
 
        def capture_warning(message, category, stacklevel=None):
94
 
            _warnings.append((message, category, stacklevel))
95
 
        old_warning_method = symbol_versioning.warn
96
 
        try:
97
 
            # an example of the kind of thing plugins might want to do through
98
 
            # the old interface - make a new option and then give it a short
99
 
            # name.
100
 
            symbol_versioning.set_warning_method(capture_warning)
101
 
            example_opt = option.Option('example', help='example option')
102
 
            option.Option.SHORT_OPTIONS['w'] = example_opt
103
 
            self.assertEqual(example_opt.short_name(), 'w')
104
 
            self.assertEqual([expected_warning], _warnings)
105
 
            # now check that it can actually be parsed with the registered
106
 
            # value
107
 
            opts, args = parse([example_opt], ['-w', 'foo'])
108
 
            self.assertEqual(opts.example, True)
109
 
            self.assertEqual(args, ['foo'])
110
 
        finally:
111
 
            symbol_versioning.set_warning_method(old_warning_method)
112
 
 
113
 
    def test_allow_dash(self):
114
 
        """Test that we can pass a plain '-' as an argument."""
115
 
        self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
116
 
 
117
 
    def parse(self, options, args):
118
 
        parser = option.get_optparser(dict((o.name, o) for o in options))
119
 
        return parser.parse_args(args)
120
 
 
121
 
    def test_conversion(self):
122
 
        options = [option.Option('hello')]
123
 
        opts, args = self.parse(options, ['--no-hello', '--hello'])
124
 
        self.assertEqual(True, opts.hello)
125
 
        opts, args = self.parse(options, [])
126
 
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
127
 
        opts, args = self.parse(options, ['--hello', '--no-hello'])
128
 
        self.assertEqual(False, opts.hello)
129
 
        options = [option.Option('number', type=int)]
130
 
        opts, args = self.parse(options, ['--number', '6'])
131
 
        self.assertEqual(6, opts.number)
132
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
133
 
                          ['--number'])
134
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
135
 
                          ['--no-number'])
136
 
 
137
 
    def test_registry_conversion(self):
138
 
        registry = bzrdir.BzrDirFormatRegistry()
139
 
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
140
 
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
141
 
        registry.set_default('one')
142
 
        options = [option.RegistryOption('format', '', registry, str)]
143
 
        opts, args = self.parse(options, ['--format', 'one'])
144
 
        self.assertEqual({'format':'one'}, opts)
145
 
        opts, args = self.parse(options, ['--format', 'two'])
146
 
        self.assertEqual({'format':'two'}, opts)
147
 
        self.assertRaises(errors.BadOptionValue, self.parse, options,
148
 
                          ['--format', 'three'])
149
 
        self.assertRaises(errors.BzrCommandError, self.parse, options,
150
 
                          ['--two'])
151
 
        options = [option.RegistryOption('format', '', registry, str,
152
 
                   value_switches=True)]
153
 
        opts, args = self.parse(options, ['--two'])
154
 
        self.assertEqual({'format':'two'}, opts)
155
 
        opts, args = self.parse(options, ['--two', '--one'])
156
 
        self.assertEqual({'format':'one'}, opts)
157
 
        opts, args = self.parse(options, ['--two', '--one',
158
 
                                          '--format', 'two'])
159
 
        self.assertEqual({'format':'two'}, opts)
160
 
 
161
 
    def test_registry_converter(self):
162
 
        options = [option.RegistryOption('format', '',
163
 
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
164
 
        opts, args = self.parse(options, ['--format', 'knit'])
165
 
        self.assertIsInstance(opts.format.repository_format,
166
 
                              knitrepo.RepositoryFormatKnit1)
167
 
 
168
 
    def test_help(self):
169
 
        registry = bzrdir.BzrDirFormatRegistry()
170
 
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
171
 
        registry.register_metadir('two',
172
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
173
 
            'two help',
174
 
            )
175
 
        registry.set_default('one')
176
 
        options = [option.RegistryOption('format', 'format help', registry,
177
 
                   str, value_switches=True, title='Formats')]
178
 
        parser = option.get_optparser(dict((o.name, o) for o in options))
179
 
        value = parser.format_option_help()
180
 
        self.assertContainsRe(value, 'format.*format help')
181
 
        self.assertContainsRe(value, 'one.*one help')
182
 
        self.assertContainsRe(value, 'Formats:\n *--format')
183
 
 
184
 
    def test_iter_switches(self):
185
 
        opt = option.Option('hello', help='fg')
186
 
        self.assertEqual(list(opt.iter_switches()),
187
 
                         [('hello', None, None, 'fg')])
188
 
        opt = option.Option('hello', help='fg', type=int)
189
 
        self.assertEqual(list(opt.iter_switches()),
190
 
                         [('hello', None, 'ARG', 'fg')])
191
 
        opt = option.Option('hello', help='fg', type=int, argname='gar')
192
 
        self.assertEqual(list(opt.iter_switches()),
193
 
                         [('hello', None, 'GAR', 'fg')])
194
 
        registry = bzrdir.BzrDirFormatRegistry()
195
 
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
196
 
        registry.register_metadir('two',
197
 
                'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
198
 
                'two help',
199
 
                )
200
 
        registry.set_default('one')
201
 
        opt = option.RegistryOption('format', 'format help', registry,
202
 
                                    value_switches=False)
203
 
        self.assertEqual(list(opt.iter_switches()),
204
 
                         [('format', None, 'ARG', 'format help')])
205
 
        opt = option.RegistryOption('format', 'format help', registry,
206
 
                                    value_switches=True)
207
 
        self.assertEqual(list(opt.iter_switches()),
208
 
                         [('format', None, 'ARG', 'format help'),
209
 
                          ('default', None, None, 'one help'),
210
 
                          ('one', None, None, 'one help'),
211
 
                          ('two', None, None, 'two help'),
212
 
                          ])
213
44
 
214
45
#     >>> parse_args('log -r 500'.split())
215
46
#     (['log'], {'revision': [<RevisionSpec_int 500>]})