1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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
3
from bzrlib.selftest import TestCase
17
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
4
18
from bzrlib.commands import Command, parse_args
5
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
19
from bzrlib import errors
20
from bzrlib import option
21
from bzrlib.tests import TestCase
23
# TODO: might be nice to just parse them into a structured form and test
24
# against that, rather than running the whole command.
8
26
class OptionTests(TestCase):
9
27
"""Command-line option tests"""
13
31
eq = self.assertEquals
14
32
eq(parse_args(cmd_commit(), ['--help']),
15
33
([], {'help': True}))
16
eq(parse_args(cmd_status(), ['--all']),
18
34
eq(parse_args(cmd_commit(), ['--message=biter']),
19
35
([], {'message': 'biter'}))
20
36
## eq(parse_args(cmd_log(), '-r 500'.split()),
28
44
def test_option_help(self):
29
45
"""Options have help strings."""
30
46
out, err = self.run_bzr_captured(['commit', '--help'])
31
self.assertContainsRe(out, r'--file.*file containing commit message')
47
self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
49
self.assertContainsRe(out, r'-h.*--help')
33
51
def test_option_help_global(self):
34
52
"""Global options have help strings."""
41
59
self.assertEquals(err, '')
42
60
self.assertContainsRe(out, r'--file[ =]MSGFILE')
62
def test_unknown_short_opt(self):
63
out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
64
self.assertContainsRe(err, r'no such option')
66
def test_allow_dash(self):
67
"""Test that we can pass a plain '-' as an argument."""
68
self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
70
def test_conversion(self):
71
def parse(options, args):
72
parser = option.get_optparser(dict((o.name, o) for o in options))
73
return parser.parse_args(args)
74
options = [option.Option('hello')]
75
opts, args = parse(options, ['--no-hello', '--hello'])
76
self.assertEqual(True, opts.hello)
77
opts, args = parse(options, [])
78
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
79
opts, args = parse(options, ['--hello', '--no-hello'])
80
self.assertEqual(False, opts.hello)
81
options = [option.Option('number', type=int)]
82
opts, args = parse(options, ['--number', '6'])
83
self.assertEqual(6, opts.number)
84
self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
85
self.assertRaises(errors.BzrCommandError, parse, options,
88
def test_iter_switches(self):
89
opt = option.Option('hello', help='fg')
90
self.assertEqual(list(opt.iter_switches()),
91
[('hello', None, None, 'fg')])
92
opt = option.Option('hello', help='fg', type=int)
93
self.assertEqual(list(opt.iter_switches()),
94
[('hello', None, 'ARG', 'fg')])
95
opt = option.Option('hello', help='fg', type=int, argname='gar')
96
self.assertEqual(list(opt.iter_switches()),
97
[('hello', None, 'GAR', 'fg')])
45
99
# >>> parse_args('log -r 500'.split())
46
100
# (['log'], {'revision': [<RevisionSpec_int 500>]})