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