~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Martin
  • Date: 2010-05-03 20:57:39 UTC
  • mto: This revision was merged to the branch mainline in revision 5204.
  • Revision ID: gzlist@googlemail.com-20100503205739-n326zdvevv0rmruh
Retain original stack and error message when translating to ValueError in bencode

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
 
19
19
import codecs
20
20
import locale
 
21
import os
21
22
import sys
22
23
 
23
24
from bzrlib import (
 
25
    errors,
24
26
    osutils,
25
27
    )
26
28
from bzrlib.tests import (
72
74
    """Test the auto-detection of proper terminal encoding."""
73
75
 
74
76
    def setUp(self):
75
 
        super(TestTerminalEncoding, self).setUp()
 
77
        TestCase.setUp(self)
76
78
        self.overrideAttr(sys, 'stdin')
77
79
        self.overrideAttr(sys, 'stdout')
78
80
        self.overrideAttr(sys, 'stderr')
112
114
        # and in the worst case, use osutils.get_user_encoding()
113
115
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
114
116
 
115
 
    def test_get_terminal_encoding_silent(self):
116
 
        self.make_wrapped_streams('stdout_encoding',
117
 
                                  'stderr_encoding',
118
 
                                  'stdin_encoding')
119
 
        # Calling get_terminal_encoding should not mutter when silent=True is
120
 
        # passed.
121
 
        log = self.get_log()
122
 
        osutils.get_terminal_encoding()
123
 
        self.assertEqual(log, self.get_log())
124
 
 
125
 
    def test_get_terminal_encoding_trace(self):
126
 
        self.make_wrapped_streams('stdout_encoding',
127
 
                                  'stderr_encoding',
128
 
                                  'stdin_encoding')
129
 
        # Calling get_terminal_encoding should not mutter when silent=True is
130
 
        # passed.
131
 
        log = self.get_log()
132
 
        osutils.get_terminal_encoding(trace=True)
133
 
        self.assertNotEqual(log, self.get_log())
134
 
 
135
117
    def test_terminal_cp0(self):
136
118
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
137
119
        self.make_wrapped_streams('cp0',
167
149
    """Test detection of default user encoding."""
168
150
 
169
151
    def setUp(self):
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)
 
152
        TestCase.setUp(self)
 
153
        self.overrideAttr(locale, 'getpreferredencoding')
 
154
        self.addCleanup(osutils.set_or_unset_env,
 
155
                        'LANG', os.environ.get('LANG'))
174
156
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
175
157
 
176
 
    def get_encoding(self, do_setlocale=True):
177
 
        return self._encoding
178
 
 
179
158
    def test_get_user_encoding(self):
180
 
        self._encoding = 'user_encoding'
 
159
        def f():
 
160
            return 'user_encoding'
 
161
 
 
162
        locale.getpreferredencoding = f
181
163
        fake_codec.add('user_encoding')
182
 
        self.assertEquals('iso8859-1', # fake_codec maps to latin-1
183
 
                          osutils.get_user_encoding())
 
164
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
184
165
        self.assertEquals('', sys.stderr.getvalue())
185
166
 
186
167
    def test_user_cp0(self):
187
 
        self._encoding = 'cp0'
188
 
        self.assertEquals('ascii', osutils.get_user_encoding())
 
168
        def f():
 
169
            return 'cp0'
 
170
 
 
171
        locale.getpreferredencoding = f
 
172
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
189
173
        self.assertEquals('', sys.stderr.getvalue())
190
174
 
191
175
    def test_user_cp_unknown(self):
192
 
        self._encoding = 'cp-unknown'
193
 
        self.assertEquals('ascii', osutils.get_user_encoding())
 
176
        def f():
 
177
            return 'cp-unknown'
 
178
 
 
179
        locale.getpreferredencoding = f
 
180
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
194
181
        self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
195
182
                          ' Continuing with ascii encoding.\n',
196
183
                          sys.stderr.getvalue())
197
184
 
198
185
    def test_user_empty(self):
199
186
        """Running bzr from a vim script gives '' for a preferred locale"""
200
 
        self._encoding = ''
201
 
        self.assertEquals('ascii', osutils.get_user_encoding())
 
187
        def f():
 
188
            return ''
 
189
 
 
190
        locale.getpreferredencoding = f
 
191
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
202
192
        self.assertEquals('', sys.stderr.getvalue())
 
193
 
 
194
    def test_user_locale_error(self):
 
195
        def f():
 
196
            raise locale.Error, 'unsupported locale'
 
197
 
 
198
        locale.getpreferredencoding = f
 
199
        os.environ['LANG'] = 'BOGUS'
 
200
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
201
        self.assertEquals('bzr: warning: unsupported locale\n'
 
202
                          '  Could not determine what text encoding to use.\n'
 
203
                          '  This error usually means your Python interpreter\n'
 
204
                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
 
205
                          '  Continuing with ascii encoding.\n',
 
206
                          sys.stderr.getvalue())