/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1616.1.13 by Martin Pool
Fix 'bzr -h' to show help (#35940)
1
# Copyright (C) 2005, 2006 Canonical Ltd
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
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
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
16
2221.4.1 by Aaron Bentley
Get registry options working
17
from bzrlib import (
18
    builtins,
19
    bzrdir,
20
    errors,
21
    option,
22
    repository,
23
    )
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
24
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
25
from bzrlib.commands import Command, parse_args
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
26
from bzrlib.tests import TestCase
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
27
1616.1.13 by Martin Pool
Fix 'bzr -h' to show help (#35940)
28
# TODO: might be nice to just parse them into a structured form and test
29
# against that, rather than running the whole command.
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
30
31
class OptionTests(TestCase):
32
    """Command-line option tests"""
33
34
    def test_parse_args(self):
35
        """Option parser"""
36
        eq = self.assertEquals
37
        eq(parse_args(cmd_commit(), ['--help']),
38
           ([], {'help': True}))
39
        eq(parse_args(cmd_commit(), ['--message=biter']),
40
           ([], {'message': 'biter'}))
41
        ## eq(parse_args(cmd_log(),  '-r 500'.split()),
42
        ##   ([], {'revision': RevisionSpec_int(500)}))
43
1185.16.49 by mbp at sourcefrog
- more refactoring and tests of commandline
44
    def test_no_more_opts(self):
45
        """Terminated options"""
46
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
47
                          (['-file-with-dashes'], {}))
48
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
49
    def test_option_help(self):
50
        """Options have help strings."""
51
        out, err = self.run_bzr_captured(['commit', '--help'])
1857.1.12 by Aaron Bentley
Fix a bunch of test cases that assumed --merge-type or log-format
52
        self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
53
                                   ' message')
54
        self.assertContainsRe(out, r'-h.*--help')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
55
56
    def test_option_help_global(self):
57
        """Global options have help strings."""
58
        out, err = self.run_bzr_captured(['help', 'status'])
59
        self.assertContainsRe(out, r'--show-ids.*show internal object')
60
61
    def test_option_arg_help(self):
62
        """Help message shows option arguments."""
63
        out, err = self.run_bzr_captured(['help', 'commit'])
64
        self.assertEquals(err, '')
65
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
66
1185.35.24 by Aaron Bentley
Fixed handling of short options not accepted by the command
67
    def test_unknown_short_opt(self):
68
        out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
1857.1.1 by Aaron Bentley
Use optparse for parsing options
69
        self.assertContainsRe(err, r'no such option')
1185.35.24 by Aaron Bentley
Fixed handling of short options not accepted by the command
70
2190.2.1 by Martin Pool
remove global registration of short options
71
    def test_get_short_name(self):
72
        file_opt = option.Option.OPTIONS['file']
73
        self.assertEquals(file_opt.short_name, 'F')
74
        force_opt = option.Option.OPTIONS['force']
75
        self.assertEquals(force_opt.short_name, None)
76
1852.1.1 by John Arbash Meinel
Allow a plain '-' to be supplied as an argument. bug #50984
77
    def test_allow_dash(self):
78
        """Test that we can pass a plain '-' as an argument."""
79
        self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
80
2221.4.1 by Aaron Bentley
Get registry options working
81
    def parse(self, options, args):
82
        parser = option.get_optparser(dict((o.name, o) for o in options))
83
        return parser.parse_args(args)
84
        
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
85
    def test_conversion(self):
86
        options = [option.Option('hello')]
2221.4.1 by Aaron Bentley
Get registry options working
87
        opts, args = self.parse(options, ['--no-hello', '--hello'])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
88
        self.assertEqual(True, opts.hello)
2221.4.1 by Aaron Bentley
Get registry options working
89
        opts, args = self.parse(options, [])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
90
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
2221.4.1 by Aaron Bentley
Get registry options working
91
        opts, args = self.parse(options, ['--hello', '--no-hello'])
1857.1.22 by Aaron Bentley
Negations set value to False, instead of Optparser.DEFAULT_VALUE
92
        self.assertEqual(False, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
93
        options = [option.Option('number', type=int)]
2221.4.1 by Aaron Bentley
Get registry options working
94
        opts, args = self.parse(options, ['--number', '6'])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
95
        self.assertEqual(6, opts.number)
2221.4.1 by Aaron Bentley
Get registry options working
96
        self.assertRaises(errors.BzrCommandError, self.parse, options, 
97
                          ['--number'])
98
        self.assertRaises(errors.BzrCommandError, self.parse, options, 
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
99
                          ['--no-number'])
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
100
2221.4.1 by Aaron Bentley
Get registry options working
101
    def test_registry_conversion(self):
102
        registry = bzrdir.BzrDirFormatRegistry()
103
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
104
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
105
        registry.set_default('one')
2221.4.2 by Aaron Bentley
Implement RegistryOption on init
106
        options = [option.RegistryOption('format', '', registry, str)]
2221.4.1 by Aaron Bentley
Get registry options working
107
        opts, args = self.parse(options, ['--format', 'one'])
108
        self.assertEqual({'format':'one'}, opts)
109
        opts, args = self.parse(options, ['--format', 'two'])
110
        self.assertEqual({'format':'two'}, opts)
2221.4.6 by Aaron Bentley
Improve text and naming
111
        self.assertRaises(errors.BadOptionValue, self.parse, options, 
2221.4.1 by Aaron Bentley
Get registry options working
112
                          ['--format', 'three'])
113
        self.assertRaises(errors.BzrCommandError, self.parse, options, 
114
                          ['--two'])
2221.4.2 by Aaron Bentley
Implement RegistryOption on init
115
        options = [option.RegistryOption('format', '', registry, str, 
116
                   value_switches=True)]
2221.4.1 by Aaron Bentley
Get registry options working
117
        opts, args = self.parse(options, ['--two'])
118
        self.assertEqual({'format':'two'}, opts)
119
        opts, args = self.parse(options, ['--two', '--one'])
120
        self.assertEqual({'format':'one'}, opts)
121
        opts, args = self.parse(options, ['--two', '--one', 
122
                                          '--format', 'two'])
123
        self.assertEqual({'format':'two'}, opts)
124
125
    def test_registry_converter(self):
2221.4.2 by Aaron Bentley
Implement RegistryOption on init
126
        options = [option.RegistryOption('format', '', 
127
                   bzrdir.format_registry, builtins.get_format_type)]
2221.4.1 by Aaron Bentley
Get registry options working
128
        opts, args = self.parse(options, ['--format', 'knit'])
129
        self.assertIsInstance(opts.format.repository_format,
130
                              repository.RepositoryFormatKnit1)
131
2221.4.2 by Aaron Bentley
Implement RegistryOption on init
132
    def test_help(self):
133
        registry = bzrdir.BzrDirFormatRegistry()
134
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
135
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
136
        registry.set_default('one')
137
        options = [option.RegistryOption('format', 'format help', registry,
138
                   str, value_switches=True)]
139
        parser = option.get_optparser(dict((o.name, o) for o in options))
140
        value = parser.format_option_help()
141
        self.assertContainsRe(value, 'format.*format help')
142
        self.assertContainsRe(value, 'one.*one help')
143
1857.1.16 by Aaron Bentley
Add tests for iter_switches
144
    def test_iter_switches(self):
145
        opt = option.Option('hello', help='fg')
146
        self.assertEqual(list(opt.iter_switches()),
147
                         [('hello', None, None, 'fg')])
148
        opt = option.Option('hello', help='fg', type=int)
149
        self.assertEqual(list(opt.iter_switches()),
150
                         [('hello', None, 'ARG', 'fg')])
151
        opt = option.Option('hello', help='fg', type=int, argname='gar')
152
        self.assertEqual(list(opt.iter_switches()),
153
                         [('hello', None, 'GAR', 'fg')])
2221.4.4 by Aaron Bentley
Fix iter_switches behavior when value_switches is true
154
        registry = bzrdir.BzrDirFormatRegistry()
155
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
156
        registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
157
        registry.set_default('one')
158
        opt = option.RegistryOption('format', 'format help', registry,
159
                                    value_switches=False)
160
        self.assertEqual(list(opt.iter_switches()),
161
                         [('format', None, 'ARG', 'format help')])
162
        opt = option.RegistryOption('format', 'format help', registry,
163
                                    value_switches=True)
164
        self.assertEqual(list(opt.iter_switches()),
165
                         [('format', None, 'ARG', 'format help'),
166
                          ('default', None, None, 'one help'), 
167
                          ('one', None, None, 'one help'), 
168
                          ('two', None, None, 'two help'), 
169
                          ])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
170
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
171
#     >>> parse_args('log -r 500'.split())
172
#     (['log'], {'revision': [<RevisionSpec_int 500>]})
173
#     >>> parse_args('log -r500..600'.split())
174
#     (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
175
#     >>> parse_args('log -vr500..600'.split())
176
#     (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
177
#     >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
178
#     (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})