1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
17
"""Tests for the osutils wrapper."""
26
from ..sixish import PY3
30
from .ui_testing import (
36
class FakeCodec(object):
37
"""Special class that helps testing over several non-existed encodings.
39
Clients can add new encoding names, but because of how codecs is
40
implemented they cannot be removed. Be careful with naming to avoid
41
collisions between tests.
44
_enabled_encodings = set()
46
def add(self, encoding_name):
47
"""Adding encoding name to fake.
49
:type encoding_name: lowercase plain string
51
if not self._registered:
53
self._registered = True
54
if encoding_name is not None:
55
self._enabled_encodings.add(encoding_name)
57
def __call__(self, encoding_name):
58
"""Called indirectly by codecs module during lookup"""
59
if encoding_name in self._enabled_encodings:
60
return codecs.lookup('latin-1')
63
fake_codec = FakeCodec()
66
class TestFakeCodec(TestCase):
68
def test_fake_codec(self):
69
self.assertRaises(LookupError, codecs.lookup, 'fake')
71
fake_codec.add('fake')
75
class TestTerminalEncoding(TestCase):
76
"""Test the auto-detection of proper terminal encoding."""
79
super(TestTerminalEncoding, self).setUp()
80
self.overrideAttr(sys, 'stdin')
81
self.overrideAttr(sys, 'stdout')
82
self.overrideAttr(sys, 'stderr')
83
self.overrideAttr(osutils, '_cached_user_encoding')
85
def make_wrapped_streams(self,
89
user_encoding='user_encoding',
90
enable_fake_encodings=True):
92
sys.stdout = StringIOWithEncoding()
94
sys.stdout = BytesIOWithEncoding()
95
sys.stdout.encoding = stdout_encoding
97
sys.stderr = StringIOWithEncoding()
99
sys.stderr = BytesIOWithEncoding()
100
sys.stderr.encoding = stderr_encoding
102
sys.stdin = StringIOWithEncoding()
104
sys.stdin = BytesIOWithEncoding()
105
sys.stdin.encoding = stdin_encoding
106
osutils._cached_user_encoding = user_encoding
107
if enable_fake_encodings:
108
fake_codec.add(stdout_encoding)
109
fake_codec.add(stderr_encoding)
110
fake_codec.add(stdin_encoding)
112
def test_get_terminal_encoding(self):
113
self.make_wrapped_streams('stdout_encoding',
117
# first preference is stdout encoding
118
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
120
sys.stdout.encoding = None
121
# if sys.stdout is None, fall back to sys.stdin
122
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
124
sys.stdin.encoding = None
125
# and in the worst case, use osutils.get_user_encoding()
126
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
128
def test_get_terminal_encoding_silent(self):
129
self.make_wrapped_streams('stdout_encoding',
132
# Calling get_terminal_encoding should not mutter when silent=True is
135
osutils.get_terminal_encoding()
136
self.assertEqual(log, self.get_log())
138
def test_get_terminal_encoding_trace(self):
139
self.make_wrapped_streams('stdout_encoding',
142
# Calling get_terminal_encoding should not mutter when silent=True is
145
osutils.get_terminal_encoding(trace=True)
146
self.assertNotEqual(log, self.get_log())
148
def test_terminal_cp0(self):
149
# test cp0 encoding (Windows returns cp0 when there is no encoding)
150
self.make_wrapped_streams('cp0',
153
user_encoding='latin-1',
154
enable_fake_encodings=False)
156
# cp0 is invalid encoding. We should fall back to user_encoding
157
self.assertEqual('latin-1', osutils.get_terminal_encoding())
160
self.assertEqual('', sys.stderr.getvalue())
162
def test_terminal_cp_unknown(self):
163
# test against really unknown encoding
164
# catch warning at stderr
165
self.make_wrapped_streams('cp-unknown',
168
user_encoding='latin-1',
169
enable_fake_encodings=False)
171
self.assertEqual('latin-1', osutils.get_terminal_encoding())
174
self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n'
175
' Using encoding latin-1 instead.\n',
176
sys.stderr.getvalue())
179
class TestUserEncoding(TestCase):
180
"""Test detection of default user encoding."""
183
super(TestUserEncoding, self).setUp()
184
self.overrideAttr(osutils, '_cached_user_encoding', None)
185
self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
186
self.overrideAttr(locale, 'CODESET', None)
188
self.overrideAttr(sys, 'stderr', StringIOWithEncoding())
190
self.overrideAttr(sys, 'stderr', BytesIOWithEncoding())
192
def get_encoding(self, do_setlocale=True):
193
return self._encoding
195
def test_get_user_encoding(self):
196
self._encoding = 'user_encoding'
197
fake_codec.add('user_encoding')
198
self.assertEqual('iso8859-1', # fake_codec maps to latin-1
199
osutils.get_user_encoding())
200
self.assertEqual('', sys.stderr.getvalue())
202
def test_user_cp0(self):
203
self._encoding = 'cp0'
204
self.assertEqual('ascii', osutils.get_user_encoding())
205
self.assertEqual('', sys.stderr.getvalue())
207
def test_user_cp_unknown(self):
208
self._encoding = 'cp-unknown'
209
self.assertEqual('ascii', osutils.get_user_encoding())
210
self.assertEqual('brz: warning: unknown encoding cp-unknown.'
211
' Continuing with ascii encoding.\n',
212
sys.stderr.getvalue())
214
def test_user_empty(self):
215
"""Running bzr from a vim script gives '' for a preferred locale"""
217
self.assertEqual('ascii', osutils.get_user_encoding())
218
self.assertEqual('', sys.stderr.getvalue())