~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: 2006-09-26 23:30:56 UTC
  • mfrom: (2044.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060926233056-203e713964b2cd51
(Robert Collins) Forward merge from 0.11rc2 NEWS and performance-regression fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
from bzrlib.branch import Branch
23
23
from bzrlib.config import ensure_config_dir_exists, config_filename
24
 
from bzrlib.msgeditor import make_commit_message_template, _get_editor
 
24
import bzrlib.msgeditor 
25
25
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
26
from bzrlib.trace import mutter
26
27
 
27
28
 
28
29
class MsgEditorTest(TestCaseWithTransport):
43
44
    def test_commit_template(self):
44
45
        """Test building a commit message template"""
45
46
        working_tree = self.make_uncommitted_tree()
46
 
        template = make_commit_message_template(working_tree, None)
 
47
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
47
48
        self.assertEqualDiff(template,
48
49
u"""\
49
50
added:
50
51
  hell\u00d8
51
52
""")
52
53
 
 
54
    def setUp(self):
 
55
        super(MsgEditorTest, self).setUp()
 
56
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
 
57
 
 
58
    def tearDown(self):
 
59
        if self._bzr_editor is not None:
 
60
            os.environ['BZR_EDITOR'] = self._bzr_editor
 
61
        else:
 
62
            if os.environ.get('BZR_EDITOR', None) is not None:
 
63
                del os.environ['BZR_EDITOR']
 
64
        super(MsgEditorTest, self).tearDown()
 
65
 
 
66
    def test_run_editor(self):
 
67
        if sys.platform == "win32":
 
68
            f = file('fed.bat', 'w')
 
69
            f.write('@rem dummy fed')
 
70
            f.close()
 
71
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
72
        else:
 
73
            f = file('fed.sh', 'wb')
 
74
            f.write('#!/bin/sh\n')
 
75
            f.close()
 
76
            os.chmod('fed.sh', 0755)
 
77
            os.environ['BZR_EDITOR'] = './fed.sh'
 
78
 
 
79
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
 
80
                         'Unable to run dummy fake editor')
 
81
 
 
82
    def test_edit_commit_message(self):
 
83
        working_tree = self.make_uncommitted_tree()
 
84
        # make fake editor
 
85
        f = file('fed.py', 'wb')
 
86
        f.write('#!%s\n' % sys.executable)
 
87
        f.write("""\
 
88
import sys
 
89
if len(sys.argv) == 2:
 
90
    fn = sys.argv[1]
 
91
    f = file(fn, 'rb')
 
92
    s = f.read()
 
93
    f.close()
 
94
    f = file(fn, 'wb')
 
95
    f.write('test message from fed\\n')
 
96
    f.write(s)
 
97
    f.close()
 
98
""")
 
99
        f.close()
 
100
        if sys.platform == "win32":
 
101
            # [win32] make batch file and set BZR_EDITOR
 
102
            f = file('fed.bat', 'w')
 
103
            f.write("""\
 
104
@echo off
 
105
"%s" fed.py %%1
 
106
""" % sys.executable)
 
107
            f.close()
 
108
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
109
        else:
 
110
            # [non-win32] make python script executable and set BZR_EDITOR
 
111
            os.chmod('fed.py', 0755)
 
112
            os.environ['BZR_EDITOR'] = './fed.py'
 
113
 
 
114
        mutter('edit_commit_message without infotext')
 
115
        self.assertEqual('test message from fed\n',
 
116
                         bzrlib.msgeditor.edit_commit_message(''))
 
117
 
 
118
        mutter('edit_commit_message with unicode infotext')
 
119
        self.assertEqual('test message from fed\n',
 
120
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
 
121
 
 
122
    def test_deleted_commit_message(self):
 
123
        working_tree = self.make_uncommitted_tree()
 
124
 
 
125
        if sys.platform == 'win32':
 
126
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
127
        else:
 
128
            os.environ['BZR_EDITOR'] = 'rm'
 
129
 
 
130
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
 
131
 
53
132
    def test__get_editor(self):
54
133
        # Test that _get_editor can return a decent list of items
55
134
        bzr_editor = os.environ.get('BZR_EDITOR')
65
144
            f.write('editor = config_editor\n')
66
145
            f.close()
67
146
 
68
 
            editors = list(_get_editor())
 
147
            editors = list(bzrlib.msgeditor._get_editor())
69
148
 
70
149
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
71
150
                              'editor'], editors[:4])