~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Andrew Bennetts
  • Date: 2009-03-04 07:10:07 UTC
  • mto: (4086.1.2 hpss-integration)
  • mto: This revision was merged to the branch mainline in revision 4087.
  • Revision ID: andrew.bennetts@canonical.com-20090304071007-8iqoi1m44ypmzg2a
Rough prototype of allowing a SearchResult to be passed to fetch, and using that to improve network conversations.

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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Test commit message editor.
18
18
"""
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,
 
38
    iter_suite_tests,
 
39
    probe_bad_non_ascii,
 
40
    split_suite_by_re,
40
41
    TestCaseWithTransport,
41
42
    TestNotApplicable,
42
43
    TestSkipped,
43
 
    multiply_tests,
44
 
    probe_bad_non_ascii,
45
 
    split_suite_by_re,
46
44
    )
47
 
from bzrlib.tests.EncodingAdapter import encoding_scenarios
 
45
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
48
46
from bzrlib.trace import mutter
49
47
 
50
48
 
52
50
    """Parameterize the test for tempfile creation with different encodings."""
53
51
    to_adapt, result = split_suite_by_re(standard_tests,
54
52
        "test__create_temp_file_with_commit_template_in_unicode_dir")
55
 
    return multiply_tests(to_adapt, encoding_scenarios, result)
 
53
    for test in iter_suite_tests(to_adapt):
 
54
        result.addTests(EncodingTestAdapter().adapt(test))
 
55
    return result
56
56
 
57
57
 
58
58
class MsgEditorTest(TestCaseWithTransport):
95
95
        tree3.commit('Feature Y, based on initial X work.',
96
96
                     timestamp=1233285960, timezone=0)
97
97
        tree.merge_from_branch(tree2.branch)
98
 
        tree.merge_from_branch(tree3.branch, force=True)
 
98
        tree.merge_from_branch(tree3.branch)
99
99
        return tree
100
100
 
101
101
    def test_commit_template_pending_merges(self):
237
237
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
238
238
 
239
239
    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
 
 
 
240
        # Test that _get_editor can return a decent list of items
 
241
        bzr_editor = os.environ.get('BZR_EDITOR')
 
242
        visual = os.environ.get('VISUAL')
 
243
        editor = os.environ.get('EDITOR')
 
244
        try:
 
245
            os.environ['BZR_EDITOR'] = 'bzr_editor'
 
246
            os.environ['VISUAL'] = 'visual'
 
247
            os.environ['EDITOR'] = 'editor'
 
248
 
 
249
            ensure_config_dir_exists()
 
250
            f = open(config_filename(), 'wb')
 
251
            f.write('editor = config_editor\n')
 
252
            f.close()
 
253
 
 
254
            editors = list(msgeditor._get_editor())
 
255
            editors = [editor for (editor, cfg_src) in editors]
 
256
 
 
257
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
 
258
                              'editor'], editors[:4])
 
259
 
 
260
            if sys.platform == 'win32':
 
261
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
262
            else:
 
263
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
 
264
                                  'joe'], editors[4:])
 
265
 
 
266
        finally:
 
267
            # Restore the environment
 
268
            if bzr_editor is None:
 
269
                del os.environ['BZR_EDITOR']
 
270
            else:
 
271
                os.environ['BZR_EDITOR'] = bzr_editor
 
272
            if visual is None:
 
273
                del os.environ['VISUAL']
 
274
            else:
 
275
                os.environ['VISUAL'] = visual
 
276
            if editor is None:
 
277
                del os.environ['EDITOR']
 
278
            else:
 
279
                os.environ['EDITOR'] = editor
259
280
 
260
281
    def test__run_editor_EACCES(self):
261
282
        """If running a configured editor raises EACESS, the user is warned."""
271
292
        # Call _run_editor, capturing mutter.warning calls.
272
293
        warnings = []
273
294
        def warning(*args):
274
 
            if len(args) > 1:
275
 
                warnings.append(args[0] % args[1:])
276
 
            else:
277
 
                warnings.append(args[0])
 
295
            warnings.append(args[0] % args[1:])
278
296
        _warning = trace.warning
279
297
        trace.warning = warning
280
298
        try:
340
358
            msgeditor.generate_commit_message_template(commit_obj))
341
359
 
342
360
    def test_generate_commit_message_template_hook(self):
 
361
        def restoreDefaults():
 
362
            msgeditor.hooks['commit_message_template'] = []
 
363
        self.addCleanup(restoreDefaults)
343
364
        msgeditor.hooks.install_named_hook("commit_message_template",
344
365
                lambda commit_obj, msg: "save me some typing\n", None)
345
366
        commit_obj = commit.Commit()
346
367
        self.assertEquals("save me some typing\n",
347
368
            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)