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 |
)
|
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
26 |
from . import ( |
6621.22.2
by Martin
Use BytesIO or StringIO from bzrlib.sixish |
27 |
TestCase, |
28 |
)
|
|
7045.4.16
by Jelmer Vernooij
Fix some osutils tests. |
29 |
from .ui_testing import ( |
30 |
BytesIOWithEncoding, |
|
31 |
StringIOWithEncoding, |
|
32 |
)
|
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
33 |
|
34 |
||
35 |
class FakeCodec(object): |
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
36 |
"""Special class that helps testing over several non-existed encodings. |
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
37 |
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
38 |
Clients can add new encoding names, but because of how codecs is
|
39 |
implemented they cannot be removed. Be careful with naming to avoid
|
|
40 |
collisions between tests.
|
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
41 |
"""
|
42 |
_registered = False |
|
43 |
_enabled_encodings = set() |
|
44 |
||
45 |
def add(self, encoding_name): |
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
46 |
"""Adding encoding name to fake. |
47 |
||
48 |
:type encoding_name: lowercase plain string
|
|
49 |
"""
|
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
50 |
if not self._registered: |
51 |
codecs.register(self) |
|
52 |
self._registered = True |
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
53 |
if encoding_name is not None: |
54 |
self._enabled_encodings.add(encoding_name) |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
55 |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
56 |
def __call__(self, encoding_name): |
57 |
"""Called indirectly by codecs module during lookup""" |
|
58 |
if encoding_name in self._enabled_encodings: |
|
59 |
return codecs.lookup('latin-1') |
|
2192.1.6
by Alexander Belchenko
Fixes after Aaron's review |
60 |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
61 |
|
62 |
fake_codec = FakeCodec() |
|
63 |
||
64 |
||
65 |
class TestFakeCodec(TestCase): |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
66 |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
67 |
def test_fake_codec(self): |
68 |
self.assertRaises(LookupError, codecs.lookup, 'fake') |
|
69 |
||
70 |
fake_codec.add('fake') |
|
71 |
codecs.lookup('fake') |
|
72 |
||
73 |
||
74 |
class TestTerminalEncoding(TestCase): |
|
75 |
"""Test the auto-detection of proper terminal encoding.""" |
|
76 |
||
77 |
def setUp(self): |
|
6552.1.4
by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super(). |
78 |
super(TestTerminalEncoding, self).setUp() |
4985.1.5
by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity |
79 |
self.overrideAttr(sys, 'stdin') |
80 |
self.overrideAttr(sys, 'stdout') |
|
81 |
self.overrideAttr(sys, 'stderr') |
|
82 |
self.overrideAttr(osutils, '_cached_user_encoding') |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
83 |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
84 |
def make_wrapped_streams(self, |
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
85 |
stdout_encoding, |
86 |
stderr_encoding, |
|
87 |
stdin_encoding, |
|
88 |
user_encoding='user_encoding', |
|
89 |
enable_fake_encodings=True): |
|
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
90 |
sys.stdout = StringIOWithEncoding() |
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
91 |
sys.stdout.encoding = stdout_encoding |
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
92 |
sys.stderr = StringIOWithEncoding() |
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
93 |
sys.stderr.encoding = stderr_encoding |
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
94 |
sys.stdin = StringIOWithEncoding() |
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
95 |
sys.stdin.encoding = stdin_encoding |
3224.5.4
by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding. |
96 |
osutils._cached_user_encoding = user_encoding |
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
97 |
if enable_fake_encodings: |
98 |
fake_codec.add(stdout_encoding) |
|
99 |
fake_codec.add(stderr_encoding) |
|
100 |
fake_codec.add(stdin_encoding) |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
101 |
|
102 |
def test_get_terminal_encoding(self): |
|
103 |
self.make_wrapped_streams('stdout_encoding', |
|
104 |
'stderr_encoding', |
|
105 |
'stdin_encoding') |
|
106 |
||
107 |
# first preference is stdout encoding
|
|
108 |
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding()) |
|
109 |
||
110 |
sys.stdout.encoding = None |
|
111 |
# if sys.stdout is None, fall back to sys.stdin
|
|
112 |
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding()) |
|
113 |
||
114 |
sys.stdin.encoding = None |
|
3224.5.4
by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding. |
115 |
# and in the worst case, use osutils.get_user_encoding()
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
116 |
self.assertEqual('user_encoding', osutils.get_terminal_encoding()) |
117 |
||
5320.2.4
by Robert Collins
``bzrlib.osutils.get_terminal_encoding`` will now only mutter its |
118 |
def test_get_terminal_encoding_silent(self): |
119 |
self.make_wrapped_streams('stdout_encoding', |
|
120 |
'stderr_encoding', |
|
121 |
'stdin_encoding') |
|
122 |
# Calling get_terminal_encoding should not mutter when silent=True is
|
|
123 |
# passed.
|
|
124 |
log = self.get_log() |
|
125 |
osutils.get_terminal_encoding() |
|
126 |
self.assertEqual(log, self.get_log()) |
|
127 |
||
128 |
def test_get_terminal_encoding_trace(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(trace=True) |
|
136 |
self.assertNotEqual(log, self.get_log()) |
|
137 |
||
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
138 |
def test_terminal_cp0(self): |
2192.1.6
by Alexander Belchenko
Fixes after Aaron's review |
139 |
# test cp0 encoding (Windows returns cp0 when there is no encoding)
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
140 |
self.make_wrapped_streams('cp0', |
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
141 |
'cp0', |
142 |
'cp0', |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
143 |
user_encoding='latin-1', |
144 |
enable_fake_encodings=False) |
|
145 |
||
146 |
# cp0 is invalid encoding. We should fall back to user_encoding
|
|
147 |
self.assertEqual('latin-1', osutils.get_terminal_encoding()) |
|
148 |
||
149 |
# check stderr
|
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
150 |
self.assertEqual('', sys.stderr.getvalue()) |
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
151 |
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
152 |
def test_terminal_cp_unknown(self): |
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
153 |
# test against really unknown encoding
|
154 |
# catch warning at stderr
|
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
155 |
self.make_wrapped_streams('cp-unknown', |
156 |
'cp-unknown', |
|
157 |
'cp-unknown', |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
158 |
user_encoding='latin-1', |
159 |
enable_fake_encodings=False) |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
160 |
|
2192.1.2
by Alexander Belchenko
Tests for osutils.get_terminal_encoding() |
161 |
self.assertEqual('latin-1', osutils.get_terminal_encoding()) |
162 |
||
163 |
# check stderr
|
|
6622.1.31
by Jelmer Vernooij
Fix more tests. |
164 |
self.assertEqual('brz: warning: unknown terminal encoding cp-unknown.\n' |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
165 |
' Using encoding latin-1 instead.\n', |
166 |
sys.stderr.getvalue()) |
|
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
167 |
|
168 |
||
169 |
class TestUserEncoding(TestCase): |
|
170 |
"""Test detection of default user encoding.""" |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
171 |
|
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
172 |
def setUp(self): |
6552.1.4
by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super(). |
173 |
super(TestUserEncoding, self).setUp() |
6383.1.3
by Martin Packman
Deprecate public use_cache parameter in favour of test specific override |
174 |
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 |
175 |
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 |
176 |
self.overrideAttr(locale, 'CODESET', None) |
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
177 |
self.overrideAttr(sys, 'stderr', StringIOWithEncoding()) |
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
178 |
|
6383.1.4
by Martin Packman
Simplify tests a little and make it clear setlocale is not being used |
179 |
def get_encoding(self, do_setlocale=True): |
180 |
return self._encoding |
|
181 |
||
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
182 |
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 |
183 |
self._encoding = 'user_encoding' |
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
184 |
fake_codec.add('user_encoding') |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
185 |
self.assertEqual('iso8859-1', # fake_codec maps to latin-1 |
186 |
osutils.get_user_encoding()) |
|
7045.4.16
by Jelmer Vernooij
Fix some osutils tests. |
187 |
self.assertEqual('', sys.stderr.getvalue()) |
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
188 |
|
189 |
def test_user_cp0(self): |
|
6383.1.4
by Martin Packman
Simplify tests a little and make it clear setlocale is not being used |
190 |
self._encoding = 'cp0' |
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
191 |
self.assertEqual('ascii', osutils.get_user_encoding()) |
7045.4.16
by Jelmer Vernooij
Fix some osutils tests. |
192 |
self.assertEqual('', sys.stderr.getvalue()) |
2192.1.3
by Alexander Belchenko
Tests for osutils.get_user_encoding |
193 |
|
2192.1.8
by Alexander Belchenko
Rework tests after John's review |
194 |
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 |
195 |
self._encoding = 'cp-unknown' |
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
196 |
self.assertEqual('ascii', osutils.get_user_encoding()) |
7045.4.16
by Jelmer Vernooij
Fix some osutils tests. |
197 |
self.assertEqual('brz: warning: unknown encoding cp-unknown.' |
198 |
' Continuing with ascii encoding.\n', |
|
199 |
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 |
200 |
|
3405.3.1
by Neil Martinsen-Burrell
accept for an encoding to mean ascii |
201 |
def test_user_empty(self): |
202 |
"""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 |
203 |
self._encoding = '' |
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()) |