~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Aaron Bentley
  • Date: 2007-02-06 14:52:16 UTC
  • mfrom: (2266 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2268.
  • Revision ID: abentley@panoramicfeedback.com-20070206145216-fcpi8o3ufvuzwbp9
Merge bzr.dev

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
 
    tests,
29
 
    )
30
23
from bzrlib.branch import Branch
31
24
from bzrlib.config import ensure_config_dir_exists, config_filename
32
 
from bzrlib.msgeditor import (
33
 
    make_commit_message_template_encoded,
34
 
    edit_commit_message_encoded
35
 
)
36
 
from bzrlib.tests import (
37
 
    iter_suite_tests,
38
 
    probe_bad_non_ascii,
39
 
    split_suite_by_re,
40
 
    TestCaseWithTransport,
41
 
    TestNotApplicable,
42
 
    TestSkipped,
43
 
    )
44
 
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
 
25
import bzrlib.msgeditor 
 
26
from bzrlib.tests import TestCaseWithTransport, TestSkipped
45
27
from bzrlib.trace import mutter
46
28
 
47
29
 
48
 
def load_tests(standard_tests, module, loader):
49
 
    """Parameterize the test for tempfile creation with different encodings."""
50
 
    to_adapt, result = split_suite_by_re(standard_tests,
51
 
        "test__create_temp_file_with_commit_template_in_unicode_dir")
52
 
    for test in iter_suite_tests(to_adapt):
53
 
        result.addTests(EncodingTestAdapter().adapt(test))
54
 
    return result
55
 
 
56
 
 
57
30
class MsgEditorTest(TestCaseWithTransport):
58
31
 
59
32
    def make_uncommitted_tree(self):
72
45
    def test_commit_template(self):
73
46
        """Test building a commit message template"""
74
47
        working_tree = self.make_uncommitted_tree()
75
 
        template = msgeditor.make_commit_message_template(working_tree,
76
 
                                                                 None)
 
48
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
77
49
        self.assertEqualDiff(template,
78
50
u"""\
79
51
added:
80
52
  hell\u00d8
81
53
""")
82
54
 
83
 
    def test_commit_template_encoded(self):
84
 
        """Test building a commit message template"""
85
 
        working_tree = self.make_uncommitted_tree()
86
 
        template = make_commit_message_template_encoded(working_tree,
87
 
                                                        None,
88
 
                                                        output_encoding='utf8')
89
 
        self.assertEqualDiff(template,
90
 
u"""\
91
 
added:
92
 
  hell\u00d8
93
 
""".encode("utf8"))
94
 
 
95
 
 
96
 
    def test_commit_template_and_diff(self):
97
 
        """Test building a commit message template"""
98
 
        working_tree = self.make_uncommitted_tree()
99
 
        template = make_commit_message_template_encoded(working_tree,
100
 
                                                        None,
101
 
                                                        diff=True,
102
 
                                                        output_encoding='utf8')
103
 
 
104
 
        self.assertTrue("""\
105
 
@@ -0,0 +1,1 @@
106
 
+contents of hello
107
 
""" in template)
108
 
        self.assertTrue(u"""\
109
 
added:
110
 
  hell\u00d8
111
 
""".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()
112
66
 
113
67
    def test_run_editor(self):
114
68
        if sys.platform == "win32":
123
77
            os.chmod('fed.sh', 0755)
124
78
            os.environ['BZR_EDITOR'] = './fed.sh'
125
79
 
126
 
        self.assertEqual(True, msgeditor._run_editor(''),
 
80
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
127
81
                         'Unable to run dummy fake editor')
128
82
 
129
 
    def make_fake_editor(self, message='test message from fed\\n'):
130
 
        """Set up environment so that an editor will be a known script.
131
 
 
132
 
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
133
 
        script that just adds a known message to the start of the file.
134
 
        """
 
83
    def test_edit_commit_message(self):
 
84
        working_tree = self.make_uncommitted_tree()
 
85
        # make fake editor
135
86
        f = file('fed.py', 'wb')
136
87
        f.write('#!%s\n' % sys.executable)
137
88
        f.write("""\
138
 
# coding=utf-8
139
89
import sys
140
90
if len(sys.argv) == 2:
141
91
    fn = sys.argv[1]
143
93
    s = f.read()
144
94
    f.close()
145
95
    f = file(fn, 'wb')
146
 
    f.write('%s')
 
96
    f.write('test message from fed\\n')
147
97
    f.write(s)
148
98
    f.close()
149
 
""" % (message, ))
 
99
""")
150
100
        f.close()
151
101
        if sys.platform == "win32":
152
102
            # [win32] make batch file and set BZR_EDITOR
162
112
            os.chmod('fed.py', 0755)
163
113
            os.environ['BZR_EDITOR'] = './fed.py'
164
114
 
165
 
    def test_edit_commit_message(self):
166
 
        working_tree = self.make_uncommitted_tree()
167
 
        self.make_fake_editor()
168
 
 
169
115
        mutter('edit_commit_message without infotext')
170
116
        self.assertEqual('test message from fed\n',
171
 
                         msgeditor.edit_commit_message(''))
172
 
 
173
 
        mutter('edit_commit_message with ascii string infotext')
174
 
        self.assertEqual('test message from fed\n',
175
 
                         msgeditor.edit_commit_message('spam'))
 
117
                         bzrlib.msgeditor.edit_commit_message(''))
176
118
 
177
119
        mutter('edit_commit_message with unicode infotext')
178
120
        self.assertEqual('test message from fed\n',
179
 
                         msgeditor.edit_commit_message(u'\u1234'))
180
 
 
181
 
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
182
 
        self.assertEqual('test message from fed\n', tmpl)
183
 
 
184
 
    def test_start_message(self):
185
 
        self.make_uncommitted_tree()
186
 
        self.make_fake_editor()
187
 
        self.assertEqual('test message from fed\nstart message\n',
188
 
                         msgeditor.edit_commit_message('',
189
 
                                              start_message='start message\n'))
190
 
        self.assertEqual('test message from fed\n',
191
 
                         msgeditor.edit_commit_message('',
192
 
                                              start_message=''))
 
121
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
193
122
 
194
123
    def test_deleted_commit_message(self):
195
124
        working_tree = self.make_uncommitted_tree()
199
128
        else:
200
129
            os.environ['BZR_EDITOR'] = 'rm'
201
130
 
202
 
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
 
131
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
203
132
 
204
133
    def test__get_editor(self):
205
134
        # Test that _get_editor can return a decent list of items
216
145
            f.write('editor = config_editor\n')
217
146
            f.close()
218
147
 
219
 
            editors = list(msgeditor._get_editor())
 
148
            editors = list(bzrlib.msgeditor._get_editor())
220
149
 
221
150
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
222
151
                              'editor'], editors[:4])
241
170
                del os.environ['EDITOR']
242
171
            else:
243
172
                os.environ['EDITOR'] = editor
244
 
 
245
 
    def test__create_temp_file_with_commit_template(self):
246
 
        # check that commit template written properly
247
 
        # and has platform native line-endings (CRLF on win32)
248
 
        create_file = msgeditor._create_temp_file_with_commit_template
249
 
        msgfilename, hasinfo = create_file('infotext','----','start message')
250
 
        self.assertNotEqual(None, msgfilename)
251
 
        self.assertTrue(hasinfo)
252
 
        expected = os.linesep.join(['start message',
253
 
                                    '',
254
 
                                    '',
255
 
                                    '----',
256
 
                                    '',
257
 
                                    'infotext'])
258
 
        self.assertFileEqual(expected, msgfilename)
259
 
 
260
 
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
261
 
        self.requireFeature(tests.UnicodeFilenameFeature)
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)