~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 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
29
29
    tests,
30
30
    trace,
31
31
    )
32
 
from bzrlib.branch import Branch
33
 
from bzrlib.config import ensure_config_dir_exists, config_filename
34
32
from bzrlib.msgeditor import (
35
33
    make_commit_message_template_encoded,
36
34
    edit_commit_message_encoded
143
141
  hell\u00d8
144
142
""".encode('utf8') in template)
145
143
 
146
 
    def make_do_nothing_editor(self):
 
144
    def make_do_nothing_editor(self, basename='fed'):
147
145
        if sys.platform == "win32":
148
 
            f = file('fed.bat', 'w')
 
146
            name = basename + '.bat'
 
147
            f = file(name, 'w')
149
148
            f.write('@rem dummy fed')
150
149
            f.close()
151
 
            return 'fed.bat'
 
150
            return name
152
151
        else:
153
 
            f = file('fed.sh', 'wb')
 
152
            name = basename + '.sh'
 
153
            f = file(name, 'wb')
154
154
            f.write('#!/bin/sh\n')
155
155
            f.close()
156
 
            os.chmod('fed.sh', 0755)
157
 
            return './fed.sh'
 
156
            os.chmod(name, 0755)
 
157
            return './' + name
158
158
 
159
159
    def test_run_editor(self):
160
 
        os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
 
160
        self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
161
161
        self.assertEqual(True, msgeditor._run_editor(''),
162
162
                         'Unable to run dummy fake editor')
163
163
 
 
164
    def test_parse_editor_name(self):
 
165
        """Correctly interpret names with spaces.
 
166
 
 
167
        See <https://bugs.launchpad.net/bzr/+bug/220331>
 
168
        """
 
169
        self.overrideEnv('BZR_EDITOR',
 
170
            '"%s"' % self.make_do_nothing_editor('name with spaces'))
 
171
        self.assertEqual(True, msgeditor._run_editor('a_filename'))    
 
172
 
164
173
    def make_fake_editor(self, message='test message from fed\\n'):
165
174
        """Set up environment so that an editor will be a known script.
166
175
 
191
200
"%s" fed.py %%1
192
201
""" % sys.executable)
193
202
            f.close()
194
 
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
203
            self.overrideEnv('BZR_EDITOR', 'fed.bat')
195
204
        else:
196
205
            # [non-win32] make python script executable and set BZR_EDITOR
197
206
            os.chmod('fed.py', 0755)
198
 
            os.environ['BZR_EDITOR'] = './fed.py'
 
207
            self.overrideEnv('BZR_EDITOR', './fed.py')
199
208
 
200
209
    def test_edit_commit_message(self):
201
210
        working_tree = self.make_uncommitted_tree()
230
239
        working_tree = self.make_uncommitted_tree()
231
240
 
232
241
        if sys.platform == 'win32':
233
 
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
242
            editor = 'cmd.exe /c del'
234
243
        else:
235
 
            os.environ['BZR_EDITOR'] = 'rm'
 
244
            editor = 'rm'
 
245
        self.overrideEnv('BZR_EDITOR', editor)
236
246
 
237
247
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
238
248
 
239
249
    def test__get_editor(self):
240
 
        os.environ['BZR_EDITOR'] = 'bzr_editor'
241
 
        os.environ['VISUAL'] = 'visual'
242
 
        os.environ['EDITOR'] = 'editor'
 
250
        self.overrideEnv('BZR_EDITOR', 'bzr_editor')
 
251
        self.overrideEnv('VISUAL', 'visual')
 
252
        self.overrideEnv('EDITOR', 'editor')
243
253
 
244
254
        conf = config.GlobalConfig.from_string('editor = config_editor\n',
245
255
                                               save=True)
259
269
 
260
270
    def test__run_editor_EACCES(self):
261
271
        """If running a configured editor raises EACESS, the user is warned."""
262
 
        os.environ['BZR_EDITOR'] = 'eacces.py'
 
272
        self.overrideEnv('BZR_EDITOR', 'eacces.py')
263
273
        f = file('eacces.py', 'wb')
264
274
        f.write('# Not a real editor')
265
275
        f.close()
267
277
        os.chmod('eacces.py', 0)
268
278
        # Set $EDITOR so that _run_editor will terminate before trying real
269
279
        # editors.
270
 
        os.environ['EDITOR'] = self.make_do_nothing_editor()
 
280
        self.overrideEnv('EDITOR', self.make_do_nothing_editor())
271
281
        # Call _run_editor, capturing mutter.warning calls.
272
282
        warnings = []
273
283
        def warning(*args):
301
311
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
302
312
        self.requireFeature(tests.UnicodeFilenameFeature)
303
313
        if hasattr(self, 'info'):
304
 
            os.mkdir(self.info['directory'])
305
 
            os.chdir(self.info['directory'])
306
 
            msgeditor._create_temp_file_with_commit_template('infotext')
 
314
            tmpdir = self.info['directory']
 
315
            os.mkdir(tmpdir)
 
316
            # Force the creation of temp file in a directory whose name
 
317
            # requires some encoding support
 
318
            msgeditor._create_temp_file_with_commit_template('infotext',
 
319
                                                             tmpdir=tmpdir)
307
320
        else:
308
321
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
309
322
 
316
329
        self.assertFileEqual('', msgfilename)
317
330
 
318
331
    def test_unsupported_encoding_commit_message(self):
319
 
        old_env = osutils.set_or_unset_env('LANG', 'C')
320
 
        try:
321
 
            # LANG env variable has no effect on Windows
322
 
            # but some characters anyway cannot be represented
323
 
            # in default user encoding
324
 
            char = probe_bad_non_ascii(osutils.get_user_encoding())
325
 
            if char is None:
326
 
                raise TestSkipped('Cannot find suitable non-ascii character '
327
 
                    'for user_encoding (%s)' % osutils.get_user_encoding())
328
 
 
329
 
            self.make_fake_editor(message=char)
330
 
 
331
 
            working_tree = self.make_uncommitted_tree()
332
 
            self.assertRaises(errors.BadCommitMessageEncoding,
333
 
                              msgeditor.edit_commit_message, '')
334
 
        finally:
335
 
            osutils.set_or_unset_env('LANG', old_env)
 
332
        self.overrideEnv('LANG', 'C')
 
333
        # LANG env variable has no effect on Windows
 
334
        # but some characters anyway cannot be represented
 
335
        # in default user encoding
 
336
        char = probe_bad_non_ascii(osutils.get_user_encoding())
 
337
        if char is None:
 
338
            raise TestSkipped('Cannot find suitable non-ascii character '
 
339
                'for user_encoding (%s)' % osutils.get_user_encoding())
 
340
 
 
341
        self.make_fake_editor(message=char)
 
342
 
 
343
        working_tree = self.make_uncommitted_tree()
 
344
        self.assertRaises(errors.BadCommitMessageEncoding,
 
345
                          msgeditor.edit_commit_message, '')
336
346
 
337
347
    def test_generate_commit_message_template_no_hooks(self):
338
348
        commit_obj = commit.Commit()
358
368
        ERROR_BAD_EXE_FORMAT = 193
359
369
        file("textfile.txt", "w").close()
360
370
        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)
 
371
        self.assertEqual(e.errno, errno.ENOEXEC)
 
372
        self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)