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."""
29
from .ui_testing import (
35
class FakeCodec(object):
36
"""Special class that helps testing over several non-existed encodings.
38
Clients can add new encoding names, but because of how codecs is
39
implemented they cannot be removed. Be careful with naming to avoid
40
collisions between tests.
43
_enabled_encodings = set()
45
def add(self, encoding_name):
46
"""Adding encoding name to fake.
48
:type encoding_name: lowercase plain string
50
if not self._registered:
52
self._registered = True
53
if encoding_name is not None:
54
self._enabled_encodings.add(encoding_name)
56
def __call__(self, encoding_name):
57
"""Called indirectly by codecs module during lookup"""
58
if encoding_name in self._enabled_encodings:
59
return codecs.lookup('latin-1')
62
fake_codec = FakeCodec()
65
class TestFakeCodec(TestCase):
67
def test_fake_codec(self):
68
self.assertRaises(LookupError, codecs.lookup, 'fake')
70
fake_codec.add('fake')
74
class TestTerminalEncoding(TestCase):
75
"""Test the auto-detection of proper terminal encoding."""
78
super(TestTerminalEncoding, self).setUp()
79
self.overrideAttr(sys, 'stdin')
80
self.overrideAttr(sys, 'stdout')
81
self.overrideAttr(sys, 'stderr')
82
self.overrideAttr(osutils, '_cached_user_encoding')
84
def make_wrapped_streams(self,
88
user_encoding='user_encoding',
89
enable_fake_encodings=True):
90
sys.stdout = StringIOWithEncoding()
91
sys.stdout.encoding = stdout_encoding
92
sys.stderr = StringIOWithEncoding()
93
sys.stderr.encoding = stderr_encoding
94
sys.stdin = StringIOWithEncoding()
95
sys.stdin.encoding = stdin_encoding
96
osutils._cached_user_encoding = user_encoding
97
if enable_fake_encodings:
98
fake_codec.add(stdout_encoding)
99
fake_codec.add(stderr_encoding)
100
fake_codec.add(stdin_encoding)
102
def test_get_terminal_encoding(self):
103
self.make_wrapped_streams('stdout_encoding',
107
# first preference is stdout encoding
108
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
110
sys.stdout.encoding = None
111
# if sys.stdout is None, fall back to sys.stdin
112
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
114
sys.stdin.encoding = None
115
# and in the worst case, use osutils.get_user_encoding()
116
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
118
def test_get_terminal_encoding_silent(self):
119
self.make_wrapped_streams('stdout_encoding',
122
# Calling get_terminal_encoding should not mutter when silent=True is
125
osutils.get_terminal_encoding()
126
self.assertEqual(log, self.get_log())
128
def test_get_terminal_encoding_trace(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(trace=True)
136
self.assertNotEqual(log, self.get_log())
138
def test_terminal_cp0(self):
139
# test cp0 encoding (Windows returns cp0 when there is no encoding)
140
self.make_wrapped_streams('cp0',
143
user_encoding='latin-1',
144
enable_fake_encodings=False)
146
# cp0 is invalid encoding. We should fall back to user_encoding
147
self.assertEqual('latin-1', osutils.get_terminal_encoding())
150
self.assertEqual('', sys.stderr.getvalue())
152
def test_terminal_cp_unknown(self):
153
# test against really unknown encoding
154
# catch warning at stderr
155
self.make_wrapped_streams('cp-unknown',
158
user_encoding='latin-1',
159
enable_fake_encodings=False)
161
self.assertEqual('latin-1', osutils.get_terminal_encoding())
164
self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n'
165
' Using encoding latin-1 instead.\n',
166
sys.stderr.getvalue())
169
class TestUserEncoding(TestCase):
170
"""Test detection of default user encoding."""
173
super(TestUserEncoding, self).setUp()
174
self.overrideAttr(osutils, '_cached_user_encoding', None)
175
self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
176
self.overrideAttr(locale, 'CODESET', None)
177
self.overrideAttr(sys, 'stderr', StringIOWithEncoding())
179
def get_encoding(self, do_setlocale=True):
180
return self._encoding
182
def test_get_user_encoding(self):
183
self._encoding = 'user_encoding'
184
fake_codec.add('user_encoding')
185
self.assertEqual('iso8859-1', # fake_codec maps to latin-1
186
osutils.get_user_encoding())
187
self.assertEqual('', sys.stderr.getvalue())
189
def test_user_cp0(self):
190
self._encoding = 'cp0'
191
self.assertEqual('ascii', osutils.get_user_encoding())
192
self.assertEqual('', sys.stderr.getvalue())
194
def test_user_cp_unknown(self):
195
self._encoding = 'cp-unknown'
196
self.assertEqual('ascii', osutils.get_user_encoding())
197
self.assertEqual('brz: warning: unknown encoding cp-unknown.'
198
' Continuing with ascii encoding.\n',
199
sys.stderr.getvalue())
201
def test_user_empty(self):
202
"""Running bzr from a vim script gives '' for a preferred locale"""
204
self.assertEqual('ascii', osutils.get_user_encoding())
205
self.assertEqual('', sys.stderr.getvalue())