/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
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
21
from ...sixish import PY3
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
22
1685.1.78 by Wouter van Heyst
more code cleanup
23
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
24
class cmd_echo_exact(Command):
25
    """This command just repeats what it is given.
26
27
    It decodes the argument, and then writes it to stdout.
28
    """
29
30
    takes_args = ['text']
31
    encoding_type = 'exact'
32
33
    def run(self, text=None):
34
        self.outf.write(text)
35
36
37
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
38
    """Raise a UnicodeError for unrepresentable characters."""
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
39
40
    encoding_type = 'strict'
41
42
43
class cmd_echo_replace(cmd_echo_exact):
44
    """Replace bogus unicode characters."""
45
46
    encoding_type = 'replace'
47
48
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
49
class TestCommandEncoding(TestCaseWithMemoryTransport):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
50
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
51
    def test_exact(self):
52
        def bzr(*args, **kwargs):
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
53
            kwargs['encoding'] = 'ascii'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
54
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
55
56
        register_command(cmd_echo_exact)
57
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
58
            self.assertEqual(b'foo', bzr('echo-exact foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
59
            # 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
60
            self.assertRaises(UnicodeEncodeError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
61
                              bzr,
62
                              ['echo-exact', u'foo\xb5'])
5785.1.4 by Martin
Remove testing that expected argv decoding to be done outside run_bzr
63
            # Previously a non-ascii bytestring was also tested, as 'exact'
64
            # outputs bytes untouched, but needed buggy argv parsing to work
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
65
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
66
            plugin_cmds.remove('echo-exact')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
67
68
    def test_strict_utf8(self):
69
        def bzr(*args, **kwargs):
70
            kwargs['encoding'] = 'utf-8'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
71
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
72
73
        register_command(cmd_echo_strict)
74
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
75
            self.assertEqual(b'foo', bzr('echo-strict foo'))
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
76
            expected = u'foo\xb5'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
77
            expected = expected.encode('utf-8')
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
78
            self.assertEqual(expected,
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
79
                             bzr(['echo-strict', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
80
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
81
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
82
83
    def test_strict_ascii(self):
84
        def bzr(*args, **kwargs):
85
            kwargs['encoding'] = 'ascii'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
86
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
87
88
        register_command(cmd_echo_strict)
89
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
90
            self.assertEqual(b'foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
91
            # 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
92
            self.assertRaises(UnicodeEncodeError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
93
                              bzr,
94
                              ['echo-strict', u'foo\xb5'])
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
95
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
96
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
97
98
    def test_replace_utf8(self):
99
        def bzr(*args, **kwargs):
100
            kwargs['encoding'] = 'utf-8'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
101
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
102
103
        register_command(cmd_echo_replace)
104
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
105
            self.assertEqual(b'foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
106
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
107
                             bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
108
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
109
            plugin_cmds.remove('echo-replace')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
110
111
    def test_replace_ascii(self):
112
        def bzr(*args, **kwargs):
113
            kwargs['encoding'] = 'ascii'
7065.3.6 by Jelmer Vernooij
Fix some more tests.
114
            return self.run_bzr_raw(*args, **kwargs)[0]
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
115
116
        register_command(cmd_echo_replace)
117
        try:
7065.3.6 by Jelmer Vernooij
Fix some more tests.
118
            self.assertEqual(b'foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
119
            # ascii can't encode \xb5
7065.3.6 by Jelmer Vernooij
Fix some more tests.
120
            self.assertEqual(b'foo?', bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
121
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
122
            plugin_cmds.remove('echo-replace')