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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for the osutils wrapper."""
25
23
from bzrlib import (
29
26
from bzrlib.tests import (
36
32
class FakeCodec(object):
37
33
"""Special class that helps testing over several non-existed encodings.
39
35
Clients can add new encoding names, but because of how codecs is
40
36
implemented they cannot be removed. Be careful with naming to avoid
41
37
collisions between tests.
76
72
"""Test the auto-detection of proper terminal encoding."""
79
self._stdout = sys.stdout
80
self._stderr = sys.stderr
81
self._stdin = sys.stdin
82
self._user_encoding = bzrlib.user_encoding
84
self.addCleanup(self._reset)
86
def make_wrapped_streams(self,
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,
95
90
sys.stderr.encoding = stderr_encoding
96
91
sys.stdin = StringIOWrapper()
97
92
sys.stdin.encoding = stdin_encoding
98
bzrlib.user_encoding = user_encoding
93
osutils._cached_user_encoding = user_encoding
99
94
if enable_fake_encodings:
100
95
fake_codec.add(stdout_encoding)
101
96
fake_codec.add(stderr_encoding)
102
97
fake_codec.add(stdin_encoding)
105
sys.stdout = self._stdout
106
sys.stderr = self._stderr
107
sys.stdin = self._stdin
108
bzrlib.user_encoding = self._user_encoding
110
99
def test_get_terminal_encoding(self):
111
100
self.make_wrapped_streams('stdout_encoding',
112
101
'stderr_encoding',
120
109
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
122
111
sys.stdin.encoding = None
123
# and in the worst case, use bzrlib.user_encoding
112
# and in the worst case, use osutils.get_user_encoding()
124
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())
126
135
def test_terminal_cp0(self):
127
136
# test cp0 encoding (Windows returns cp0 when there is no encoding)
128
137
self.make_wrapped_streams('cp0',
146
155
user_encoding='latin-1',
147
156
enable_fake_encodings=False)
149
158
self.assertEqual('latin-1', osutils.get_terminal_encoding())
152
161
self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
153
' Using encoding latin-1 instead.\n',
162
' Using encoding latin-1 instead.\n',
154
163
sys.stderr.getvalue())
157
166
class TestUserEncoding(TestCase):
158
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')
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
def test_user_locale_error(self):
203
raise locale.Error, 'unsupported locale'
205
locale.getpreferredencoding = f
206
os.environ['LANG'] = 'BOGUS'
207
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
208
self.assertEquals('bzr: warning: unsupported locale\n'
209
' Could not determine what text encoding to use.\n'
210
' This error usually means your Python interpreter\n'
211
' doesn\'t support the locale set by $LANG (BOGUS)\n'
212
' Continuing with ascii encoding.\n',
213
sys.stderr.getvalue())
198
def test_user_empty(self):
199
"""Running bzr from a vim script gives '' for a preferred locale"""
201
self.assertEquals('ascii', osutils.get_user_encoding())
202
self.assertEquals('', sys.stderr.getvalue())