~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-12-19 06:56:52 UTC
  • mfrom: (3910.1.4 msgeditor-failure-handling)
  • Revision ID: pqm@pqm.ubuntu.com-20081219065652-z3g78j4hrvdnf8bj
Improve error handling in msgeditor._run_editor. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    msgeditor,
27
27
    osutils,
28
28
    tests,
 
29
    trace,
29
30
    )
30
31
from bzrlib.branch import Branch
31
32
from bzrlib.config import ensure_config_dir_exists, config_filename
110
111
  hell\u00d8
111
112
""".encode('utf8') in template)
112
113
 
113
 
    def test_run_editor(self):
 
114
    def make_do_nothing_editor(self):
114
115
        if sys.platform == "win32":
115
116
            f = file('fed.bat', 'w')
116
117
            f.write('@rem dummy fed')
117
118
            f.close()
118
 
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
119
            return 'fed.bat'
119
120
        else:
120
121
            f = file('fed.sh', 'wb')
121
122
            f.write('#!/bin/sh\n')
122
123
            f.close()
123
124
            os.chmod('fed.sh', 0755)
124
 
            os.environ['BZR_EDITOR'] = './fed.sh'
 
125
            return './fed.sh'
125
126
 
 
127
    def test_run_editor(self):
 
128
        os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
126
129
        self.assertEqual(True, msgeditor._run_editor(''),
127
130
                         'Unable to run dummy fake editor')
128
131
 
217
220
            f.close()
218
221
 
219
222
            editors = list(msgeditor._get_editor())
 
223
            editors = [editor for (editor, cfg_src) in editors]
220
224
 
221
225
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
222
226
                              'editor'], editors[:4])
242
246
            else:
243
247
                os.environ['EDITOR'] = editor
244
248
 
 
249
    def test__run_editor_EACCES(self):
 
250
        """If running a configured editor raises EACESS, the user is warned."""
 
251
        os.environ['BZR_EDITOR'] = 'eacces.py'
 
252
        f = file('eacces.py', 'wb')
 
253
        f.write('# Not a real editor')
 
254
        f.close()
 
255
        # Make the fake editor unreadable (and unexecutable)
 
256
        os.chmod('eacces.py', 0)
 
257
        # Set $EDITOR so that _run_editor will terminate before trying real
 
258
        # editors.
 
259
        os.environ['EDITOR'] = self.make_do_nothing_editor()
 
260
        # Call _run_editor, capturing mutter.warning calls.
 
261
        warnings = []
 
262
        def warning(*args):
 
263
            warnings.append(args[0] % args[1:])
 
264
        _warning = trace.warning
 
265
        trace.warning = warning
 
266
        try:
 
267
            msgeditor._run_editor('')
 
268
        finally:
 
269
            trace.warning = _warning
 
270
        self.assertStartsWith(warnings[0], 'Could not start editor "eacces.py"')
 
271
 
245
272
    def test__create_temp_file_with_commit_template(self):
246
273
        # check that commit template written properly
247
274
        # and has platform native line-endings (CRLF on win32)