/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
 
from .. import (
 
24
from bzrlib import (
 
25
    errors,
24
26
    osutils,
25
27
    )
26
 
from ..sixish import PY3
27
 
from . import (
28
 
    TestCase,
29
 
    )
30
 
from .ui_testing import (
31
 
    BytesIOWithEncoding,
32
 
    StringIOWithEncoding,
33
 
    )
 
28
from bzrlib.tests import (
 
29
        StringIOWrapper,
 
30
        TestCase,
 
31
        )
34
32
 
35
33
 
36
34
class FakeCodec(object):
76
74
    """Test the auto-detection of proper terminal encoding."""
77
75
 
78
76
    def setUp(self):
79
 
        super(TestTerminalEncoding, self).setUp()
 
77
        TestCase.setUp(self)
80
78
        self.overrideAttr(sys, 'stdin')
81
79
        self.overrideAttr(sys, 'stdout')
82
80
        self.overrideAttr(sys, 'stderr')
88
86
                             stdin_encoding,
89
87
                             user_encoding='user_encoding',
90
88
                             enable_fake_encodings=True):
91
 
        if PY3:
92
 
            sys.stdout = StringIOWithEncoding()
93
 
        else:
94
 
            sys.stdout = BytesIOWithEncoding()
 
89
        sys.stdout = StringIOWrapper()
95
90
        sys.stdout.encoding = stdout_encoding
96
 
        if PY3:
97
 
            sys.stderr = StringIOWithEncoding()
98
 
        else:
99
 
            sys.stderr = BytesIOWithEncoding()
 
91
        sys.stderr = StringIOWrapper()
100
92
        sys.stderr.encoding = stderr_encoding
101
 
        if PY3:
102
 
            sys.stdin = StringIOWithEncoding()
103
 
        else:
104
 
            sys.stdin = BytesIOWithEncoding()
 
93
        sys.stdin = StringIOWrapper()
105
94
        sys.stdin.encoding = stdin_encoding
106
95
        osutils._cached_user_encoding = user_encoding
107
96
        if enable_fake_encodings:
157
146
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
158
147
 
159
148
        # check stderr
160
 
        self.assertEqual('', sys.stderr.getvalue())
 
149
        self.assertEquals('', sys.stderr.getvalue())
161
150
 
162
151
    def test_terminal_cp_unknown(self):
163
152
        # test against really unknown encoding
171
160
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
172
161
 
173
162
        # check stderr
174
 
        self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n'
175
 
                         '  Using encoding latin-1 instead.\n',
176
 
                         sys.stderr.getvalue())
 
163
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
 
164
                          '  Using encoding latin-1 instead.\n',
 
165
                          sys.stderr.getvalue())
177
166
 
178
167
 
179
168
class TestUserEncoding(TestCase):
180
169
    """Test detection of default user encoding."""
181
170
 
182
171
    def setUp(self):
183
 
        super(TestUserEncoding, self).setUp()
184
 
        self.overrideAttr(osutils, '_cached_user_encoding', None)
185
 
        self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
186
 
        self.overrideAttr(locale, 'CODESET', None)
187
 
        if PY3:
188
 
            self.overrideAttr(sys, 'stderr', StringIOWithEncoding())
189
 
        else:
190
 
            self.overrideAttr(sys, 'stderr', BytesIOWithEncoding())
191
 
 
192
 
    def get_encoding(self, do_setlocale=True):
193
 
        return self._encoding
 
172
        TestCase.setUp(self)
 
173
        self.overrideAttr(locale, 'getpreferredencoding')
 
174
        self.addCleanup(osutils.set_or_unset_env,
 
175
                        'LANG', os.environ.get('LANG'))
 
176
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
194
177
 
195
178
    def test_get_user_encoding(self):
196
 
        self._encoding = 'user_encoding'
 
179
        def f():
 
180
            return 'user_encoding'
 
181
 
 
182
        locale.getpreferredencoding = f
197
183
        fake_codec.add('user_encoding')
198
 
        self.assertEqual('iso8859-1',  # fake_codec maps to latin-1
199
 
                         osutils.get_user_encoding())
200
 
        self.assertEqual('', sys.stderr.getvalue())
 
184
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
 
185
        self.assertEquals('', sys.stderr.getvalue())
201
186
 
202
187
    def test_user_cp0(self):
203
 
        self._encoding = 'cp0'
204
 
        self.assertEqual('ascii', osutils.get_user_encoding())
205
 
        self.assertEqual('', sys.stderr.getvalue())
 
188
        def f():
 
189
            return 'cp0'
 
190
 
 
191
        locale.getpreferredencoding = f
 
192
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
193
        self.assertEquals('', sys.stderr.getvalue())
206
194
 
207
195
    def test_user_cp_unknown(self):
208
 
        self._encoding = 'cp-unknown'
209
 
        self.assertEqual('ascii', osutils.get_user_encoding())
210
 
        self.assertEqual('brz: warning: unknown encoding cp-unknown.'
211
 
                         ' Continuing with ascii encoding.\n',
212
 
                         sys.stderr.getvalue())
 
196
        def f():
 
197
            return 'cp-unknown'
 
198
 
 
199
        locale.getpreferredencoding = f
 
200
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
201
        self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
 
202
                          ' Continuing with ascii encoding.\n',
 
203
                          sys.stderr.getvalue())
213
204
 
214
205
    def test_user_empty(self):
215
206
        """Running bzr from a vim script gives '' for a preferred locale"""
216
 
        self._encoding = ''
217
 
        self.assertEqual('ascii', osutils.get_user_encoding())
218
 
        self.assertEqual('', sys.stderr.getvalue())
 
207
        def f():
 
208
            return ''
 
209
 
 
210
        locale.getpreferredencoding = f
 
211
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
212
        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())