32
from bzrlib.branch import Branch
33
from bzrlib.config import ensure_config_dir_exists, config_filename
34
32
from bzrlib.msgeditor import (
35
33
make_commit_message_template_encoded,
36
34
edit_commit_message_encoded
38
36
from bzrlib.tests import (
40
39
TestCaseWithTransport,
144
143
""".encode('utf8') in template)
146
def make_do_nothing_editor(self):
145
def make_do_nothing_editor(self, basename='fed'):
147
146
if sys.platform == "win32":
148
f = file('fed.bat', 'w')
147
name = basename + '.bat'
149
149
f.write('@rem dummy fed')
153
f = file('fed.sh', 'wb')
153
name = basename + '.sh'
154
155
f.write('#!/bin/sh\n')
156
os.chmod('fed.sh', 0755)
159
160
def test_run_editor(self):
160
os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
161
self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
161
162
self.assertEqual(True, msgeditor._run_editor(''),
162
163
'Unable to run dummy fake editor')
165
def test_parse_editor_name(self):
166
"""Correctly interpret names with spaces.
168
See <https://bugs.launchpad.net/bzr/+bug/220331>
170
self.overrideEnv('BZR_EDITOR',
171
'"%s"' % self.make_do_nothing_editor('name with spaces'))
172
self.assertEqual(True, msgeditor._run_editor('a_filename'))
164
174
def make_fake_editor(self, message='test message from fed\\n'):
165
175
"""Set up environment so that an editor will be a known script.
192
202
""" % sys.executable)
194
os.environ['BZR_EDITOR'] = 'fed.bat'
204
self.overrideEnv('BZR_EDITOR', 'fed.bat')
196
206
# [non-win32] make python script executable and set BZR_EDITOR
197
207
os.chmod('fed.py', 0755)
198
os.environ['BZR_EDITOR'] = './fed.py'
208
self.overrideEnv('BZR_EDITOR', './fed.py')
200
210
def test_edit_commit_message(self):
201
211
working_tree = self.make_uncommitted_tree()
230
240
working_tree = self.make_uncommitted_tree()
232
242
if sys.platform == 'win32':
233
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
243
editor = 'cmd.exe /c del'
235
os.environ['BZR_EDITOR'] = 'rm'
246
self.overrideEnv('BZR_EDITOR', editor)
237
248
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
239
250
def test__get_editor(self):
240
os.environ['BZR_EDITOR'] = 'bzr_editor'
241
os.environ['VISUAL'] = 'visual'
242
os.environ['EDITOR'] = 'editor'
244
conf = config.GlobalConfig.from_string('editor = config_editor\n',
251
self.overrideEnv('BZR_EDITOR', 'bzr_editor')
252
self.overrideEnv('VISUAL', 'visual')
253
self.overrideEnv('EDITOR', 'editor')
255
conf = config.GlobalStack()
256
conf.store._load_from_string('[DEFAULT]\neditor = config_editor\n')
247
258
editors = list(msgeditor._get_editor())
248
259
editors = [editor for (editor, cfg_src) in editors]
260
271
def test__run_editor_EACCES(self):
261
272
"""If running a configured editor raises EACESS, the user is warned."""
262
os.environ['BZR_EDITOR'] = 'eacces.py'
273
self.overrideEnv('BZR_EDITOR', 'eacces.py')
263
274
f = file('eacces.py', 'wb')
264
275
f.write('# Not a real editor')
267
278
os.chmod('eacces.py', 0)
268
279
# Set $EDITOR so that _run_editor will terminate before trying real
270
os.environ['EDITOR'] = self.make_do_nothing_editor()
281
self.overrideEnv('EDITOR', self.make_do_nothing_editor())
271
282
# Call _run_editor, capturing mutter.warning calls.
273
284
def warning(*args):
299
310
self.assertFileEqual(expected, msgfilename)
301
312
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
302
self.requireFeature(tests.UnicodeFilenameFeature)
313
self.requireFeature(features.UnicodeFilenameFeature)
303
314
if hasattr(self, 'info'):
304
os.mkdir(self.info['directory'])
305
os.chdir(self.info['directory'])
306
msgeditor._create_temp_file_with_commit_template('infotext')
315
tmpdir = self.info['directory']
317
# Force the creation of temp file in a directory whose name
318
# requires some encoding support
319
msgeditor._create_temp_file_with_commit_template('infotext',
308
322
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
316
330
self.assertFileEqual('', msgfilename)
318
332
def test_unsupported_encoding_commit_message(self):
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)
333
self.overrideEnv('LANG', 'C')
334
# LANG env variable has no effect on Windows
335
# but some characters anyway cannot be represented
336
# in default user encoding
337
char = probe_bad_non_ascii(osutils.get_user_encoding())
339
raise TestSkipped('Cannot find suitable non-ascii character '
340
'for user_encoding (%s)' % osutils.get_user_encoding())
342
self.make_fake_editor(message=char)
344
working_tree = self.make_uncommitted_tree()
345
self.assertRaises(errors.BadCommitMessageEncoding,
346
msgeditor.edit_commit_message, '')
348
def test_set_commit_message_no_hooks(self):
349
commit_obj = commit.Commit()
351
msgeditor.set_commit_message(commit_obj))
353
def test_set_commit_message_hook(self):
354
msgeditor.hooks.install_named_hook("set_commit_message",
355
lambda commit_obj, existing_message: "save me some typing\n", None)
356
commit_obj = commit.Commit()
357
self.assertEquals("save me some typing\n",
358
msgeditor.set_commit_message(commit_obj))
337
360
def test_generate_commit_message_template_no_hooks(self):
338
361
commit_obj = commit.Commit()
358
381
ERROR_BAD_EXE_FORMAT = 193
359
382
file("textfile.txt", "w").close()
360
383
e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
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)
384
self.assertEqual(e.errno, errno.ENOEXEC)
385
self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)