/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-02-19 23:18:42 UTC
  • mto: (7490.3.4 work)
  • mto: This revision was merged to the branch mainline in revision 7495.
  • Revision ID: jelmer@jelmer.uk-20200219231842-agwjh2db66cpajqg
Consistent return values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Tests for the osutils wrapper."""
 
18
 
 
19
import codecs
 
20
import locale
 
21
import sys
 
22
 
 
23
from .. import (
 
24
    osutils,
 
25
    )
 
26
from ..sixish import PY3
 
27
from . import (
 
28
    TestCase,
 
29
    )
 
30
from .ui_testing import (
 
31
    BytesIOWithEncoding,
 
32
    StringIOWithEncoding,
 
33
    )
 
34
 
 
35
 
 
36
class FakeCodec(object):
 
37
    """Special class that helps testing over several non-existed encodings.
 
38
 
 
39
    Clients can add new encoding names, but because of how codecs is
 
40
    implemented they cannot be removed. Be careful with naming to avoid
 
41
    collisions between tests.
 
42
    """
 
43
    _registered = False
 
44
    _enabled_encodings = set()
 
45
 
 
46
    def add(self, encoding_name):
 
47
        """Adding encoding name to fake.
 
48
 
 
49
        :type   encoding_name:  lowercase plain string
 
50
        """
 
51
        if not self._registered:
 
52
            codecs.register(self)
 
53
            self._registered = True
 
54
        if encoding_name is not None:
 
55
            self._enabled_encodings.add(encoding_name)
 
56
 
 
57
    def __call__(self, encoding_name):
 
58
        """Called indirectly by codecs module during lookup"""
 
59
        if encoding_name in self._enabled_encodings:
 
60
            return codecs.lookup('latin-1')
 
61
 
 
62
 
 
63
fake_codec = FakeCodec()
 
64
 
 
65
 
 
66
class TestFakeCodec(TestCase):
 
67
 
 
68
    def test_fake_codec(self):
 
69
        self.assertRaises(LookupError, codecs.lookup, 'fake')
 
70
 
 
71
        fake_codec.add('fake')
 
72
        codecs.lookup('fake')
 
73
 
 
74
 
 
75
class TestTerminalEncoding(TestCase):
 
76
    """Test the auto-detection of proper terminal encoding."""
 
77
 
 
78
    def setUp(self):
 
79
        super(TestTerminalEncoding, self).setUp()
 
80
        self.overrideAttr(sys, 'stdin')
 
81
        self.overrideAttr(sys, 'stdout')
 
82
        self.overrideAttr(sys, 'stderr')
 
83
        self.overrideAttr(osutils, '_cached_user_encoding')
 
84
 
 
85
    def make_wrapped_streams(self,
 
86
                             stdout_encoding,
 
87
                             stderr_encoding,
 
88
                             stdin_encoding,
 
89
                             user_encoding='user_encoding',
 
90
                             enable_fake_encodings=True):
 
91
        if PY3:
 
92
            sys.stdout = StringIOWithEncoding()
 
93
        else:
 
94
            sys.stdout = BytesIOWithEncoding()
 
95
        sys.stdout.encoding = stdout_encoding
 
96
        if PY3:
 
97
            sys.stderr = StringIOWithEncoding()
 
98
        else:
 
99
            sys.stderr = BytesIOWithEncoding()
 
100
        sys.stderr.encoding = stderr_encoding
 
101
        if PY3:
 
102
            sys.stdin = StringIOWithEncoding()
 
103
        else:
 
104
            sys.stdin = BytesIOWithEncoding()
 
105
        sys.stdin.encoding = stdin_encoding
 
106
        osutils._cached_user_encoding = user_encoding
 
107
        if enable_fake_encodings:
 
108
            fake_codec.add(stdout_encoding)
 
109
            fake_codec.add(stderr_encoding)
 
110
            fake_codec.add(stdin_encoding)
 
111
 
 
112
    def test_get_terminal_encoding(self):
 
113
        self.make_wrapped_streams('stdout_encoding',
 
114
                                  'stderr_encoding',
 
115
                                  'stdin_encoding')
 
116
 
 
117
        # first preference is stdout encoding
 
118
        self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
 
119
 
 
120
        sys.stdout.encoding = None
 
121
        # if sys.stdout is None, fall back to sys.stdin
 
122
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
 
123
 
 
124
        sys.stdin.encoding = None
 
125
        # and in the worst case, use osutils.get_user_encoding()
 
126
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
 
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
 
 
148
    def test_terminal_cp0(self):
 
149
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
 
150
        self.make_wrapped_streams('cp0',
 
151
                                  'cp0',
 
152
                                  'cp0',
 
153
                                  user_encoding='latin-1',
 
154
                                  enable_fake_encodings=False)
 
155
 
 
156
        # cp0 is invalid encoding. We should fall back to user_encoding
 
157
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
 
158
 
 
159
        # check stderr
 
160
        self.assertEqual('', sys.stderr.getvalue())
 
161
 
 
162
    def test_terminal_cp_unknown(self):
 
163
        # test against really unknown encoding
 
164
        # catch warning at stderr
 
165
        self.make_wrapped_streams('cp-unknown',
 
166
                                  'cp-unknown',
 
167
                                  'cp-unknown',
 
168
                                  user_encoding='latin-1',
 
169
                                  enable_fake_encodings=False)
 
170
 
 
171
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
 
172
 
 
173
        # check stderr
 
174
        self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n'
 
175
                         '  Using encoding latin-1 instead.\n',
 
176
                         sys.stderr.getvalue())
 
177
 
 
178
 
 
179
class TestUserEncoding(TestCase):
 
180
    """Test detection of default user encoding."""
 
181
 
 
182
    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
 
194
 
 
195
    def test_get_user_encoding(self):
 
196
        self._encoding = 'user_encoding'
 
197
        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())
 
201
 
 
202
    def test_user_cp0(self):
 
203
        self._encoding = 'cp0'
 
204
        self.assertEqual('ascii', osutils.get_user_encoding())
 
205
        self.assertEqual('', sys.stderr.getvalue())
 
206
 
 
207
    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())
 
213
 
 
214
    def test_user_empty(self):
 
215
        """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())