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 (
39
TestCaseWithTransport,
43
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
44
from bzrlib.trace import mutter
47
def load_tests(standard_tests, module, loader):
48
"""Parameterize the test for tempfile creation with different encodings."""
49
to_adapt, result = split_suite_by_re(standard_tests,
50
"test__create_temp_file_with_commit_template_in_unicode_dir")
51
for test in iter_suite_tests(to_adapt):
52
result.addTests(EncodingTestAdapter().adapt(test))
56
class MsgEditorTest(TestCaseWithTransport):
58
def make_uncommitted_tree(self):
59
"""Build a branch with uncommitted unicode named changes in the cwd."""
60
working_tree = self.make_branch_and_tree('.')
61
b = working_tree.branch
62
filename = u'hell\u00d8'
64
self.build_tree_contents([(filename, 'contents of hello')])
65
except UnicodeEncodeError:
66
raise TestSkipped("can't build unicode working tree in "
67
"filesystem encoding %s" % sys.getfilesystemencoding())
68
working_tree.add(filename)
71
def test_commit_template(self):
72
"""Test building a commit message template"""
73
working_tree = self.make_uncommitted_tree()
74
template = msgeditor.make_commit_message_template(working_tree,
76
self.assertEqualDiff(template,
82
def test_commit_template_encoded(self):
83
"""Test building a commit message template"""
84
working_tree = self.make_uncommitted_tree()
85
template = make_commit_message_template_encoded(working_tree,
87
output_encoding='utf8')
88
self.assertEqualDiff(template,
95
def test_commit_template_and_diff(self):
96
"""Test building a commit message template"""
97
working_tree = self.make_uncommitted_tree()
98
template = make_commit_message_template_encoded(working_tree,
101
output_encoding='utf8')
107
self.assertTrue(u"""\
110
""".encode('utf8') in template)
112
def test_run_editor(self):
113
if sys.platform == "win32":
114
f = file('fed.bat', 'w')
115
f.write('@rem dummy fed')
117
os.environ['BZR_EDITOR'] = 'fed.bat'
119
f = file('fed.sh', 'wb')
120
f.write('#!/bin/sh\n')
122
os.chmod('fed.sh', 0755)
123
os.environ['BZR_EDITOR'] = './fed.sh'
125
self.assertEqual(True, msgeditor._run_editor(''),
126
'Unable to run dummy fake editor')
128
def make_fake_editor(self, message='test message from fed\\n'):
129
"""Set up environment so that an editor will be a known script.
131
Sets up BZR_EDITOR so that if an editor is spawned it will run a
132
script that just adds a known message to the start of the file.
134
f = file('fed.py', 'wb')
135
f.write('#!%s\n' % sys.executable)
139
if len(sys.argv) == 2:
150
if sys.platform == "win32":
151
# [win32] make batch file and set BZR_EDITOR
152
f = file('fed.bat', 'w')
156
""" % sys.executable)
158
os.environ['BZR_EDITOR'] = 'fed.bat'
160
# [non-win32] make python script executable and set BZR_EDITOR
161
os.chmod('fed.py', 0755)
162
os.environ['BZR_EDITOR'] = './fed.py'
164
def test_edit_commit_message(self):
165
working_tree = self.make_uncommitted_tree()
166
self.make_fake_editor()
168
mutter('edit_commit_message without infotext')
169
self.assertEqual('test message from fed\n',
170
msgeditor.edit_commit_message(''))
172
mutter('edit_commit_message with ascii string infotext')
173
self.assertEqual('test message from fed\n',
174
msgeditor.edit_commit_message('spam'))
176
mutter('edit_commit_message with unicode infotext')
177
self.assertEqual('test message from fed\n',
178
msgeditor.edit_commit_message(u'\u1234'))
180
tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
181
self.assertEqual('test message from fed\n', tmpl)
183
def test_start_message(self):
184
self.make_uncommitted_tree()
185
self.make_fake_editor()
186
self.assertEqual('test message from fed\nstart message\n',
187
msgeditor.edit_commit_message('',
188
start_message='start message\n'))
189
self.assertEqual('test message from fed\n',
190
msgeditor.edit_commit_message('',
193
def test_deleted_commit_message(self):
194
working_tree = self.make_uncommitted_tree()
196
if sys.platform == 'win32':
197
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
199
os.environ['BZR_EDITOR'] = 'rm'
201
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
203
def test__get_editor(self):
204
# Test that _get_editor can return a decent list of items
205
bzr_editor = os.environ.get('BZR_EDITOR')
206
visual = os.environ.get('VISUAL')
207
editor = os.environ.get('EDITOR')
209
os.environ['BZR_EDITOR'] = 'bzr_editor'
210
os.environ['VISUAL'] = 'visual'
211
os.environ['EDITOR'] = 'editor'
213
ensure_config_dir_exists()
214
f = open(config_filename(), 'wb')
215
f.write('editor = config_editor\n')
218
editors = list(msgeditor._get_editor())
220
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
221
'editor'], editors[:4])
223
if sys.platform == 'win32':
224
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
226
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
230
# Restore the environment
231
if bzr_editor is None:
232
del os.environ['BZR_EDITOR']
234
os.environ['BZR_EDITOR'] = bzr_editor
236
del os.environ['VISUAL']
238
os.environ['VISUAL'] = visual
240
del os.environ['EDITOR']
242
os.environ['EDITOR'] = editor
244
def test__create_temp_file_with_commit_template(self):
245
# check that commit template written properly
246
# and has platform native line-endings (CRLF on win32)
247
create_file = msgeditor._create_temp_file_with_commit_template
248
msgfilename, hasinfo = create_file('infotext','----','start message')
249
self.assertNotEqual(None, msgfilename)
250
self.assertTrue(hasinfo)
251
expected = os.linesep.join(['start message',
257
self.assertFileEqual(expected, msgfilename)
259
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
260
from bzrlib.tests.test_diff import UnicodeFilename
261
self.requireFeature(UnicodeFilename)
262
if hasattr(self, 'info'):
263
os.mkdir(self.info['directory'])
264
os.chdir(self.info['directory'])
265
msgeditor._create_temp_file_with_commit_template('infotext')
267
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
269
def test__create_temp_file_with_empty_commit_template(self):
271
create_file = msgeditor._create_temp_file_with_commit_template
272
msgfilename, hasinfo = create_file('')
273
self.assertNotEqual(None, msgfilename)
274
self.assertFalse(hasinfo)
275
self.assertFileEqual('', msgfilename)
277
def test_unsupported_encoding_commit_message(self):
278
old_env = osutils.set_or_unset_env('LANG', 'C')
280
# LANG env variable has no effect on Windows
281
# but some characters anyway cannot be represented
282
# in default user encoding
283
char = probe_bad_non_ascii(bzrlib.user_encoding)
285
raise TestSkipped('Cannot find suitable non-ascii character '
286
'for user_encoding (%s)' % bzrlib.user_encoding)
288
self.make_fake_editor(message=char)
290
working_tree = self.make_uncommitted_tree()
291
self.assertRaises(errors.BadCommitMessageEncoding,
292
msgeditor.edit_commit_message, '')
294
osutils.set_or_unset_env('LANG', old_env)