1
# Copyright (C) 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
19
Black-box tests for encoding of bzr status.
21
Status command usually prints output (possible unicode) to sys.stdout.
22
When status output is redirected to a file or pipe, or the encoding of sys.stdout
23
is not able to encode non-ascii filenames then status
24
used to fail because of UnicodeEncode error:
25
bzr: ERROR: exceptions.UnicodeEncodeError: 'ascii' codec can't encode characters: ordinal not in range(128)
27
In case when sys.stdout.encoding is None
28
bzr should use bzrlib.user_encoding for output.
29
bzr should also use `replace` error handling scheme, to allow
30
status to run even if the exact filenames cannot be displayed.
33
from cStringIO import StringIO
38
from bzrlib.branch import Branch
39
from bzrlib.tests import TestCaseInTempDir, TestSkipped
40
from bzrlib.trace import mutter
43
class TestStatusEncodings(TestCaseInTempDir):
46
TestCaseInTempDir.setUp(self)
47
self.user_encoding = bzrlib.user_encoding
48
self.stdout = sys.stdout
51
bzrlib.user_encoding = self.user_encoding
52
sys.stdout = self.stdout
53
TestCaseInTempDir.tearDown(self)
55
def make_uncommitted_tree(self):
56
"""Build a branch with uncommitted unicode named changes in the cwd."""
57
b = Branch.initialize(u'.')
58
working_tree = b.working_tree()
59
filename = u'hell\u00d8'
61
self.build_tree_contents([(filename, 'contents of hello')])
62
except UnicodeEncodeError:
63
raise TestSkipped("can't build unicode working tree in "
64
"filesystem encoding %s" % sys.getfilesystemencoding())
65
working_tree.add(filename)
68
def test_stdout_ascii(self):
69
sys.stdout = StringIO()
70
bzrlib.user_encoding = 'ascii'
71
working_tree = self.make_uncommitted_tree()
72
stdout, stderr = self.run_bzr("status")
74
self.assertEquals(stdout, """\
79
def test_stdout_latin1(self):
80
sys.stdout = StringIO()
81
bzrlib.user_encoding = 'latin-1'
82
working_tree = self.make_uncommitted_tree()
83
stdout, stderr = self.run_bzr('status')
85
self.assertEquals(stdout, u"""\
88
""".encode('latin-1'))