~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 10:58:39 UTC
  • mfrom: (6383 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219105839-uji05ck4rkm1mj4j
Merge bzr.dev.

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 (
114
114
        # and in the worst case, use osutils.get_user_encoding()
115
115
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
116
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
 
117
137
    def test_terminal_cp0(self):
118
138
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
119
139
        self.make_wrapped_streams('cp0',
151
171
    def setUp(self):
152
172
        TestCase.setUp(self)
153
173
        self.overrideAttr(locale, 'getpreferredencoding')
154
 
        self.addCleanup(osutils.set_or_unset_env,
155
 
                        'LANG', os.environ.get('LANG'))
156
174
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
157
175
 
158
176
    def test_get_user_encoding(self):
161
179
 
162
180
        locale.getpreferredencoding = f
163
181
        fake_codec.add('user_encoding')
164
 
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
 
182
        self.assertEquals('user_encoding',
 
183
                          osutils.get_user_encoding(use_cache=False))
165
184
        self.assertEquals('', sys.stderr.getvalue())
166
185
 
167
186
    def test_user_cp0(self):
196
215
            raise locale.Error, 'unsupported locale'
197
216
 
198
217
        locale.getpreferredencoding = f
199
 
        os.environ['LANG'] = 'BOGUS'
 
218
        self.overrideEnv('LANG', 'BOGUS')
200
219
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
201
220
        self.assertEquals('bzr: warning: unsupported locale\n'
202
221
                          '  Could not determine what text encoding to use.\n'
204
223
                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
205
224
                          '  Continuing with ascii encoding.\n',
206
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)