/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'
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
54
            return self.run_bzr(*args, **kwargs)[0]
55
56
        register_command(cmd_echo_exact)
57
        try:
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
58
            self.assertEqual('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,
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'
71
            return self.run_bzr(*args, **kwargs)[0]
72
73
        register_command(cmd_echo_strict)
74
        try:
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
75
            self.assertEqual('foo', bzr('echo-strict foo'))
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
76
            expected = u'foo\xb5'
77
            if not PY3:
7045.1.4 by Jelmer Vernooij
Fix test.
78
                expected = expected.encode('utf-8')
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
79
            self.assertEqual(expected,
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
80
                             bzr(['echo-strict', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
81
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
82
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
83
84
    def test_strict_ascii(self):
85
        def bzr(*args, **kwargs):
86
            kwargs['encoding'] = 'ascii'
87
            return self.run_bzr(*args, **kwargs)[0]
88
89
        register_command(cmd_echo_strict)
90
        try:
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
91
            self.assertEqual('foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
92
            # 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
93
            self.assertRaises(UnicodeEncodeError,
94
                bzr,
95
                ['echo-strict', u'foo\xb5'])
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
96
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
97
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
98
99
    def test_replace_utf8(self):
100
        def bzr(*args, **kwargs):
101
            kwargs['encoding'] = 'utf-8'
102
            return self.run_bzr(*args, **kwargs)[0]
103
104
        register_command(cmd_echo_replace)
105
        try:
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
106
            self.assertEqual('foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
107
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
108
                             bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
109
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
110
            plugin_cmds.remove('echo-replace')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
111
112
    def test_replace_ascii(self):
113
        def bzr(*args, **kwargs):
114
            kwargs['encoding'] = 'ascii'
115
            return self.run_bzr(*args, **kwargs)[0]
116
117
        register_command(cmd_echo_replace)
118
        try:
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
119
            self.assertEqual('foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
120
            # ascii can't encode \xb5
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
121
            self.assertEqual('foo?', bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
122
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
123
            plugin_cmds.remove('echo-replace')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
124
125