/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Martin Pool
  • Date: 2007-09-14 06:31:28 UTC
  • mfrom: (2822 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2823.
  • Revision ID: mbp@sourcefrog.net-20070914063128-0p7mh6zfb4pzdg9p
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import os
21
21
import sys
22
22
 
 
23
from bzrlib import (
 
24
    errors,
 
25
    msgeditor,
 
26
    osutils,
 
27
    )
23
28
from bzrlib.branch import Branch
24
29
from bzrlib.config import ensure_config_dir_exists, config_filename
25
 
import bzrlib.msgeditor 
 
30
from bzrlib.msgeditor import (
 
31
    make_commit_message_template_encoded,
 
32
    edit_commit_message_encoded
 
33
)
26
34
from bzrlib.tests import TestCaseWithTransport, TestSkipped
27
35
from bzrlib.trace import mutter
28
36
 
29
 
 
30
37
class MsgEditorTest(TestCaseWithTransport):
31
38
 
32
39
    def make_uncommitted_tree(self):
45
52
    def test_commit_template(self):
46
53
        """Test building a commit message template"""
47
54
        working_tree = self.make_uncommitted_tree()
48
 
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
 
55
        template = msgeditor.make_commit_message_template(working_tree,
 
56
                                                                 None)
49
57
        self.assertEqualDiff(template,
50
58
u"""\
51
59
added:
52
60
  hell\u00d8
53
61
""")
54
62
 
 
63
    def test_commit_template_encoded(self):
 
64
        """Test building a commit message template"""
 
65
        working_tree = self.make_uncommitted_tree()
 
66
        template = make_commit_message_template_encoded(working_tree,
 
67
                                                        None,
 
68
                                                        output_encoding='utf8')
 
69
        self.assertEqualDiff(template,
 
70
u"""\
 
71
added:
 
72
  hell\u00d8
 
73
""".encode("utf8"))
 
74
 
 
75
 
 
76
    def test_commit_template_and_diff(self):
 
77
        """Test building a commit message template"""
 
78
        working_tree = self.make_uncommitted_tree()
 
79
        template = make_commit_message_template_encoded(working_tree,
 
80
                                                        None,
 
81
                                                        diff=True,
 
82
                                                        output_encoding='utf8')
 
83
 
 
84
        self.assertTrue("""\
 
85
@@ -0,0 +1,1 @@
 
86
+contents of hello
 
87
""" in template)
 
88
        self.assertTrue(u"""\
 
89
added:
 
90
  hell\u00d8
 
91
""".encode('utf8') in template)
 
92
 
55
93
    def setUp(self):
56
94
        super(MsgEditorTest, self).setUp()
57
95
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
77
115
            os.chmod('fed.sh', 0755)
78
116
            os.environ['BZR_EDITOR'] = './fed.sh'
79
117
 
80
 
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
 
118
        self.assertEqual(True, msgeditor._run_editor(''),
81
119
                         'Unable to run dummy fake editor')
82
120
 
83
 
    def make_fake_editor(self):
 
121
    def make_fake_editor(self, message='test message from fed\\n'):
84
122
        """Set up environment so that an editor will be a known script.
85
123
 
86
124
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
89
127
        f = file('fed.py', 'wb')
90
128
        f.write('#!%s\n' % sys.executable)
91
129
        f.write("""\
 
130
# coding=utf-8
92
131
import sys
93
132
if len(sys.argv) == 2:
94
133
    fn = sys.argv[1]
96
135
    s = f.read()
97
136
    f.close()
98
137
    f = file(fn, 'wb')
99
 
    f.write('test message from fed\\n')
 
138
    f.write('%s')
100
139
    f.write(s)
101
140
    f.close()
102
 
""")
 
141
""" % (message, ))
103
142
        f.close()
104
143
        if sys.platform == "win32":
105
144
            # [win32] make batch file and set BZR_EDITOR
121
160
 
122
161
        mutter('edit_commit_message without infotext')
123
162
        self.assertEqual('test message from fed\n',
124
 
                         bzrlib.msgeditor.edit_commit_message(''))
 
163
                         msgeditor.edit_commit_message(''))
125
164
 
126
165
        mutter('edit_commit_message with unicode infotext')
127
166
        self.assertEqual('test message from fed\n',
128
 
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
 
167
                         msgeditor.edit_commit_message(u'\u1234'))
 
168
 
 
169
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
 
170
        self.assertEqual('test message from fed\n', tmpl)
129
171
 
130
172
    def test_start_message(self):
131
173
        self.make_uncommitted_tree()
132
174
        self.make_fake_editor()
133
175
        self.assertEqual('test message from fed\nstart message\n',
134
 
                         bzrlib.msgeditor.edit_commit_message('',
 
176
                         msgeditor.edit_commit_message('',
135
177
                                              start_message='start message\n'))
136
178
        self.assertEqual('test message from fed\n',
137
 
                         bzrlib.msgeditor.edit_commit_message('',
 
179
                         msgeditor.edit_commit_message('',
138
180
                                              start_message=''))
139
181
 
140
182
    def test_deleted_commit_message(self):
145
187
        else:
146
188
            os.environ['BZR_EDITOR'] = 'rm'
147
189
 
148
 
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
 
190
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
149
191
 
150
192
    def test__get_editor(self):
151
193
        # Test that _get_editor can return a decent list of items
162
204
            f.write('editor = config_editor\n')
163
205
            f.close()
164
206
 
165
 
            editors = list(bzrlib.msgeditor._get_editor())
 
207
            editors = list(msgeditor._get_editor())
166
208
 
167
209
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
168
210
                              'editor'], editors[:4])
191
233
    def test__create_temp_file_with_commit_template(self):
192
234
        # check that commit template written properly
193
235
        # and has platform native line-endings (CRLF on win32)
194
 
        create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
 
236
        create_file = msgeditor._create_temp_file_with_commit_template
195
237
        msgfilename, hasinfo = create_file('infotext','----','start message')
196
238
        self.assertNotEqual(None, msgfilename)
197
239
        self.assertTrue(hasinfo)
205
247
 
206
248
    def test__create_temp_file_with_empty_commit_template(self):
207
249
        # empty file
208
 
        create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
 
250
        create_file = msgeditor._create_temp_file_with_commit_template
209
251
        msgfilename, hasinfo = create_file('')
210
252
        self.assertNotEqual(None, msgfilename)
211
253
        self.assertFalse(hasinfo)
212
254
        self.assertFileEqual('', msgfilename)
 
255
 
 
256
    def test_unsupported_encoding_commit_message(self):
 
257
        old_env = osutils.set_or_unset_env('LANG', 'C')
 
258
        try:
 
259
            self.make_fake_editor(message='\xff')
 
260
 
 
261
            working_tree = self.make_uncommitted_tree()
 
262
            self.assertRaises(errors.BadCommitMessageEncoding,
 
263
                              msgeditor.edit_commit_message, '')
 
264
        finally:
 
265
            osutils.set_or_unset_env('LANG', old_env)