143
142
""".encode('utf8') in template)
145
def make_do_nothing_editor(self, basename='fed'):
144
def make_do_nothing_editor(self):
146
145
if sys.platform == "win32":
147
name = basename + '.bat'
146
f = file('fed.bat', 'w')
149
147
f.write('@rem dummy fed')
153
name = basename + '.sh'
151
f = file('fed.sh', 'wb')
155
152
f.write('#!/bin/sh\n')
154
os.chmod('fed.sh', 0755)
160
157
def test_run_editor(self):
161
self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
158
os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
162
159
self.assertEqual(True, msgeditor._run_editor(''),
163
160
'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'))
174
162
def make_fake_editor(self, message='test message from fed\\n'):
175
163
"""Set up environment so that an editor will be a known script.
240
228
working_tree = self.make_uncommitted_tree()
242
230
if sys.platform == 'win32':
243
editor = 'cmd.exe /c del'
231
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
246
self.overrideEnv('BZR_EDITOR', editor)
233
os.environ['BZR_EDITOR'] = 'rm'
248
235
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
250
237
def test__get_editor(self):
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')
258
editors = list(msgeditor._get_editor())
259
editors = [editor for (editor, cfg_src) in editors]
261
self.assertEqual(['bzr_editor', 'config_editor', 'visual', 'editor'],
264
if sys.platform == 'win32':
265
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
267
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
238
# Test that _get_editor can return a decent list of items
239
bzr_editor = os.environ.get('BZR_EDITOR')
240
visual = os.environ.get('VISUAL')
241
editor = os.environ.get('EDITOR')
243
os.environ['BZR_EDITOR'] = 'bzr_editor'
244
os.environ['VISUAL'] = 'visual'
245
os.environ['EDITOR'] = 'editor'
247
ensure_config_dir_exists()
248
f = open(config_filename(), 'wb')
249
f.write('editor = config_editor\n')
252
editors = list(msgeditor._get_editor())
253
editors = [editor for (editor, cfg_src) in editors]
255
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
256
'editor'], editors[:4])
258
if sys.platform == 'win32':
259
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
261
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
265
# Restore the environment
266
if bzr_editor is None:
267
del os.environ['BZR_EDITOR']
269
os.environ['BZR_EDITOR'] = bzr_editor
271
del os.environ['VISUAL']
273
os.environ['VISUAL'] = visual
275
del os.environ['EDITOR']
277
os.environ['EDITOR'] = editor
271
279
def test__run_editor_EACCES(self):
272
280
"""If running a configured editor raises EACESS, the user is warned."""
273
self.overrideEnv('BZR_EDITOR', 'eacces.py')
281
os.environ['BZR_EDITOR'] = 'eacces.py'
274
282
f = file('eacces.py', 'wb')
275
283
f.write('# Not a real editor')
310
315
self.assertFileEqual(expected, msgfilename)
312
317
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
313
self.requireFeature(features.UnicodeFilenameFeature)
318
self.requireFeature(tests.UnicodeFilenameFeature)
314
319
if hasattr(self, 'info'):
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',
320
os.mkdir(self.info['directory'])
321
os.chdir(self.info['directory'])
322
msgeditor._create_temp_file_with_commit_template('infotext')
322
324
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
330
332
self.assertFileEqual('', msgfilename)
332
334
def test_unsupported_encoding_commit_message(self):
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))
335
old_env = osutils.set_or_unset_env('LANG', 'C')
337
# LANG env variable has no effect on Windows
338
# but some characters anyway cannot be represented
339
# in default user encoding
340
char = probe_bad_non_ascii(osutils.get_user_encoding())
342
raise TestSkipped('Cannot find suitable non-ascii character '
343
'for user_encoding (%s)' % osutils.get_user_encoding())
345
self.make_fake_editor(message=char)
347
working_tree = self.make_uncommitted_tree()
348
self.assertRaises(errors.BadCommitMessageEncoding,
349
msgeditor.edit_commit_message, '')
351
osutils.set_or_unset_env('LANG', old_env)
360
353
def test_generate_commit_message_template_no_hooks(self):
361
354
commit_obj = commit.Commit()
363
356
msgeditor.generate_commit_message_template(commit_obj))
365
358
def test_generate_commit_message_template_hook(self):
359
def restoreDefaults():
360
msgeditor.hooks['commit_message_template'] = []
361
self.addCleanup(restoreDefaults)
366
362
msgeditor.hooks.install_named_hook("commit_message_template",
367
363
lambda commit_obj, msg: "save me some typing\n", None)
368
364
commit_obj = commit.Commit()
369
365
self.assertEquals("save me some typing\n",
370
366
msgeditor.generate_commit_message_template(commit_obj))
373
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
374
class TestPlatformErrnoWorkarounds(TestCaseInTempDir):
375
"""Ensuring workarounds enshrined in code actually serve a purpose"""
377
def test_subprocess_call_bad_file(self):
378
if sys.platform != "win32":
379
raise TestNotApplicable("Workarounds for windows only")
380
import subprocess, errno
381
ERROR_BAD_EXE_FORMAT = 193
382
file("textfile.txt", "w").close()
383
e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
384
self.assertEqual(e.errno, errno.ENOEXEC)
385
self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)