167
169
"""Test detection of default user encoding."""
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)
173
self.overrideAttr(locale, 'getpreferredencoding')
174
self.addCleanup(osutils.set_or_unset_env,
175
'LANG', os.environ.get('LANG'))
174
176
self.overrideAttr(sys, 'stderr', StringIOWrapper())
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
179
178
def test_get_user_encoding(self):
180
self._encoding = 'user_encoding'
180
return 'user_encoding'
182
locale.getpreferredencoding = f
181
183
fake_codec.add('user_encoding')
182
self.assertEqual('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
184
self.assertEqual('', sys.stderr.getvalue())
184
self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
185
self.assertEquals('', sys.stderr.getvalue())
186
187
def test_user_cp0(self):
187
self._encoding = 'cp0'
188
self.assertEqual('ascii', osutils.get_user_encoding())
189
self.assertEqual('', sys.stderr.getvalue())
191
locale.getpreferredencoding = f
192
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
193
self.assertEquals('', sys.stderr.getvalue())
191
195
def test_user_cp_unknown(self):
192
self._encoding = 'cp-unknown'
193
self.assertEqual('ascii', osutils.get_user_encoding())
194
self.assertEqual('bzr: warning: unknown encoding cp-unknown.'
199
locale.getpreferredencoding = f
200
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
201
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
195
202
' Continuing with ascii encoding.\n',
196
203
sys.stderr.getvalue())
198
205
def test_user_empty(self):
199
206
"""Running bzr from a vim script gives '' for a preferred locale"""
201
self.assertEqual('ascii', osutils.get_user_encoding())
202
self.assertEqual('', sys.stderr.getvalue())
210
locale.getpreferredencoding = f
211
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
212
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())