~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: 2007-12-05 19:55:07 UTC
  • mto: This revision was merged to the branch mainline in revision 3082.
  • Revision ID: john@arbash-meinel.com-20071205195507-1ql7vuval5qug5eu
Clean up some vim: lines to make them proper ReST comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Test commit message editor.
 
18
"""
 
19
 
 
20
import os
 
21
import sys
 
22
 
 
23
import bzrlib
 
24
from bzrlib import (
 
25
    errors,
 
26
    msgeditor,
 
27
    osutils,
 
28
    )
 
29
from bzrlib.branch import Branch
 
30
from bzrlib.config import ensure_config_dir_exists, config_filename
 
31
from bzrlib.msgeditor import (
 
32
    make_commit_message_template_encoded,
 
33
    edit_commit_message_encoded
 
34
)
 
35
from bzrlib.tests import (
 
36
    probe_bad_non_ascii,
 
37
    TestCaseWithTransport,
 
38
    TestNotApplicable,
 
39
    TestSkipped,
 
40
    )
 
41
from bzrlib.trace import mutter
 
42
 
 
43
 
 
44
class MsgEditorTest(TestCaseWithTransport):
 
45
 
 
46
    def make_uncommitted_tree(self):
 
47
        """Build a branch with uncommitted unicode named changes in the cwd."""
 
48
        working_tree = self.make_branch_and_tree('.')
 
49
        b = working_tree.branch
 
50
        filename = u'hell\u00d8'
 
51
        try:
 
52
            self.build_tree_contents([(filename, 'contents of hello')])
 
53
        except UnicodeEncodeError:
 
54
            raise TestSkipped("can't build unicode working tree in "
 
55
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
56
        working_tree.add(filename)
 
57
        return working_tree
 
58
    
 
59
    def test_commit_template(self):
 
60
        """Test building a commit message template"""
 
61
        working_tree = self.make_uncommitted_tree()
 
62
        template = msgeditor.make_commit_message_template(working_tree,
 
63
                                                                 None)
 
64
        self.assertEqualDiff(template,
 
65
u"""\
 
66
added:
 
67
  hell\u00d8
 
68
""")
 
69
 
 
70
    def test_commit_template_encoded(self):
 
71
        """Test building a commit message template"""
 
72
        working_tree = self.make_uncommitted_tree()
 
73
        template = make_commit_message_template_encoded(working_tree,
 
74
                                                        None,
 
75
                                                        output_encoding='utf8')
 
76
        self.assertEqualDiff(template,
 
77
u"""\
 
78
added:
 
79
  hell\u00d8
 
80
""".encode("utf8"))
 
81
 
 
82
 
 
83
    def test_commit_template_and_diff(self):
 
84
        """Test building a commit message template"""
 
85
        working_tree = self.make_uncommitted_tree()
 
86
        template = make_commit_message_template_encoded(working_tree,
 
87
                                                        None,
 
88
                                                        diff=True,
 
89
                                                        output_encoding='utf8')
 
90
 
 
91
        self.assertTrue("""\
 
92
@@ -0,0 +1,1 @@
 
93
+contents of hello
 
94
""" in template)
 
95
        self.assertTrue(u"""\
 
96
added:
 
97
  hell\u00d8
 
98
""".encode('utf8') in template)
 
99
 
 
100
    def test_run_editor(self):
 
101
        if sys.platform == "win32":
 
102
            f = file('fed.bat', 'w')
 
103
            f.write('@rem dummy fed')
 
104
            f.close()
 
105
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
106
        else:
 
107
            f = file('fed.sh', 'wb')
 
108
            f.write('#!/bin/sh\n')
 
109
            f.close()
 
110
            os.chmod('fed.sh', 0755)
 
111
            os.environ['BZR_EDITOR'] = './fed.sh'
 
112
 
 
113
        self.assertEqual(True, msgeditor._run_editor(''),
 
114
                         'Unable to run dummy fake editor')
 
115
 
 
116
    def make_fake_editor(self, message='test message from fed\\n'):
 
117
        """Set up environment so that an editor will be a known script.
 
118
 
 
119
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
 
120
        script that just adds a known message to the start of the file.
 
121
        """
 
122
        f = file('fed.py', 'wb')
 
123
        f.write('#!%s\n' % sys.executable)
 
124
        f.write("""\
 
125
# coding=utf-8
 
126
import sys
 
127
if len(sys.argv) == 2:
 
128
    fn = sys.argv[1]
 
129
    f = file(fn, 'rb')
 
130
    s = f.read()
 
131
    f.close()
 
132
    f = file(fn, 'wb')
 
133
    f.write('%s')
 
134
    f.write(s)
 
135
    f.close()
 
136
""" % (message, ))
 
137
        f.close()
 
138
        if sys.platform == "win32":
 
139
            # [win32] make batch file and set BZR_EDITOR
 
140
            f = file('fed.bat', 'w')
 
141
            f.write("""\
 
142
@echo off
 
143
"%s" fed.py %%1
 
144
""" % sys.executable)
 
145
            f.close()
 
146
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
147
        else:
 
148
            # [non-win32] make python script executable and set BZR_EDITOR
 
149
            os.chmod('fed.py', 0755)
 
150
            os.environ['BZR_EDITOR'] = './fed.py'
 
151
 
 
152
    def test_edit_commit_message(self):
 
153
        working_tree = self.make_uncommitted_tree()
 
154
        self.make_fake_editor()
 
155
 
 
156
        mutter('edit_commit_message without infotext')
 
157
        self.assertEqual('test message from fed\n',
 
158
                         msgeditor.edit_commit_message(''))
 
159
 
 
160
        mutter('edit_commit_message with ascii string infotext')
 
161
        self.assertEqual('test message from fed\n',
 
162
                         msgeditor.edit_commit_message('spam'))
 
163
 
 
164
        mutter('edit_commit_message with unicode infotext')
 
165
        self.assertEqual('test message from fed\n',
 
166
                         msgeditor.edit_commit_message(u'\u1234'))
 
167
 
 
168
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
 
169
        self.assertEqual('test message from fed\n', tmpl)
 
170
 
 
171
    def test_start_message(self):
 
172
        self.make_uncommitted_tree()
 
173
        self.make_fake_editor()
 
174
        self.assertEqual('test message from fed\nstart message\n',
 
175
                         msgeditor.edit_commit_message('',
 
176
                                              start_message='start message\n'))
 
177
        self.assertEqual('test message from fed\n',
 
178
                         msgeditor.edit_commit_message('',
 
179
                                              start_message=''))
 
180
 
 
181
    def test_deleted_commit_message(self):
 
182
        working_tree = self.make_uncommitted_tree()
 
183
 
 
184
        if sys.platform == 'win32':
 
185
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
186
        else:
 
187
            os.environ['BZR_EDITOR'] = 'rm'
 
188
 
 
189
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
 
190
 
 
191
    def test__get_editor(self):
 
192
        # Test that _get_editor can return a decent list of items
 
193
        bzr_editor = os.environ.get('BZR_EDITOR')
 
194
        visual = os.environ.get('VISUAL')
 
195
        editor = os.environ.get('EDITOR')
 
196
        try:
 
197
            os.environ['BZR_EDITOR'] = 'bzr_editor'
 
198
            os.environ['VISUAL'] = 'visual'
 
199
            os.environ['EDITOR'] = 'editor'
 
200
 
 
201
            ensure_config_dir_exists()
 
202
            f = open(config_filename(), 'wb')
 
203
            f.write('editor = config_editor\n')
 
204
            f.close()
 
205
 
 
206
            editors = list(msgeditor._get_editor())
 
207
 
 
208
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
 
209
                              'editor'], editors[:4])
 
210
 
 
211
            if sys.platform == 'win32':
 
212
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
213
            else:
 
214
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
 
215
                                  'joe'], editors[4:])
 
216
 
 
217
        finally:
 
218
            # Restore the environment
 
219
            if bzr_editor is None:
 
220
                del os.environ['BZR_EDITOR']
 
221
            else:
 
222
                os.environ['BZR_EDITOR'] = bzr_editor
 
223
            if visual is None:
 
224
                del os.environ['VISUAL']
 
225
            else:
 
226
                os.environ['VISUAL'] = visual
 
227
            if editor is None:
 
228
                del os.environ['EDITOR']
 
229
            else:
 
230
                os.environ['EDITOR'] = editor
 
231
 
 
232
    def test__create_temp_file_with_commit_template(self):
 
233
        # check that commit template written properly
 
234
        # and has platform native line-endings (CRLF on win32)
 
235
        create_file = msgeditor._create_temp_file_with_commit_template
 
236
        msgfilename, hasinfo = create_file('infotext','----','start message')
 
237
        self.assertNotEqual(None, msgfilename)
 
238
        self.assertTrue(hasinfo)
 
239
        expected = os.linesep.join(['start message',
 
240
                                    '',
 
241
                                    '',
 
242
                                    '----',
 
243
                                    '',
 
244
                                    'infotext'])
 
245
        self.assertFileEqual(expected, msgfilename)
 
246
 
 
247
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
 
248
        if hasattr(self, 'info'):
 
249
            os.mkdir(self.info['directory'])
 
250
            os.chdir(self.info['directory'])
 
251
            msgeditor._create_temp_file_with_commit_template('infotext')
 
252
        else:
 
253
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
 
254
 
 
255
    def test__create_temp_file_with_empty_commit_template(self):
 
256
        # empty file
 
257
        create_file = msgeditor._create_temp_file_with_commit_template
 
258
        msgfilename, hasinfo = create_file('')
 
259
        self.assertNotEqual(None, msgfilename)
 
260
        self.assertFalse(hasinfo)
 
261
        self.assertFileEqual('', msgfilename)
 
262
 
 
263
    def test_unsupported_encoding_commit_message(self):
 
264
        old_env = osutils.set_or_unset_env('LANG', 'C')
 
265
        try:
 
266
            # LANG env variable has no effect on Windows
 
267
            # but some characters anyway cannot be represented
 
268
            # in default user encoding
 
269
            char = probe_bad_non_ascii(bzrlib.user_encoding)
 
270
            if char is None:
 
271
                raise TestSkipped('Cannot find suitable non-ascii character '
 
272
                    'for user_encoding (%s)' % bzrlib.user_encoding)
 
273
 
 
274
            self.make_fake_editor(message=char)
 
275
 
 
276
            working_tree = self.make_uncommitted_tree()
 
277
            self.assertRaises(errors.BadCommitMessageEncoding,
 
278
                              msgeditor.edit_commit_message, '')
 
279
        finally:
 
280
            osutils.set_or_unset_env('LANG', old_env)