~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
19
import codecs
 
20
import errno
20
21
import locale
21
22
import os
22
23
import sys
23
24
 
24
25
from bzrlib import (
25
 
    errors,
26
26
    osutils,
27
27
    )
28
28
from bzrlib.tests import (
75
75
 
76
76
    def setUp(self):
77
77
        TestCase.setUp(self)
78
 
        self._stdout = sys.stdout
79
 
        self._stderr = sys.stderr
80
 
        self._stdin = sys.stdin
81
 
        self._user_encoding = osutils._cached_user_encoding
82
 
 
83
 
        self.addCleanup(self._reset)
 
78
        self.overrideAttr(sys, 'stdin')
 
79
        self.overrideAttr(sys, 'stdout')
 
80
        self.overrideAttr(sys, 'stderr')
 
81
        self.overrideAttr(osutils, '_cached_user_encoding')
84
82
 
85
83
    def make_wrapped_streams(self,
86
84
                             stdout_encoding,
100
98
            fake_codec.add(stderr_encoding)
101
99
            fake_codec.add(stdin_encoding)
102
100
 
103
 
    def _reset(self):
104
 
        sys.stdout = self._stdout
105
 
        sys.stderr = self._stderr
106
 
        sys.stdin = self._stdin
107
 
        osutils._cached_user_encoding = self._user_encoding
108
 
 
109
101
    def test_get_terminal_encoding(self):
110
102
        self.make_wrapped_streams('stdout_encoding',
111
103
                                  'stderr_encoding',
122
114
        # and in the worst case, use osutils.get_user_encoding()
123
115
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
124
116
 
 
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
 
125
137
    def test_terminal_cp0(self):
126
138
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
127
139
        self.make_wrapped_streams('cp0',
158
170
 
159
171
    def setUp(self):
160
172
        TestCase.setUp(self)
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)
 
173
        self.overrideAttr(locale, 'getpreferredencoding')
 
174
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
173
175
 
174
176
    def test_get_user_encoding(self):
175
177
        def f():
177
179
 
178
180
        locale.getpreferredencoding = f
179
181
        fake_codec.add('user_encoding')
180
 
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
 
182
        self.assertEquals('user_encoding',
 
183
                          osutils.get_user_encoding(use_cache=False))
181
184
        self.assertEquals('', sys.stderr.getvalue())
182
185
 
183
186
    def test_user_cp0(self):
212
215
            raise locale.Error, 'unsupported locale'
213
216
 
214
217
        locale.getpreferredencoding = f
215
 
        os.environ['LANG'] = 'BOGUS'
 
218
        self.overrideEnv('LANG', 'BOGUS')
216
219
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
217
220
        self.assertEquals('bzr: warning: unsupported locale\n'
218
221
                          '  Could not determine what text encoding to use.\n'
220
223
                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
221
224
                          '  Continuing with ascii encoding.\n',
222
225
                          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)