22
22
from bzrlib.branch import Branch
23
from bzrlib.msgeditor import make_commit_message_template
24
from bzrlib.tests import TestCaseInTempDir, TestSkipped
27
class MsgEditorTest(TestCaseInTempDir):
23
from bzrlib.config import ensure_config_dir_exists, config_filename
24
import bzrlib.msgeditor
25
from bzrlib.tests import TestCaseWithTransport, TestSkipped
26
from bzrlib.trace import mutter
29
class MsgEditorTest(TestCaseWithTransport):
29
31
def make_uncommitted_tree(self):
30
32
"""Build a branch with uncommitted unicode named changes in the cwd."""
31
b = Branch.initialize('.')
32
working_tree = b.working_tree()
33
working_tree = self.make_branch_and_tree('.')
34
b = working_tree.branch
33
35
filename = u'hell\u00d8'
35
37
self.build_tree_contents([(filename, 'contents of hello')])
42
44
def test_commit_template(self):
43
45
"""Test building a commit message template"""
44
46
working_tree = self.make_uncommitted_tree()
45
template = make_commit_message_template(working_tree, None)
47
template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
46
48
self.assertEqualDiff(template,
55
super(MsgEditorTest, self).setUp()
56
self._bzr_editor = os.environ.get('BZR_EDITOR', None)
59
if self._bzr_editor != None:
60
os.environ['BZR_EDITOR'] = self._bzr_editor
62
if os.environ.get('BZR_EDITOR', None) != None:
63
del os.environ['BZR_EDITOR']
64
super(MsgEditorTest, self).tearDown()
66
def test_run_editor(self):
67
if sys.platform == "win32":
68
f = file('fed.bat', 'w')
69
f.write('@rem dummy fed')
71
os.environ['BZR_EDITOR'] = 'fed.bat'
73
f = file('fed.sh', 'wb')
74
f.write('#!/bin/sh\n')
76
os.chmod('fed.sh', 0755)
77
os.environ['BZR_EDITOR'] = './fed.sh'
79
self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
80
'Unable to run dummy fake editor')
82
def test_edit_commit_message(self):
83
working_tree = self.make_uncommitted_tree()
85
f = file('fed.py', 'wb')
86
f.write('#!%s\n' % sys.executable)
89
if len(sys.argv) == 2:
95
f.write('test message from fed\\n')
100
if sys.platform == "win32":
101
# [win32] make batch file and set BZR_EDITOR
102
f = file('fed.bat', 'w')
106
""" % sys.executable)
108
os.environ['BZR_EDITOR'] = 'fed.bat'
110
# [non-win32] make python script executable and set BZR_EDITOR
111
os.chmod('fed.py', 0755)
112
os.environ['BZR_EDITOR'] = './fed.py'
114
mutter('edit_commit_message without infotext')
115
self.assertEqual('test message from fed\n',
116
bzrlib.msgeditor.edit_commit_message(''))
118
mutter('edit_commit_message with unicode infotext')
119
self.assertEqual('test message from fed\n',
120
bzrlib.msgeditor.edit_commit_message(u'\u1234'))
122
def test_deleted_commit_message(self):
123
working_tree = self.make_uncommitted_tree()
125
if sys.platform == 'win32':
126
os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
128
os.environ['BZR_EDITOR'] = 'rm'
130
self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
132
def test__get_editor(self):
133
# Test that _get_editor can return a decent list of items
134
bzr_editor = os.environ.get('BZR_EDITOR')
135
visual = os.environ.get('VISUAL')
136
editor = os.environ.get('EDITOR')
138
os.environ['BZR_EDITOR'] = 'bzr_editor'
139
os.environ['VISUAL'] = 'visual'
140
os.environ['EDITOR'] = 'editor'
142
ensure_config_dir_exists()
143
f = open(config_filename(), 'wb')
144
f.write('editor = config_editor\n')
147
editors = list(bzrlib.msgeditor._get_editor())
149
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
150
'editor'], editors[:4])
152
if sys.platform == 'win32':
153
self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
155
self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
159
# Restore the environment
160
if bzr_editor is None:
161
del os.environ['BZR_EDITOR']
163
os.environ['BZR_EDITOR'] = bzr_editor
165
del os.environ['VISUAL']
167
os.environ['VISUAL'] = visual
169
del os.environ['EDITOR']
171
os.environ['EDITOR'] = editor