167
167
"""Test detection of default user encoding."""
171
self.overrideAttr(locale, 'getpreferredencoding')
170
super(TestUserEncoding, self).setUp()
171
self.overrideAttr(osutils, '_cached_user_encoding', None)
172
self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
173
self.overrideAttr(locale, 'CODESET', None)
172
174
self.overrideAttr(sys, 'stderr', StringIOWrapper())
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
174
179
def test_get_user_encoding(self):
176
return 'user_encoding'
178
locale.getpreferredencoding = f
180
self._encoding = 'user_encoding'
179
181
fake_codec.add('user_encoding')
180
self.assertEquals('user_encoding',
181
osutils.get_user_encoding(use_cache=False))
182
self.assertEquals('', sys.stderr.getvalue())
182
self.assertEqual('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
184
self.assertEqual('', sys.stderr.getvalue())
184
186
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())
187
self._encoding = 'cp0'
188
self.assertEqual('ascii', osutils.get_user_encoding())
189
self.assertEqual('', sys.stderr.getvalue())
192
191
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.'
192
self._encoding = 'cp-unknown'
193
self.assertEqual('ascii', osutils.get_user_encoding())
194
self.assertEqual('bzr: warning: unknown encoding cp-unknown.'
199
195
' Continuing with ascii encoding.\n',
200
196
sys.stderr.getvalue())
202
198
def test_user_empty(self):
203
199
"""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())
201
self.assertEqual('ascii', osutils.get_user_encoding())
202
self.assertEqual('', sys.stderr.getvalue())