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