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_get_short_name(self):
67
file_opt = option.Option.OPTIONS['file']
68
self.assertEquals(file_opt.short_name, 'F')
69
force_opt = option.Option.OPTIONS['force']
70
self.assertEquals(force_opt.short_name, None)
72
def test_allow_dash(self):
73
"""Test that we can pass a plain '-' as an argument."""
74
self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
76
def test_conversion(self):
77
def parse(options, args):
78
parser = option.get_optparser(dict((o.name, o) for o in options))
79
return parser.parse_args(args)
80
options = [option.Option('hello')]
81
opts, args = parse(options, ['--no-hello', '--hello'])
82
self.assertEqual(True, opts.hello)
83
opts, args = parse(options, [])
84
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
85
opts, args = parse(options, ['--hello', '--no-hello'])
86
self.assertEqual(False, opts.hello)
87
options = [option.Option('number', type=int)]
88
opts, args = parse(options, ['--number', '6'])
89
self.assertEqual(6, opts.number)
90
self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
91
self.assertRaises(errors.BzrCommandError, parse, options,
94
def test_iter_switches(self):
95
opt = option.Option('hello', help='fg')
96
self.assertEqual(list(opt.iter_switches()),
97
[('hello', None, None, 'fg')])
98
opt = option.Option('hello', help='fg', type=int)
99
self.assertEqual(list(opt.iter_switches()),
100
[('hello', None, 'ARG', 'fg')])
101
opt = option.Option('hello', help='fg', type=int, argname='gar')
102
self.assertEqual(list(opt.iter_switches()),
103
[('hello', None, 'GAR', 'fg')])
105
# >>> parse_args('log -r 500'.split())
106
# (['log'], {'revision': [<RevisionSpec_int 500>]})
107
# >>> parse_args('log -r500..600'.split())
108
# (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
109
# >>> parse_args('log -vr500..600'.split())
110
# (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
111
# >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
112
# (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})