~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Martin Pool
  • Date: 2010-06-02 05:03:31 UTC
  • mto: This revision was merged to the branch mainline in revision 5279.
  • Revision ID: mbp@canonical.com-20100602050331-n2p1qt8hfsahspnv
Correct more sloppy use of the term 'Linux'

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
21
21
import os
22
22
import sys
23
23
 
24
 
import bzrlib
25
24
from bzrlib import (
26
25
    errors,
27
26
    osutils,
28
27
    )
29
28
from bzrlib.tests import (
30
29
        StringIOWrapper,
31
 
        TestCase, 
32
 
        TestSkipped,
 
30
        TestCase,
33
31
        )
34
32
 
35
33
 
36
34
class FakeCodec(object):
37
35
    """Special class that helps testing over several non-existed encodings.
38
 
    
 
36
 
39
37
    Clients can add new encoding names, but because of how codecs is
40
38
    implemented they cannot be removed. Be careful with naming to avoid
41
39
    collisions between tests.
53
51
            self._registered = True
54
52
        if encoding_name is not None:
55
53
            self._enabled_encodings.add(encoding_name)
56
 
        
 
54
 
57
55
    def __call__(self, encoding_name):
58
56
        """Called indirectly by codecs module during lookup"""
59
57
        if encoding_name in self._enabled_encodings:
64
62
 
65
63
 
66
64
class TestFakeCodec(TestCase):
67
 
    
 
65
 
68
66
    def test_fake_codec(self):
69
67
        self.assertRaises(LookupError, codecs.lookup, 'fake')
70
68
 
76
74
    """Test the auto-detection of proper terminal encoding."""
77
75
 
78
76
    def setUp(self):
79
 
        self._stdout = sys.stdout
80
 
        self._stderr = sys.stderr
81
 
        self._stdin = sys.stdin
82
 
        self._user_encoding = bzrlib.user_encoding
83
 
 
84
 
        self.addCleanup(self._reset)
85
 
 
86
 
    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,
87
84
                             stdout_encoding,
88
85
                             stderr_encoding,
89
86
                             stdin_encoding,
95
92
        sys.stderr.encoding = stderr_encoding
96
93
        sys.stdin = StringIOWrapper()
97
94
        sys.stdin.encoding = stdin_encoding
98
 
        bzrlib.user_encoding = user_encoding
 
95
        osutils._cached_user_encoding = user_encoding
99
96
        if enable_fake_encodings:
100
97
            fake_codec.add(stdout_encoding)
101
98
            fake_codec.add(stderr_encoding)
102
99
            fake_codec.add(stdin_encoding)
103
100
 
104
 
    def _reset(self):
105
 
        sys.stdout = self._stdout
106
 
        sys.stderr = self._stderr
107
 
        sys.stdin = self._stdin
108
 
        bzrlib.user_encoding = self._user_encoding
109
 
 
110
101
    def test_get_terminal_encoding(self):
111
102
        self.make_wrapped_streams('stdout_encoding',
112
103
                                  'stderr_encoding',
120
111
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
121
112
 
122
113
        sys.stdin.encoding = None
123
 
        # and in the worst case, use bzrlib.user_encoding
 
114
        # and in the worst case, use osutils.get_user_encoding()
124
115
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
125
116
 
126
117
    def test_terminal_cp0(self):
145
136
                                  'cp-unknown',
146
137
                                  user_encoding='latin-1',
147
138
                                  enable_fake_encodings=False)
148
 
        
 
139
 
149
140
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
150
141
 
151
142
        # check stderr
152
143
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
153
 
                          '  Using encoding latin-1 instead.\n', 
 
144
                          '  Using encoding latin-1 instead.\n',
154
145
                          sys.stderr.getvalue())
155
146
 
156
147
 
157
148
class TestUserEncoding(TestCase):
158
149
    """Test detection of default user encoding."""
159
 
    
 
150
 
160
151
    def 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)
 
152
        TestCase.setUp(self)
 
153
        self.overrideAttr(locale, 'getpreferredencoding')
 
154
        self.addCleanup(osutils.set_or_unset_env,
 
155
                        'LANG', os.environ.get('LANG'))
 
156
        self.overrideAttr(sys, 'stderr', StringIOWrapper())
173
157
 
174
158
    def test_get_user_encoding(self):
175
159
        def f():
198
182
                          ' Continuing with ascii encoding.\n',
199
183
                          sys.stderr.getvalue())
200
184
 
 
185
    def test_user_empty(self):
 
186
        """Running bzr from a vim script gives '' for a preferred locale"""
 
187
        def f():
 
188
            return ''
 
189
 
 
190
        locale.getpreferredencoding = f
 
191
        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
 
192
        self.assertEquals('', sys.stderr.getvalue())
 
193
 
201
194
    def test_user_locale_error(self):
202
195
        def f():
203
196
            raise locale.Error, 'unsupported locale'