~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils_encodings.py

  • Committer: John Arbash Meinel
  • Date: 2008-08-25 21:50:11 UTC
  • mfrom: (0.11.3 tools)
  • mto: This revision was merged to the branch mainline in revision 3659.
  • Revision ID: john@arbash-meinel.com-20080825215011-de9esmzgkue3e522
Merge in Lukáš's helper scripts.
Update the packaging documents to describe how to do the releases
using bzr-builddeb to package all distro platforms
simultaneously.

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