23
29
from bzrlib.branch import Branch
24
30
from bzrlib.config import ensure_config_dir_exists, config_filename
25
import bzrlib.msgeditor
26
from bzrlib.tests import TestCaseWithTransport, TestSkipped
31
from bzrlib.msgeditor import (
32
make_commit_message_template_encoded,
33
edit_commit_message_encoded
35
from bzrlib.tests import (
39
TestCaseWithTransport,
43
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
27
44
from bzrlib.trace import mutter
47
def load_tests(standard_tests, module, loader):
48
"""Parameterize the test for tempfile creation with different encodings."""
49
to_adapt, result = split_suite_by_re(standard_tests,
50
"test__create_temp_file_with_commit_template_in_unicode_dir")
51
for test in iter_suite_tests(to_adapt):
52
result.addTests(EncodingTestAdapter().adapt(test))
30
56
class MsgEditorTest(TestCaseWithTransport):
32
58
def make_uncommitted_tree(self):
45
71
def test_commit_template(self):
46
72
"""Test building a commit message template"""
47
73
working_tree = self.make_uncommitted_tree()
48
template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
74
template = msgeditor.make_commit_message_template(working_tree,
49
76
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()
82
def test_commit_template_encoded(self):
83
"""Test building a commit message template"""
84
working_tree = self.make_uncommitted_tree()
85
template = make_commit_message_template_encoded(working_tree,
87
output_encoding='utf8')
88
self.assertEqualDiff(template,
95
def test_commit_template_and_diff(self):
96
"""Test building a commit message template"""
97
working_tree = self.make_uncommitted_tree()
98
template = make_commit_message_template_encoded(working_tree,
101
output_encoding='utf8')
107
self.assertTrue(u"""\
110
""".encode('utf8') in template)
67
112
def test_run_editor(self):
68
113
if sys.platform == "win32":
77
122
os.chmod('fed.sh', 0755)
78
123
os.environ['BZR_EDITOR'] = './fed.sh'
80
self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
125
self.assertEqual(True, msgeditor._run_editor(''),
81
126
'Unable to run dummy fake editor')
83
def make_fake_editor(self):
128
def make_fake_editor(self, message='test message from fed\\n'):
84
129
"""Set up environment so that an editor will be a known script.
86
131
Sets up BZR_EDITOR so that if an editor is spawned it will run a
122
168
mutter('edit_commit_message without infotext')
123
169
self.assertEqual('test message from fed\n',
124
bzrlib.msgeditor.edit_commit_message(''))
170
msgeditor.edit_commit_message(''))
172
mutter('edit_commit_message with ascii string infotext')
173
self.assertEqual('test message from fed\n',
174
msgeditor.edit_commit_message('spam'))
126
176
mutter('edit_commit_message with unicode infotext')
127
177
self.assertEqual('test message from fed\n',
128
bzrlib.msgeditor.edit_commit_message(u'\u1234'))
178
msgeditor.edit_commit_message(u'\u1234'))
180
tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
181
self.assertEqual('test message from fed\n', tmpl)
130
183
def test_start_message(self):
131
184
self.make_uncommitted_tree()
132
185
self.make_fake_editor()
133
186
self.assertEqual('test message from fed\nstart message\n',
134
bzrlib.msgeditor.edit_commit_message('',
187
msgeditor.edit_commit_message('',
135
188
start_message='start message\n'))
136
189
self.assertEqual('test message from fed\n',
137
bzrlib.msgeditor.edit_commit_message('',
190
msgeditor.edit_commit_message('',
138
191
start_message=''))
140
193
def test_deleted_commit_message(self):
146
199
os.environ['BZR_EDITOR'] = 'rm'
148
self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
201
self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
150
203
def test__get_editor(self):
151
204
# Test that _get_editor can return a decent list of items
162
215
f.write('editor = config_editor\n')
165
editors = list(bzrlib.msgeditor._get_editor())
218
editors = list(msgeditor._get_editor())
167
220
self.assertEqual(['bzr_editor', 'config_editor', 'visual',
168
221
'editor'], editors[:4])
191
244
def test__create_temp_file_with_commit_template(self):
192
245
# check that commit template written properly
193
246
# and has platform native line-endings (CRLF on win32)
194
create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
247
create_file = msgeditor._create_temp_file_with_commit_template
195
248
msgfilename, hasinfo = create_file('infotext','----','start message')
196
249
self.assertNotEqual(None, msgfilename)
197
250
self.assertTrue(hasinfo)
204
257
self.assertFileEqual(expected, msgfilename)
259
def test__create_temp_file_with_commit_template_in_unicode_dir(self):
260
from bzrlib.tests.test_diff import UnicodeFilename
261
self.requireFeature(UnicodeFilename)
262
if hasattr(self, 'info'):
263
os.mkdir(self.info['directory'])
264
os.chdir(self.info['directory'])
265
msgeditor._create_temp_file_with_commit_template('infotext')
267
raise TestNotApplicable('Test run elsewhere with non-ascii data.')
206
269
def test__create_temp_file_with_empty_commit_template(self):
208
create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
271
create_file = msgeditor._create_temp_file_with_commit_template
209
272
msgfilename, hasinfo = create_file('')
210
273
self.assertNotEqual(None, msgfilename)
211
274
self.assertFalse(hasinfo)
212
275
self.assertFileEqual('', msgfilename)
277
def test_unsupported_encoding_commit_message(self):
278
old_env = osutils.set_or_unset_env('LANG', 'C')
280
# LANG env variable has no effect on Windows
281
# but some characters anyway cannot be represented
282
# in default user encoding
283
char = probe_bad_non_ascii(bzrlib.user_encoding)
285
raise TestSkipped('Cannot find suitable non-ascii character '
286
'for user_encoding (%s)' % bzrlib.user_encoding)
288
self.make_fake_editor(message=char)
290
working_tree = self.make_uncommitted_tree()
291
self.assertRaises(errors.BadCommitMessageEncoding,
292
msgeditor.edit_commit_message, '')
294
osutils.set_or_unset_env('LANG', old_env)