32
from bzrlib.branch import Branch
33
from bzrlib.config import ensure_config_dir_exists, config_filename
32
34
from bzrlib.msgeditor import (
33
35
make_commit_message_template_encoded,
34
36
edit_commit_message_encoded
142
144
""".encode('utf8') in template)
144
def make_do_nothing_editor(self, basename='fed'):
146
def make_do_nothing_editor(self):
145
147
if sys.platform == "win32":
146
name = basename + '.bat'
148
f = file('fed.bat', 'w')
148
149
f.write('@rem dummy fed')
152
name = basename + '.sh'
153
f = file('fed.sh', 'wb')
154
154
f.write('#!/bin/sh\n')
156
os.chmod('fed.sh', 0755)
159
159
def test_run_editor(self):
160
self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
160
os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
161
161
self.assertEqual(True, msgeditor._run_editor(''),
162
162
'Unable to run dummy fake editor')
164
def test_parse_editor_name(self):
165
"""Correctly interpret names with spaces.
167
See <https://bugs.launchpad.net/bzr/+bug/220331>
169
self.overrideEnv('BZR_EDITOR',
170
'"%s"' % self.make_do_nothing_editor('name with spaces'))
171
self.assertEqual(True, msgeditor._run_editor('a_filename'))
173
164
def make_fake_editor(self, message='test message from fed\\n'):
174
165
"""Set up environment so that an editor will be a known script.
201
192
""" % sys.executable)
203
self.overrideEnv('BZR_EDITOR', 'fed.bat')
194
os.environ['BZR_EDITOR'] = 'fed.bat'
205
196
# [non-win32] make python script executable and set BZR_EDITOR
206
197
os.chmod('fed.py', 0755)
207
self.overrideEnv('BZR_EDITOR', './fed.py')
198
os.environ['BZR_EDITOR'] = './fed.py'
209
200
def test_edit_commit_message(self):
210
201
working_tree = self.make_uncommitted_tree()
239
230
working_tree = self.make_uncommitted_tree()
241
232
if sys.platform == 'win32':
242
editor = 'cmd.exe /c del'
233
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
245
self.overrideEnv('BZR_EDITOR', editor)
235
os.environ['BZR_EDITOR'] = 'rm'
247
237
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
249
239
def test__get_editor(self):
250
self.overrideEnv('BZR_EDITOR', 'bzr_editor')
251
self.overrideEnv('VISUAL', 'visual')
252
self.overrideEnv('EDITOR', 'editor')
240
os.environ['BZR_EDITOR'] = 'bzr_editor'
241
os.environ['VISUAL'] = 'visual'
242
os.environ['EDITOR'] = 'editor'
254
244
conf = config.GlobalConfig.from_string('editor = config_editor\n',
270
260
def test__run_editor_EACCES(self):
271
261
"""If running a configured editor raises EACESS, the user is warned."""
272
self.overrideEnv('BZR_EDITOR', 'eacces.py')
262
os.environ['BZR_EDITOR'] = 'eacces.py'
273
263
f = file('eacces.py', 'wb')
274
264
f.write('# Not a real editor')
277
267
os.chmod('eacces.py', 0)
278
268
# Set $EDITOR so that _run_editor will terminate before trying real
280
self.overrideEnv('EDITOR', self.make_do_nothing_editor())
270
os.environ['EDITOR'] = self.make_do_nothing_editor()
281
271
# Call _run_editor, capturing mutter.warning calls.
283
273
def warning(*args):
311
301
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
312
302
self.requireFeature(tests.UnicodeFilenameFeature)
313
303
if hasattr(self, 'info'):
314
tmpdir = self.info['directory']
316
# Force the creation of temp file in a directory whose name
317
# requires some encoding support
318
msgeditor._create_temp_file_with_commit_template('infotext',
304
os.mkdir(self.info['directory'])
305
os.chdir(self.info['directory'])
306
msgeditor._create_temp_file_with_commit_template('infotext')
321
308
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
329
316
self.assertFileEqual('', msgfilename)
331
318
def test_unsupported_encoding_commit_message(self):
332
self.overrideEnv('LANG', 'C')
333
# LANG env variable has no effect on Windows
334
# but some characters anyway cannot be represented
335
# in default user encoding
336
char = probe_bad_non_ascii(osutils.get_user_encoding())
338
raise TestSkipped('Cannot find suitable non-ascii character '
339
'for user_encoding (%s)' % osutils.get_user_encoding())
341
self.make_fake_editor(message=char)
343
working_tree = self.make_uncommitted_tree()
344
self.assertRaises(errors.BadCommitMessageEncoding,
345
msgeditor.edit_commit_message, '')
347
def test_set_commit_message_no_hooks(self):
348
commit_obj = commit.Commit()
350
msgeditor.set_commit_message(commit_obj))
352
def test_set_commit_message_hook(self):
353
msgeditor.hooks.install_named_hook("set_commit_message",
354
lambda commit_obj, existing_message: "save me some typing\n", None)
355
commit_obj = commit.Commit()
356
self.assertEquals("save me some typing\n",
357
msgeditor.set_commit_message(commit_obj))
319
old_env = osutils.set_or_unset_env('LANG', 'C')
321
# LANG env variable has no effect on Windows
322
# but some characters anyway cannot be represented
323
# in default user encoding
324
char = probe_bad_non_ascii(osutils.get_user_encoding())
326
raise TestSkipped('Cannot find suitable non-ascii character '
327
'for user_encoding (%s)' % osutils.get_user_encoding())
329
self.make_fake_editor(message=char)
331
working_tree = self.make_uncommitted_tree()
332
self.assertRaises(errors.BadCommitMessageEncoding,
333
msgeditor.edit_commit_message, '')
335
osutils.set_or_unset_env('LANG', old_env)
359
337
def test_generate_commit_message_template_no_hooks(self):
360
338
commit_obj = commit.Commit()
380
358
ERROR_BAD_EXE_FORMAT = 193
381
359
file("textfile.txt", "w").close()
382
360
e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
383
self.assertEqual(e.errno, errno.ENOEXEC)
384
self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)
361
# Python2.4 used the 'winerror' as the errno, which confuses a lot of
362
# our error trapping code. Make sure that we understand the mapping
364
if sys.version_info >= (2, 5):
365
self.assertEqual(e.errno, errno.ENOEXEC)
366
self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)
368
self.assertEqual(e.errno, ERROR_BAD_EXE_FORMAT)