~bzr-pqm/bzr/bzr.dev

1616.1.13 by Martin Pool
Fix 'bzr -h' to show help (#35940)
1
# Copyright (C) 2005, 2006 Canonical Ltd
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
2
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
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
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
7
from bzrlib.tests import TestCase
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
8
1616.1.13 by Martin Pool
Fix 'bzr -h' to show help (#35940)
9
# TODO: might be nice to just parse them into a structured form and test
10
# against that, rather than running the whole command.
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
11
12
class OptionTests(TestCase):
13
    """Command-line option tests"""
14
15
    def test_parse_args(self):
16
        """Option parser"""
17
        eq = self.assertEquals
18
        eq(parse_args(cmd_commit(), ['--help']),
19
           ([], {'help': True}))
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)}))
24
1185.16.49 by mbp at sourcefrog
- more refactoring and tests of commandline
25
    def test_no_more_opts(self):
26
        """Terminated options"""
27
        self.assertEquals(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
28
                          (['-file-with-dashes'], {}))
29
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
30
    def test_option_help(self):
31
        """Options have help strings."""
32
        out, err = self.run_bzr_captured(['commit', '--help'])
1857.1.12 by Aaron Bentley
Fix a bunch of test cases that assumed --merge-type or log-format
33
        self.assertContainsRe(out, r'--file(.|\n)*file containing commit'
34
                                   ' message')
35
        self.assertContainsRe(out, r'-h.*--help')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
36
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')
41
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')
47
1185.35.24 by Aaron Bentley
Fixed handling of short options not accepted by the command
48
    def test_unknown_short_opt(self):
49
        out, err = self.run_bzr_captured(['help', '-r'], retcode=3)
1857.1.1 by Aaron Bentley
Use optparse for parsing options
50
        self.assertContainsRe(err, r'no such option')
1185.35.24 by Aaron Bentley
Fixed handling of short options not accepted by the command
51
1852.1.1 by John Arbash Meinel
Allow a plain '-' to be supplied as an argument. bug #50984
52
    def test_allow_dash(self):
53
        """Test that we can pass a plain '-' as an argument."""
54
        self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
55
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
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'])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
62
        self.assertEqual(True, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
63
        opts, args = parse(options, [])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
64
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
65
        opts, args = parse(options, ['--hello', '--no-hello'])
1857.1.22 by Aaron Bentley
Negations set value to False, instead of Optparser.DEFAULT_VALUE
66
        self.assertEqual(False, opts.hello)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
67
        options = [option.Option('number', type=int)]
68
        opts, args = parse(options, ['--number', '6'])
1857.1.16 by Aaron Bentley
Add tests for iter_switches
69
        self.assertEqual(6, opts.number)
1857.1.15 by Aaron Bentley
Add tests for generating an option parser
70
        self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
71
        self.assertRaises(errors.BzrCommandError, parse, options, 
72
                          ['--no-number'])
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
73
1857.1.16 by Aaron Bentley
Add tests for iter_switches
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')])
84
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
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>]})