~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)
69
70
    def test_unicode_chars(self):
71
        self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
72
                             u'f\xb5\xee \u1234\u3456')
73
74
    def test_newline_in_quoted_section(self):
75
        self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
76
        self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u"'foo\nbar\nbaz\n'",
77
            single_quotes_allowed=True)
78
79
    def test_escape_chars(self):
80
        self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
81
82
    def test_escape_quote(self):
83
        self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
84
        self.assertAsTokens([(True, u'foo\\"bar')], u'"foo\\\\\\"bar"')
85
        self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\"bar"')
86
87
    def test_double_escape(self):
88
        self.assertAsTokens([(True, u'foo\\\\bar')], u'"foo\\\\bar"')
89
        self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
90
4913.5.20 by Gordon Tyler
Fixed end-of-lines in test_cmdline.py.
91
    def test_multiple_quoted_args(self):
92
        self.assertAsTokens([(True, u'x x'), (True, u'y y')],
93
            u'"x x" "y y"')
94
        self.assertAsTokens([(True, u'x x'), (True, u'y y')],
95
            u'"x x" \'y y\'', single_quotes_allowed=True)
5241.2.1 by Robert Collins
Merge up from 2.0/2.1:
96
97
    def test_n_backslashes_handling(self):
98
        # https://bugs.launchpad.net/bzr/+bug/528944
99
        # actually we care about the doubled backslashes when they're
100
        # represents UNC paths.
101
        # But in fact there is too much weird corner cases
102
        # (see https://bugs.launchpad.net/tortoisebzr/+bug/569050)
103
        # so to reproduce every bit of windows command-line handling
104
        # could be not worth of efforts?
105
        self.requireFeature(backslashdir_feature)
106
        self.assertAsTokens([(True, r'\\host\path')], r'"\\host\path"')
107
        self.assertAsTokens([(False, r'\\host\path')], r'\\host\path')
108
        # handling of " after the 2n and 2n+1 backslashes
109
        # inside and outside the quoted string
110
        self.assertAsTokens([(True, r'\\'), (False, r'*.py')], r'"\\\\" *.py')
111
        self.assertAsTokens([(True, r'\\" *.py')], r'"\\\\\" *.py"')
112
        self.assertAsTokens([(True, r'\\ *.py')], r'\\\\" *.py"')
113
        self.assertAsTokens([(False, r'\\"'), (False, r'*.py')],
114
                            r'\\\\\" *.py')
115
        self.assertAsTokens([(True, u'\\\\')], u'"\\\\')