~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-09-20 02:40:52 UTC
  • mfrom: (2835.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070920024052-y2l7r5o00zrpnr73
No longer propagate index differences automatically (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by 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
 
# it under the terms of the GNU General Public License version 2 as published by
5
 
# the Free Software Foundation.
 
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.
6
7
#
7
8
# This program is distributed in the hope that it will be useful,
8
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
20
import os
20
21
import sys
21
22
 
 
23
from bzrlib import (
 
24
    errors,
 
25
    msgeditor,
 
26
    osutils,
 
27
    )
22
28
from bzrlib.branch import Branch
23
29
from bzrlib.config import ensure_config_dir_exists, config_filename
24
 
from bzrlib.msgeditor import make_commit_message_template, _get_editor
 
30
from bzrlib.msgeditor import (
 
31
    make_commit_message_template_encoded,
 
32
    edit_commit_message_encoded
 
33
)
25
34
from bzrlib.tests import TestCaseWithTransport, TestSkipped
26
 
 
 
35
from bzrlib.trace import mutter
27
36
 
28
37
class MsgEditorTest(TestCaseWithTransport):
29
38
 
43
52
    def test_commit_template(self):
44
53
        """Test building a commit message template"""
45
54
        working_tree = self.make_uncommitted_tree()
46
 
        template = make_commit_message_template(working_tree, None)
 
55
        template = msgeditor.make_commit_message_template(working_tree,
 
56
                                                                 None)
47
57
        self.assertEqualDiff(template,
48
58
u"""\
49
59
added:
50
60
  hell\u00d8
51
61
""")
52
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
 
53
192
    def test__get_editor(self):
54
193
        # Test that _get_editor can return a decent list of items
55
194
        bzr_editor = os.environ.get('BZR_EDITOR')
65
204
            f.write('editor = config_editor\n')
66
205
            f.close()
67
206
 
68
 
            editors = list(_get_editor())
 
207
            editors = list(msgeditor._get_editor())
69
208
 
70
209
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
71
210
                              'editor'], editors[:4])
90
229
                del os.environ['EDITOR']
91
230
            else:
92
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)