~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Vincent Ladeuil
  • Date: 2011-08-20 09:28:27 UTC
  • mfrom: (5050.78.2 2.2)
  • mto: (5609.48.8 2.3)
  • mto: This revision was merged to the branch mainline in revision 6090.
  • Revision ID: v.ladeuil+lp@free.fr-20110820092827-9dyakfslp0r3hb1k
Merge 2.2 into 2.3 (including fix for #614713, #609187 and #812928)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
 
23
23
from bzrlib import (
24
24
    commit,
 
25
    config,
25
26
    errors,
26
27
    msgeditor,
27
28
    osutils,
28
29
    tests,
29
30
    trace,
30
31
    )
31
 
from bzrlib.branch import Branch
32
 
from bzrlib.config import ensure_config_dir_exists, config_filename
33
32
from bzrlib.msgeditor import (
34
33
    make_commit_message_template_encoded,
35
34
    edit_commit_message_encoded
36
35
)
37
36
from bzrlib.tests import (
 
37
    TestCaseInTempDir,
38
38
    TestCaseWithTransport,
39
39
    TestNotApplicable,
40
40
    TestSkipped,
93
93
        tree3.commit('Feature Y, based on initial X work.',
94
94
                     timestamp=1233285960, timezone=0)
95
95
        tree.merge_from_branch(tree2.branch)
96
 
        tree.merge_from_branch(tree3.branch)
 
96
        tree.merge_from_branch(tree3.branch, force=True)
97
97
        return tree
98
98
 
99
99
    def test_commit_template_pending_merges(self):
155
155
            return './fed.sh'
156
156
 
157
157
    def test_run_editor(self):
158
 
        os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
 
158
        self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
159
159
        self.assertEqual(True, msgeditor._run_editor(''),
160
160
                         'Unable to run dummy fake editor')
161
161
 
189
189
"%s" fed.py %%1
190
190
""" % sys.executable)
191
191
            f.close()
192
 
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
192
            self.overrideEnv('BZR_EDITOR', 'fed.bat')
193
193
        else:
194
194
            # [non-win32] make python script executable and set BZR_EDITOR
195
195
            os.chmod('fed.py', 0755)
196
 
            os.environ['BZR_EDITOR'] = './fed.py'
 
196
            self.overrideEnv('BZR_EDITOR', './fed.py')
197
197
 
198
198
    def test_edit_commit_message(self):
199
199
        working_tree = self.make_uncommitted_tree()
228
228
        working_tree = self.make_uncommitted_tree()
229
229
 
230
230
        if sys.platform == 'win32':
231
 
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
231
            editor = 'cmd.exe /c del'
232
232
        else:
233
 
            os.environ['BZR_EDITOR'] = 'rm'
 
233
            editor = 'rm'
 
234
        self.overrideEnv('BZR_EDITOR', editor)
234
235
 
235
236
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
236
237
 
237
238
    def test__get_editor(self):
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')
242
 
        try:
243
 
            os.environ['BZR_EDITOR'] = 'bzr_editor'
244
 
            os.environ['VISUAL'] = 'visual'
245
 
            os.environ['EDITOR'] = 'editor'
246
 
 
247
 
            ensure_config_dir_exists()
248
 
            f = open(config_filename(), 'wb')
249
 
            f.write('editor = config_editor\n')
250
 
            f.close()
251
 
 
252
 
            editors = list(msgeditor._get_editor())
253
 
            editors = [editor for (editor, cfg_src) in editors]
254
 
 
255
 
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
256
 
                              'editor'], editors[:4])
257
 
 
258
 
            if sys.platform == 'win32':
259
 
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
260
 
            else:
261
 
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
262
 
                                  'joe'], editors[4:])
263
 
 
264
 
        finally:
265
 
            # Restore the environment
266
 
            if bzr_editor is None:
267
 
                del os.environ['BZR_EDITOR']
268
 
            else:
269
 
                os.environ['BZR_EDITOR'] = bzr_editor
270
 
            if visual is None:
271
 
                del os.environ['VISUAL']
272
 
            else:
273
 
                os.environ['VISUAL'] = visual
274
 
            if editor is None:
275
 
                del os.environ['EDITOR']
276
 
            else:
277
 
                os.environ['EDITOR'] = editor
 
239
        self.overrideEnv('BZR_EDITOR', 'bzr_editor')
 
240
        self.overrideEnv('VISUAL', 'visual')
 
241
        self.overrideEnv('EDITOR', 'editor')
 
242
 
 
243
        conf = config.GlobalConfig.from_string('editor = config_editor\n',
 
244
                                               save=True)
 
245
 
 
246
        editors = list(msgeditor._get_editor())
 
247
        editors = [editor for (editor, cfg_src) in editors]
 
248
 
 
249
        self.assertEqual(['bzr_editor', 'config_editor', 'visual', 'editor'],
 
250
                         editors[:4])
 
251
 
 
252
        if sys.platform == 'win32':
 
253
            self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
254
        else:
 
255
            self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
 
256
                             editors[4:])
 
257
 
278
258
 
279
259
    def test__run_editor_EACCES(self):
280
260
        """If running a configured editor raises EACESS, the user is warned."""
281
 
        os.environ['BZR_EDITOR'] = 'eacces.py'
 
261
        self.overrideEnv('BZR_EDITOR', 'eacces.py')
282
262
        f = file('eacces.py', 'wb')
283
263
        f.write('# Not a real editor')
284
264
        f.close()
286
266
        os.chmod('eacces.py', 0)
287
267
        # Set $EDITOR so that _run_editor will terminate before trying real
288
268
        # editors.
289
 
        os.environ['EDITOR'] = self.make_do_nothing_editor()
 
269
        self.overrideEnv('EDITOR', self.make_do_nothing_editor())
290
270
        # Call _run_editor, capturing mutter.warning calls.
291
271
        warnings = []
292
272
        def warning(*args):
293
 
            warnings.append(args[0] % args[1:])
 
273
            if len(args) > 1:
 
274
                warnings.append(args[0] % args[1:])
 
275
            else:
 
276
                warnings.append(args[0])
294
277
        _warning = trace.warning
295
278
        trace.warning = warning
296
279
        try:
317
300
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
318
301
        self.requireFeature(tests.UnicodeFilenameFeature)
319
302
        if hasattr(self, 'info'):
320
 
            os.mkdir(self.info['directory'])
321
 
            os.chdir(self.info['directory'])
322
 
            msgeditor._create_temp_file_with_commit_template('infotext')
 
303
            tmpdir = self.info['directory']
 
304
            os.mkdir(tmpdir)
 
305
            # Force the creation of temp file in a directory whose name
 
306
            # requires some encoding support
 
307
            msgeditor._create_temp_file_with_commit_template('infotext',
 
308
                                                             tmpdir=tmpdir)
323
309
        else:
324
310
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
325
311
 
332
318
        self.assertFileEqual('', msgfilename)
333
319
 
334
320
    def test_unsupported_encoding_commit_message(self):
335
 
        old_env = osutils.set_or_unset_env('LANG', 'C')
336
 
        try:
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())
341
 
            if char is None:
342
 
                raise TestSkipped('Cannot find suitable non-ascii character '
343
 
                    'for user_encoding (%s)' % osutils.get_user_encoding())
344
 
 
345
 
            self.make_fake_editor(message=char)
346
 
 
347
 
            working_tree = self.make_uncommitted_tree()
348
 
            self.assertRaises(errors.BadCommitMessageEncoding,
349
 
                              msgeditor.edit_commit_message, '')
350
 
        finally:
351
 
            osutils.set_or_unset_env('LANG', old_env)
 
321
        self.overrideEnv('LANG', 'C')
 
322
        # LANG env variable has no effect on Windows
 
323
        # but some characters anyway cannot be represented
 
324
        # in default user encoding
 
325
        char = probe_bad_non_ascii(osutils.get_user_encoding())
 
326
        if char is None:
 
327
            raise TestSkipped('Cannot find suitable non-ascii character '
 
328
                'for user_encoding (%s)' % osutils.get_user_encoding())
 
329
 
 
330
        self.make_fake_editor(message=char)
 
331
 
 
332
        working_tree = self.make_uncommitted_tree()
 
333
        self.assertRaises(errors.BadCommitMessageEncoding,
 
334
                          msgeditor.edit_commit_message, '')
352
335
 
353
336
    def test_generate_commit_message_template_no_hooks(self):
354
337
        commit_obj = commit.Commit()
356
339
            msgeditor.generate_commit_message_template(commit_obj))
357
340
 
358
341
    def test_generate_commit_message_template_hook(self):
359
 
        def restoreDefaults():
360
 
            msgeditor.hooks['commit_message_template'] = []
361
 
        self.addCleanup(restoreDefaults)
362
342
        msgeditor.hooks.install_named_hook("commit_message_template",
363
343
                lambda commit_obj, msg: "save me some typing\n", None)
364
344
        commit_obj = commit.Commit()
365
345
        self.assertEquals("save me some typing\n",
366
346
            msgeditor.generate_commit_message_template(commit_obj))
 
347
 
 
348
 
 
349
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
 
350
class TestPlatformErrnoWorkarounds(TestCaseInTempDir):
 
351
    """Ensuring workarounds enshrined in code actually serve a purpose"""
 
352
 
 
353
    def test_subprocess_call_bad_file(self):
 
354
        if sys.platform != "win32":
 
355
            raise TestNotApplicable("Workarounds for windows only")
 
356
        import subprocess, errno
 
357
        ERROR_BAD_EXE_FORMAT = 193
 
358
        file("textfile.txt", "w").close()
 
359
        e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
 
360
        # Python2.4 used the 'winerror' as the errno, which confuses a lot of
 
361
        # our error trapping code. Make sure that we understand the mapping
 
362
        # correctly.
 
363
        if sys.version_info >= (2, 5):
 
364
            self.assertEqual(e.errno, errno.ENOEXEC)
 
365
            self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)
 
366
        else:
 
367
            self.assertEqual(e.errno, ERROR_BAD_EXE_FORMAT)