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
17
from bzrlib.builtins import cmd_commit, cmd_log, cmd_status
18
from bzrlib.commands import Command, parse_args
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.
26
class OptionTests(TestCase):
27
"""Command-line option tests"""
29
def test_parse_args(self):
31
eq = self.assertEquals
32
eq(parse_args(cmd_commit(), ['--help']),
34
eq(parse_args(cmd_commit(), ['--message=biter']),
35
([], {'message': 'biter'}))
36
## eq(parse_args(cmd_log(), '-r 500'.split()),
37
## ([], {'revision': RevisionSpec_int(500)}))
39
def test_no_more_opts(self):
40
"""Terminated options"""
41
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
42
(['-file-with-dashes'], {}))
44
def test_option_help(self):
45
"""Options have help strings."""
46
out, err = self.run_bzr_captured(['commit', '--help'])
47
self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
49
self.assertContainsRe(out, r'-h.*--help')
51
def test_option_help_global(self):
52
"""Global options have help strings."""
53
out, err = self.run_bzr_captured(['help', 'status'])
54
self.assertContainsRe(out, r'--show-ids.*show internal object')
56
def test_option_arg_help(self):
57
"""Help message shows option arguments."""
58
out, err = self.run_bzr_captured(['help', 'commit'])
59
self.assertEquals(err, '')
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')])
99
# >>> parse_args('log -r 500'.split())
100
# (['log'], {'revision': [<RevisionSpec_int 500>]})
101
# >>> parse_args('log -r500..600'.split())
102
# (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
103
# >>> parse_args('log -vr500..600'.split())
104
# (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
105
# >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
106
# (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})