13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the osutils wrapper."""
23
25
from bzrlib import (
26
29
from bzrlib.tests import (
32
35
class FakeCodec(object):
33
36
"""Special class that helps testing over several non-existed encodings.
35
38
Clients can add new encoding names, but because of how codecs is
36
39
implemented they cannot be removed. Be careful with naming to avoid
37
40
collisions between tests.
72
75
"""Test the auto-detection of proper terminal encoding."""
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')
81
def make_wrapped_streams(self,
78
self._stdout = sys.stdout
79
self._stderr = sys.stderr
80
self._stdin = sys.stdin
81
self._user_encoding = bzrlib.user_encoding
83
self.addCleanup(self._reset)
85
def make_wrapped_streams(self,
90
94
sys.stderr.encoding = stderr_encoding
91
95
sys.stdin = StringIOWrapper()
92
96
sys.stdin.encoding = stdin_encoding
93
osutils._cached_user_encoding = user_encoding
97
bzrlib.user_encoding = user_encoding
94
98
if enable_fake_encodings:
95
99
fake_codec.add(stdout_encoding)
96
100
fake_codec.add(stderr_encoding)
97
101
fake_codec.add(stdin_encoding)
104
sys.stdout = self._stdout
105
sys.stderr = self._stderr
106
sys.stdin = self._stdin
107
bzrlib.user_encoding = self._user_encoding
99
109
def test_get_terminal_encoding(self):
100
110
self.make_wrapped_streams('stdout_encoding',
101
111
'stderr_encoding',
109
119
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
111
121
sys.stdin.encoding = None
112
# and in the worst case, use osutils.get_user_encoding()
122
# and in the worst case, use bzrlib.user_encoding
113
123
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())
135
125
def test_terminal_cp0(self):
136
126
# test cp0 encoding (Windows returns cp0 when there is no encoding)
137
127
self.make_wrapped_streams('cp0',
155
145
user_encoding='latin-1',
156
146
enable_fake_encodings=False)
158
148
self.assertEqual('latin-1', osutils.get_terminal_encoding())
161
self.assertEqual('bzr: warning: unknown terminal encoding cp-unknown.\n'
162
' Using encoding latin-1 instead.\n',
151
self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
152
' Using encoding latin-1 instead.\n',
163
153
sys.stderr.getvalue())
166
156
class TestUserEncoding(TestCase):
167
157
"""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)
174
self.overrideAttr(sys, 'stderr', StringIOWrapper())
160
self._stderr = sys.stderr
161
self._getpreferredencoding = locale.getpreferredencoding
162
self.addCleanup(self._reset)
163
sys.stderr = StringIOWrapper()
165
self._LANG = os.environ.get('LANG')
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
168
locale.getpreferredencoding = self._getpreferredencoding
169
sys.stderr = self._stderr
171
osutils.set_or_unset_env('LANG', self._LANG)
179
173
def test_get_user_encoding(self):
180
self._encoding = 'user_encoding'
175
return 'user_encoding'
177
locale.getpreferredencoding = f
181
178
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())
179
self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
180
self.assertEquals('', sys.stderr.getvalue())
186
182
def test_user_cp0(self):
187
self._encoding = 'cp0'
188
self.assertEqual('ascii', osutils.get_user_encoding())
189
self.assertEqual('', sys.stderr.getvalue())
186
locale.getpreferredencoding = f
187
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
188
self.assertEquals('', sys.stderr.getvalue())
191
190
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.'
194
locale.getpreferredencoding = f
195
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
196
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
195
197
' Continuing with ascii encoding.\n',
196
198
sys.stderr.getvalue())
198
200
def test_user_empty(self):
199
201
"""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())
205
locale.getpreferredencoding = f
206
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
207
self.assertEquals('', sys.stderr.getvalue())
209
def test_user_locale_error(self):
211
raise locale.Error, 'unsupported locale'
213
locale.getpreferredencoding = f
214
os.environ['LANG'] = 'BOGUS'
215
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
216
self.assertEquals('bzr: warning: unsupported locale\n'
217
' Could not determine what text encoding to use.\n'
218
' This error usually means your Python interpreter\n'
219
' doesn\'t support the locale set by $LANG (BOGUS)\n'
220
' Continuing with ascii encoding.\n',
221
sys.stderr.getvalue())