169
167
"""Test detection of default user encoding."""
173
self.overrideAttr(locale, 'getpreferredencoding')
174
self.addCleanup(osutils.set_or_unset_env,
175
'LANG', os.environ.get('LANG'))
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)
176
174
self.overrideAttr(sys, 'stderr', StringIOWrapper())
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
178
179
def test_get_user_encoding(self):
180
return 'user_encoding'
182
locale.getpreferredencoding = f
180
self._encoding = 'user_encoding'
183
181
fake_codec.add('user_encoding')
184
self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
182
self.assertEquals('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
185
184
self.assertEquals('', sys.stderr.getvalue())
187
186
def test_user_cp0(self):
191
locale.getpreferredencoding = f
192
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
187
self._encoding = 'cp0'
188
self.assertEquals('ascii', osutils.get_user_encoding())
193
189
self.assertEquals('', sys.stderr.getvalue())
195
191
def test_user_cp_unknown(self):
199
locale.getpreferredencoding = f
200
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
192
self._encoding = 'cp-unknown'
193
self.assertEquals('ascii', osutils.get_user_encoding())
201
194
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
202
195
' Continuing with ascii encoding.\n',
203
196
sys.stderr.getvalue())
205
198
def test_user_empty(self):
206
199
"""Running bzr from a vim script gives '' for a preferred locale"""
210
locale.getpreferredencoding = f
211
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
201
self.assertEquals('ascii', osutils.get_user_encoding())
212
202
self.assertEquals('', sys.stderr.getvalue())
214
def test_user_locale_error(self):
216
raise locale.Error, 'unsupported locale'
218
locale.getpreferredencoding = f
219
os.environ['LANG'] = 'BOGUS'
220
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
221
self.assertEquals('bzr: warning: unsupported locale\n'
222
' Could not determine what text encoding to use.\n'
223
' This error usually means your Python interpreter\n'
224
' doesn\'t support the locale set by $LANG (BOGUS)\n'
225
' Continuing with ascii encoding.\n',
226
sys.stderr.getvalue())