~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: 2007-11-03 23:02:16 UTC
  • mfrom: (2951.1.1 pack)
  • Revision ID: pqm@pqm.ubuntu.com-20071103230216-mnmwuxm413lyhjdv
(robertc) Fix data-refresh logic for packs not to refresh mid-transaction when a names write lock is held. (Robert Collins)

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
    TestSkipped,
 
39
    )
 
40
from bzrlib.trace import mutter
 
41
 
 
42
 
 
43
class MsgEditorTest(TestCaseWithTransport):
 
44
 
 
45
    def make_uncommitted_tree(self):
 
46
        """Build a branch with uncommitted unicode named changes in the cwd."""
 
47
        working_tree = self.make_branch_and_tree('.')
 
48
        b = working_tree.branch
 
49
        filename = u'hell\u00d8'
 
50
        try:
 
51
            self.build_tree_contents([(filename, 'contents of hello')])
 
52
        except UnicodeEncodeError:
 
53
            raise TestSkipped("can't build unicode working tree in "
 
54
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
55
        working_tree.add(filename)
 
56
        return working_tree
 
57
    
 
58
    def test_commit_template(self):
 
59
        """Test building a commit message template"""
 
60
        working_tree = self.make_uncommitted_tree()
 
61
        template = msgeditor.make_commit_message_template(working_tree,
 
62
                                                                 None)
 
63
        self.assertEqualDiff(template,
 
64
u"""\
 
65
added:
 
66
  hell\u00d8
 
67
""")
 
68
 
 
69
    def test_commit_template_encoded(self):
 
70
        """Test building a commit message template"""
 
71
        working_tree = self.make_uncommitted_tree()
 
72
        template = make_commit_message_template_encoded(working_tree,
 
73
                                                        None,
 
74
                                                        output_encoding='utf8')
 
75
        self.assertEqualDiff(template,
 
76
u"""\
 
77
added:
 
78
  hell\u00d8
 
79
""".encode("utf8"))
 
80
 
 
81
 
 
82
    def test_commit_template_and_diff(self):
 
83
        """Test building a commit message template"""
 
84
        working_tree = self.make_uncommitted_tree()
 
85
        template = make_commit_message_template_encoded(working_tree,
 
86
                                                        None,
 
87
                                                        diff=True,
 
88
                                                        output_encoding='utf8')
 
89
 
 
90
        self.assertTrue("""\
 
91
@@ -0,0 +1,1 @@
 
92
+contents of hello
 
93
""" in template)
 
94
        self.assertTrue(u"""\
 
95
added:
 
96
  hell\u00d8
 
97
""".encode('utf8') in template)
 
98
 
 
99
    def test_run_editor(self):
 
100
        if sys.platform == "win32":
 
101
            f = file('fed.bat', 'w')
 
102
            f.write('@rem dummy fed')
 
103
            f.close()
 
104
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
105
        else:
 
106
            f = file('fed.sh', 'wb')
 
107
            f.write('#!/bin/sh\n')
 
108
            f.close()
 
109
            os.chmod('fed.sh', 0755)
 
110
            os.environ['BZR_EDITOR'] = './fed.sh'
 
111
 
 
112
        self.assertEqual(True, msgeditor._run_editor(''),
 
113
                         'Unable to run dummy fake editor')
 
114
 
 
115
    def make_fake_editor(self, message='test message from fed\\n'):
 
116
        """Set up environment so that an editor will be a known script.
 
117
 
 
118
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
 
119
        script that just adds a known message to the start of the file.
 
120
        """
 
121
        f = file('fed.py', 'wb')
 
122
        f.write('#!%s\n' % sys.executable)
 
123
        f.write("""\
 
124
# coding=utf-8
 
125
import sys
 
126
if len(sys.argv) == 2:
 
127
    fn = sys.argv[1]
 
128
    f = file(fn, 'rb')
 
129
    s = f.read()
 
130
    f.close()
 
131
    f = file(fn, 'wb')
 
132
    f.write('%s')
 
133
    f.write(s)
 
134
    f.close()
 
135
""" % (message, ))
 
136
        f.close()
 
137
        if sys.platform == "win32":
 
138
            # [win32] make batch file and set BZR_EDITOR
 
139
            f = file('fed.bat', 'w')
 
140
            f.write("""\
 
141
@echo off
 
142
"%s" fed.py %%1
 
143
""" % sys.executable)
 
144
            f.close()
 
145
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
146
        else:
 
147
            # [non-win32] make python script executable and set BZR_EDITOR
 
148
            os.chmod('fed.py', 0755)
 
149
            os.environ['BZR_EDITOR'] = './fed.py'
 
150
 
 
151
    def test_edit_commit_message(self):
 
152
        working_tree = self.make_uncommitted_tree()
 
153
        self.make_fake_editor()
 
154
 
 
155
        mutter('edit_commit_message without infotext')
 
156
        self.assertEqual('test message from fed\n',
 
157
                         msgeditor.edit_commit_message(''))
 
158
 
 
159
        mutter('edit_commit_message with ascii string infotext')
 
160
        self.assertEqual('test message from fed\n',
 
161
                         msgeditor.edit_commit_message('spam'))
 
162
 
 
163
        mutter('edit_commit_message with unicode infotext')
 
164
        self.assertEqual('test message from fed\n',
 
165
                         msgeditor.edit_commit_message(u'\u1234'))
 
166
 
 
167
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
 
168
        self.assertEqual('test message from fed\n', tmpl)
 
169
 
 
170
    def test_start_message(self):
 
171
        self.make_uncommitted_tree()
 
172
        self.make_fake_editor()
 
173
        self.assertEqual('test message from fed\nstart message\n',
 
174
                         msgeditor.edit_commit_message('',
 
175
                                              start_message='start message\n'))
 
176
        self.assertEqual('test message from fed\n',
 
177
                         msgeditor.edit_commit_message('',
 
178
                                              start_message=''))
 
179
 
 
180
    def test_deleted_commit_message(self):
 
181
        working_tree = self.make_uncommitted_tree()
 
182
 
 
183
        if sys.platform == 'win32':
 
184
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
185
        else:
 
186
            os.environ['BZR_EDITOR'] = 'rm'
 
187
 
 
188
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
 
189
 
 
190
    def test__get_editor(self):
 
191
        # Test that _get_editor can return a decent list of items
 
192
        bzr_editor = os.environ.get('BZR_EDITOR')
 
193
        visual = os.environ.get('VISUAL')
 
194
        editor = os.environ.get('EDITOR')
 
195
        try:
 
196
            os.environ['BZR_EDITOR'] = 'bzr_editor'
 
197
            os.environ['VISUAL'] = 'visual'
 
198
            os.environ['EDITOR'] = 'editor'
 
199
 
 
200
            ensure_config_dir_exists()
 
201
            f = open(config_filename(), 'wb')
 
202
            f.write('editor = config_editor\n')
 
203
            f.close()
 
204
 
 
205
            editors = list(msgeditor._get_editor())
 
206
 
 
207
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
 
208
                              'editor'], editors[:4])
 
209
 
 
210
            if sys.platform == 'win32':
 
211
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
212
            else:
 
213
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
 
214
                                  'joe'], editors[4:])
 
215
 
 
216
        finally:
 
217
            # Restore the environment
 
218
            if bzr_editor is None:
 
219
                del os.environ['BZR_EDITOR']
 
220
            else:
 
221
                os.environ['BZR_EDITOR'] = bzr_editor
 
222
            if visual is None:
 
223
                del os.environ['VISUAL']
 
224
            else:
 
225
                os.environ['VISUAL'] = visual
 
226
            if editor is None:
 
227
                del os.environ['EDITOR']
 
228
            else:
 
229
                os.environ['EDITOR'] = editor
 
230
 
 
231
    def test__create_temp_file_with_commit_template(self):
 
232
        # check that commit template written properly
 
233
        # and has platform native line-endings (CRLF on win32)
 
234
        create_file = msgeditor._create_temp_file_with_commit_template
 
235
        msgfilename, hasinfo = create_file('infotext','----','start message')
 
236
        self.assertNotEqual(None, msgfilename)
 
237
        self.assertTrue(hasinfo)
 
238
        expected = os.linesep.join(['start message',
 
239
                                    '',
 
240
                                    '',
 
241
                                    '----',
 
242
                                    '',
 
243
                                    'infotext'])
 
244
        self.assertFileEqual(expected, msgfilename)
 
245
 
 
246
    def test__create_temp_file_with_empty_commit_template(self):
 
247
        # empty file
 
248
        create_file = msgeditor._create_temp_file_with_commit_template
 
249
        msgfilename, hasinfo = create_file('')
 
250
        self.assertNotEqual(None, msgfilename)
 
251
        self.assertFalse(hasinfo)
 
252
        self.assertFileEqual('', msgfilename)
 
253
 
 
254
    def test_unsupported_encoding_commit_message(self):
 
255
        old_env = osutils.set_or_unset_env('LANG', 'C')
 
256
        try:
 
257
            # LANG env variable has no effect on Windows
 
258
            # but some characters anyway cannot be represented
 
259
            # in default user encoding
 
260
            char = probe_bad_non_ascii(bzrlib.user_encoding)
 
261
            if char is None:
 
262
                raise TestSkipped('Cannot find suitable non-ascii character '
 
263
                    'for user_encoding (%s)' % bzrlib.user_encoding)
 
264
 
 
265
            self.make_fake_editor(message=char)
 
266
 
 
267
            working_tree = self.make_uncommitted_tree()
 
268
            self.assertRaises(errors.BadCommitMessageEncoding,
 
269
                              msgeditor.edit_commit_message, '')
 
270
        finally:
 
271
            osutils.set_or_unset_env('LANG', old_env)