43
52
def test_commit_template(self):
44
53
"""Test building a commit message template"""
45
54
working_tree = self.make_uncommitted_tree()
46
template = make_commit_message_template(working_tree, None)
55
template = msgeditor.make_commit_message_template(working_tree,
47
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, '')
53
192
def test__get_editor(self):
54
193
# Test that _get_editor can return a decent list of items
55
194
bzr_editor = os.environ.get('BZR_EDITOR')
90
229
del os.environ['EDITOR']
92
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)