1
# Copyright (C) 2005 Canonical Ltd
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.
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.
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
17
"""Test commit message editor.
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
35
class MsgEditorTest(TestCaseWithTransport):
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'
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)
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,
61
super(MsgEditorTest, self).setUp()
62
self._bzr_editor = os.environ.get('BZR_EDITOR', None)
65
if self._bzr_editor is not None:
66
os.environ['BZR_EDITOR'] = self._bzr_editor
68
if os.environ.get('BZR_EDITOR', None) is not None:
69
del os.environ['BZR_EDITOR']
70
super(MsgEditorTest, self).tearDown()
72
def test_run_editor(self):
73
if sys.platform == "win32":
74
f = file('fed.bat', 'w')
75
f.write('@rem dummy fed')
77
os.environ['BZR_EDITOR'] = 'fed.bat'
79
f = file('fed.sh', 'wb')
80
f.write('#!/bin/sh\n')
82
os.chmod('fed.sh', 0755)
83
os.environ['BZR_EDITOR'] = './fed.sh'
85
self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
86
'Unable to run dummy fake editor')
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.
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.
94
f = file('fed.py', 'wb')
95
f.write('#!%s\n' % sys.executable)
99
if len(sys.argv) == 2:
110
if sys.platform == "win32":
111
# [win32] make batch file and set BZR_EDITOR
112
f = file('fed.bat', 'w')
116
""" % sys.executable)
118
os.environ['BZR_EDITOR'] = 'fed.bat'
120
# [non-win32] make python script executable and set BZR_EDITOR
121
os.chmod('fed.py', 0755)
122
os.environ['BZR_EDITOR'] = './fed.py'
124
def test_edit_commit_message(self):
125
working_tree = self.make_uncommitted_tree()
126
self.make_fake_editor()
128
mutter('edit_commit_message without infotext')
129
self.assertEqual('test message from fed\n',
130
bzrlib.msgeditor.edit_commit_message(''))
132
mutter('edit_commit_message with unicode infotext')
133
self.assertEqual('test message from fed\n',
134
bzrlib.msgeditor.edit_commit_message(u'\u1234'))
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('',
146
def test_deleted_commit_message(self):
147
working_tree = self.make_uncommitted_tree()
149
if sys.platform == 'win32':
150
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
152
os.environ['BZR_EDITOR'] = 'rm'
154
self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
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')
162
os.environ['BZR_EDITOR'] = 'bzr_editor'
163
os.environ['VISUAL'] = 'visual'
164
os.environ['EDITOR'] = 'editor'
166
ensure_config_dir_exists()
167
f = open(config_filename(), 'wb')
168
f.write('editor = config_editor\n')
171
editors = list(bzrlib.msgeditor._get_editor())
173
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
174
'editor'], editors[:4])
176
if sys.platform == 'win32':
177
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
179
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
183
# Restore the environment
184
if bzr_editor is None:
185
del os.environ['BZR_EDITOR']
187
os.environ['BZR_EDITOR'] = bzr_editor
189
del os.environ['VISUAL']
191
os.environ['VISUAL'] = visual
193
del os.environ['EDITOR']
195
os.environ['EDITOR'] = editor
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',
210
self.assertFileEqual(expected, msgfilename)
212
def test__create_temp_file_with_empty_commit_template(self):
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)
220
def test_unsupported_encoding_commit_message(self):
221
old_env = osutils.set_or_unset_env('LANG', 'C')
223
self.make_fake_editor(message='\xff')
225
working_tree = self.make_uncommitted_tree()
226
self.assertRaises(errors.BadCommitMessageEncoding,
227
bzrlib.msgeditor.edit_commit_message, '')
229
osutils.set_or_unset_env('LANG', old_env)