/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 breezy/tests/test_osutils_encodings.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 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
 
from bzrlib import (
25
 
    errors,
 
23
from .. import (
26
24
    osutils,
27
25
    )
28
 
from bzrlib.tests import (
29
 
        StringIOWrapper,
30
 
        TestCase,
31
 
        )
 
26
from ..sixish import PY3
 
27
from . import (
 
28
    TestCase,
 
29
    )
 
30
from .ui_testing import (
 
31
    BytesIOWithEncoding,
 
32
    StringIOWithEncoding,
 
33
    )
32
34
 
33
35
 
34
36
class FakeCodec(object):
74
76
    """Test the auto-detection of proper terminal encoding."""
75
77
 
76
78
    def setUp(self):
77
 
        TestCase.setUp(self)
 
79
        super(TestTerminalEncoding, self).setUp()
78
80
        self.overrideAttr(sys, 'stdin')
79
81
        self.overrideAttr(sys, 'stdout')
80
82
        self.overrideAttr(sys, 'stderr')
86
88
                             stdin_encoding,
87
89
                             user_encoding='user_encoding',
88
90
                             enable_fake_encodings=True):
89
 
        sys.stdout = StringIOWrapper()
 
91
        if PY3:
 
92
            sys.stdout = StringIOWithEncoding()
 
93
        else:
 
94
            sys.stdout = BytesIOWithEncoding()
90
95
        sys.stdout.encoding = stdout_encoding
91
 
        sys.stderr = StringIOWrapper()
 
96
        if PY3:
 
97
            sys.stderr = StringIOWithEncoding()
 
98
        else:
 
99
            sys.stderr = BytesIOWithEncoding()
92
100
        sys.stderr.encoding = stderr_encoding
93
 
        sys.stdin = StringIOWrapper()
 
101
        if PY3:
 
102
            sys.stdin = StringIOWithEncoding()
 
103
        else:
 
104
            sys.stdin = BytesIOWithEncoding()
94
105
        sys.stdin.encoding = stdin_encoding
95
106
        osutils._cached_user_encoding = user_encoding
96
107
        if enable_fake_encodings:
114
125
        # and in the worst case, use osutils.get_user_encoding()
115
126
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
116
127
 
 
128
    def test_get_terminal_encoding_silent(self):
 
129
        self.make_wrapped_streams('stdout_encoding',
 
130
                                  'stderr_encoding',
 
131
                                  'stdin_encoding')
 
132
        # Calling get_terminal_encoding should not mutter when silent=True is
 
133
        # passed.
 
134
        log = self.get_log()
 
135
        osutils.get_terminal_encoding()
 
136
        self.assertEqual(log, self.get_log())
 
137
 
 
138
    def test_get_terminal_encoding_trace(self):
 
139
        self.make_wrapped_streams('stdout_encoding',
 
140
                                  'stderr_encoding',
 
141
                                  'stdin_encoding')
 
142
        # Calling get_terminal_encoding should not mutter when silent=True is
 
143
        # passed.
 
144
        log = self.get_log()
 
145
        osutils.get_terminal_encoding(trace=True)
 
146
        self.assertNotEqual(log, self.get_log())
 
147
 
117
148
    def test_terminal_cp0(self):
118
149
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
119
150
        self.make_wrapped_streams('cp0',
126
157
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
127
158
 
128
159
        # check stderr
129
 
        self.assertEquals('', sys.stderr.getvalue())
 
160
        self.assertEqual('', sys.stderr.getvalue())
130
161
 
131
162
    def test_terminal_cp_unknown(self):
132
163
        # test against really unknown encoding
140
171
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
141
172
 
142
173
        # check stderr
143
 
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
144
 
                          '  Using encoding latin-1 instead.\n',
145
 
                          sys.stderr.getvalue())
 
174
        self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n'
 
175
                         '  Using encoding latin-1 instead.\n',
 
176
                         sys.stderr.getvalue())
146
177
 
147
178
 
148
179
class TestUserEncoding(TestCase):
149
180
    """Test detection of default user encoding."""
150
181
 
151
182
    def setUp(self):
152
 
        TestCase.setUp(self)
153
 
        self.overrideAttr(locale, 'getpreferredencoding')
154
 
        self.addCleanup(osutils.set_or_unset_env,
155
 
                        'LANG', os.environ.get('LANG'))
156
 
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
 
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
157
194
 
158
195
    def test_get_user_encoding(self):
159
 
        def f():
160
 
            return 'user_encoding'
161
 
 
162
 
        locale.getpreferredencoding = f
 
196
        self._encoding = 'user_encoding'
163
197
        fake_codec.add('user_encoding')
164
 
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
165
 
        self.assertEquals('', sys.stderr.getvalue())
 
198
        self.assertEqual('iso8859-1',  # fake_codec maps to latin-1
 
199
                         osutils.get_user_encoding())
 
200
        self.assertEqual('', sys.stderr.getvalue())
166
201
 
167
202
    def test_user_cp0(self):
168
 
        def f():
169
 
            return 'cp0'
170
 
 
171
 
        locale.getpreferredencoding = f
172
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
173
 
        self.assertEquals('', sys.stderr.getvalue())
 
203
        self._encoding = 'cp0'
 
204
        self.assertEqual('ascii', osutils.get_user_encoding())
 
205
        self.assertEqual('', sys.stderr.getvalue())
174
206
 
175
207
    def test_user_cp_unknown(self):
176
 
        def f():
177
 
            return 'cp-unknown'
178
 
 
179
 
        locale.getpreferredencoding = f
180
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
181
 
        self.assertEquals('bzr: warning: unknown encoding cp-unknown.'
182
 
                          ' Continuing with ascii encoding.\n',
183
 
                          sys.stderr.getvalue())
 
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())
184
213
 
185
214
    def test_user_empty(self):
186
215
        """Running bzr from a vim script gives '' for a preferred locale"""
187
 
        def f():
188
 
            return ''
189
 
 
190
 
        locale.getpreferredencoding = f
191
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
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())
 
216
        self._encoding = ''
 
217
        self.assertEqual('ascii', osutils.get_user_encoding())
 
218
        self.assertEqual('', sys.stderr.getvalue())