~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2006-2011 Canonical Ltd
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
16
17
"""Tests for the osutils wrapper."""
18
19
import codecs
2192.1.9 by Alexander Belchenko
final fix suggested by John Meinel
20
import locale
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
21
import sys
22
23
from bzrlib import (
24
    osutils,
25
    )
26
from bzrlib.tests import (
27
        StringIOWrapper,
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
28
        TestCase,
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
29
        )
30
31
32
class FakeCodec(object):
2192.1.8 by Alexander Belchenko
Rework tests after John's review
33
    """Special class that helps testing over several non-existed encodings.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
34
2192.1.8 by Alexander Belchenko
Rework tests after John's review
35
    Clients can add new encoding names, but because of how codecs is
36
    implemented they cannot be removed. Be careful with naming to avoid
37
    collisions between tests.
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
38
    """
39
    _registered = False
40
    _enabled_encodings = set()
41
42
    def add(self, encoding_name):
2192.1.8 by Alexander Belchenko
Rework tests after John's review
43
        """Adding encoding name to fake.
44
45
        :type   encoding_name:  lowercase plain string
46
        """
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
47
        if not self._registered:
48
            codecs.register(self)
49
            self._registered = True
2192.1.8 by Alexander Belchenko
Rework tests after John's review
50
        if encoding_name is not None:
51
            self._enabled_encodings.add(encoding_name)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
52
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
53
    def __call__(self, encoding_name):
54
        """Called indirectly by codecs module during lookup"""
55
        if encoding_name in self._enabled_encodings:
56
            return codecs.lookup('latin-1')
2192.1.6 by Alexander Belchenko
Fixes after Aaron's review
57
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
58
59
fake_codec = FakeCodec()
60
61
62
class TestFakeCodec(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
63
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
64
    def test_fake_codec(self):
65
        self.assertRaises(LookupError, codecs.lookup, 'fake')
66
67
        fake_codec.add('fake')
68
        codecs.lookup('fake')
69
70
71
class TestTerminalEncoding(TestCase):
72
    """Test the auto-detection of proper terminal encoding."""
73
74
    def setUp(self):
4153.1.2 by Andrew Bennetts
Add missing TestCase.setUp upcalls.
75
        TestCase.setUp(self)
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
76
        self.overrideAttr(sys, 'stdin')
77
        self.overrideAttr(sys, 'stdout')
78
        self.overrideAttr(sys, 'stderr')
79
        self.overrideAttr(osutils, '_cached_user_encoding')
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
80
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
81
    def make_wrapped_streams(self,
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
82
                             stdout_encoding,
83
                             stderr_encoding,
84
                             stdin_encoding,
85
                             user_encoding='user_encoding',
86
                             enable_fake_encodings=True):
87
        sys.stdout = StringIOWrapper()
88
        sys.stdout.encoding = stdout_encoding
89
        sys.stderr = StringIOWrapper()
90
        sys.stderr.encoding = stderr_encoding
91
        sys.stdin = StringIOWrapper()
92
        sys.stdin.encoding = stdin_encoding
3224.5.4 by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding.
93
        osutils._cached_user_encoding = user_encoding
2192.1.8 by Alexander Belchenko
Rework tests after John's review
94
        if enable_fake_encodings:
95
            fake_codec.add(stdout_encoding)
96
            fake_codec.add(stderr_encoding)
97
            fake_codec.add(stdin_encoding)
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
98
99
    def test_get_terminal_encoding(self):
100
        self.make_wrapped_streams('stdout_encoding',
101
                                  'stderr_encoding',
102
                                  'stdin_encoding')
103
104
        # first preference is stdout encoding
105
        self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
106
107
        sys.stdout.encoding = None
108
        # if sys.stdout is None, fall back to sys.stdin
109
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
110
111
        sys.stdin.encoding = None
3224.5.4 by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding.
112
        # and in the worst case, use osutils.get_user_encoding()
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
113
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
114
5320.2.4 by Robert Collins
``bzrlib.osutils.get_terminal_encoding`` will now only mutter its
115
    def test_get_terminal_encoding_silent(self):
116
        self.make_wrapped_streams('stdout_encoding',
117
                                  'stderr_encoding',
118
                                  'stdin_encoding')
119
        # Calling get_terminal_encoding should not mutter when silent=True is
120
        # passed.
121
        log = self.get_log()
122
        osutils.get_terminal_encoding()
123
        self.assertEqual(log, self.get_log())
124
125
    def test_get_terminal_encoding_trace(self):
126
        self.make_wrapped_streams('stdout_encoding',
127
                                  'stderr_encoding',
128
                                  'stdin_encoding')
129
        # Calling get_terminal_encoding should not mutter when silent=True is
130
        # passed.
131
        log = self.get_log()
132
        osutils.get_terminal_encoding(trace=True)
133
        self.assertNotEqual(log, self.get_log())
134
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
135
    def test_terminal_cp0(self):
2192.1.6 by Alexander Belchenko
Fixes after Aaron's review
136
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
137
        self.make_wrapped_streams('cp0',
2192.1.8 by Alexander Belchenko
Rework tests after John's review
138
                                  'cp0',
139
                                  'cp0',
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
140
                                  user_encoding='latin-1',
141
                                  enable_fake_encodings=False)
142
143
        # cp0 is invalid encoding. We should fall back to user_encoding
144
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
145
146
        # check stderr
147
        self.assertEquals('', sys.stderr.getvalue())
148
2192.1.8 by Alexander Belchenko
Rework tests after John's review
149
    def test_terminal_cp_unknown(self):
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
150
        # test against really unknown encoding
151
        # catch warning at stderr
2192.1.8 by Alexander Belchenko
Rework tests after John's review
152
        self.make_wrapped_streams('cp-unknown',
153
                                  'cp-unknown',
154
                                  'cp-unknown',
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
155
                                  user_encoding='latin-1',
156
                                  enable_fake_encodings=False)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
157
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
158
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
159
160
        # check stderr
2192.1.10 by Alexander Belchenko
merge bzr.dev
161
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
162
                          '  Using encoding latin-1 instead.\n',
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
163
                          sys.stderr.getvalue())
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
164
165
166
class TestUserEncoding(TestCase):
167
    """Test detection of default user encoding."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
168
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
169
    def setUp(self):
4153.1.2 by Andrew Bennetts
Add missing TestCase.setUp upcalls.
170
        TestCase.setUp(self)
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
171
        self.overrideAttr(locale, 'getpreferredencoding')
172
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
173
174
    def test_get_user_encoding(self):
175
        def f():
176
            return 'user_encoding'
177
178
        locale.getpreferredencoding = f
179
        fake_codec.add('user_encoding')
5570.3.12 by Vincent Ladeuil
Replace osutils.set_or_unset_env calls with self.overrideEnv.
180
        self.assertEquals('user_encoding',
181
                          osutils.get_user_encoding(use_cache=False))
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
182
        self.assertEquals('', sys.stderr.getvalue())
183
184
    def test_user_cp0(self):
185
        def f():
186
            return 'cp0'
187
188
        locale.getpreferredencoding = f
189
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
190
        self.assertEquals('', sys.stderr.getvalue())
191
2192.1.8 by Alexander Belchenko
Rework tests after John's review
192
    def test_user_cp_unknown(self):
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
193
        def f():
2192.1.8 by Alexander Belchenko
Rework tests after John's review
194
            return 'cp-unknown'
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
195
196
        locale.getpreferredencoding = f
197
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
2192.1.8 by Alexander Belchenko
Rework tests after John's review
198
        self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
199
                          ' Continuing with ascii encoding.\n',
200
                          sys.stderr.getvalue())
2192.1.7 by Alexander Belchenko
get_user_encoding: if locale.Error raised we need to set user_encoding to 'ascii' as warning says
201
3405.3.1 by Neil Martinsen-Burrell
accept for an encoding to mean ascii
202
    def test_user_empty(self):
203
        """Running bzr from a vim script gives '' for a preferred locale"""
204
        def f():
205
            return ''
206
207
        locale.getpreferredencoding = f
208
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
209
        self.assertEquals('', sys.stderr.getvalue())
210
2192.1.7 by Alexander Belchenko
get_user_encoding: if locale.Error raised we need to set user_encoding to 'ascii' as warning says
211
    def test_user_locale_error(self):
212
        def f():
213
            raise locale.Error, 'unsupported locale'
214
215
        locale.getpreferredencoding = f
5570.3.9 by Vincent Ladeuil
More use cases for overrideEnv, _cleanEnvironment *may* contain too much variables now.
216
        self.overrideEnv('LANG', 'BOGUS')
2192.1.7 by Alexander Belchenko
get_user_encoding: if locale.Error raised we need to set user_encoding to 'ascii' as warning says
217
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
218
        self.assertEquals('bzr: warning: unsupported locale\n'
219
                          '  Could not determine what text encoding to use.\n'
220
                          '  This error usually means your Python interpreter\n'
221
                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
222
                          '  Continuing with ascii encoding.\n',
223
                          sys.stderr.getvalue())