/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: Robert Collins
  • Date: 2007-09-19 05:14:14 UTC
  • mto: (2835.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2836.
  • Revision ID: robertc@robertcollins.net-20070919051414-2tgjqteg7k3ps4h0
* ``pull``, ``merge`` and ``push`` will no longer silently correct some
  repository index errors that occured as a result of the Weave disk format.
  Instead the ``reconcile`` command needs to be run to correct those
  problems if they exist (and it has been able to fix most such problems
  since bzr 0.8). Some new problems have been identified during this release
  and you should run ``bzr check`` once on every repository to see if you
  need to reconcile. If you cannot ``pull`` or ``merge`` from a remote
  repository due to mismatched parent errors - a symptom of index errors -
  you should simply take a full copy of that remote repository to a clean
  directory outside any local repositories, then run reconcile on it, and
  finally pull from it locally. (And naturally email the repositories owner
  to ask them to upgrade and run reconcile).
  (Robert Collins)

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