~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Jelmer Vernooij
  • Date: 2010-12-20 11:57:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5577.
  • Revision ID: jelmer@samba.org-20101220115714-2ru3hfappjweeg7q
Don't use no-plugins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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,
35
36
    edit_commit_message_encoded
36
37
)
37
38
from bzrlib.tests import (
 
39
    TestCaseInTempDir,
38
40
    TestCaseWithTransport,
39
41
    TestNotApplicable,
40
42
    TestSkipped,
93
95
        tree3.commit('Feature Y, based on initial X work.',
94
96
                     timestamp=1233285960, timezone=0)
95
97
        tree.merge_from_branch(tree2.branch)
96
 
        tree.merge_from_branch(tree3.branch)
 
98
        tree.merge_from_branch(tree3.branch, force=True)
97
99
        return tree
98
100
 
99
101
    def test_commit_template_pending_merges(self):
235
237
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
236
238
 
237
239
    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
 
240
        os.environ['BZR_EDITOR'] = 'bzr_editor'
 
241
        os.environ['VISUAL'] = 'visual'
 
242
        os.environ['EDITOR'] = 'editor'
 
243
 
 
244
        conf = config.GlobalConfig.from_string('editor = config_editor\n',
 
245
                                               save=True)
 
246
 
 
247
        editors = list(msgeditor._get_editor())
 
248
        editors = [editor for (editor, cfg_src) in editors]
 
249
 
 
250
        self.assertEqual(['bzr_editor', 'config_editor', 'visual', 'editor'],
 
251
                         editors[:4])
 
252
 
 
253
        if sys.platform == 'win32':
 
254
            self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
255
        else:
 
256
            self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
 
257
                             editors[4:])
 
258
 
278
259
 
279
260
    def test__run_editor_EACCES(self):
280
261
        """If running a configured editor raises EACESS, the user is warned."""
290
271
        # Call _run_editor, capturing mutter.warning calls.
291
272
        warnings = []
292
273
        def warning(*args):
293
 
            warnings.append(args[0] % args[1:])
 
274
            if len(args) > 1:
 
275
                warnings.append(args[0] % args[1:])
 
276
            else:
 
277
                warnings.append(args[0])
294
278
        _warning = trace.warning
295
279
        trace.warning = warning
296
280
        try:
317
301
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
318
302
        self.requireFeature(tests.UnicodeFilenameFeature)
319
303
        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')
 
304
            tmpdir = self.info['directory']
 
305
            os.mkdir(tmpdir)
 
306
            # Force the creation of temp file in a directory whose name
 
307
            # requires some encoding support
 
308
            msgeditor._create_temp_file_with_commit_template('infotext',
 
309
                                                             tmpdir=tmpdir)
323
310
        else:
324
311
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
325
312
 
356
343
            msgeditor.generate_commit_message_template(commit_obj))
357
344
 
358
345
    def test_generate_commit_message_template_hook(self):
359
 
        def restoreDefaults():
360
 
            msgeditor.hooks['commit_message_template'] = []
361
 
        self.addCleanup(restoreDefaults)
362
346
        msgeditor.hooks.install_named_hook("commit_message_template",
363
347
                lambda commit_obj, msg: "save me some typing\n", None)
364
348
        commit_obj = commit.Commit()
365
349
        self.assertEquals("save me some typing\n",
366
350
            msgeditor.generate_commit_message_template(commit_obj))
 
351
 
 
352
 
 
353
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
 
354
class TestPlatformErrnoWorkarounds(TestCaseInTempDir):
 
355
    """Ensuring workarounds enshrined in code actually serve a purpose"""
 
356
 
 
357
    def test_subprocess_call_bad_file(self):
 
358
        if sys.platform != "win32":
 
359
            raise TestNotApplicable("Workarounds for windows only")
 
360
        import subprocess, errno
 
361
        ERROR_BAD_EXE_FORMAT = 193
 
362
        file("textfile.txt", "w").close()
 
363
        e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
 
364
        # Python2.4 used the 'winerror' as the errno, which confuses a lot of
 
365
        # our error trapping code. Make sure that we understand the mapping
 
366
        # correctly.
 
367
        if sys.version_info >= (2, 5):
 
368
            self.assertEqual(e.errno, errno.ENOEXEC)
 
369
            self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)
 
370
        else:
 
371
            self.assertEqual(e.errno, ERROR_BAD_EXE_FORMAT)