49
49
def load_tests(loader, standard_tests, pattern):
50
50
"""Parameterize the test for tempfile creation with different encodings."""
51
to_adapt, result = split_suite_by_re(standard_tests,
51
to_adapt, result = split_suite_by_re(
52
53
"test__create_temp_file_with_commit_template_in_unicode_dir")
53
54
return multiply_tests(to_adapt, encoding_scenarios, result)
58
59
def make_uncommitted_tree(self):
59
60
"""Build a branch with uncommitted unicode named changes in the cwd."""
60
61
working_tree = self.make_branch_and_tree('.')
61
b = working_tree.branch
62
62
filename = u'hell\u00d8'
64
self.build_tree_contents([(filename, 'contents of hello')])
64
self.build_tree_contents([(filename, b'contents of hello')])
65
65
except UnicodeEncodeError:
66
66
self.skipTest("can't build unicode working tree in "
67
"filesystem encoding %s" % sys.getfilesystemencoding())
67
"filesystem encoding %s" % sys.getfilesystemencoding())
68
68
working_tree.add(filename)
69
69
return working_tree
102
102
working_tree = self.make_multiple_pending_tree()
103
103
template = msgeditor.make_commit_message_template(working_tree, None)
104
104
self.assertEqualDiff(template,
107
107
Bilbo Baggins 2009-01-29 Feature X finished.
108
108
Bilbo Baggins 2009-01-28 Feature X work.
142
141
def make_do_nothing_editor(self, basename='fed'):
143
142
if sys.platform == "win32":
144
143
name = basename + '.bat'
146
f.write('@rem dummy fed')
144
with open(name, 'w') as f:
145
f.write('@rem dummy fed')
150
148
name = basename + '.sh'
152
f.write('#!/bin/sh\n')
149
with open(name, 'wb') as f:
150
f.write(b'#!/bin/sh\n')
154
151
os.chmod(name, 0o755)
155
152
return './' + name
165
162
See <https://bugs.launchpad.net/bzr/+bug/220331>
167
164
self.overrideEnv('BRZ_EDITOR',
168
'"%s"' % self.make_do_nothing_editor('name with spaces'))
169
self.assertEqual(True, msgeditor._run_editor('a_filename'))
165
'"%s"' % self.make_do_nothing_editor('name with spaces'))
166
self.assertEqual(True, msgeditor._run_editor('a_filename'))
171
def make_fake_editor(self, message='test message from fed\\n'):
168
def make_fake_editor(self, message='test message from fed\n'):
172
169
"""Set up environment so that an editor will be a known script.
174
171
Sets up BRZ_EDITOR so that if an editor is spawned it will run a
175
172
script that just adds a known message to the start of the file.
177
f = file('fed.py', 'wb')
178
f.write('#!%s\n' % sys.executable)
174
if not isinstance(message, bytes):
175
message = message.encode('utf-8')
176
with open('fed.py', 'w') as f:
177
f.write('#!%s\n' % sys.executable)
182
181
if len(sys.argv) == 2:
183
with open(fn, 'rb') as f:
185
with open(fn, 'wb') as f:
191
188
""" % (message, ))
193
189
if sys.platform == "win32":
194
190
# [win32] make batch file and set BRZ_EDITOR
195
f = file('fed.bat', 'w')
191
with open('fed.bat', 'w') as f:
199
195
""" % sys.executable)
201
196
self.overrideEnv('BRZ_EDITOR', 'fed.bat')
203
198
# [non-win32] make python script executable and set BRZ_EDITOR
205
200
self.overrideEnv('BRZ_EDITOR', './fed.py')
207
202
def test_edit_commit_message_without_infotext(self):
208
working_tree = self.make_uncommitted_tree()
203
self.make_uncommitted_tree()
209
204
self.make_fake_editor()
211
206
mutter('edit_commit_message without infotext')
213
208
msgeditor.edit_commit_message(''))
215
210
def test_edit_commit_message_with_ascii_infotext(self):
216
working_tree = self.make_uncommitted_tree()
211
self.make_uncommitted_tree()
217
212
self.make_fake_editor()
219
214
mutter('edit_commit_message with ascii string infotext')
221
216
msgeditor.edit_commit_message('spam'))
223
218
def test_edit_commit_message_with_unicode_infotext(self):
224
working_tree = self.make_uncommitted_tree()
219
self.make_uncommitted_tree()
225
220
self.make_fake_editor()
227
222
mutter('edit_commit_message with unicode infotext')
241
236
self.make_uncommitted_tree()
242
237
self.make_fake_editor()
243
238
self.assertEqual('test message from fed\nstart message\n',
244
msgeditor.edit_commit_message('',
245
start_message='start message\n'))
239
msgeditor.edit_commit_message(
240
'', start_message='start message\n'))
246
241
self.assertEqual('test message from fed\n',
247
msgeditor.edit_commit_message('',
242
msgeditor.edit_commit_message(
243
'', start_message=''))
250
245
def test_deleted_commit_message(self):
251
working_tree = self.make_uncommitted_tree()
246
self.make_uncommitted_tree()
253
248
if sys.platform == 'win32':
254
249
editor = 'cmd.exe /c del'
257
252
self.overrideEnv('BRZ_EDITOR', editor)
259
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
254
self.assertRaises((EnvironmentError, errors.NoSuchFile),
255
msgeditor.edit_commit_message, '')
261
257
def test__get_editor(self):
262
258
self.overrideEnv('BRZ_EDITOR', 'bzr_editor')
264
260
self.overrideEnv('EDITOR', 'editor')
266
262
conf = config.GlobalStack()
267
conf.store._load_from_string('[DEFAULT]\neditor = config_editor\n')
263
conf.store._load_from_string(b'[DEFAULT]\neditor = config_editor\n')
268
264
conf.store.save()
269
265
editors = list(msgeditor._get_editor())
270
266
editors = [editor for (editor, cfg_src) in editors]
278
274
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
282
277
def test__run_editor_EACCES(self):
283
278
"""If running a configured editor raises EACESS, the user is warned."""
284
279
self.overrideEnv('BRZ_EDITOR', 'eacces.py')
285
f = file('eacces.py', 'wb')
286
f.write('# Not a real editor')
280
with open('eacces.py', 'wb') as f:
281
f.write(b'# Not a real editor')
288
282
# Make the fake editor unreadable (and unexecutable)
289
283
os.chmod('eacces.py', 0)
290
284
# Set $EDITOR so that _run_editor will terminate before trying real
303
298
msgeditor._run_editor('')
305
300
trace.warning = _warning
306
self.assertStartsWith(warnings[0], 'Could not start editor "eacces.py"')
301
self.assertStartsWith(
302
warnings[0], 'Could not start editor "eacces.py"')
308
304
def test__create_temp_file_with_commit_template(self):
309
305
# check that commit template written properly
310
306
# and has platform native line-endings (CRLF on win32)
311
307
create_file = msgeditor._create_temp_file_with_commit_template
312
msgfilename, hasinfo = create_file('infotext', '----', 'start message')
308
msgfilename, hasinfo = create_file(
309
b'infotext', '----', b'start message')
313
310
self.assertNotEqual(None, msgfilename)
314
311
self.assertTrue(hasinfo)
315
312
expected = os.linesep.join(['start message',
328
325
# Force the creation of temp file in a directory whose name
329
326
# requires some encoding support
330
msgeditor._create_temp_file_with_commit_template('infotext',
327
msgeditor._create_temp_file_with_commit_template(b'infotext',
333
330
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
347
344
# in default user encoding
348
345
char = probe_bad_non_ascii(osutils.get_user_encoding())
350
self.skipTest('Cannot find suitable non-ascii character '
348
'Cannot find suitable non-ascii character '
351
349
'for user_encoding (%s)' % osutils.get_user_encoding())
353
351
self.make_fake_editor(message=char)
355
working_tree = self.make_uncommitted_tree()
353
self.make_uncommitted_tree()
356
354
self.assertRaises(msgeditor.BadCommitMessageEncoding,
357
355
msgeditor.edit_commit_message, '')
359
357
def test_set_commit_message_no_hooks(self):
360
358
commit_obj = commit.Commit()
361
359
self.assertIs(None,
362
msgeditor.set_commit_message(commit_obj))
360
msgeditor.set_commit_message(commit_obj))
364
362
def test_set_commit_message_hook(self):
365
363
msgeditor.hooks.install_named_hook("set_commit_message",
366
lambda commit_obj, existing_message: "save me some typing\n", None)
364
lambda commit_obj, existing_message: "save me some typing\n", None)
367
365
commit_obj = commit.Commit()
368
366
self.assertEqual("save me some typing\n",
369
msgeditor.set_commit_message(commit_obj))
367
msgeditor.set_commit_message(commit_obj))
371
369
def test_generate_commit_message_template_no_hooks(self):
372
370
commit_obj = commit.Commit()
373
371
self.assertIs(None,
374
msgeditor.generate_commit_message_template(commit_obj))
372
msgeditor.generate_commit_message_template(commit_obj))
376
374
def test_generate_commit_message_template_hook(self):
377
375
msgeditor.hooks.install_named_hook("commit_message_template",
378
lambda commit_obj, msg: "save me some typing\n", None)
376
lambda commit_obj, msg: "save me some typing\n", None)
379
377
commit_obj = commit.Commit()
380
378
self.assertEqual("save me some typing\n",
381
msgeditor.generate_commit_message_template(commit_obj))
379
msgeditor.generate_commit_message_template(commit_obj))
384
382
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
388
386
def test_subprocess_call_bad_file(self):
389
387
if sys.platform != "win32":
390
388
raise TestNotApplicable("Workarounds for windows only")
391
import subprocess, errno
392
391
ERROR_BAD_EXE_FORMAT = 193
393
file("textfile.txt", "w").close()
392
open("textfile.txt", "w").close()
394
393
e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
395
394
self.assertEqual(e.errno, errno.ENOEXEC)
396
395
self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)