1
# Copyright (C) 2005 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
17
"""Test commit message editor.
29
from bzrlib.branch import Branch
30
from bzrlib.config import ensure_config_dir_exists, config_filename
31
from bzrlib.msgeditor import (
32
make_commit_message_template_encoded,
33
edit_commit_message_encoded
35
from bzrlib.tests import (
37
TestCaseWithTransport,
40
from bzrlib.trace import mutter
43
class MsgEditorTest(TestCaseWithTransport):
45
def make_uncommitted_tree(self):
46
"""Build a branch with uncommitted unicode named changes in the cwd."""
47
working_tree = self.make_branch_and_tree('.')
48
b = working_tree.branch
49
filename = u'hell\u00d8'
51
self.build_tree_contents([(filename, 'contents of hello')])
52
except UnicodeEncodeError:
53
raise TestSkipped("can't build unicode working tree in "
54
"filesystem encoding %s" % sys.getfilesystemencoding())
55
working_tree.add(filename)
58
def test_commit_template(self):
59
"""Test building a commit message template"""
60
working_tree = self.make_uncommitted_tree()
61
template = msgeditor.make_commit_message_template(working_tree,
63
self.assertEqualDiff(template,
69
def test_commit_template_encoded(self):
70
"""Test building a commit message template"""
71
working_tree = self.make_uncommitted_tree()
72
template = make_commit_message_template_encoded(working_tree,
74
output_encoding='utf8')
75
self.assertEqualDiff(template,
82
def test_commit_template_and_diff(self):
83
"""Test building a commit message template"""
84
working_tree = self.make_uncommitted_tree()
85
template = make_commit_message_template_encoded(working_tree,
88
output_encoding='utf8')
97
""".encode('utf8') in template)
99
def test_run_editor(self):
100
if sys.platform == "win32":
101
f = file('fed.bat', 'w')
102
f.write('@rem dummy fed')
104
os.environ['BZR_EDITOR'] = 'fed.bat'
106
f = file('fed.sh', 'wb')
107
f.write('#!/bin/sh\n')
109
os.chmod('fed.sh', 0755)
110
os.environ['BZR_EDITOR'] = './fed.sh'
112
self.assertEqual(True, msgeditor._run_editor(''),
113
'Unable to run dummy fake editor')
115
def make_fake_editor(self, message='test message from fed\\n'):
116
"""Set up environment so that an editor will be a known script.
118
Sets up BZR_EDITOR so that if an editor is spawned it will run a
119
script that just adds a known message to the start of the file.
121
f = file('fed.py', 'wb')
122
f.write('#!%s\n' % sys.executable)
126
if len(sys.argv) == 2:
137
if sys.platform == "win32":
138
# [win32] make batch file and set BZR_EDITOR
139
f = file('fed.bat', 'w')
143
""" % sys.executable)
145
os.environ['BZR_EDITOR'] = 'fed.bat'
147
# [non-win32] make python script executable and set BZR_EDITOR
148
os.chmod('fed.py', 0755)
149
os.environ['BZR_EDITOR'] = './fed.py'
151
def test_edit_commit_message(self):
152
working_tree = self.make_uncommitted_tree()
153
self.make_fake_editor()
155
mutter('edit_commit_message without infotext')
156
self.assertEqual('test message from fed\n',
157
msgeditor.edit_commit_message(''))
159
mutter('edit_commit_message with ascii string infotext')
160
self.assertEqual('test message from fed\n',
161
msgeditor.edit_commit_message('spam'))
163
mutter('edit_commit_message with unicode infotext')
164
self.assertEqual('test message from fed\n',
165
msgeditor.edit_commit_message(u'\u1234'))
167
tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
168
self.assertEqual('test message from fed\n', tmpl)
170
def test_start_message(self):
171
self.make_uncommitted_tree()
172
self.make_fake_editor()
173
self.assertEqual('test message from fed\nstart message\n',
174
msgeditor.edit_commit_message('',
175
start_message='start message\n'))
176
self.assertEqual('test message from fed\n',
177
msgeditor.edit_commit_message('',
180
def test_deleted_commit_message(self):
181
working_tree = self.make_uncommitted_tree()
183
if sys.platform == 'win32':
184
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
186
os.environ['BZR_EDITOR'] = 'rm'
188
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
190
def test__get_editor(self):
191
# Test that _get_editor can return a decent list of items
192
bzr_editor = os.environ.get('BZR_EDITOR')
193
visual = os.environ.get('VISUAL')
194
editor = os.environ.get('EDITOR')
196
os.environ['BZR_EDITOR'] = 'bzr_editor'
197
os.environ['VISUAL'] = 'visual'
198
os.environ['EDITOR'] = 'editor'
200
ensure_config_dir_exists()
201
f = open(config_filename(), 'wb')
202
f.write('editor = config_editor\n')
205
editors = list(msgeditor._get_editor())
207
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
208
'editor'], editors[:4])
210
if sys.platform == 'win32':
211
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
213
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
217
# Restore the environment
218
if bzr_editor is None:
219
del os.environ['BZR_EDITOR']
221
os.environ['BZR_EDITOR'] = bzr_editor
223
del os.environ['VISUAL']
225
os.environ['VISUAL'] = visual
227
del os.environ['EDITOR']
229
os.environ['EDITOR'] = editor
231
def test__create_temp_file_with_commit_template(self):
232
# check that commit template written properly
233
# and has platform native line-endings (CRLF on win32)
234
create_file = msgeditor._create_temp_file_with_commit_template
235
msgfilename, hasinfo = create_file('infotext','----','start message')
236
self.assertNotEqual(None, msgfilename)
237
self.assertTrue(hasinfo)
238
expected = os.linesep.join(['start message',
244
self.assertFileEqual(expected, msgfilename)
246
def test__create_temp_file_with_empty_commit_template(self):
248
create_file = msgeditor._create_temp_file_with_commit_template
249
msgfilename, hasinfo = create_file('')
250
self.assertNotEqual(None, msgfilename)
251
self.assertFalse(hasinfo)
252
self.assertFileEqual('', msgfilename)
254
def test_unsupported_encoding_commit_message(self):
255
old_env = osutils.set_or_unset_env('LANG', 'C')
257
# LANG env variable has no effect on Windows
258
# but some characters anyway cannot be represented
259
# in default user encoding
260
char = probe_bad_non_ascii(bzrlib.user_encoding)
262
raise TestSkipped('Cannot find suitable non-ascii character '
263
'for user_encoding (%s)' % bzrlib.user_encoding)
265
self.make_fake_editor(message=char)
267
working_tree = self.make_uncommitted_tree()
268
self.assertRaises(errors.BadCommitMessageEncoding,
269
msgeditor.edit_commit_message, '')
271
osutils.set_or_unset_env('LANG', old_env)