/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
1
# Copyright (C) 2005, 2007 Canonical Ltd
1685.1.78 by Wouter van Heyst
more code cleanup
2
#
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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.
1685.1.78 by Wouter van Heyst
more code cleanup
7
#
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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.
1685.1.78 by Wouter van Heyst
more code cleanup
12
#
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
16
1685.1.76 by Wouter van Heyst
codecleanup
17
"""Tests for the Command.encoding_type interface."""
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from .. import TestCaseWithMemoryTransport
20
from ...commands import Command, register_command, plugin_cmds
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
21
1685.1.78 by Wouter van Heyst
more code cleanup
22
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
23
class cmd_echo_exact(Command):
24
    """This command just repeats what it is given.
25
26
    It decodes the argument, and then writes it to stdout.
27
    """
28
29
    takes_args = ['text']
30
    encoding_type = 'exact'
31
32
    def run(self, text=None):
33
        self.outf.write(text)
34
35
36
class cmd_echo_strict(cmd_echo_exact):
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
37
    """Raise a UnicodeError for unrepresentable characters."""
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
38
39
    encoding_type = 'strict'
40
41
42
class cmd_echo_replace(cmd_echo_exact):
43
    """Replace bogus unicode characters."""
44
45
    encoding_type = 'replace'
46
47
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
48
class TestCommandEncoding(TestCaseWithMemoryTransport):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
49
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
50
    def test_exact(self):
51
        def bzr(*args, **kwargs):
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
52
            kwargs['encoding'] = 'ascii'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
53
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
54
55
        register_command(cmd_echo_exact)
56
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
57
            self.assertEqual(b'foo', bzr('echo-exact foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
58
            # Exact should fail to decode the string
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
59
            self.assertRaises(UnicodeEncodeError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
                              bzr,
61
                              ['echo-exact', u'foo\xb5'])
5785.1.4 by Martin
Remove testing that expected argv decoding to be done outside run_bzr
62
            # Previously a non-ascii bytestring was also tested, as 'exact'
63
            # outputs bytes untouched, but needed buggy argv parsing to work
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
64
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
65
            plugin_cmds.remove('echo-exact')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
66
67
    def test_strict_utf8(self):
68
        def bzr(*args, **kwargs):
69
            kwargs['encoding'] = 'utf-8'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
70
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
71
72
        register_command(cmd_echo_strict)
73
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
74
            self.assertEqual(b'foo', bzr('echo-strict foo'))
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
75
            expected = u'foo\xb5'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
76
            expected = expected.encode('utf-8')
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
77
            self.assertEqual(expected,
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
78
                             bzr(['echo-strict', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
79
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
80
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
81
82
    def test_strict_ascii(self):
83
        def bzr(*args, **kwargs):
84
            kwargs['encoding'] = 'ascii'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
85
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
86
87
        register_command(cmd_echo_strict)
88
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
89
            self.assertEqual(b'foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
90
            # ascii can't encode \xb5
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
91
            self.assertRaises(UnicodeEncodeError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
92
                              bzr,
93
                              ['echo-strict', u'foo\xb5'])
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
94
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
95
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
96
97
    def test_replace_utf8(self):
98
        def bzr(*args, **kwargs):
99
            kwargs['encoding'] = 'utf-8'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
100
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
101
102
        register_command(cmd_echo_replace)
103
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
104
            self.assertEqual(b'foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
105
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
106
                             bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
107
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
108
            plugin_cmds.remove('echo-replace')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
109
110
    def test_replace_ascii(self):
111
        def bzr(*args, **kwargs):
112
            kwargs['encoding'] = 'ascii'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
113
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
114
115
        register_command(cmd_echo_replace)
116
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
117
            self.assertEqual(b'foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
118
            # ascii can't encode \xb5
7065.3.6 by Jelmer Vernooij
Fix some more tests.
119
            self.assertEqual(b'foo?', bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
120
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
121
            plugin_cmds.remove('echo-replace')