1
# Copyright (C) 2005 by 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 version 2 as published by
5
# the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Test commit message editor.
22
from bzrlib.branch import Branch
23
import bzrlib.msgeditor
24
from bzrlib.tests import TestCaseWithTransport, TestSkipped
25
from bzrlib.trace import mutter
28
class MsgEditorTest(TestCaseWithTransport):
30
def make_uncommitted_tree(self):
31
"""Build a branch with uncommitted unicode named changes in the cwd."""
32
working_tree = self.make_branch_and_tree('.')
33
b = working_tree.branch
34
filename = u'hell\u00d8'
36
self.build_tree_contents([(filename, 'contents of hello')])
37
except UnicodeEncodeError:
38
raise TestSkipped("can't build unicode working tree in "
39
"filesystem encoding %s" % sys.getfilesystemencoding())
40
working_tree.add(filename)
43
def test_commit_template(self):
44
"""Test building a commit message template"""
45
working_tree = self.make_uncommitted_tree()
46
template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
47
self.assertEqualDiff(template,
54
super(MsgEditorTest, self).setUp()
55
self._bzr_editor = os.environ.get('BZR_EDITOR', None)
58
if self._bzr_editor != None:
59
os.environ['BZR_EDITOR'] = self._bzr_editor
61
if os.environ.get('BZR_EDITOR', None) != None:
62
del os.environ['BZR_EDITOR']
63
super(MsgEditorTest, self).tearDown()
65
def test_get_editor(self):
66
os.environ['BZR_EDITOR'] = 'fed'
67
editors = [i for i in bzrlib.msgeditor._get_editor()]
68
self.assertNotEqual([], editors, 'No editor found')
69
self.assertEqual('fed', editors[0])
71
def test_run_editor(self):
72
if sys.platform == "win32":
73
f = file('fed.bat', 'w')
74
f.write('@rem dummy fed')
76
os.environ['BZR_EDITOR'] = 'fed.bat'
78
f = file('fed.sh', 'wb')
79
f.write('#!/bin/sh\n')
81
os.chmod('fed.sh', 0755)
82
os.environ['BZR_EDITOR'] = './fed.sh'
84
self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
85
'Unable to run dummy fake editor')
87
def test_edit_commit_message(self):
88
working_tree = self.make_uncommitted_tree()
90
f = file('fed.py', 'wb')
91
f.write('#!%s\n' % sys.executable)
94
if len(sys.argv) == 2:
100
f.write('test message from fed\\n')
105
if sys.platform == "win32":
106
# [win32] make batch file and set BZR_EDITOR
107
f = file('fed.bat', 'w')
111
""" % sys.executable)
113
os.environ['BZR_EDITOR'] = 'fed.bat'
115
# [non-win32] make python script executable and set BZR_EDITOR
116
os.chmod('fed.py', 0755)
117
os.environ['BZR_EDITOR'] = './fed.py'
119
mutter('edit_commit_message without infotext')
120
self.assertEqual('test message from fed\n',
121
bzrlib.msgeditor.edit_commit_message(''))
123
mutter('edit_commit_message with unicode infotext')
124
self.assertEqual('test message from fed\n',
125
bzrlib.msgeditor.edit_commit_message(u'\u1234'))
127
def test_deleted_commit_message(self):
128
working_tree = self.make_uncommitted_tree()
130
if sys.platform == 'win32':
131
os.environ['BZR_EDITOR'] = 'del'
133
os.environ['BZR_EDITOR'] = 'rm'
135
self.assertRaises(IOError, bzrlib.msgeditor.edit_commit_message, '')