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
30
class MsgEditorTest(TestCaseWithTransport):
32
def make_uncommitted_tree(self):
33
"""Build a branch with uncommitted unicode named changes in the cwd."""
34
working_tree = self.make_branch_and_tree('.')
35
b = working_tree.branch
36
filename = u'hell\u00d8'
38
self.build_tree_contents([(filename, 'contents of hello')])
39
except UnicodeEncodeError:
40
raise TestSkipped("can't build unicode working tree in "
41
"filesystem encoding %s" % sys.getfilesystemencoding())
42
working_tree.add(filename)
45
def test_commit_template(self):
46
"""Test building a commit message template"""
47
working_tree = self.make_uncommitted_tree()
48
template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
49
self.assertEqualDiff(template,
56
super(MsgEditorTest, self).setUp()
57
self._bzr_editor = os.environ.get('BZR_EDITOR', None)
60
if self._bzr_editor is not None:
61
os.environ['BZR_EDITOR'] = self._bzr_editor
63
if os.environ.get('BZR_EDITOR', None) is not None:
64
del os.environ['BZR_EDITOR']
65
super(MsgEditorTest, self).tearDown()
67
def test_run_editor(self):
68
if sys.platform == "win32":
69
f = file('fed.bat', 'w')
70
f.write('@rem dummy fed')
72
os.environ['BZR_EDITOR'] = 'fed.bat'
74
f = file('fed.sh', 'wb')
75
f.write('#!/bin/sh\n')
77
os.chmod('fed.sh', 0755)
78
os.environ['BZR_EDITOR'] = './fed.sh'
80
self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
81
'Unable to run dummy fake editor')
83
def make_fake_editor(self):
84
"""Set up environment so that an editor will be a known script.
86
Sets up BZR_EDITOR so that if an editor is spawned it will run a
87
script that just adds a known message to the start of the file.
89
f = file('fed.py', 'wb')
90
f.write('#!%s\n' % sys.executable)
93
if len(sys.argv) == 2:
99
f.write('test message from fed\\n')
104
if sys.platform == "win32":
105
# [win32] make batch file and set BZR_EDITOR
106
f = file('fed.bat', 'w')
110
""" % sys.executable)
112
os.environ['BZR_EDITOR'] = 'fed.bat'
114
# [non-win32] make python script executable and set BZR_EDITOR
115
os.chmod('fed.py', 0755)
116
os.environ['BZR_EDITOR'] = './fed.py'
118
def test_edit_commit_message(self):
119
working_tree = self.make_uncommitted_tree()
120
self.make_fake_editor()
122
mutter('edit_commit_message without infotext')
123
self.assertEqual('test message from fed\n',
124
bzrlib.msgeditor.edit_commit_message(''))
126
mutter('edit_commit_message with unicode infotext')
127
self.assertEqual('test message from fed\n',
128
bzrlib.msgeditor.edit_commit_message(u'\u1234'))
130
def test_start_message(self):
131
self.make_uncommitted_tree()
132
self.make_fake_editor()
133
self.assertEqual('test message from fed\nstart message\n',
134
bzrlib.msgeditor.edit_commit_message('',
135
start_message='start message\n'))
136
self.assertEqual('test message from fed\n',
137
bzrlib.msgeditor.edit_commit_message('',
140
def test_deleted_commit_message(self):
141
working_tree = self.make_uncommitted_tree()
143
if sys.platform == 'win32':
144
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
146
os.environ['BZR_EDITOR'] = 'rm'
148
self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
150
def test__get_editor(self):
151
# Test that _get_editor can return a decent list of items
152
bzr_editor = os.environ.get('BZR_EDITOR')
153
visual = os.environ.get('VISUAL')
154
editor = os.environ.get('EDITOR')
156
os.environ['BZR_EDITOR'] = 'bzr_editor'
157
os.environ['VISUAL'] = 'visual'
158
os.environ['EDITOR'] = 'editor'
160
ensure_config_dir_exists()
161
f = open(config_filename(), 'wb')
162
f.write('editor = config_editor\n')
165
editors = list(bzrlib.msgeditor._get_editor())
167
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
168
'editor'], editors[:4])
170
if sys.platform == 'win32':
171
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
173
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
177
# Restore the environment
178
if bzr_editor is None:
179
del os.environ['BZR_EDITOR']
181
os.environ['BZR_EDITOR'] = bzr_editor
183
del os.environ['VISUAL']
185
os.environ['VISUAL'] = visual
187
del os.environ['EDITOR']
189
os.environ['EDITOR'] = editor