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(osutils, '_cached_user_encoding', None)
172
self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
173
self.overrideAttr(locale, 'CODESET', None)
174
self.overrideAttr(sys, 'stderr', StringIOWrapper())
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
179
def test_get_user_encoding(self):
180
self._encoding = 'user_encoding'
181
fake_codec.add('user_encoding')
182
self.assertEquals('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
184
self.assertEquals('', sys.stderr.getvalue())
186
def test_user_cp0(self):
187
self._encoding = 'cp0'
188
self.assertEquals('ascii', osutils.get_user_encoding())
189
self.assertEquals('', sys.stderr.getvalue())
191
def test_user_cp_unknown(self):
192
self._encoding = 'cp-unknown'
193
self.assertEquals('ascii', osutils.get_user_encoding())
194
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
195
' Continuing with ascii encoding.\n',
196
sys.stderr.getvalue())
198
def test_user_empty(self):
199
"""Running bzr from a vim script gives '' for a preferred locale"""
201
self.assertEquals('ascii', osutils.get_user_encoding())
202
self.assertEquals('', sys.stderr.getvalue())