~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-04-08 06:17:41 UTC
  • mfrom: (4797.33.16 apport)
  • Revision ID: pqm@pqm.ubuntu.com-20100408061741-m7vl6z97vu33riv7
(robertc) Make sure ExecutablePath and InterpreterPath are set in
        Apport. (Martin Pool, James Westby, lp:528114)

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, 
 
30
        TestCase,
32
31
        )
33
32
 
34
33
 
35
34
class FakeCodec(object):
36
35
    """Special class that helps testing over several non-existed encodings.
37
 
    
 
36
 
38
37
    Clients can add new encoding names, but because of how codecs is
39
38
    implemented they cannot be removed. Be careful with naming to avoid
40
39
    collisions between tests.
52
51
            self._registered = True
53
52
        if encoding_name is not None:
54
53
            self._enabled_encodings.add(encoding_name)
55
 
        
 
54
 
56
55
    def __call__(self, encoding_name):
57
56
        """Called indirectly by codecs module during lookup"""
58
57
        if encoding_name in self._enabled_encodings:
63
62
 
64
63
 
65
64
class TestFakeCodec(TestCase):
66
 
    
 
65
 
67
66
    def test_fake_codec(self):
68
67
        self.assertRaises(LookupError, codecs.lookup, 'fake')
69
68
 
75
74
    """Test the auto-detection of proper terminal encoding."""
76
75
 
77
76
    def setUp(self):
78
 
        self._stdout = sys.stdout
79
 
        self._stderr = sys.stderr
80
 
        self._stdin = sys.stdin
81
 
        self._user_encoding = bzrlib.user_encoding
82
 
 
83
 
        self.addCleanup(self._reset)
84
 
 
85
 
    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,
86
84
                             stdout_encoding,
87
85
                             stderr_encoding,
88
86
                             stdin_encoding,
94
92
        sys.stderr.encoding = stderr_encoding
95
93
        sys.stdin = StringIOWrapper()
96
94
        sys.stdin.encoding = stdin_encoding
97
 
        bzrlib.user_encoding = user_encoding
 
95
        osutils._cached_user_encoding = user_encoding
98
96
        if enable_fake_encodings:
99
97
            fake_codec.add(stdout_encoding)
100
98
            fake_codec.add(stderr_encoding)
101
99
            fake_codec.add(stdin_encoding)
102
100
 
103
 
    def _reset(self):
104
 
        sys.stdout = self._stdout
105
 
        sys.stderr = self._stderr
106
 
        sys.stdin = self._stdin
107
 
        bzrlib.user_encoding = self._user_encoding
108
 
 
109
101
    def test_get_terminal_encoding(self):
110
102
        self.make_wrapped_streams('stdout_encoding',
111
103
                                  'stderr_encoding',
119
111
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
120
112
 
121
113
        sys.stdin.encoding = None
122
 
        # and in the worst case, use bzrlib.user_encoding
 
114
        # and in the worst case, use osutils.get_user_encoding()
123
115
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
124
116
 
125
117
    def test_terminal_cp0(self):
144
136
                                  'cp-unknown',
145
137
                                  user_encoding='latin-1',
146
138
                                  enable_fake_encodings=False)
147
 
        
 
139
 
148
140
        self.assertEqual('latin-1', osutils.get_terminal_encoding())
149
141
 
150
142
        # check stderr
151
143
        self.assertEquals('bzr: warning: unknown terminal encoding cp-unknown.\n'
152
 
                          '  Using encoding latin-1 instead.\n', 
 
144
                          '  Using encoding latin-1 instead.\n',
153
145
                          sys.stderr.getvalue())
154
146
 
155
147
 
156
148
class TestUserEncoding(TestCase):
157
149
    """Test detection of default user encoding."""
158
 
    
 
150
 
159
151
    def setUp(self):
160
 
        self._stderr = sys.stderr
161
 
        self._getpreferredencoding = locale.getpreferredencoding
162
 
        self.addCleanup(self._reset)
163
 
        sys.stderr = StringIOWrapper()
164
 
        # save $LANG
165
 
        self._LANG = os.environ.get('LANG')
166
 
 
167
 
    def _reset(self):
168
 
        locale.getpreferredencoding = self._getpreferredencoding
169
 
        sys.stderr = self._stderr
170
 
        # restore $LANG
171
 
        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())
172
157
 
173
158
    def test_get_user_encoding(self):
174
159
        def f():