~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
22
21
import sys
23
22
 
24
23
from bzrlib import (
25
 
    errors,
26
24
    osutils,
27
25
    )
28
26
from bzrlib.tests import (
74
72
    """Test the auto-detection of proper terminal encoding."""
75
73
 
76
74
    def setUp(self):
77
 
        TestCase.setUp(self)
 
75
        super(TestTerminalEncoding, self).setUp()
78
76
        self.overrideAttr(sys, 'stdin')
79
77
        self.overrideAttr(sys, 'stdout')
80
78
        self.overrideAttr(sys, 'stderr')
169
167
    """Test detection of default user encoding."""
170
168
 
171
169
    def setUp(self):
172
 
        TestCase.setUp(self)
173
 
        self.overrideAttr(locale, 'getpreferredencoding')
174
 
        self.addCleanup(osutils.set_or_unset_env,
175
 
                        'LANG', os.environ.get('LANG'))
 
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)
176
174
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
177
175
 
 
176
    def get_encoding(self, do_setlocale=True):
 
177
        return self._encoding
 
178
 
178
179
    def test_get_user_encoding(self):
179
 
        def f():
180
 
            return 'user_encoding'
181
 
 
182
 
        locale.getpreferredencoding = f
 
180
        self._encoding = 'user_encoding'
183
181
        fake_codec.add('user_encoding')
184
 
        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())
185
184
        self.assertEquals('', sys.stderr.getvalue())
186
185
 
187
186
    def test_user_cp0(self):
188
 
        def f():
189
 
            return 'cp0'
190
 
 
191
 
        locale.getpreferredencoding = f
192
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
187
        self._encoding = 'cp0'
 
188
        self.assertEquals('ascii', osutils.get_user_encoding())
193
189
        self.assertEquals('', sys.stderr.getvalue())
194
190
 
195
191
    def test_user_cp_unknown(self):
196
 
        def f():
197
 
            return 'cp-unknown'
198
 
 
199
 
        locale.getpreferredencoding = f
200
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
192
        self._encoding = 'cp-unknown'
 
193
        self.assertEquals('ascii', osutils.get_user_encoding())
201
194
        self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
202
195
                          ' Continuing with ascii encoding.\n',
203
196
                          sys.stderr.getvalue())
204
197
 
205
198
    def test_user_empty(self):
206
199
        """Running bzr from a vim script gives '' for a preferred locale"""
207
 
        def f():
208
 
            return ''
209
 
 
210
 
        locale.getpreferredencoding = f
211
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
200
        self._encoding = ''
 
201
        self.assertEquals('ascii', osutils.get_user_encoding())
212
202
        self.assertEquals('', sys.stderr.getvalue())
213
 
 
214
 
    def test_user_locale_error(self):
215
 
        def f():
216
 
            raise locale.Error, 'unsupported locale'
217
 
 
218
 
        locale.getpreferredencoding = f
219
 
        os.environ['LANG'] = 'BOGUS'
220
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
221
 
        self.assertEquals('bzr: warning: unsupported locale\n'
222
 
                          '  Could not determine what text encoding to use.\n'
223
 
                          '  This error usually means your Python interpreter\n'
224
 
                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
225
 
                          '  Continuing with ascii encoding.\n',
226
 
                          sys.stderr.getvalue())