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.
28
from bzrlib.branch import Branch
29
from bzrlib.config import ensure_config_dir_exists, config_filename
30
from bzrlib.msgeditor import (
31
make_commit_message_template_encoded,
32
edit_commit_message_encoded
34
from bzrlib.tests import TestCaseWithTransport, TestSkipped
35
from bzrlib.trace import mutter
37
class MsgEditorTest(TestCaseWithTransport):
39
def make_uncommitted_tree(self):
40
"""Build a branch with uncommitted unicode named changes in the cwd."""
41
working_tree = self.make_branch_and_tree('.')
42
b = working_tree.branch
43
filename = u'hell\u00d8'
45
self.build_tree_contents([(filename, 'contents of hello')])
46
except UnicodeEncodeError:
47
raise TestSkipped("can't build unicode working tree in "
48
"filesystem encoding %s" % sys.getfilesystemencoding())
49
working_tree.add(filename)
52
def test_commit_template(self):
53
"""Test building a commit message template"""
54
working_tree = self.make_uncommitted_tree()
55
template = msgeditor.make_commit_message_template(working_tree,
57
self.assertEqualDiff(template,
63
def test_commit_template_encoded(self):
64
"""Test building a commit message template"""
65
working_tree = self.make_uncommitted_tree()
66
template = make_commit_message_template_encoded(working_tree,
68
output_encoding='utf8')
69
self.assertEqualDiff(template,
76
def test_commit_template_and_diff(self):
77
"""Test building a commit message template"""
78
working_tree = self.make_uncommitted_tree()
79
template = make_commit_message_template_encoded(working_tree,
82
output_encoding='utf8')
91
""".encode('utf8') in template)
94
super(MsgEditorTest, self).setUp()
95
self._bzr_editor = os.environ.get('BZR_EDITOR', None)
98
if self._bzr_editor is not None:
99
os.environ['BZR_EDITOR'] = self._bzr_editor
101
if os.environ.get('BZR_EDITOR', None) is not None:
102
del os.environ['BZR_EDITOR']
103
super(MsgEditorTest, self).tearDown()
105
def test_run_editor(self):
106
if sys.platform == "win32":
107
f = file('fed.bat', 'w')
108
f.write('@rem dummy fed')
110
os.environ['BZR_EDITOR'] = 'fed.bat'
112
f = file('fed.sh', 'wb')
113
f.write('#!/bin/sh\n')
115
os.chmod('fed.sh', 0755)
116
os.environ['BZR_EDITOR'] = './fed.sh'
118
self.assertEqual(True, msgeditor._run_editor(''),
119
'Unable to run dummy fake editor')
121
def make_fake_editor(self, message='test message from fed\\n'):
122
"""Set up environment so that an editor will be a known script.
124
Sets up BZR_EDITOR so that if an editor is spawned it will run a
125
script that just adds a known message to the start of the file.
127
f = file('fed.py', 'wb')
128
f.write('#!%s\n' % sys.executable)
132
if len(sys.argv) == 2:
143
if sys.platform == "win32":
144
# [win32] make batch file and set BZR_EDITOR
145
f = file('fed.bat', 'w')
149
""" % sys.executable)
151
os.environ['BZR_EDITOR'] = 'fed.bat'
153
# [non-win32] make python script executable and set BZR_EDITOR
154
os.chmod('fed.py', 0755)
155
os.environ['BZR_EDITOR'] = './fed.py'
157
def test_edit_commit_message(self):
158
working_tree = self.make_uncommitted_tree()
159
self.make_fake_editor()
161
mutter('edit_commit_message without infotext')
162
self.assertEqual('test message from fed\n',
163
msgeditor.edit_commit_message(''))
165
mutter('edit_commit_message with unicode infotext')
166
self.assertEqual('test message from fed\n',
167
msgeditor.edit_commit_message(u'\u1234'))
169
tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
170
self.assertEqual('test message from fed\n', tmpl)
172
def test_start_message(self):
173
self.make_uncommitted_tree()
174
self.make_fake_editor()
175
self.assertEqual('test message from fed\nstart message\n',
176
msgeditor.edit_commit_message('',
177
start_message='start message\n'))
178
self.assertEqual('test message from fed\n',
179
msgeditor.edit_commit_message('',
182
def test_deleted_commit_message(self):
183
working_tree = self.make_uncommitted_tree()
185
if sys.platform == 'win32':
186
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
188
os.environ['BZR_EDITOR'] = 'rm'
190
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
192
def test__get_editor(self):
193
# Test that _get_editor can return a decent list of items
194
bzr_editor = os.environ.get('BZR_EDITOR')
195
visual = os.environ.get('VISUAL')
196
editor = os.environ.get('EDITOR')
198
os.environ['BZR_EDITOR'] = 'bzr_editor'
199
os.environ['VISUAL'] = 'visual'
200
os.environ['EDITOR'] = 'editor'
202
ensure_config_dir_exists()
203
f = open(config_filename(), 'wb')
204
f.write('editor = config_editor\n')
207
editors = list(msgeditor._get_editor())
209
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
210
'editor'], editors[:4])
212
if sys.platform == 'win32':
213
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
215
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
219
# Restore the environment
220
if bzr_editor is None:
221
del os.environ['BZR_EDITOR']
223
os.environ['BZR_EDITOR'] = bzr_editor
225
del os.environ['VISUAL']
227
os.environ['VISUAL'] = visual
229
del os.environ['EDITOR']
231
os.environ['EDITOR'] = editor
233
def test__create_temp_file_with_commit_template(self):
234
# check that commit template written properly
235
# and has platform native line-endings (CRLF on win32)
236
create_file = msgeditor._create_temp_file_with_commit_template
237
msgfilename, hasinfo = create_file('infotext','----','start message')
238
self.assertNotEqual(None, msgfilename)
239
self.assertTrue(hasinfo)
240
expected = os.linesep.join(['start message',
246
self.assertFileEqual(expected, msgfilename)
248
def test__create_temp_file_with_empty_commit_template(self):
250
create_file = msgeditor._create_temp_file_with_commit_template
251
msgfilename, hasinfo = create_file('')
252
self.assertNotEqual(None, msgfilename)
253
self.assertFalse(hasinfo)
254
self.assertFileEqual('', msgfilename)
256
def test_unsupported_encoding_commit_message(self):
257
old_env = osutils.set_or_unset_env('LANG', 'C')
259
self.make_fake_editor(message='\xff')
261
working_tree = self.make_uncommitted_tree()
262
self.assertRaises(errors.BadCommitMessageEncoding,
263
msgeditor.edit_commit_message, '')
265
osutils.set_or_unset_env('LANG', old_env)