172
170
TestCase.setUp(self)
173
self.overrideAttr(locale, 'getpreferredencoding')
171
self.overrideAttr(osutils, '_cached_user_encoding', None)
172
self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
173
self.overrideAttr(locale, 'CODESET', None)
174
174
self.overrideAttr(sys, 'stderr', StringIOWrapper())
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
176
179
def test_get_user_encoding(self):
178
return 'user_encoding'
180
locale.getpreferredencoding = f
180
self._encoding = 'user_encoding'
181
181
fake_codec.add('user_encoding')
182
self.assertEquals('user_encoding',
183
osutils.get_user_encoding(use_cache=False))
182
self.assertEquals('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
184
184
self.assertEquals('', sys.stderr.getvalue())
186
186
def test_user_cp0(self):
190
locale.getpreferredencoding = f
191
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
187
self._encoding = 'cp0'
188
self.assertEquals('ascii', osutils.get_user_encoding())
192
189
self.assertEquals('', sys.stderr.getvalue())
194
191
def test_user_cp_unknown(self):
198
locale.getpreferredencoding = f
199
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
192
self._encoding = 'cp-unknown'
193
self.assertEquals('ascii', osutils.get_user_encoding())
200
194
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
201
195
' Continuing with ascii encoding.\n',
202
196
sys.stderr.getvalue())
204
198
def test_user_empty(self):
205
199
"""Running bzr from a vim script gives '' for a preferred locale"""
209
locale.getpreferredencoding = f
210
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
201
self.assertEquals('ascii', osutils.get_user_encoding())
211
202
self.assertEquals('', sys.stderr.getvalue())
213
def test_user_locale_error(self):
215
raise locale.Error, 'unsupported locale'
217
locale.getpreferredencoding = f
218
self.overrideEnv('LANG', 'BOGUS')
219
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
220
self.assertEquals('bzr: warning: unsupported locale\n'
221
' Could not determine what text encoding to use.\n'
222
' This error usually means your Python interpreter\n'
223
' doesn\'t support the locale set by $LANG (BOGUS)\n'
224
' Continuing with ascii encoding.\n',
225
sys.stderr.getvalue())
228
class TestMessageEncoding(TestCase):
229
"""Tests for getting the encoding used by system messages"""
231
def test_get_message_encoding(self):
232
encoding_name = osutils.get_message_encoding()
233
"".decode(encoding_name) # should be a valid encoding name
235
def test_get_message_encoding_decodes_strerror(self):
236
encoding_name = osutils.get_message_encoding()
237
for number, name in errno.errorcode.iteritems():
238
string = os.strerror(number)
239
string.decode(encoding_name)