1
# Copyright (C) 2006-2011 Canonical Ltd
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.
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.
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
17
"""Tests for the osutils wrapper."""
26
from bzrlib.tests import (
32
class FakeCodec(object):
33
"""Special class that helps testing over several non-existed encodings.
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.
40
_enabled_encodings = set()
42
def add(self, encoding_name):
43
"""Adding encoding name to fake.
45
:type encoding_name: lowercase plain string
47
if not self._registered:
49
self._registered = True
50
if encoding_name is not None:
51
self._enabled_encodings.add(encoding_name)
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')
59
fake_codec = FakeCodec()
62
class TestFakeCodec(TestCase):
64
def test_fake_codec(self):
65
self.assertRaises(LookupError, codecs.lookup, 'fake')
67
fake_codec.add('fake')
71
class TestTerminalEncoding(TestCase):
72
"""Test the auto-detection of proper terminal encoding."""
76
self.overrideAttr(sys, 'stdin')
77
self.overrideAttr(sys, 'stdout')
78
self.overrideAttr(sys, 'stderr')
79
self.overrideAttr(osutils, '_cached_user_encoding')
81
def make_wrapped_streams(self,
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
93
osutils._cached_user_encoding = user_encoding
94
if enable_fake_encodings:
95
fake_codec.add(stdout_encoding)
96
fake_codec.add(stderr_encoding)
97
fake_codec.add(stdin_encoding)
99
def test_get_terminal_encoding(self):
100
self.make_wrapped_streams('stdout_encoding',
104
# first preference is stdout encoding
105
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
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())
111
sys.stdin.encoding = None
112
# and in the worst case, use osutils.get_user_encoding()
113
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
115
def test_get_terminal_encoding_silent(self):
116
self.make_wrapped_streams('stdout_encoding',
119
# Calling get_terminal_encoding should not mutter when silent=True is
122
osutils.get_terminal_encoding()
123
self.assertEqual(log, self.get_log())
125
def test_get_terminal_encoding_trace(self):
126
self.make_wrapped_streams('stdout_encoding',
129
# Calling get_terminal_encoding should not mutter when silent=True is
132
osutils.get_terminal_encoding(trace=True)
133
self.assertNotEqual(log, self.get_log())
135
def test_terminal_cp0(self):
136
# test cp0 encoding (Windows returns cp0 when there is no encoding)
137
self.make_wrapped_streams('cp0',
140
user_encoding='latin-1',
141
enable_fake_encodings=False)
143
# cp0 is invalid encoding. We should fall back to user_encoding
144
self.assertEqual('latin-1', osutils.get_terminal_encoding())
147
self.assertEquals('', sys.stderr.getvalue())
149
def test_terminal_cp_unknown(self):
150
# test against really unknown encoding
151
# catch warning at stderr
152
self.make_wrapped_streams('cp-unknown',
155
user_encoding='latin-1',
156
enable_fake_encodings=False)
158
self.assertEqual('latin-1', osutils.get_terminal_encoding())
161
self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
162
' Using encoding latin-1 instead.\n',
163
sys.stderr.getvalue())
166
class TestUserEncoding(TestCase):
167
"""Test detection of default user encoding."""
171
self.overrideAttr(locale, 'getpreferredencoding')
172
self.overrideAttr(sys, 'stderr', StringIOWrapper())
174
def test_get_user_encoding(self):
176
return 'user_encoding'
178
locale.getpreferredencoding = f
179
fake_codec.add('user_encoding')
180
self.assertEquals('user_encoding',
181
osutils.get_user_encoding(use_cache=False))
182
self.assertEquals('', sys.stderr.getvalue())
184
def test_user_cp0(self):
188
locale.getpreferredencoding = f
189
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
190
self.assertEquals('', sys.stderr.getvalue())
192
def test_user_cp_unknown(self):
196
locale.getpreferredencoding = f
197
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
198
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
199
' Continuing with ascii encoding.\n',
200
sys.stderr.getvalue())
202
def test_user_empty(self):
203
"""Running bzr from a vim script gives '' for a preferred locale"""
207
locale.getpreferredencoding = f
208
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
209
self.assertEquals('', sys.stderr.getvalue())
211
def test_user_locale_error(self):
213
raise locale.Error, 'unsupported locale'
215
locale.getpreferredencoding = f
216
self.overrideEnv('LANG', 'BOGUS')
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())