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
36
class FakeCodec(object):
33
37
"""Special class that helps testing over several non-existed encodings.
35
39
Clients can add new encoding names, but because of how codecs is
36
40
implemented they cannot be removed. Be careful with naming to avoid
37
41
collisions between tests.
72
76
"""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,
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,
90
95
sys.stderr.encoding = stderr_encoding
91
96
sys.stdin = StringIOWrapper()
92
97
sys.stdin.encoding = stdin_encoding
93
osutils._cached_user_encoding = user_encoding
98
bzrlib.user_encoding = user_encoding
94
99
if enable_fake_encodings:
95
100
fake_codec.add(stdout_encoding)
96
101
fake_codec.add(stderr_encoding)
97
102
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
99
110
def test_get_terminal_encoding(self):
100
111
self.make_wrapped_streams('stdout_encoding',
101
112
'stderr_encoding',
109
120
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
111
122
sys.stdin.encoding = None
112
# and in the worst case, use osutils.get_user_encoding()
123
# and in the worst case, use bzrlib.user_encoding
113
124
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
126
def test_terminal_cp0(self):
136
127
# test cp0 encoding (Windows returns cp0 when there is no encoding)
137
128
self.make_wrapped_streams('cp0',
155
146
user_encoding='latin-1',
156
147
enable_fake_encodings=False)
158
149
self.assertEqual('latin-1', osutils.get_terminal_encoding())
161
152
self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
162
' Using encoding latin-1 instead.\n',
153
' Using encoding latin-1 instead.\n',
163
154
sys.stderr.getvalue())
166
157
class TestUserEncoding(TestCase):
167
158
"""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())
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')
176
def get_encoding(self, do_setlocale=True):
177
return self._encoding
169
locale.getpreferredencoding = self._getpreferredencoding
170
sys.stderr = self._stderr
172
osutils.set_or_unset_env('LANG', self._LANG)
179
174
def test_get_user_encoding(self):
180
self._encoding = 'user_encoding'
176
return 'user_encoding'
178
locale.getpreferredencoding = f
181
179
fake_codec.add('user_encoding')
182
self.assertEquals('iso8859-1', # fake_codec maps to latin-1
183
osutils.get_user_encoding())
180
self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
184
181
self.assertEquals('', sys.stderr.getvalue())
186
183
def test_user_cp0(self):
187
self._encoding = 'cp0'
188
self.assertEquals('ascii', osutils.get_user_encoding())
187
locale.getpreferredencoding = f
188
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
189
189
self.assertEquals('', sys.stderr.getvalue())
191
191
def test_user_cp_unknown(self):
192
self._encoding = 'cp-unknown'
193
self.assertEquals('ascii', osutils.get_user_encoding())
195
locale.getpreferredencoding = f
196
self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
194
197
self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
195
198
' Continuing with ascii encoding.\n',
196
199
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())
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())