74
72
"""Test the auto-detection of proper terminal encoding."""
78
self._stdout = sys.stdout
79
self._stderr = sys.stderr
80
self._stdin = sys.stdin
81
self._user_encoding = osutils._cached_user_encoding
83
self.addCleanup(self._reset)
75
super(TestTerminalEncoding, self).setUp()
76
self.overrideAttr(sys, 'stdin')
77
self.overrideAttr(sys, 'stdout')
78
self.overrideAttr(sys, 'stderr')
79
self.overrideAttr(osutils, '_cached_user_encoding')
85
81
def make_wrapped_streams(self,
122
112
# and in the worst case, use osutils.get_user_encoding()
123
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())
125
135
def test_terminal_cp0(self):
126
136
# test cp0 encoding (Windows returns cp0 when there is no encoding)
127
137
self.make_wrapped_streams('cp0',
157
167
"""Test detection of default user encoding."""
161
self._stderr = sys.stderr
162
self._getpreferredencoding = locale.getpreferredencoding
163
self.addCleanup(self._reset)
164
sys.stderr = StringIOWrapper()
166
self._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)
174
self.overrideAttr(sys, 'stderr', StringIOWrapper())
169
locale.getpreferredencoding = self._getpreferredencoding
170
sys.stderr = self._stderr
172
osutils.set_or_unset_env('LANG', self._LANG)
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', osutils.get_user_encoding(use_cache=False))
182
self.assertEquals('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
181
184
self.assertEquals('', sys.stderr.getvalue())
183
186
def test_user_cp0(self):
187
locale.getpreferredencoding = f
188
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
187
self._encoding = 'cp0'
188
self.assertEquals('ascii', osutils.get_user_encoding())
189
189
self.assertEquals('', sys.stderr.getvalue())
191
191
def test_user_cp_unknown(self):
195
locale.getpreferredencoding = f
196
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
192
self._encoding = 'cp-unknown'
193
self.assertEquals('ascii', osutils.get_user_encoding())
197
194
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
198
195
' Continuing with ascii encoding.\n',
199
196
sys.stderr.getvalue())
201
198
def test_user_empty(self):
202
199
"""Running bzr from a vim script gives '' for a preferred locale"""
206
locale.getpreferredencoding = f
207
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
201
self.assertEquals('ascii', osutils.get_user_encoding())
208
202
self.assertEquals('', sys.stderr.getvalue())
210
def test_user_locale_error(self):
212
raise locale.Error, 'unsupported locale'
214
locale.getpreferredencoding = f
215
os.environ['LANG'] = 'BOGUS'
216
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
217
self.assertEquals('bzr: warning: unsupported locale\n'
218
' Could not determine what text encoding to use.\n'
219
' This error usually means your Python interpreter\n'
220
' doesn\'t support the locale set by $LANG (BOGUS)\n'
221
' Continuing with ascii encoding.\n',
222
sys.stderr.getvalue())