/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2227.1.3 by mbp at sourcefrog
Restore access to SHORT_OPTIONS for compatibility
1
# Copyright (C) 2005, 2006, 2007 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
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
17
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
18
from bzrlib.commands import Command, parse_args
2227.1.3 by mbp at sourcefrog
Restore access to SHORT_OPTIONS for compatibility
19
from bzrlib import (
20
        errors,
21
        option,
22
        symbol_versioning,
23
        )
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
24
from bzrlib.tests import TestCase
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
25
2227.1.4 by mbp at sourcefrog
add test that legacy SHORT_OPTIONS really works, and set_short_name
26
def parse(options, args):
27
    parser = option.get_optparser(dict((o.name, o) for o in options))
28
    return parser.parse_args(args)
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
29
30
class OptionTests(TestCase):
31
    """Command-line option tests"""
32
33
    def test_parse_args(self):
34
        """Option parser"""
35
        eq = self.assertEquals
36
        eq(parse_args(cmd_commit(), ['--help']),
37
           ([], {'help': True}))
38
        eq(parse_args(cmd_commit(), ['--message=biter']),
39
           ([], {'message': 'biter'}))
40
        ## eq(parse_args(cmd_log(),  '-r 500'.split()),
41
        ##   ([], {'revision': RevisionSpec_int(500)}))
42
1185.16.49 by mbp at sourcefrog
- more refactoring and tests of commandline
43
    def test_no_more_opts(self):
44
        """Terminated options"""
45
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
46
                          (['-file-with-dashes'], {}))
47
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
48
    def test_option_help(self):
49
        """Options have help strings."""
50
        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
51
        self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
52
                                   ' message')
53
        self.assertContainsRe(out, r'-h.*--help')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
54
55
    def test_option_help_global(self):
56
        """Global options have help strings."""
57
        out, err = self.run_bzr_captured(['help', 'status'])
58
        self.assertContainsRe(out, r'--show-ids.*show internal object')
59
60
    def test_option_arg_help(self):
61
        """Help message shows option arguments."""
62
        out, err = self.run_bzr_captured(['help', 'commit'])
63
        self.assertEquals(err, '')
64
        self.assertContainsRe(out, r'--file[ =]MSGFILE')
65
1185.35.24 by Aaron Bentley
Fixed handling of short options not accepted by the command
66
    def test_unknown_short_opt(self):
67
        out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
1857.1.1 by Aaron Bentley
Use optparse for parsing options
68
        self.assertContainsRe(err, r'no such option')
1185.35.24 by Aaron Bentley
Fixed handling of short options not accepted by the command
69
2190.2.1 by Martin Pool
remove global registration of short options
70
    def test_get_short_name(self):
71
        file_opt = option.Option.OPTIONS['file']
2227.1.1 by mbp at sourcefrog
Back out previous incompatible change: Option.short_name is now again
72
        self.assertEquals(file_opt.short_name(), 'F')
2190.2.1 by Martin Pool
remove global registration of short options
73
        force_opt = option.Option.OPTIONS['force']
2227.1.1 by mbp at sourcefrog
Back out previous incompatible change: Option.short_name is now again
74
        self.assertEquals(force_opt.short_name(), None)
2190.2.1 by Martin Pool
remove global registration of short options
75
2227.1.4 by mbp at sourcefrog
add test that legacy SHORT_OPTIONS really works, and set_short_name
76
    def test_set_short_name(self):
77
        o = option.Option('wiggle')
78
        o.set_short_name('w')
79
        self.assertEqual(o.short_name(), 'w')
80
2227.1.3 by mbp at sourcefrog
Restore access to SHORT_OPTIONS for compatibility
81
    def test_old_short_names(self):
82
        # test the deprecated method for getting and setting short option
83
        # names
84
        expected_warning = (
85
            "access to SHORT_OPTIONS was deprecated in version 0.14."
86
            " Set the short option name when constructing the Option.",
87
            DeprecationWarning, 2)
88
        _warnings = []
89
        def capture_warning(message, category, stacklevel=None):
90
            _warnings.append((message, category, stacklevel))
91
        old_warning_method = symbol_versioning.warn
92
        try:
93
            # an example of the kind of thing plugins might want to do through
94
            # the old interface - make a new option and then give it a short
95
            # name.
96
            symbol_versioning.set_warning_method(capture_warning)
2227.1.4 by mbp at sourcefrog
add test that legacy SHORT_OPTIONS really works, and set_short_name
97
            example_opt = option.Option('example', help='example option')
98
            option.Option.SHORT_OPTIONS['w'] = example_opt
99
            self.assertEqual(example_opt.short_name(), 'w')
2227.1.3 by mbp at sourcefrog
Restore access to SHORT_OPTIONS for compatibility
100
            self.assertEqual([expected_warning], _warnings)
2227.1.4 by mbp at sourcefrog
add test that legacy SHORT_OPTIONS really works, and set_short_name
101
            # now check that it can actually be parsed with the registered
102
            # value
103
            opts, args = parse([example_opt], ['-w', 'foo'])
104
            self.assertEqual(opts.example, True)
105
            self.assertEqual(args, ['foo'])
2227.1.3 by mbp at sourcefrog
Restore access to SHORT_OPTIONS for compatibility
106
        finally:
107
            symbol_versioning.set_warning_method(old_warning_method)
108
1852.1.1 by John Arbash Meinel
Allow a plain '-' to be supplied as an argument. bug #50984
109
    def test_allow_dash(self):
110
        """Test that we can pass a plain '-' as an argument."""
111
        self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
112
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
113
    def test_conversion(self):
114
        options = [option.Option('hello')]
115
        opts, args = parse(options, ['--no-hello', '--hello'])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
116
        self.assertEqual(True, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
117
        opts, args = parse(options, [])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
118
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
119
        opts, args = parse(options, ['--hello', '--no-hello'])
1857.1.22 by Aaron Bentley
Negations set value to False, instead of Optparser.DEFAULT_VALUE
120
        self.assertEqual(False, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
121
        options = [option.Option('number', type=int)]
122
        opts, args = parse(options, ['--number', '6'])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
123
        self.assertEqual(6, opts.number)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
124
        self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
125
        self.assertRaises(errors.BzrCommandError, parse, options, 
126
                          ['--no-number'])
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
127
1857.1.16 by Aaron Bentley
Add tests for iter_switches
128
    def test_iter_switches(self):
129
        opt = option.Option('hello', help='fg')
130
        self.assertEqual(list(opt.iter_switches()),
131
                         [('hello', None, None, 'fg')])
132
        opt = option.Option('hello', help='fg', type=int)
133
        self.assertEqual(list(opt.iter_switches()),
134
                         [('hello', None, 'ARG', 'fg')])
135
        opt = option.Option('hello', help='fg', type=int, argname='gar')
136
        self.assertEqual(list(opt.iter_switches()),
137
                         [('hello', None, 'GAR', 'fg')])
138
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
139
#     >>> parse_args('log -r 500'.split())
140
#     (['log'], {'revision': [<RevisionSpec_int 500>]})
141
#     >>> parse_args('log -r500..600'.split())
142
#     (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
143
#     >>> parse_args('log -vr500..600'.split())
144
#     (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
145
#     >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
146
#     (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})