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
7
from bzrlib.tests import TestCase
9
# TODO: might be nice to just parse them into a structured form and test
10
# against that, rather than running the whole command.
12
class OptionTests(TestCase):
13
"""Command-line option tests"""
15
def test_parse_args(self):
17
eq = self.assertEquals
18
eq(parse_args(cmd_commit(), ['--help']),
20
eq(parse_args(cmd_commit(), ['--message=biter']),
21
([], {'message': 'biter'}))
22
## eq(parse_args(cmd_log(), '-r 500'.split()),
23
## ([], {'revision': RevisionSpec_int(500)}))
25
def test_no_more_opts(self):
26
"""Terminated options"""
27
self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
28
(['-file-with-dashes'], {}))
30
def test_option_help(self):
31
"""Options have help strings."""
32
out, err = self.run_bzr_captured(['commit', '--help'])
33
self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
35
self.assertContainsRe(out, r'-h.*--help')
37
def test_option_help_global(self):
38
"""Global options have help strings."""
39
out, err = self.run_bzr_captured(['help', 'status'])
40
self.assertContainsRe(out, r'--show-ids.*show internal object')
42
def test_option_arg_help(self):
43
"""Help message shows option arguments."""
44
out, err = self.run_bzr_captured(['help', 'commit'])
45
self.assertEquals(err, '')
46
self.assertContainsRe(out, r'--file[ =]MSGFILE')
48
def test_unknown_short_opt(self):
49
out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
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')])
85
# >>> parse_args('log -r 500'.split())
86
# (['log'], {'revision': [<RevisionSpec_int 500>]})
87
# >>> parse_args('log -r500..600'.split())
88
# (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
89
# >>> parse_args('log -vr500..600'.split())
90
# (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
91
# >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
92
# (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})