~bzr-pqm/bzr/bzr.dev

5037.3.2 by Martin Pool
Fix up test_cmdline to actually work
1
# Copyright (C) 2010 Canonical Ltd
2
#
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.
7
#
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.
12
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18
from bzrlib import (
19
    cmdline,
5241.2.1 by Robert Collins
Merge up from 2.0/2.1:
20
    tests,
21
    )
22
from bzrlib.tests.features import backslashdir_feature
4913.5.20 by Gordon Tyler
Fixed end-of-lines in test_cmdline.py.
23
4913.5.23 by Gordon Tyler
Renamed cmdline.Parser to Splitter to better match its usage.
24
class TestSplitter(tests.TestCase):
4913.5.20 by Gordon Tyler
Fixed end-of-lines in test_cmdline.py.
25
26
    def assertAsTokens(self, expected, line, single_quotes_allowed=False):
4913.5.23 by Gordon Tyler
Renamed cmdline.Parser to Splitter to better match its usage.
27
        s = cmdline.Splitter(line, single_quotes_allowed=single_quotes_allowed)
4913.5.20 by Gordon Tyler
Fixed end-of-lines in test_cmdline.py.
28
        self.assertEqual(expected, list(s))
29
30
    def test_simple(self):
31
        self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
32
                            u'foo bar baz')
33
34
    def test_ignore_multiple_spaces(self):
35
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo  bar')
36
37
    def test_ignore_leading_space(self):
38
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'  foo bar')
39
40
    def test_ignore_trailing_space(self):
41
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar  ')
42
43
    def test_posix_quotations(self):
44
        self.assertAsTokens([(True, u'foo bar')], u"'foo bar'",
45
            single_quotes_allowed=True)
46
        self.assertAsTokens([(True, u'foo bar')], u"'fo''o b''ar'",
47
            single_quotes_allowed=True)
48
        self.assertAsTokens([(True, u'foo bar')], u'"fo""o b""ar"',
49
            single_quotes_allowed=True)
50
        self.assertAsTokens([(True, u'foo bar')], u'"fo"\'o b\'"ar"',
51
            single_quotes_allowed=True)
52
53
    def test_nested_quotations(self):
54
        self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
55
        self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
56
        self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"",
57
            single_quotes_allowed=True)
58
        self.assertAsTokens([(True, u'foo"" bar')], u"'foo\"\" bar'",
59
            single_quotes_allowed=True)
60
61
    def test_empty_result(self):
62
        self.assertAsTokens([], u'')
63
        self.assertAsTokens([], u'    ')
64
65
    def test_quoted_empty(self):
66
        self.assertAsTokens([(True, '')], u'""')
67
        self.assertAsTokens([(False, u"''")], u"''")
68
        self.assertAsTokens([(True, '')], u"''", single_quotes_allowed=True)
6571.2.1 by Ross Lagerwall
Handle empty quoted strings in command lines
69
        self.assertAsTokens([(False, u'a'), (True, u''), (False, u'c')],
70
                            u'a "" c')
71
        self.assertAsTokens([(False, u'a'), (True, u''), (False, u'c')],
72
                            u"a '' c", single_quotes_allowed=True)
4913.5.20 by Gordon Tyler
Fixed end-of-lines in test_cmdline.py.
73
74
    def test_unicode_chars(self):
75
        self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
76
                             u'f\xb5\xee \u1234\u3456')
77
78
    def test_newline_in_quoted_section(self):
79
        self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
80
        self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u"'foo\nbar\nbaz\n'",
81
            single_quotes_allowed=True)
82
83
    def test_escape_chars(self):
84
        self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
85
86
    def test_escape_quote(self):
87
        self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
88
        self.assertAsTokens([(True, u'foo\\"bar')], u'"foo\\\\\\"bar"')
89
        self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\"bar"')
90
91
    def test_double_escape(self):
92
        self.assertAsTokens([(True, u'foo\\\\bar')], u'"foo\\\\bar"')
93
        self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
94
4913.5.20 by Gordon Tyler
Fixed end-of-lines in test_cmdline.py.
95
    def test_multiple_quoted_args(self):
96
        self.assertAsTokens([(True, u'x x'), (True, u'y y')],
97
            u'"x x" "y y"')
98
        self.assertAsTokens([(True, u'x x'), (True, u'y y')],
99
            u'"x x" \'y y\'', single_quotes_allowed=True)
5241.2.1 by Robert Collins
Merge up from 2.0/2.1:
100
101
    def test_n_backslashes_handling(self):
102
        # https://bugs.launchpad.net/bzr/+bug/528944
103
        # actually we care about the doubled backslashes when they're
104
        # represents UNC paths.
105
        # But in fact there is too much weird corner cases
106
        # (see https://bugs.launchpad.net/tortoisebzr/+bug/569050)
107
        # so to reproduce every bit of windows command-line handling
108
        # could be not worth of efforts?
109
        self.requireFeature(backslashdir_feature)
110
        self.assertAsTokens([(True, r'\\host\path')], r'"\\host\path"')
111
        self.assertAsTokens([(False, r'\\host\path')], r'\\host\path')
112
        # handling of " after the 2n and 2n+1 backslashes
113
        # inside and outside the quoted string
114
        self.assertAsTokens([(True, r'\\'), (False, r'*.py')], r'"\\\\" *.py')
115
        self.assertAsTokens([(True, r'\\" *.py')], r'"\\\\\" *.py"')
116
        self.assertAsTokens([(True, r'\\ *.py')], r'\\\\" *.py"')
117
        self.assertAsTokens([(False, r'\\"'), (False, r'*.py')],
118
                            r'\\\\\" *.py')
119
        self.assertAsTokens([(True, u'\\\\')], u'"\\\\')