~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: John Arbash Meinel
  • Date: 2007-05-04 18:59:36 UTC
  • mto: This revision was merged to the branch mainline in revision 2643.
  • Revision ID: john@arbash-meinel.com-20070504185936-1mjdoqmtz74xe5mg
A C implementation of _fields_to_entry_0_parents drops the time from 400ms to 330ms for a 21k-entry tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import os
21
21
import sys
22
22
 
23
 
import bzrlib
24
 
from bzrlib import (
25
 
    errors,
26
 
    msgeditor,
27
 
    osutils,
28
 
    )
29
23
from bzrlib.branch import Branch
30
24
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
 
25
import bzrlib.msgeditor 
 
26
from bzrlib.tests import TestCaseWithTransport, TestSkipped
44
27
from bzrlib.trace import mutter
45
28
 
46
29
 
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
30
class MsgEditorTest(TestCaseWithTransport):
57
31
 
58
32
    def make_uncommitted_tree(self):
71
45
    def test_commit_template(self):
72
46
        """Test building a commit message template"""
73
47
        working_tree = self.make_uncommitted_tree()
74
 
        template = msgeditor.make_commit_message_template(working_tree,
75
 
                                                                 None)
 
48
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
76
49
        self.assertEqualDiff(template,
77
50
u"""\
78
51
added:
79
52
  hell\u00d8
80
53
""")
81
54
 
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)
 
55
    def setUp(self):
 
56
        super(MsgEditorTest, self).setUp()
 
57
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
 
58
 
 
59
    def tearDown(self):
 
60
        if self._bzr_editor is not None:
 
61
            os.environ['BZR_EDITOR'] = self._bzr_editor
 
62
        else:
 
63
            if os.environ.get('BZR_EDITOR', None) is not None:
 
64
                del os.environ['BZR_EDITOR']
 
65
        super(MsgEditorTest, self).tearDown()
111
66
 
112
67
    def test_run_editor(self):
113
68
        if sys.platform == "win32":
122
77
            os.chmod('fed.sh', 0755)
123
78
            os.environ['BZR_EDITOR'] = './fed.sh'
124
79
 
125
 
        self.assertEqual(True, msgeditor._run_editor(''),
 
80
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
126
81
                         'Unable to run dummy fake editor')
127
82
 
128
 
    def make_fake_editor(self, message='test message from fed\\n'):
 
83
    def make_fake_editor(self):
129
84
        """Set up environment so that an editor will be a known script.
130
85
 
131
86
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
134
89
        f = file('fed.py', 'wb')
135
90
        f.write('#!%s\n' % sys.executable)
136
91
        f.write("""\
137
 
# coding=utf-8
138
92
import sys
139
93
if len(sys.argv) == 2:
140
94
    fn = sys.argv[1]
142
96
    s = f.read()
143
97
    f.close()
144
98
    f = file(fn, 'wb')
145
 
    f.write('%s')
 
99
    f.write('test message from fed\\n')
146
100
    f.write(s)
147
101
    f.close()
148
 
""" % (message, ))
 
102
""")
149
103
        f.close()
150
104
        if sys.platform == "win32":
151
105
            # [win32] make batch file and set BZR_EDITOR
167
121
 
168
122
        mutter('edit_commit_message without infotext')
169
123
        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'))
 
124
                         bzrlib.msgeditor.edit_commit_message(''))
175
125
 
176
126
        mutter('edit_commit_message with unicode infotext')
177
127
        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)
 
128
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
182
129
 
183
130
    def test_start_message(self):
184
131
        self.make_uncommitted_tree()
185
132
        self.make_fake_editor()
186
133
        self.assertEqual('test message from fed\nstart message\n',
187
 
                         msgeditor.edit_commit_message('',
 
134
                         bzrlib.msgeditor.edit_commit_message('',
188
135
                                              start_message='start message\n'))
189
136
        self.assertEqual('test message from fed\n',
190
 
                         msgeditor.edit_commit_message('',
 
137
                         bzrlib.msgeditor.edit_commit_message('',
191
138
                                              start_message=''))
192
139
 
193
140
    def test_deleted_commit_message(self):
198
145
        else:
199
146
            os.environ['BZR_EDITOR'] = 'rm'
200
147
 
201
 
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
 
148
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
202
149
 
203
150
    def test__get_editor(self):
204
151
        # Test that _get_editor can return a decent list of items
215
162
            f.write('editor = config_editor\n')
216
163
            f.close()
217
164
 
218
 
            editors = list(msgeditor._get_editor())
 
165
            editors = list(bzrlib.msgeditor._get_editor())
219
166
 
220
167
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
221
168
                              'editor'], editors[:4])
240
187
                del os.environ['EDITOR']
241
188
            else:
242
189
                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
 
        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')
266
 
        else:
267
 
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
268
 
 
269
 
    def test__create_temp_file_with_empty_commit_template(self):
270
 
        # empty file
271
 
        create_file = msgeditor._create_temp_file_with_commit_template
272
 
        msgfilename, hasinfo = create_file('')
273
 
        self.assertNotEqual(None, msgfilename)
274
 
        self.assertFalse(hasinfo)
275
 
        self.assertFileEqual('', msgfilename)
276
 
 
277
 
    def test_unsupported_encoding_commit_message(self):
278
 
        old_env = osutils.set_or_unset_env('LANG', 'C')
279
 
        try:
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)
284
 
            if char is None:
285
 
                raise TestSkipped('Cannot find suitable non-ascii character '
286
 
                    'for user_encoding (%s)' % bzrlib.user_encoding)
287
 
 
288
 
            self.make_fake_editor(message=char)
289
 
 
290
 
            working_tree = self.make_uncommitted_tree()
291
 
            self.assertRaises(errors.BadCommitMessageEncoding,
292
 
                              msgeditor.edit_commit_message, '')
293
 
        finally:
294
 
            osutils.set_or_unset_env('LANG', old_env)