~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Danny van Heumen
  • Date: 2010-03-09 16:38:10 UTC
  • mto: (4634.139.5 2.0)
  • mto: This revision was merged to the branch mainline in revision 5160.
  • Revision ID: danny@dannyvanheumen.nl-20100309163810-ujn8hcx08f75nlf1
Refined test to make use of locking hooks and also validate if lock is truly a checkout-lock.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005 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,
26
25
    errors,
27
26
    msgeditor,
28
27
    osutils,
36
35
    edit_commit_message_encoded
37
36
)
38
37
from bzrlib.tests import (
39
 
    TestCaseInTempDir,
40
38
    TestCaseWithTransport,
41
39
    TestNotApplicable,
42
40
    TestSkipped,
95
93
        tree3.commit('Feature Y, based on initial X work.',
96
94
                     timestamp=1233285960, timezone=0)
97
95
        tree.merge_from_branch(tree2.branch)
98
 
        tree.merge_from_branch(tree3.branch, force=True)
 
96
        tree.merge_from_branch(tree3.branch)
99
97
        return tree
100
98
 
101
99
    def test_commit_template_pending_merges(self):
237
235
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
238
236
 
239
237
    def test__get_editor(self):
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
 
 
 
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
259
278
 
260
279
    def test__run_editor_EACCES(self):
261
280
        """If running a configured editor raises EACESS, the user is warned."""
271
290
        # Call _run_editor, capturing mutter.warning calls.
272
291
        warnings = []
273
292
        def warning(*args):
274
 
            if len(args) > 1:
275
 
                warnings.append(args[0] % args[1:])
276
 
            else:
277
 
                warnings.append(args[0])
 
293
            warnings.append(args[0] % args[1:])
278
294
        _warning = trace.warning
279
295
        trace.warning = warning
280
296
        try:
340
356
            msgeditor.generate_commit_message_template(commit_obj))
341
357
 
342
358
    def test_generate_commit_message_template_hook(self):
 
359
        def restoreDefaults():
 
360
            msgeditor.hooks['commit_message_template'] = []
 
361
        self.addCleanup(restoreDefaults)
343
362
        msgeditor.hooks.install_named_hook("commit_message_template",
344
363
                lambda commit_obj, msg: "save me some typing\n", None)
345
364
        commit_obj = commit.Commit()
346
365
        self.assertEquals("save me some typing\n",
347
366
            msgeditor.generate_commit_message_template(commit_obj))
348
 
 
349
 
 
350
 
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
351
 
class TestPlatformErrnoWorkarounds(TestCaseInTempDir):
352
 
    """Ensuring workarounds enshrined in code actually serve a purpose"""
353
 
 
354
 
    def test_subprocess_call_bad_file(self):
355
 
        if sys.platform != "win32":
356
 
            raise TestNotApplicable("Workarounds for windows only")
357
 
        import subprocess, errno
358
 
        ERROR_BAD_EXE_FORMAT = 193
359
 
        file("textfile.txt", "w").close()
360
 
        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
363
 
        # correctly.
364
 
        if sys.version_info >= (2, 5):
365
 
            self.assertEqual(e.errno, errno.ENOEXEC)
366
 
            self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)
367
 
        else:
368
 
            self.assertEqual(e.errno, ERROR_BAD_EXE_FORMAT)