~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Aaron Bentley
  • Date: 2006-06-14 19:45:57 UTC
  • mto: This revision was merged to the branch mainline in revision 1777.
  • Revision ID: abentley@panoramicfeedback.com-20060614194557-6b499aa1cf03f7e6
Use create_signature for signing policy, deprecate check_signatures for this

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#
3
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.
 
4
# it under the terms of the GNU General Public License version 2 as published by
 
5
# the Free Software Foundation.
7
6
#
8
7
# This program is distributed in the hope that it will be useful,
9
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20
19
import os
21
20
import sys
22
21
 
23
 
from bzrlib import (
24
 
    errors,
25
 
    msgeditor,
26
 
    osutils,
27
 
    tests,
28
 
    )
29
22
from bzrlib.branch import Branch
30
23
from bzrlib.config import ensure_config_dir_exists, config_filename
31
 
from bzrlib.msgeditor import (
32
 
    make_commit_message_template_encoded,
33
 
    edit_commit_message_encoded
34
 
)
35
 
from bzrlib.tests import (
36
 
    iter_suite_tests,
37
 
    probe_bad_non_ascii,
38
 
    split_suite_by_re,
39
 
    TestCaseWithTransport,
40
 
    TestNotApplicable,
41
 
    TestSkipped,
42
 
    )
43
 
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
 
24
import bzrlib.msgeditor 
 
25
from bzrlib.tests import TestCaseWithTransport, TestSkipped
44
26
from bzrlib.trace import mutter
45
27
 
46
28
 
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))
53
 
    return result
54
 
 
55
 
 
56
29
class MsgEditorTest(TestCaseWithTransport):
57
30
 
58
31
    def make_uncommitted_tree(self):
71
44
    def test_commit_template(self):
72
45
        """Test building a commit message template"""
73
46
        working_tree = self.make_uncommitted_tree()
74
 
        template = msgeditor.make_commit_message_template(working_tree,
75
 
                                                                 None)
 
47
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
76
48
        self.assertEqualDiff(template,
77
49
u"""\
78
50
added:
79
51
  hell\u00d8
80
52
""")
81
53
 
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,
86
 
                                                        None,
87
 
                                                        output_encoding='utf8')
88
 
        self.assertEqualDiff(template,
89
 
u"""\
90
 
added:
91
 
  hell\u00d8
92
 
""".encode("utf8"))
93
 
 
94
 
 
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,
99
 
                                                        None,
100
 
                                                        diff=True,
101
 
                                                        output_encoding='utf8')
102
 
 
103
 
        self.assertTrue("""\
104
 
@@ -0,0 +1,1 @@
105
 
+contents of hello
106
 
""" in template)
107
 
        self.assertTrue(u"""\
108
 
added:
109
 
  hell\u00d8
110
 
""".encode('utf8') in template)
 
54
    def setUp(self):
 
55
        super(MsgEditorTest, self).setUp()
 
56
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
 
57
 
 
58
    def tearDown(self):
 
59
        if self._bzr_editor != None:
 
60
            os.environ['BZR_EDITOR'] = self._bzr_editor
 
61
        else:
 
62
            if os.environ.get('BZR_EDITOR', None) != None:
 
63
                del os.environ['BZR_EDITOR']
 
64
        super(MsgEditorTest, self).tearDown()
111
65
 
112
66
    def test_run_editor(self):
113
67
        if sys.platform == "win32":
122
76
            os.chmod('fed.sh', 0755)
123
77
            os.environ['BZR_EDITOR'] = './fed.sh'
124
78
 
125
 
        self.assertEqual(True, msgeditor._run_editor(''),
 
79
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
126
80
                         'Unable to run dummy fake editor')
127
81
 
128
 
    def make_fake_editor(self, message='test message from fed\\n'):
129
 
        """Set up environment so that an editor will be a known script.
130
 
 
131
 
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
132
 
        script that just adds a known message to the start of the file.
133
 
        """
 
82
    def test_edit_commit_message(self):
 
83
        working_tree = self.make_uncommitted_tree()
 
84
        # make fake editor
134
85
        f = file('fed.py', 'wb')
135
86
        f.write('#!%s\n' % sys.executable)
136
87
        f.write("""\
137
 
# coding=utf-8
138
88
import sys
139
89
if len(sys.argv) == 2:
140
90
    fn = sys.argv[1]
142
92
    s = f.read()
143
93
    f.close()
144
94
    f = file(fn, 'wb')
145
 
    f.write('%s')
 
95
    f.write('test message from fed\\n')
146
96
    f.write(s)
147
97
    f.close()
148
 
""" % (message, ))
 
98
""")
149
99
        f.close()
150
100
        if sys.platform == "win32":
151
101
            # [win32] make batch file and set BZR_EDITOR
152
102
            f = file('fed.bat', 'w')
153
103
            f.write("""\
154
104
@echo off
155
 
"%s" fed.py %%1
 
105
%s fed.py %%1
156
106
""" % sys.executable)
157
107
            f.close()
158
108
            os.environ['BZR_EDITOR'] = 'fed.bat'
161
111
            os.chmod('fed.py', 0755)
162
112
            os.environ['BZR_EDITOR'] = './fed.py'
163
113
 
164
 
    def test_edit_commit_message(self):
165
 
        working_tree = self.make_uncommitted_tree()
166
 
        self.make_fake_editor()
167
 
 
168
114
        mutter('edit_commit_message without infotext')
169
115
        self.assertEqual('test message from fed\n',
170
 
                         msgeditor.edit_commit_message(''))
171
 
 
172
 
        mutter('edit_commit_message with ascii string infotext')
173
 
        self.assertEqual('test message from fed\n',
174
 
                         msgeditor.edit_commit_message('spam'))
 
116
                         bzrlib.msgeditor.edit_commit_message(''))
175
117
 
176
118
        mutter('edit_commit_message with unicode infotext')
177
119
        self.assertEqual('test message from fed\n',
178
 
                         msgeditor.edit_commit_message(u'\u1234'))
179
 
 
180
 
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
181
 
        self.assertEqual('test message from fed\n', tmpl)
182
 
 
183
 
    def test_start_message(self):
184
 
        self.make_uncommitted_tree()
185
 
        self.make_fake_editor()
186
 
        self.assertEqual('test message from fed\nstart message\n',
187
 
                         msgeditor.edit_commit_message('',
188
 
                                              start_message='start message\n'))
189
 
        self.assertEqual('test message from fed\n',
190
 
                         msgeditor.edit_commit_message('',
191
 
                                              start_message=''))
 
120
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
192
121
 
193
122
    def test_deleted_commit_message(self):
194
123
        working_tree = self.make_uncommitted_tree()
195
124
 
196
125
        if sys.platform == 'win32':
197
 
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
126
            os.environ['BZR_EDITOR'] = 'del'
198
127
        else:
199
128
            os.environ['BZR_EDITOR'] = 'rm'
200
129
 
201
 
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
 
130
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
202
131
 
203
132
    def test__get_editor(self):
204
133
        # Test that _get_editor can return a decent list of items
215
144
            f.write('editor = config_editor\n')
216
145
            f.close()
217
146
 
218
 
            editors = list(msgeditor._get_editor())
 
147
            editors = list(bzrlib.msgeditor._get_editor())
219
148
 
220
149
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
221
150
                              'editor'], editors[:4])
240
169
                del os.environ['EDITOR']
241
170
            else:
242
171
                os.environ['EDITOR'] = editor
243
 
 
244
 
    def test__create_temp_file_with_commit_template(self):
245
 
        # check that commit template written properly
246
 
        # and has platform native line-endings (CRLF on win32)
247
 
        create_file = msgeditor._create_temp_file_with_commit_template
248
 
        msgfilename, hasinfo = create_file('infotext','----','start message')
249
 
        self.assertNotEqual(None, msgfilename)
250
 
        self.assertTrue(hasinfo)
251
 
        expected = os.linesep.join(['start message',
252
 
                                    '',
253
 
                                    '',
254
 
                                    '----',
255
 
                                    '',
256
 
                                    'infotext'])
257
 
        self.assertFileEqual(expected, msgfilename)
258
 
 
259
 
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
260
 
        self.requireFeature(tests.UnicodeFilenameFeature)
261
 
        if hasattr(self, 'info'):
262
 
            os.mkdir(self.info['directory'])
263
 
            os.chdir(self.info['directory'])
264
 
            msgeditor._create_temp_file_with_commit_template('infotext')
265
 
        else:
266
 
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
267
 
 
268
 
    def test__create_temp_file_with_empty_commit_template(self):
269
 
        # empty file
270
 
        create_file = msgeditor._create_temp_file_with_commit_template
271
 
        msgfilename, hasinfo = create_file('')
272
 
        self.assertNotEqual(None, msgfilename)
273
 
        self.assertFalse(hasinfo)
274
 
        self.assertFileEqual('', msgfilename)
275
 
 
276
 
    def test_unsupported_encoding_commit_message(self):
277
 
        old_env = osutils.set_or_unset_env('LANG', 'C')
278
 
        try:
279
 
            # LANG env variable has no effect on Windows
280
 
            # but some characters anyway cannot be represented
281
 
            # in default user encoding
282
 
            char = probe_bad_non_ascii(osutils.get_user_encoding())
283
 
            if char is None:
284
 
                raise TestSkipped('Cannot find suitable non-ascii character '
285
 
                    'for user_encoding (%s)' % osutils.get_user_encoding())
286
 
 
287
 
            self.make_fake_editor(message=char)
288
 
 
289
 
            working_tree = self.make_uncommitted_tree()
290
 
            self.assertRaises(errors.BadCommitMessageEncoding,
291
 
                              msgeditor.edit_commit_message, '')
292
 
        finally:
293
 
            osutils.set_or_unset_env('LANG', old_env)