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,
41
from bzrlib.trace import mutter
44
class MsgEditorTest(TestCaseWithTransport):
46
def make_uncommitted_tree(self):
47
"""Build a branch with uncommitted unicode named changes in the cwd."""
48
working_tree = self.make_branch_and_tree('.')
49
b = working_tree.branch
50
filename = u'hell\u00d8'
52
self.build_tree_contents([(filename, 'contents of hello')])
53
except UnicodeEncodeError:
54
raise TestSkipped("can't build unicode working tree in "
55
"filesystem encoding %s" % sys.getfilesystemencoding())
56
working_tree.add(filename)
59
def test_commit_template(self):
60
"""Test building a commit message template"""
61
working_tree = self.make_uncommitted_tree()
62
template = msgeditor.make_commit_message_template(working_tree,
64
self.assertEqualDiff(template,
70
def test_commit_template_encoded(self):
71
"""Test building a commit message template"""
72
working_tree = self.make_uncommitted_tree()
73
template = make_commit_message_template_encoded(working_tree,
75
output_encoding='utf8')
76
self.assertEqualDiff(template,
83
def test_commit_template_and_diff(self):
84
"""Test building a commit message template"""
85
working_tree = self.make_uncommitted_tree()
86
template = make_commit_message_template_encoded(working_tree,
89
output_encoding='utf8')
98
""".encode('utf8') in template)
100
def test_run_editor(self):
101
if sys.platform == "win32":
102
f = file('fed.bat', 'w')
103
f.write('@rem dummy fed')
105
os.environ['BZR_EDITOR'] = 'fed.bat'
107
f = file('fed.sh', 'wb')
108
f.write('#!/bin/sh\n')
110
os.chmod('fed.sh', 0755)
111
os.environ['BZR_EDITOR'] = './fed.sh'
113
self.assertEqual(True, msgeditor._run_editor(''),
114
'Unable to run dummy fake editor')
116
def make_fake_editor(self, message='test message from fed\\n'):
117
"""Set up environment so that an editor will be a known script.
119
Sets up BZR_EDITOR so that if an editor is spawned it will run a
120
script that just adds a known message to the start of the file.
122
f = file('fed.py', 'wb')
123
f.write('#!%s\n' % sys.executable)
127
if len(sys.argv) == 2:
138
if sys.platform == "win32":
139
# [win32] make batch file and set BZR_EDITOR
140
f = file('fed.bat', 'w')
144
""" % sys.executable)
146
os.environ['BZR_EDITOR'] = 'fed.bat'
148
# [non-win32] make python script executable and set BZR_EDITOR
149
os.chmod('fed.py', 0755)
150
os.environ['BZR_EDITOR'] = './fed.py'
152
def test_edit_commit_message(self):
153
working_tree = self.make_uncommitted_tree()
154
self.make_fake_editor()
156
mutter('edit_commit_message without infotext')
157
self.assertEqual('test message from fed\n',
158
msgeditor.edit_commit_message(''))
160
mutter('edit_commit_message with ascii string infotext')
161
self.assertEqual('test message from fed\n',
162
msgeditor.edit_commit_message('spam'))
164
mutter('edit_commit_message with unicode infotext')
165
self.assertEqual('test message from fed\n',
166
msgeditor.edit_commit_message(u'\u1234'))
168
tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
169
self.assertEqual('test message from fed\n', tmpl)
171
def test_start_message(self):
172
self.make_uncommitted_tree()
173
self.make_fake_editor()
174
self.assertEqual('test message from fed\nstart message\n',
175
msgeditor.edit_commit_message('',
176
start_message='start message\n'))
177
self.assertEqual('test message from fed\n',
178
msgeditor.edit_commit_message('',
181
def test_deleted_commit_message(self):
182
working_tree = self.make_uncommitted_tree()
184
if sys.platform == 'win32':
185
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
187
os.environ['BZR_EDITOR'] = 'rm'
189
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
191
def test__get_editor(self):
192
# Test that _get_editor can return a decent list of items
193
bzr_editor = os.environ.get('BZR_EDITOR')
194
visual = os.environ.get('VISUAL')
195
editor = os.environ.get('EDITOR')
197
os.environ['BZR_EDITOR'] = 'bzr_editor'
198
os.environ['VISUAL'] = 'visual'
199
os.environ['EDITOR'] = 'editor'
201
ensure_config_dir_exists()
202
f = open(config_filename(), 'wb')
203
f.write('editor = config_editor\n')
206
editors = list(msgeditor._get_editor())
208
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
209
'editor'], editors[:4])
211
if sys.platform == 'win32':
212
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
214
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
218
# Restore the environment
219
if bzr_editor is None:
220
del os.environ['BZR_EDITOR']
222
os.environ['BZR_EDITOR'] = bzr_editor
224
del os.environ['VISUAL']
226
os.environ['VISUAL'] = visual
228
del os.environ['EDITOR']
230
os.environ['EDITOR'] = editor
232
def test__create_temp_file_with_commit_template(self):
233
# check that commit template written properly
234
# and has platform native line-endings (CRLF on win32)
235
create_file = msgeditor._create_temp_file_with_commit_template
236
msgfilename, hasinfo = create_file('infotext','----','start message')
237
self.assertNotEqual(None, msgfilename)
238
self.assertTrue(hasinfo)
239
expected = os.linesep.join(['start message',
245
self.assertFileEqual(expected, msgfilename)
247
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
248
if hasattr(self, 'info'):
249
os.mkdir(self.info['directory'])
250
os.chdir(self.info['directory'])
251
msgeditor._create_temp_file_with_commit_template('infotext')
253
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
255
def test__create_temp_file_with_empty_commit_template(self):
257
create_file = msgeditor._create_temp_file_with_commit_template
258
msgfilename, hasinfo = create_file('')
259
self.assertNotEqual(None, msgfilename)
260
self.assertFalse(hasinfo)
261
self.assertFileEqual('', msgfilename)
263
def test_unsupported_encoding_commit_message(self):
264
old_env = osutils.set_or_unset_env('LANG', 'C')
266
# LANG env variable has no effect on Windows
267
# but some characters anyway cannot be represented
268
# in default user encoding
269
char = probe_bad_non_ascii(bzrlib.user_encoding)
271
raise TestSkipped('Cannot find suitable non-ascii character '
272
'for user_encoding (%s)' % bzrlib.user_encoding)
274
self.make_fake_editor(message=char)
276
working_tree = self.make_uncommitted_tree()
277
self.assertRaises(errors.BadCommitMessageEncoding,
278
msgeditor.edit_commit_message, '')
280
osutils.set_or_unset_env('LANG', old_env)