~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Ian Clatworthy
  • Date: 2008-12-15 04:37:10 UTC
  • mfrom: (3892.1.6 bzr.help-formats)
  • mto: This revision was merged to the branch mainline in revision 3905.
  • Revision ID: ian.clatworthy@canonical.com-20081215043710-ybhxvqjeir13k5ht
Improved help on storage formats (Ian Clatworthy)

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
20
import locale
 
21
import os
21
22
import sys
22
23
 
23
24
from bzrlib import (
 
25
    errors,
24
26
    osutils,
25
27
    )
26
28
from bzrlib.tests import (
27
29
        StringIOWrapper,
28
 
        TestCase,
 
30
        TestCase, 
29
31
        )
30
32
 
31
33
 
32
34
class FakeCodec(object):
33
35
    """Special class that helps testing over several non-existed encodings.
34
 
 
 
36
    
35
37
    Clients can add new encoding names, but because of how codecs is
36
38
    implemented they cannot be removed. Be careful with naming to avoid
37
39
    collisions between tests.
49
51
            self._registered = True
50
52
        if encoding_name is not None:
51
53
            self._enabled_encodings.add(encoding_name)
52
 
 
 
54
        
53
55
    def __call__(self, encoding_name):
54
56
        """Called indirectly by codecs module during lookup"""
55
57
        if encoding_name in self._enabled_encodings:
60
62
 
61
63
 
62
64
class TestFakeCodec(TestCase):
63
 
 
 
65
    
64
66
    def test_fake_codec(self):
65
67
        self.assertRaises(LookupError, codecs.lookup, 'fake')
66
68
 
72
74
    """Test the auto-detection of proper terminal encoding."""
73
75
 
74
76
    def setUp(self):
75
 
        TestCase.setUp(self)
76
 
        self.overrideAttr(sys, 'stdin')
77
 
        self.overrideAttr(sys, 'stdout')
78
 
        self.overrideAttr(sys, 'stderr')
79
 
        self.overrideAttr(osutils, '_cached_user_encoding')
80
 
 
81
 
    def make_wrapped_streams(self,
 
77
        self._stdout = sys.stdout
 
78
        self._stderr = sys.stderr
 
79
        self._stdin = sys.stdin
 
80
        self._user_encoding = osutils._cached_user_encoding
 
81
 
 
82
        self.addCleanup(self._reset)
 
83
 
 
84
    def make_wrapped_streams(self, 
82
85
                             stdout_encoding,
83
86
                             stderr_encoding,
84
87
                             stdin_encoding,
96
99
            fake_codec.add(stderr_encoding)
97
100
            fake_codec.add(stdin_encoding)
98
101
 
 
102
    def _reset(self):
 
103
        sys.stdout = self._stdout
 
104
        sys.stderr = self._stderr
 
105
        sys.stdin = self._stdin
 
106
        osutils._cached_user_encoding = self._user_encoding
 
107
 
99
108
    def test_get_terminal_encoding(self):
100
109
        self.make_wrapped_streams('stdout_encoding',
101
110
                                  'stderr_encoding',
112
121
        # and in the worst case, use osutils.get_user_encoding()
113
122
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
114
123
 
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
 
 
135
124
    def test_terminal_cp0(self):
136
125
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
137
126
        self.make_wrapped_streams('cp0',
154
143
                                  'cp-unknown',
155
144
                                  user_encoding='latin-1',
156
145
                                  enable_fake_encodings=False)
157
 
 
 
146
        
158
147
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
159
148
 
160
149
        # check stderr
161
150
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
162
 
                          '  Using encoding latin-1 instead.\n',
 
151
                          '  Using encoding latin-1 instead.\n', 
163
152
                          sys.stderr.getvalue())
164
153
 
165
154
 
166
155
class TestUserEncoding(TestCase):
167
156
    """Test detection of default user encoding."""
168
 
 
 
157
    
169
158
    def setUp(self):
170
 
        TestCase.setUp(self)
171
 
        self.overrideAttr(locale, 'getpreferredencoding')
172
 
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
 
159
        self._stderr = sys.stderr
 
160
        self._getpreferredencoding = locale.getpreferredencoding
 
161
        self.addCleanup(self._reset)
 
162
        sys.stderr = StringIOWrapper()
 
163
        # save $LANG
 
164
        self._LANG = os.environ.get('LANG')
 
165
 
 
166
    def _reset(self):
 
167
        locale.getpreferredencoding = self._getpreferredencoding
 
168
        sys.stderr = self._stderr
 
169
        # restore $LANG
 
170
        osutils.set_or_unset_env('LANG', self._LANG)
173
171
 
174
172
    def test_get_user_encoding(self):
175
173
        def f():
177
175
 
178
176
        locale.getpreferredencoding = f
179
177
        fake_codec.add('user_encoding')
180
 
        self.assertEquals('user_encoding',
181
 
                          osutils.get_user_encoding(use_cache=False))
 
178
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
182
179
        self.assertEquals('', sys.stderr.getvalue())
183
180
 
184
181
    def test_user_cp0(self):
213
210
            raise locale.Error, 'unsupported locale'
214
211
 
215
212
        locale.getpreferredencoding = f
216
 
        self.overrideEnv('LANG', 'BOGUS')
 
213
        os.environ['LANG'] = 'BOGUS'
217
214
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
218
215
        self.assertEquals('bzr: warning: unsupported locale\n'
219
216
                          '  Could not determine what text encoding to use.\n'