~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 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
26
26
    errors,
27
27
    msgeditor,
28
28
    osutils,
29
 
    tests,
30
29
    trace,
31
30
    )
32
 
from bzrlib.branch import Branch
33
 
from bzrlib.config import ensure_config_dir_exists, config_filename
34
31
from bzrlib.msgeditor import (
35
32
    make_commit_message_template_encoded,
36
33
    edit_commit_message_encoded
37
34
)
38
35
from bzrlib.tests import (
 
36
    features,
39
37
    TestCaseInTempDir,
40
38
    TestCaseWithTransport,
41
39
    TestNotApplicable,
82
80
""")
83
81
 
84
82
    def make_multiple_pending_tree(self):
85
 
        from bzrlib import config
86
 
        config.GlobalConfig().set_user_option('email',
87
 
                                              'Bilbo Baggins <bb@hobbit.net>')
 
83
        config.GlobalStack().set('email', 'Bilbo Baggins <bb@hobbit.net>')
88
84
        tree = self.make_branch_and_tree('a')
89
85
        tree.commit('Initial checkin.', timestamp=1230912900, timezone=0)
90
86
        tree2 = tree.bzrdir.clone('b').open_workingtree()
143
139
  hell\u00d8
144
140
""".encode('utf8') in template)
145
141
 
146
 
    def make_do_nothing_editor(self):
 
142
    def make_do_nothing_editor(self, basename='fed'):
147
143
        if sys.platform == "win32":
148
 
            f = file('fed.bat', 'w')
 
144
            name = basename + '.bat'
 
145
            f = file(name, 'w')
149
146
            f.write('@rem dummy fed')
150
147
            f.close()
151
 
            return 'fed.bat'
 
148
            return name
152
149
        else:
153
 
            f = file('fed.sh', 'wb')
 
150
            name = basename + '.sh'
 
151
            f = file(name, 'wb')
154
152
            f.write('#!/bin/sh\n')
155
153
            f.close()
156
 
            os.chmod('fed.sh', 0755)
157
 
            return './fed.sh'
 
154
            os.chmod(name, 0755)
 
155
            return './' + name
158
156
 
159
157
    def test_run_editor(self):
160
 
        os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
 
158
        self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
161
159
        self.assertEqual(True, msgeditor._run_editor(''),
162
160
                         'Unable to run dummy fake editor')
163
161
 
 
162
    def test_parse_editor_name(self):
 
163
        """Correctly interpret names with spaces.
 
164
 
 
165
        See <https://bugs.launchpad.net/bzr/+bug/220331>
 
166
        """
 
167
        self.overrideEnv('BZR_EDITOR',
 
168
            '"%s"' % self.make_do_nothing_editor('name with spaces'))
 
169
        self.assertEqual(True, msgeditor._run_editor('a_filename'))    
 
170
 
164
171
    def make_fake_editor(self, message='test message from fed\\n'):
165
172
        """Set up environment so that an editor will be a known script.
166
173
 
191
198
"%s" fed.py %%1
192
199
""" % sys.executable)
193
200
            f.close()
194
 
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
201
            self.overrideEnv('BZR_EDITOR', 'fed.bat')
195
202
        else:
196
203
            # [non-win32] make python script executable and set BZR_EDITOR
197
204
            os.chmod('fed.py', 0755)
198
 
            os.environ['BZR_EDITOR'] = './fed.py'
 
205
            self.overrideEnv('BZR_EDITOR', './fed.py')
199
206
 
200
207
    def test_edit_commit_message(self):
201
208
        working_tree = self.make_uncommitted_tree()
230
237
        working_tree = self.make_uncommitted_tree()
231
238
 
232
239
        if sys.platform == 'win32':
233
 
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
240
            editor = 'cmd.exe /c del'
234
241
        else:
235
 
            os.environ['BZR_EDITOR'] = 'rm'
 
242
            editor = 'rm'
 
243
        self.overrideEnv('BZR_EDITOR', editor)
236
244
 
237
245
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
238
246
 
239
247
    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
 
 
 
248
        self.overrideEnv('BZR_EDITOR', 'bzr_editor')
 
249
        self.overrideEnv('VISUAL', 'visual')
 
250
        self.overrideEnv('EDITOR', 'editor')
 
251
 
 
252
        conf = config.GlobalStack()
 
253
        conf.store._load_from_string('[DEFAULT]\neditor = config_editor\n')
 
254
        conf.store.save()
247
255
        editors = list(msgeditor._get_editor())
248
256
        editors = [editor for (editor, cfg_src) in editors]
249
257
 
259
267
 
260
268
    def test__run_editor_EACCES(self):
261
269
        """If running a configured editor raises EACESS, the user is warned."""
262
 
        os.environ['BZR_EDITOR'] = 'eacces.py'
 
270
        self.overrideEnv('BZR_EDITOR', 'eacces.py')
263
271
        f = file('eacces.py', 'wb')
264
272
        f.write('# Not a real editor')
265
273
        f.close()
267
275
        os.chmod('eacces.py', 0)
268
276
        # Set $EDITOR so that _run_editor will terminate before trying real
269
277
        # editors.
270
 
        os.environ['EDITOR'] = self.make_do_nothing_editor()
 
278
        self.overrideEnv('EDITOR', self.make_do_nothing_editor())
271
279
        # Call _run_editor, capturing mutter.warning calls.
272
280
        warnings = []
273
281
        def warning(*args):
299
307
        self.assertFileEqual(expected, msgfilename)
300
308
 
301
309
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
302
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
310
        self.requireFeature(features.UnicodeFilenameFeature)
303
311
        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')
 
312
            tmpdir = self.info['directory']
 
313
            os.mkdir(tmpdir)
 
314
            # Force the creation of temp file in a directory whose name
 
315
            # requires some encoding support
 
316
            msgeditor._create_temp_file_with_commit_template('infotext',
 
317
                                                             tmpdir=tmpdir)
307
318
        else:
308
319
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
309
320
 
316
327
        self.assertFileEqual('', msgfilename)
317
328
 
318
329
    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)
 
330
        self.overrideEnv('LANG', 'C')
 
331
        # LANG env variable has no effect on Windows
 
332
        # but some characters anyway cannot be represented
 
333
        # in default user encoding
 
334
        char = probe_bad_non_ascii(osutils.get_user_encoding())
 
335
        if char is None:
 
336
            raise TestSkipped('Cannot find suitable non-ascii character '
 
337
                'for user_encoding (%s)' % osutils.get_user_encoding())
 
338
 
 
339
        self.make_fake_editor(message=char)
 
340
 
 
341
        working_tree = self.make_uncommitted_tree()
 
342
        self.assertRaises(errors.BadCommitMessageEncoding,
 
343
                          msgeditor.edit_commit_message, '')
 
344
 
 
345
    def test_set_commit_message_no_hooks(self):
 
346
        commit_obj = commit.Commit()
 
347
        self.assertIs(None,
 
348
            msgeditor.set_commit_message(commit_obj))
 
349
 
 
350
    def test_set_commit_message_hook(self):
 
351
        msgeditor.hooks.install_named_hook("set_commit_message",
 
352
                lambda commit_obj, existing_message: "save me some typing\n", None)
 
353
        commit_obj = commit.Commit()
 
354
        self.assertEqual("save me some typing\n",
 
355
            msgeditor.set_commit_message(commit_obj))
336
356
 
337
357
    def test_generate_commit_message_template_no_hooks(self):
338
358
        commit_obj = commit.Commit()
343
363
        msgeditor.hooks.install_named_hook("commit_message_template",
344
364
                lambda commit_obj, msg: "save me some typing\n", None)
345
365
        commit_obj = commit.Commit()
346
 
        self.assertEquals("save me some typing\n",
 
366
        self.assertEqual("save me some typing\n",
347
367
            msgeditor.generate_commit_message_template(commit_obj))
348
368
 
349
369
 
358
378
        ERROR_BAD_EXE_FORMAT = 193
359
379
        file("textfile.txt", "w").close()
360
380
        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)
 
381
        self.assertEqual(e.errno, errno.ENOEXEC)
 
382
        self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)