/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
16
17
"""Tests for the osutils wrapper."""
18
19
import codecs
2192.1.9 by Alexander Belchenko
final fix suggested by John Meinel
20
import locale
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
21
import sys
22
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from .. import (
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
24
    osutils,
25
    )
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
26
from ..sixish import PY3
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from . import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
28
    TestCase,
29
    )
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
30
from .ui_testing import (
31
    BytesIOWithEncoding,
32
    StringIOWithEncoding,
33
    )
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
34
35
36
class FakeCodec(object):
2192.1.8 by Alexander Belchenko
Rework tests after John's review
37
    """Special class that helps testing over several non-existed encodings.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
38
2192.1.8 by Alexander Belchenko
Rework tests after John's review
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.
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
42
    """
43
    _registered = False
44
    _enabled_encodings = set()
45
46
    def add(self, encoding_name):
2192.1.8 by Alexander Belchenko
Rework tests after John's review
47
        """Adding encoding name to fake.
48
49
        :type   encoding_name:  lowercase plain string
50
        """
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
51
        if not self._registered:
52
            codecs.register(self)
53
            self._registered = True
2192.1.8 by Alexander Belchenko
Rework tests after John's review
54
        if encoding_name is not None:
55
            self._enabled_encodings.add(encoding_name)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
56
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
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')
2192.1.6 by Alexander Belchenko
Fixes after Aaron's review
61
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
62
63
fake_codec = FakeCodec()
64
65
66
class TestFakeCodec(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
67
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
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):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
79
        super(TestTerminalEncoding, self).setUp()
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
80
        self.overrideAttr(sys, 'stdin')
81
        self.overrideAttr(sys, 'stdout')
82
        self.overrideAttr(sys, 'stderr')
83
        self.overrideAttr(osutils, '_cached_user_encoding')
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
84
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
85
    def make_wrapped_streams(self,
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
86
                             stdout_encoding,
87
                             stderr_encoding,
88
                             stdin_encoding,
89
                             user_encoding='user_encoding',
90
                             enable_fake_encodings=True):
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
91
        if PY3:
92
            sys.stdout = StringIOWithEncoding()
93
        else:
94
            sys.stdout = BytesIOWithEncoding()
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
95
        sys.stdout.encoding = stdout_encoding
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
96
        if PY3:
97
            sys.stderr = StringIOWithEncoding()
98
        else:
99
            sys.stderr = BytesIOWithEncoding()
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
100
        sys.stderr.encoding = stderr_encoding
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
101
        if PY3:
102
            sys.stdin = StringIOWithEncoding()
103
        else:
104
            sys.stdin = BytesIOWithEncoding()
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
105
        sys.stdin.encoding = stdin_encoding
3224.5.4 by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding.
106
        osutils._cached_user_encoding = user_encoding
2192.1.8 by Alexander Belchenko
Rework tests after John's review
107
        if enable_fake_encodings:
108
            fake_codec.add(stdout_encoding)
109
            fake_codec.add(stderr_encoding)
110
            fake_codec.add(stdin_encoding)
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_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
3224.5.4 by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding.
125
        # and in the worst case, use osutils.get_user_encoding()
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
126
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
127
5320.2.4 by Robert Collins
``bzrlib.osutils.get_terminal_encoding`` will now only mutter its
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
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
148
    def test_terminal_cp0(self):
2192.1.6 by Alexander Belchenko
Fixes after Aaron's review
149
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
150
        self.make_wrapped_streams('cp0',
2192.1.8 by Alexander Belchenko
Rework tests after John's review
151
                                  'cp0',
152
                                  'cp0',
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
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
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
160
        self.assertEqual('', sys.stderr.getvalue())
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
161
2192.1.8 by Alexander Belchenko
Rework tests after John's review
162
    def test_terminal_cp_unknown(self):
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
163
        # test against really unknown encoding
164
        # catch warning at stderr
2192.1.8 by Alexander Belchenko
Rework tests after John's review
165
        self.make_wrapped_streams('cp-unknown',
166
                                  'cp-unknown',
167
                                  'cp-unknown',
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
168
                                  user_encoding='latin-1',
169
                                  enable_fake_encodings=False)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
170
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
171
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
172
173
        # check stderr
6622.1.31 by Jelmer Vernooij
Fix more tests.
174
        self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
175
                          '  Using encoding latin-1 instead.\n',
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
176
                          sys.stderr.getvalue())
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
177
178
179
class TestUserEncoding(TestCase):
180
    """Test detection of default user encoding."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
181
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
182
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
183
        super(TestUserEncoding, self).setUp()
6383.1.3 by Martin Packman
Deprecate public use_cache parameter in favour of test specific override
184
        self.overrideAttr(osutils, '_cached_user_encoding', None)
6383.1.4 by Martin Packman
Simplify tests a little and make it clear setlocale is not being used
185
        self.overrideAttr(locale, 'getpreferredencoding', self.get_encoding)
6383.1.1 by Martin Packman
Simplify get_user_encoding by avoiding locale hacks and assuming setlocale has been called
186
        self.overrideAttr(locale, 'CODESET', None)
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
187
        if PY3:
188
            self.overrideAttr(sys, 'stderr', StringIOWithEncoding())
189
        else:
190
            self.overrideAttr(sys, 'stderr', BytesIOWithEncoding())
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
191
6383.1.4 by Martin Packman
Simplify tests a little and make it clear setlocale is not being used
192
    def get_encoding(self, do_setlocale=True):
193
        return self._encoding
194
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
195
    def test_get_user_encoding(self):
6383.1.4 by Martin Packman
Simplify tests a little and make it clear setlocale is not being used
196
        self._encoding = 'user_encoding'
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
197
        fake_codec.add('user_encoding')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
198
        self.assertEqual('iso8859-1', # fake_codec maps to latin-1
6383.1.3 by Martin Packman
Deprecate public use_cache parameter in favour of test specific override
199
                          osutils.get_user_encoding())
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
200
        self.assertEqual('', sys.stderr.getvalue())
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
201
202
    def test_user_cp0(self):
6383.1.4 by Martin Packman
Simplify tests a little and make it clear setlocale is not being used
203
        self._encoding = 'cp0'
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
204
        self.assertEqual('ascii', osutils.get_user_encoding())
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
205
        self.assertEqual('', sys.stderr.getvalue())
2192.1.3 by Alexander Belchenko
Tests for osutils.get_user_encoding
206
2192.1.8 by Alexander Belchenko
Rework tests after John's review
207
    def test_user_cp_unknown(self):
6383.1.4 by Martin Packman
Simplify tests a little and make it clear setlocale is not being used
208
        self._encoding = 'cp-unknown'
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
209
        self.assertEqual('ascii', osutils.get_user_encoding())
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
210
        self.assertEqual('brz: warning: unknown encoding cp-unknown.'
211
                         ' Continuing with ascii encoding.\n',
212
                         sys.stderr.getvalue())
2192.1.7 by Alexander Belchenko
get_user_encoding: if locale.Error raised we need to set user_encoding to 'ascii' as warning says
213
3405.3.1 by Neil Martinsen-Burrell
accept for an encoding to mean ascii
214
    def test_user_empty(self):
215
        """Running bzr from a vim script gives '' for a preferred locale"""
6383.1.4 by Martin Packman
Simplify tests a little and make it clear setlocale is not being used
216
        self._encoding = ''
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
217
        self.assertEqual('ascii', osutils.get_user_encoding())
7045.4.16 by Jelmer Vernooij
Fix some osutils tests.
218
        self.assertEqual('', sys.stderr.getvalue())