~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Andrew Bennetts
  • Date: 2009-07-27 08:02:52 UTC
  • mto: This revision was merged to the branch mainline in revision 4573.
  • Revision ID: andrew.bennetts@canonical.com-20090727080252-1r4s9oqwlkzgywx7
Fix trivial bug in _vfs_set_tags_bytes.

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
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 (
73
75
 
74
76
    def setUp(self):
75
77
        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')
 
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)
80
84
 
81
85
    def make_wrapped_streams(self,
82
86
                             stdout_encoding,
96
100
            fake_codec.add(stderr_encoding)
97
101
            fake_codec.add(stdin_encoding)
98
102
 
 
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
 
99
109
    def test_get_terminal_encoding(self):
100
110
        self.make_wrapped_streams('stdout_encoding',
101
111
                                  'stderr_encoding',
112
122
        # and in the worst case, use osutils.get_user_encoding()
113
123
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
114
124
 
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
125
    def test_terminal_cp0(self):
136
126
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
137
127
        self.make_wrapped_streams('cp0',
168
158
 
169
159
    def setUp(self):
170
160
        TestCase.setUp(self)
171
 
        self.overrideAttr(locale, 'getpreferredencoding')
172
 
        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)
173
173
 
174
174
    def test_get_user_encoding(self):
175
175
        def f():
177
177
 
178
178
        locale.getpreferredencoding = f
179
179
        fake_codec.add('user_encoding')
180
 
        self.assertEquals('user_encoding',
181
 
                          osutils.get_user_encoding(use_cache=False))
 
180
        self.assertEquals('user_encoding', osutils.get_user_encoding(use_cache=False))
182
181
        self.assertEquals('', sys.stderr.getvalue())
183
182
 
184
183
    def test_user_cp0(self):
213
212
            raise locale.Error, 'unsupported locale'
214
213
 
215
214
        locale.getpreferredencoding = f
216
 
        self.overrideEnv('LANG', 'BOGUS')
 
215
        os.environ['LANG'] = 'BOGUS'
217
216
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
218
217
        self.assertEquals('bzr: warning: unsupported locale\n'
219
218
                          '  Could not determine what text encoding to use.\n'