22
30
from bzrlib.branch import Branch
23
from bzrlib.msgeditor import make_commit_message_template
24
from bzrlib.tests import TestCaseInTempDir, TestSkipped
27
class MsgEditorTest(TestCaseInTempDir):
31
from bzrlib.config import ensure_config_dir_exists, config_filename
32
from bzrlib.msgeditor import (
33
make_commit_message_template_encoded,
34
edit_commit_message_encoded
36
from bzrlib.tests import (
40
TestCaseWithTransport,
44
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
45
from bzrlib.trace import mutter
48
def load_tests(standard_tests, module, loader):
49
"""Parameterize the test for tempfile creation with different encodings."""
50
to_adapt, result = split_suite_by_re(standard_tests,
51
"test__create_temp_file_with_commit_template_in_unicode_dir")
52
for test in iter_suite_tests(to_adapt):
53
result.addTests(EncodingTestAdapter().adapt(test))
57
class MsgEditorTest(TestCaseWithTransport):
29
59
def make_uncommitted_tree(self):
30
60
"""Build a branch with uncommitted unicode named changes in the cwd."""
31
b = Branch.initialize('.')
32
working_tree = b.working_tree()
61
working_tree = self.make_branch_and_tree('.')
62
b = working_tree.branch
33
63
filename = u'hell\u00d8'
35
65
self.build_tree_contents([(filename, 'contents of hello')])
42
72
def test_commit_template(self):
43
73
"""Test building a commit message template"""
44
74
working_tree = self.make_uncommitted_tree()
45
template = make_commit_message_template(working_tree, None)
75
template = msgeditor.make_commit_message_template(working_tree,
46
77
self.assertEqualDiff(template,
83
def test_commit_template_encoded(self):
84
"""Test building a commit message template"""
85
working_tree = self.make_uncommitted_tree()
86
template = make_commit_message_template_encoded(working_tree,
88
output_encoding='utf8')
89
self.assertEqualDiff(template,
96
def test_commit_template_and_diff(self):
97
"""Test building a commit message template"""
98
working_tree = self.make_uncommitted_tree()
99
template = make_commit_message_template_encoded(working_tree,
102
output_encoding='utf8')
108
self.assertTrue(u"""\
111
""".encode('utf8') in template)
113
def test_run_editor(self):
114
if sys.platform == "win32":
115
f = file('fed.bat', 'w')
116
f.write('@rem dummy fed')
118
os.environ['BZR_EDITOR'] = 'fed.bat'
120
f = file('fed.sh', 'wb')
121
f.write('#!/bin/sh\n')
123
os.chmod('fed.sh', 0755)
124
os.environ['BZR_EDITOR'] = './fed.sh'
126
self.assertEqual(True, msgeditor._run_editor(''),
127
'Unable to run dummy fake editor')
129
def make_fake_editor(self, message='test message from fed\\n'):
130
"""Set up environment so that an editor will be a known script.
132
Sets up BZR_EDITOR so that if an editor is spawned it will run a
133
script that just adds a known message to the start of the file.
135
f = file('fed.py', 'wb')
136
f.write('#!%s\n' % sys.executable)
140
if len(sys.argv) == 2:
151
if sys.platform == "win32":
152
# [win32] make batch file and set BZR_EDITOR
153
f = file('fed.bat', 'w')
157
""" % sys.executable)
159
os.environ['BZR_EDITOR'] = 'fed.bat'
161
# [non-win32] make python script executable and set BZR_EDITOR
162
os.chmod('fed.py', 0755)
163
os.environ['BZR_EDITOR'] = './fed.py'
165
def test_edit_commit_message(self):
166
working_tree = self.make_uncommitted_tree()
167
self.make_fake_editor()
169
mutter('edit_commit_message without infotext')
170
self.assertEqual('test message from fed\n',
171
msgeditor.edit_commit_message(''))
173
mutter('edit_commit_message with ascii string infotext')
174
self.assertEqual('test message from fed\n',
175
msgeditor.edit_commit_message('spam'))
177
mutter('edit_commit_message with unicode infotext')
178
self.assertEqual('test message from fed\n',
179
msgeditor.edit_commit_message(u'\u1234'))
181
tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
182
self.assertEqual('test message from fed\n', tmpl)
184
def test_start_message(self):
185
self.make_uncommitted_tree()
186
self.make_fake_editor()
187
self.assertEqual('test message from fed\nstart message\n',
188
msgeditor.edit_commit_message('',
189
start_message='start message\n'))
190
self.assertEqual('test message from fed\n',
191
msgeditor.edit_commit_message('',
194
def test_deleted_commit_message(self):
195
working_tree = self.make_uncommitted_tree()
197
if sys.platform == 'win32':
198
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
200
os.environ['BZR_EDITOR'] = 'rm'
202
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
204
def test__get_editor(self):
205
# Test that _get_editor can return a decent list of items
206
bzr_editor = os.environ.get('BZR_EDITOR')
207
visual = os.environ.get('VISUAL')
208
editor = os.environ.get('EDITOR')
210
os.environ['BZR_EDITOR'] = 'bzr_editor'
211
os.environ['VISUAL'] = 'visual'
212
os.environ['EDITOR'] = 'editor'
214
ensure_config_dir_exists()
215
f = open(config_filename(), 'wb')
216
f.write('editor = config_editor\n')
219
editors = list(msgeditor._get_editor())
221
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
222
'editor'], editors[:4])
224
if sys.platform == 'win32':
225
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
227
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
231
# Restore the environment
232
if bzr_editor is None:
233
del os.environ['BZR_EDITOR']
235
os.environ['BZR_EDITOR'] = bzr_editor
237
del os.environ['VISUAL']
239
os.environ['VISUAL'] = visual
241
del os.environ['EDITOR']
243
os.environ['EDITOR'] = editor
245
def test__create_temp_file_with_commit_template(self):
246
# check that commit template written properly
247
# and has platform native line-endings (CRLF on win32)
248
create_file = msgeditor._create_temp_file_with_commit_template
249
msgfilename, hasinfo = create_file('infotext','----','start message')
250
self.assertNotEqual(None, msgfilename)
251
self.assertTrue(hasinfo)
252
expected = os.linesep.join(['start message',
258
self.assertFileEqual(expected, msgfilename)
260
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
261
self.requireFeature(tests.UnicodeFilenameFeature)
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)