1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Black-box tests for bzr log.
24
from bzrlib.tests import TestCaseInTempDir, TestSkipped
26
class TestLog(TestCaseInTempDir):
29
_message = u'Message with \xb5'
31
# Encodings which can encode mu
36
'cp437', # Common windows encoding
37
'cp1251', # Alexander Belchenko's windows encoding
38
'cp1258', # Common windows encoding
40
# Encodings which cannot encode mu
47
def create_branch(self):
50
open('a', 'wb').write('some stuff\n')
52
bzr('commit', '-m', self._message)
54
def try_encoding(self, encoding, fail=False):
57
self.assertRaises(UnicodeEncodeError,
58
self._mu.encode, encoding)
59
encoded_msg = self._message.encode(encoding, 'replace')
61
encoded_msg = self._message.encode(encoding)
63
old_encoding = bzrlib.user_encoding
64
# This test requires that 'run_bzr' uses the current
65
# bzrlib, because we override user_encoding, and expect
68
bzrlib.user_encoding = 'ascii'
69
# We should be able to handle any encoding
70
out, err = bzr('log', encoding=encoding)
72
# Make sure we wrote mu as we expected it to exist
73
self.assertNotEqual(-1, out.find(encoded_msg))
74
out_unicode = out.decode(encoding)
75
self.assertNotEqual(-1, out_unicode.find(self._message))
77
self.assertNotEqual(-1, out.find('Message with ?'))
79
bzrlib.user_encoding = old_encoding
81
def test_log_handles_encoding(self):
84
for encoding in self.good_encodings:
85
self.try_encoding(encoding)
87
def test_log_handles_bad_encoding(self):
90
for encoding in self.bad_encodings:
91
self.try_encoding(encoding, fail=True)
94
class TestLogEncodings(TestCaseInTempDir):
97
TestCaseInTempDir.setUp(self)
98
self.user_encoding = bzrlib.user_encoding
101
bzrlib.user_encoding = self.user_encoding
102
TestCaseInTempDir.tearDown(self)
104
def test_stdout_encoding(self):
106
bzrlib.user_encoding = "cp1251"
109
self.build_tree(['a'])
111
bzr('commit', '-m', u'\u0422\u0435\u0441\u0442')
112
stdout, stderr = self.run_bzr('log', encoding='cp866')
114
message = stdout.splitlines()[-1]
116
# explanation of the check:
117
# u'\u0422\u0435\u0441\u0442' is word 'Test' in russian
118
# in cp866 encoding this is string '\x92\xa5\xe1\xe2'
119
# in cp1251 encoding this is string '\xd2\xe5\xf1\xf2'
120
# This test should check that output of log command
121
# encoded to sys.stdout.encoding
122
test_in_cp866 = '\x92\xa5\xe1\xe2'
123
test_in_cp1251 = '\xd2\xe5\xf1\xf2'
124
# Make sure the log string is encoded in cp866
125
self.assertEquals(test_in_cp866, message[2:])
126
# Make sure the cp1251 string is not found anywhere
127
self.assertEquals(-1, stdout.find(test_in_cp1251))