~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-07-19 16:09:34 UTC
  • mfrom: (2520.4.135 bzr.mpbundle)
  • Revision ID: pqm@pqm.ubuntu.com-20070719160934-d51fyijw69oto88p
Add new bundle and merge-directive formats

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
19
import codecs
20
 
import errno
21
20
import locale
22
21
import os
23
22
import sys
24
23
 
 
24
import bzrlib
25
25
from bzrlib import (
 
26
    errors,
26
27
    osutils,
27
28
    )
28
29
from bzrlib.tests import (
29
30
        StringIOWrapper,
30
 
        TestCase,
 
31
        TestCase, 
 
32
        TestSkipped,
31
33
        )
32
34
 
33
35
 
34
36
class FakeCodec(object):
35
37
    """Special class that helps testing over several non-existed encodings.
36
 
 
 
38
    
37
39
    Clients can add new encoding names, but because of how codecs is
38
40
    implemented they cannot be removed. Be careful with naming to avoid
39
41
    collisions between tests.
51
53
            self._registered = True
52
54
        if encoding_name is not None:
53
55
            self._enabled_encodings.add(encoding_name)
54
 
 
 
56
        
55
57
    def __call__(self, encoding_name):
56
58
        """Called indirectly by codecs module during lookup"""
57
59
        if encoding_name in self._enabled_encodings:
62
64
 
63
65
 
64
66
class TestFakeCodec(TestCase):
65
 
 
 
67
    
66
68
    def test_fake_codec(self):
67
69
        self.assertRaises(LookupError, codecs.lookup, 'fake')
68
70
 
74
76
    """Test the auto-detection of proper terminal encoding."""
75
77
 
76
78
    def setUp(self):
77
 
        TestCase.setUp(self)
78
 
        self.overrideAttr(sys, 'stdin')
79
 
        self.overrideAttr(sys, 'stdout')
80
 
        self.overrideAttr(sys, 'stderr')
81
 
        self.overrideAttr(osutils, '_cached_user_encoding')
82
 
 
83
 
    def make_wrapped_streams(self,
 
79
        self._stdout = sys.stdout
 
80
        self._stderr = sys.stderr
 
81
        self._stdin = sys.stdin
 
82
        self._user_encoding = bzrlib.user_encoding
 
83
 
 
84
        self.addCleanup(self._reset)
 
85
 
 
86
    def make_wrapped_streams(self, 
84
87
                             stdout_encoding,
85
88
                             stderr_encoding,
86
89
                             stdin_encoding,
92
95
        sys.stderr.encoding = stderr_encoding
93
96
        sys.stdin = StringIOWrapper()
94
97
        sys.stdin.encoding = stdin_encoding
95
 
        osutils._cached_user_encoding = user_encoding
 
98
        bzrlib.user_encoding = user_encoding
96
99
        if enable_fake_encodings:
97
100
            fake_codec.add(stdout_encoding)
98
101
            fake_codec.add(stderr_encoding)
99
102
            fake_codec.add(stdin_encoding)
100
103
 
 
104
    def _reset(self):
 
105
        sys.stdout = self._stdout
 
106
        sys.stderr = self._stderr
 
107
        sys.stdin = self._stdin
 
108
        bzrlib.user_encoding = self._user_encoding
 
109
 
101
110
    def test_get_terminal_encoding(self):
102
111
        self.make_wrapped_streams('stdout_encoding',
103
112
                                  'stderr_encoding',
111
120
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
112
121
 
113
122
        sys.stdin.encoding = None
114
 
        # and in the worst case, use osutils.get_user_encoding()
 
123
        # and in the worst case, use bzrlib.user_encoding
115
124
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
116
125
 
117
 
    def test_get_terminal_encoding_silent(self):
118
 
        self.make_wrapped_streams('stdout_encoding',
119
 
                                  'stderr_encoding',
120
 
                                  'stdin_encoding')
121
 
        # Calling get_terminal_encoding should not mutter when silent=True is
122
 
        # passed.
123
 
        log = self.get_log()
124
 
        osutils.get_terminal_encoding()
125
 
        self.assertEqual(log, self.get_log())
126
 
 
127
 
    def test_get_terminal_encoding_trace(self):
128
 
        self.make_wrapped_streams('stdout_encoding',
129
 
                                  'stderr_encoding',
130
 
                                  'stdin_encoding')
131
 
        # Calling get_terminal_encoding should not mutter when silent=True is
132
 
        # passed.
133
 
        log = self.get_log()
134
 
        osutils.get_terminal_encoding(trace=True)
135
 
        self.assertNotEqual(log, self.get_log())
136
 
 
137
126
    def test_terminal_cp0(self):
138
127
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
139
128
        self.make_wrapped_streams('cp0',
156
145
                                  'cp-unknown',
157
146
                                  user_encoding='latin-1',
158
147
                                  enable_fake_encodings=False)
159
 
 
 
148
        
160
149
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
161
150
 
162
151
        # check stderr
163
152
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
164
 
                          '  Using encoding latin-1 instead.\n',
 
153
                          '  Using encoding latin-1 instead.\n', 
165
154
                          sys.stderr.getvalue())
166
155
 
167
156
 
168
157
class TestUserEncoding(TestCase):
169
158
    """Test detection of default user encoding."""
170
 
 
 
159
    
171
160
    def setUp(self):
172
 
        TestCase.setUp(self)
173
 
        self.overrideAttr(locale, 'getpreferredencoding')
174
 
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
 
161
        self._stderr = sys.stderr
 
162
        self._getpreferredencoding = locale.getpreferredencoding
 
163
        self.addCleanup(self._reset)
 
164
        sys.stderr = StringIOWrapper()
 
165
        # save $LANG
 
166
        self._LANG = os.environ.get('LANG')
 
167
 
 
168
    def _reset(self):
 
169
        locale.getpreferredencoding = self._getpreferredencoding
 
170
        sys.stderr = self._stderr
 
171
        # restore $LANG
 
172
        osutils.set_or_unset_env('LANG', self._LANG)
175
173
 
176
174
    def test_get_user_encoding(self):
177
175
        def f():
179
177
 
180
178
        locale.getpreferredencoding = f
181
179
        fake_codec.add('user_encoding')
182
 
        self.assertEquals('user_encoding',
183
 
                          osutils.get_user_encoding(use_cache=False))
 
180
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
184
181
        self.assertEquals('', sys.stderr.getvalue())
185
182
 
186
183
    def test_user_cp0(self):
201
198
                          ' Continuing with ascii encoding.\n',
202
199
                          sys.stderr.getvalue())
203
200
 
204
 
    def test_user_empty(self):
205
 
        """Running bzr from a vim script gives '' for a preferred locale"""
206
 
        def f():
207
 
            return ''
208
 
 
209
 
        locale.getpreferredencoding = f
210
 
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
211
 
        self.assertEquals('', sys.stderr.getvalue())
212
 
 
213
201
    def test_user_locale_error(self):
214
202
        def f():
215
203
            raise locale.Error, 'unsupported locale'
216
204
 
217
205
        locale.getpreferredencoding = f
218
 
        self.overrideEnv('LANG', 'BOGUS')
 
206
        os.environ['LANG'] = 'BOGUS'
219
207
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
220
208
        self.assertEquals('bzr: warning: unsupported locale\n'
221
209
                          '  Could not determine what text encoding to use.\n'
223
211
                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
224
212
                          '  Continuing with ascii encoding.\n',
225
213
                          sys.stderr.getvalue())
226
 
 
227
 
 
228
 
class TestMessageEncoding(TestCase):
229
 
    """Tests for getting the encoding used by system messages"""
230
 
 
231
 
    def test_get_message_encoding(self):
232
 
        encoding_name = osutils.get_message_encoding()
233
 
        "".decode(encoding_name) # should be a valid encoding name
234
 
 
235
 
    def test_get_message_encoding_decodes_strerror(self):
236
 
        encoding_name = osutils.get_message_encoding()
237
 
        for number, name in errno.errorcode.iteritems():
238
 
            string = os.strerror(number)
239
 
            string.decode(encoding_name)