/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: John Arbash Meinel
  • Date: 2011-01-25 22:54:08 UTC
  • mto: This revision was merged to the branch mainline in revision 5636.
  • Revision ID: john@arbash-meinel.com-20110125225408-w5b5mmh117q4jjz1
Implement a reset-to-known-state ability for DirState.

Use this in reset_state(). Allow it to use header information if it can
be parsed, otherwise allow us to pass in the information.

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
22
22
 
23
23
from bzrlib import (
24
24
    commit,
 
25
    config,
25
26
    errors,
26
27
    msgeditor,
27
28
    osutils,
28
29
    tests,
29
30
    trace,
30
31
    )
31
 
from bzrlib.branch import Branch
32
 
from bzrlib.config import ensure_config_dir_exists, config_filename
33
32
from bzrlib.msgeditor import (
34
33
    make_commit_message_template_encoded,
35
34
    edit_commit_message_encoded
156
155
            return './fed.sh'
157
156
 
158
157
    def test_run_editor(self):
159
 
        os.environ['BZR_EDITOR'] = self.make_do_nothing_editor()
 
158
        self.overrideEnv('BZR_EDITOR', self.make_do_nothing_editor())
160
159
        self.assertEqual(True, msgeditor._run_editor(''),
161
160
                         'Unable to run dummy fake editor')
162
161
 
190
189
"%s" fed.py %%1
191
190
""" % sys.executable)
192
191
            f.close()
193
 
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
192
            self.overrideEnv('BZR_EDITOR', 'fed.bat')
194
193
        else:
195
194
            # [non-win32] make python script executable and set BZR_EDITOR
196
195
            os.chmod('fed.py', 0755)
197
 
            os.environ['BZR_EDITOR'] = './fed.py'
 
196
            self.overrideEnv('BZR_EDITOR', './fed.py')
198
197
 
199
198
    def test_edit_commit_message(self):
200
199
        working_tree = self.make_uncommitted_tree()
229
228
        working_tree = self.make_uncommitted_tree()
230
229
 
231
230
        if sys.platform == 'win32':
232
 
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
231
            editor = 'cmd.exe /c del'
233
232
        else:
234
 
            os.environ['BZR_EDITOR'] = 'rm'
 
233
            editor = 'rm'
 
234
        self.overrideEnv('BZR_EDITOR', editor)
235
235
 
236
236
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
237
237
 
238
238
    def test__get_editor(self):
239
 
        # Test that _get_editor can return a decent list of items
240
 
        bzr_editor = os.environ.get('BZR_EDITOR')
241
 
        visual = os.environ.get('VISUAL')
242
 
        editor = os.environ.get('EDITOR')
243
 
        try:
244
 
            os.environ['BZR_EDITOR'] = 'bzr_editor'
245
 
            os.environ['VISUAL'] = 'visual'
246
 
            os.environ['EDITOR'] = 'editor'
247
 
 
248
 
            ensure_config_dir_exists()
249
 
            f = open(config_filename(), 'wb')
250
 
            f.write('editor = config_editor\n')
251
 
            f.close()
252
 
 
253
 
            editors = list(msgeditor._get_editor())
254
 
            editors = [editor for (editor, cfg_src) in editors]
255
 
 
256
 
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
257
 
                              'editor'], editors[:4])
258
 
 
259
 
            if sys.platform == 'win32':
260
 
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
261
 
            else:
262
 
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
263
 
                                  'joe'], editors[4:])
264
 
 
265
 
        finally:
266
 
            # Restore the environment
267
 
            if bzr_editor is None:
268
 
                del os.environ['BZR_EDITOR']
269
 
            else:
270
 
                os.environ['BZR_EDITOR'] = bzr_editor
271
 
            if visual is None:
272
 
                del os.environ['VISUAL']
273
 
            else:
274
 
                os.environ['VISUAL'] = visual
275
 
            if editor is None:
276
 
                del os.environ['EDITOR']
277
 
            else:
278
 
                os.environ['EDITOR'] = editor
 
239
        self.overrideEnv('BZR_EDITOR', 'bzr_editor')
 
240
        self.overrideEnv('VISUAL', 'visual')
 
241
        self.overrideEnv('EDITOR', 'editor')
 
242
 
 
243
        conf = config.GlobalConfig.from_string('editor = config_editor\n',
 
244
                                               save=True)
 
245
 
 
246
        editors = list(msgeditor._get_editor())
 
247
        editors = [editor for (editor, cfg_src) in editors]
 
248
 
 
249
        self.assertEqual(['bzr_editor', 'config_editor', 'visual', 'editor'],
 
250
                         editors[:4])
 
251
 
 
252
        if sys.platform == 'win32':
 
253
            self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
254
        else:
 
255
            self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
 
256
                             editors[4:])
 
257
 
279
258
 
280
259
    def test__run_editor_EACCES(self):
281
260
        """If running a configured editor raises EACESS, the user is warned."""
282
 
        os.environ['BZR_EDITOR'] = 'eacces.py'
 
261
        self.overrideEnv('BZR_EDITOR', 'eacces.py')
283
262
        f = file('eacces.py', 'wb')
284
263
        f.write('# Not a real editor')
285
264
        f.close()
287
266
        os.chmod('eacces.py', 0)
288
267
        # Set $EDITOR so that _run_editor will terminate before trying real
289
268
        # editors.
290
 
        os.environ['EDITOR'] = self.make_do_nothing_editor()
 
269
        self.overrideEnv('EDITOR', self.make_do_nothing_editor())
291
270
        # Call _run_editor, capturing mutter.warning calls.
292
271
        warnings = []
293
272
        def warning(*args):
321
300
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
322
301
        self.requireFeature(tests.UnicodeFilenameFeature)
323
302
        if hasattr(self, 'info'):
324
 
            os.mkdir(self.info['directory'])
325
 
            os.chdir(self.info['directory'])
326
 
            msgeditor._create_temp_file_with_commit_template('infotext')
 
303
            tmpdir = self.info['directory']
 
304
            os.mkdir(tmpdir)
 
305
            # Force the creation of temp file in a directory whose name
 
306
            # requires some encoding support
 
307
            msgeditor._create_temp_file_with_commit_template('infotext',
 
308
                                                             tmpdir=tmpdir)
327
309
        else:
328
310
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
329
311
 
336
318
        self.assertFileEqual('', msgfilename)
337
319
 
338
320
    def test_unsupported_encoding_commit_message(self):
339
 
        old_env = osutils.set_or_unset_env('LANG', 'C')
340
 
        try:
341
 
            # LANG env variable has no effect on Windows
342
 
            # but some characters anyway cannot be represented
343
 
            # in default user encoding
344
 
            char = probe_bad_non_ascii(osutils.get_user_encoding())
345
 
            if char is None:
346
 
                raise TestSkipped('Cannot find suitable non-ascii character '
347
 
                    'for user_encoding (%s)' % osutils.get_user_encoding())
348
 
 
349
 
            self.make_fake_editor(message=char)
350
 
 
351
 
            working_tree = self.make_uncommitted_tree()
352
 
            self.assertRaises(errors.BadCommitMessageEncoding,
353
 
                              msgeditor.edit_commit_message, '')
354
 
        finally:
355
 
            osutils.set_or_unset_env('LANG', old_env)
 
321
        self.overrideEnv('LANG', 'C')
 
322
        # LANG env variable has no effect on Windows
 
323
        # but some characters anyway cannot be represented
 
324
        # in default user encoding
 
325
        char = probe_bad_non_ascii(osutils.get_user_encoding())
 
326
        if char is None:
 
327
            raise TestSkipped('Cannot find suitable non-ascii character '
 
328
                'for user_encoding (%s)' % osutils.get_user_encoding())
 
329
 
 
330
        self.make_fake_editor(message=char)
 
331
 
 
332
        working_tree = self.make_uncommitted_tree()
 
333
        self.assertRaises(errors.BadCommitMessageEncoding,
 
334
                          msgeditor.edit_commit_message, '')
356
335
 
357
336
    def test_generate_commit_message_template_no_hooks(self):
358
337
        commit_obj = commit.Commit()