~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

Replace remaining to unittest.TestResult methods with super

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
27
27
    )
28
28
from bzrlib.tests import (
29
29
        StringIOWrapper,
30
 
        TestCase, 
 
30
        TestCase,
31
31
        )
32
32
 
33
33
 
34
34
class FakeCodec(object):
35
35
    """Special class that helps testing over several non-existed encodings.
36
 
    
 
36
 
37
37
    Clients can add new encoding names, but because of how codecs is
38
38
    implemented they cannot be removed. Be careful with naming to avoid
39
39
    collisions between tests.
51
51
            self._registered = True
52
52
        if encoding_name is not None:
53
53
            self._enabled_encodings.add(encoding_name)
54
 
        
 
54
 
55
55
    def __call__(self, encoding_name):
56
56
        """Called indirectly by codecs module during lookup"""
57
57
        if encoding_name in self._enabled_encodings:
62
62
 
63
63
 
64
64
class TestFakeCodec(TestCase):
65
 
    
 
65
 
66
66
    def test_fake_codec(self):
67
67
        self.assertRaises(LookupError, codecs.lookup, 'fake')
68
68
 
74
74
    """Test the auto-detection of proper terminal encoding."""
75
75
 
76
76
    def setUp(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, 
 
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,
85
84
                             stdout_encoding,
86
85
                             stderr_encoding,
87
86
                             stdin_encoding,
99
98
            fake_codec.add(stderr_encoding)
100
99
            fake_codec.add(stdin_encoding)
101
100
 
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
 
 
108
101
    def test_get_terminal_encoding(self):
109
102
        self.make_wrapped_streams('stdout_encoding',
110
103
                                  'stderr_encoding',
121
114
        # and in the worst case, use osutils.get_user_encoding()
122
115
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
123
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
 
124
137
    def test_terminal_cp0(self):
125
138
        # test cp0 encoding (Windows returns cp0 when there is no encoding)
126
139
        self.make_wrapped_streams('cp0',
143
156
                                  'cp-unknown',
144
157
                                  user_encoding='latin-1',
145
158
                                  enable_fake_encodings=False)
146
 
        
 
159
 
147
160
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
148
161
 
149
162
        # check stderr
150
163
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
151
 
                          '  Using encoding latin-1 instead.\n', 
 
164
                          '  Using encoding latin-1 instead.\n',
152
165
                          sys.stderr.getvalue())
153
166
 
154
167
 
155
168
class TestUserEncoding(TestCase):
156
169
    """Test detection of default user encoding."""
157
 
    
 
170
 
158
171
    def setUp(self):
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)
 
172
        TestCase.setUp(self)
 
173
        self.overrideAttr(locale, 'getpreferredencoding')
 
174
        self.addCleanup(osutils.set_or_unset_env,
 
175
                        'LANG', os.environ.get('LANG'))
 
176
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
171
177
 
172
178
    def test_get_user_encoding(self):
173
179
        def f():