~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Aaron Bentley
  • Date: 2005-10-18 18:48:27 UTC
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1474.
  • Revision ID: abentley@panoramicfeedback.com-20051018184827-2cc69376beb1cdf3
Switched to ConfigObj

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