/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 breezy/tests/test_msgeditor.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
 
49
49
def load_tests(loader, standard_tests, pattern):
50
50
    """Parameterize the test for tempfile creation with different encodings."""
51
 
    to_adapt, result = split_suite_by_re(
52
 
        standard_tests,
 
51
    to_adapt, result = split_suite_by_re(standard_tests,
53
52
        "test__create_temp_file_with_commit_template_in_unicode_dir")
54
53
    return multiply_tests(to_adapt, encoding_scenarios, result)
55
54
 
59
58
    def make_uncommitted_tree(self):
60
59
        """Build a branch with uncommitted unicode named changes in the cwd."""
61
60
        working_tree = self.make_branch_and_tree('.')
 
61
        b = working_tree.branch
62
62
        filename = u'hell\u00d8'
63
63
        try:
64
64
            self.build_tree_contents([(filename, b'contents of hello')])
65
65
        except UnicodeEncodeError:
66
66
            self.skipTest("can't build unicode working tree in "
67
 
                          "filesystem encoding %s" % sys.getfilesystemencoding())
 
67
                "filesystem encoding %s" % sys.getfilesystemencoding())
68
68
        working_tree.add(filename)
69
69
        return working_tree
70
70
 
72
72
        """Test building a commit message template"""
73
73
        working_tree = self.make_uncommitted_tree()
74
74
        template = msgeditor.make_commit_message_template(working_tree,
75
 
                                                          None)
 
75
                                                                 None)
76
76
        self.assertEqualDiff(template,
77
 
                             u"""\
 
77
u"""\
78
78
added:
79
79
  hell\u00d8
80
80
""")
102
102
        working_tree = self.make_multiple_pending_tree()
103
103
        template = msgeditor.make_commit_message_template(working_tree, None)
104
104
        self.assertEqualDiff(template,
105
 
                             u"""\
 
105
u"""\
106
106
pending merges:
107
107
  Bilbo Baggins 2009-01-29 Feature X finished.
108
108
    Bilbo Baggins 2009-01-28 Feature X work.
116
116
                                                        None,
117
117
                                                        output_encoding='utf8')
118
118
        self.assertEqualDiff(template,
119
 
                             u"""\
 
119
u"""\
120
120
added:
121
121
  hell\u00d8
122
122
""".encode("utf8"))
123
123
 
 
124
 
124
125
    def test_commit_template_and_diff(self):
125
126
        """Test building a commit message template"""
126
127
        working_tree = self.make_uncommitted_tree()
129
130
                                                        diff=True,
130
131
                                                        output_encoding='utf8')
131
132
 
132
 
        self.assertTrue(b"""\
 
133
        self.assertTrue("""\
133
134
@@ -0,0 +1,1 @@
134
135
+contents of hello
135
136
""" in template)
141
142
    def make_do_nothing_editor(self, basename='fed'):
142
143
        if sys.platform == "win32":
143
144
            name = basename + '.bat'
144
 
            with open(name, 'w') as f:
145
 
                f.write('@rem dummy fed')
 
145
            f = file(name, 'w')
 
146
            f.write('@rem dummy fed')
 
147
            f.close()
146
148
            return name
147
149
        else:
148
150
            name = basename + '.sh'
149
 
            with open(name, 'wb') as f:
150
 
                f.write(b'#!/bin/sh\n')
 
151
            f = file(name, 'wb')
 
152
            f.write('#!/bin/sh\n')
 
153
            f.close()
151
154
            os.chmod(name, 0o755)
152
155
            return './' + name
153
156
 
162
165
        See <https://bugs.launchpad.net/bzr/+bug/220331>
163
166
        """
164
167
        self.overrideEnv('BRZ_EDITOR',
165
 
                         '"%s"' % self.make_do_nothing_editor('name with spaces'))
166
 
        self.assertEqual(True, msgeditor._run_editor('a_filename'))
 
168
            '"%s"' % self.make_do_nothing_editor('name with spaces'))
 
169
        self.assertEqual(True, msgeditor._run_editor('a_filename'))    
167
170
 
168
 
    def make_fake_editor(self, message='test message from fed\n'):
 
171
    def make_fake_editor(self, message='test message from fed\\n'):
169
172
        """Set up environment so that an editor will be a known script.
170
173
 
171
174
        Sets up BRZ_EDITOR so that if an editor is spawned it will run a
172
175
        script that just adds a known message to the start of the file.
173
176
        """
174
 
        if not isinstance(message, bytes):
175
 
            message = message.encode('utf-8')
176
 
        with open('fed.py', 'w') as f:
177
 
            f.write('#!%s\n' % sys.executable)
178
 
            f.write("""\
 
177
        f = file('fed.py', 'wb')
 
178
        f.write('#!%s\n' % sys.executable)
 
179
        f.write("""\
179
180
# coding=utf-8
180
181
import sys
181
182
if len(sys.argv) == 2:
182
183
    fn = sys.argv[1]
183
 
    with open(fn, 'rb') as f:
184
 
        s = f.read()
185
 
    with open(fn, 'wb') as f:
186
 
        f.write(%r)
187
 
        f.write(s)
 
184
    f = file(fn, 'rb')
 
185
    s = f.read()
 
186
    f.close()
 
187
    f = file(fn, 'wb')
 
188
    f.write('%s')
 
189
    f.write(s)
 
190
    f.close()
188
191
""" % (message, ))
 
192
        f.close()
189
193
        if sys.platform == "win32":
190
194
            # [win32] make batch file and set BRZ_EDITOR
191
 
            with open('fed.bat', 'w') as f:
192
 
                f.write("""\
 
195
            f = file('fed.bat', 'w')
 
196
            f.write("""\
193
197
@echo off
194
198
"%s" fed.py %%1
195
199
""" % sys.executable)
 
200
            f.close()
196
201
            self.overrideEnv('BRZ_EDITOR', 'fed.bat')
197
202
        else:
198
203
            # [non-win32] make python script executable and set BRZ_EDITOR
200
205
            self.overrideEnv('BRZ_EDITOR', './fed.py')
201
206
 
202
207
    def test_edit_commit_message_without_infotext(self):
203
 
        self.make_uncommitted_tree()
 
208
        working_tree = self.make_uncommitted_tree()
204
209
        self.make_fake_editor()
205
210
 
206
211
        mutter('edit_commit_message without infotext')
208
213
                         msgeditor.edit_commit_message(''))
209
214
 
210
215
    def test_edit_commit_message_with_ascii_infotext(self):
211
 
        self.make_uncommitted_tree()
 
216
        working_tree = self.make_uncommitted_tree()
212
217
        self.make_fake_editor()
213
218
 
214
219
        mutter('edit_commit_message with ascii string infotext')
216
221
                         msgeditor.edit_commit_message('spam'))
217
222
 
218
223
    def test_edit_commit_message_with_unicode_infotext(self):
219
 
        self.make_uncommitted_tree()
 
224
        working_tree = self.make_uncommitted_tree()
220
225
        self.make_fake_editor()
221
226
 
222
227
        mutter('edit_commit_message with unicode infotext')
236
241
        self.make_uncommitted_tree()
237
242
        self.make_fake_editor()
238
243
        self.assertEqual('test message from fed\nstart message\n',
239
 
                         msgeditor.edit_commit_message(
240
 
                             '', start_message='start message\n'))
 
244
                         msgeditor.edit_commit_message('',
 
245
                                              start_message='start message\n'))
241
246
        self.assertEqual('test message from fed\n',
242
 
                         msgeditor.edit_commit_message(
243
 
                             '', start_message=''))
 
247
                         msgeditor.edit_commit_message('',
 
248
                                              start_message=''))
244
249
 
245
250
    def test_deleted_commit_message(self):
246
 
        self.make_uncommitted_tree()
 
251
        working_tree = self.make_uncommitted_tree()
247
252
 
248
253
        if sys.platform == 'win32':
249
254
            editor = 'cmd.exe /c del'
251
256
            editor = 'rm'
252
257
        self.overrideEnv('BRZ_EDITOR', editor)
253
258
 
254
 
        self.assertRaises((EnvironmentError, errors.NoSuchFile),
255
 
                          msgeditor.edit_commit_message, '')
 
259
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
256
260
 
257
261
    def test__get_editor(self):
258
262
        self.overrideEnv('BRZ_EDITOR', 'bzr_editor')
260
264
        self.overrideEnv('EDITOR', 'editor')
261
265
 
262
266
        conf = config.GlobalStack()
263
 
        conf.store._load_from_string(b'[DEFAULT]\neditor = config_editor\n')
 
267
        conf.store._load_from_string('[DEFAULT]\neditor = config_editor\n')
264
268
        conf.store.save()
265
269
        editors = list(msgeditor._get_editor())
266
270
        editors = [editor for (editor, cfg_src) in editors]
274
278
            self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
275
279
                             editors[4:])
276
280
 
 
281
 
277
282
    def test__run_editor_EACCES(self):
278
283
        """If running a configured editor raises EACESS, the user is warned."""
279
284
        self.overrideEnv('BRZ_EDITOR', 'eacces.py')
280
 
        with open('eacces.py', 'wb') as f:
281
 
            f.write(b'# Not a real editor')
 
285
        f = file('eacces.py', 'wb')
 
286
        f.write('# Not a real editor')
 
287
        f.close()
282
288
        # Make the fake editor unreadable (and unexecutable)
283
289
        os.chmod('eacces.py', 0)
284
290
        # Set $EDITOR so that _run_editor will terminate before trying real
286
292
        self.overrideEnv('EDITOR', self.make_do_nothing_editor())
287
293
        # Call _run_editor, capturing mutter.warning calls.
288
294
        warnings = []
289
 
 
290
295
        def warning(*args):
291
296
            if len(args) > 1:
292
297
                warnings.append(args[0] % args[1:])
298
303
            msgeditor._run_editor('')
299
304
        finally:
300
305
            trace.warning = _warning
301
 
        self.assertStartsWith(
302
 
            warnings[0], 'Could not start editor "eacces.py"')
 
306
        self.assertStartsWith(warnings[0], 'Could not start editor "eacces.py"')
303
307
 
304
308
    def test__create_temp_file_with_commit_template(self):
305
309
        # check that commit template written properly
306
310
        # and has platform native line-endings (CRLF on win32)
307
311
        create_file = msgeditor._create_temp_file_with_commit_template
308
 
        msgfilename, hasinfo = create_file(
309
 
            b'infotext', '----', b'start message')
 
312
        msgfilename, hasinfo = create_file('infotext', '----', 'start message')
310
313
        self.assertNotEqual(None, msgfilename)
311
314
        self.assertTrue(hasinfo)
312
315
        expected = os.linesep.join(['start message',
324
327
            os.mkdir(tmpdir)
325
328
            # Force the creation of temp file in a directory whose name
326
329
            # requires some encoding support
327
 
            msgeditor._create_temp_file_with_commit_template(b'infotext',
 
330
            msgeditor._create_temp_file_with_commit_template('infotext',
328
331
                                                             tmpdir=tmpdir)
329
332
        else:
330
333
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
344
347
        # in default user encoding
345
348
        char = probe_bad_non_ascii(osutils.get_user_encoding())
346
349
        if char is None:
347
 
            self.skipTest(
348
 
                'Cannot find suitable non-ascii character '
 
350
            self.skipTest('Cannot find suitable non-ascii character '
349
351
                'for user_encoding (%s)' % osutils.get_user_encoding())
350
352
 
351
353
        self.make_fake_editor(message=char)
352
354
 
353
 
        self.make_uncommitted_tree()
 
355
        working_tree = self.make_uncommitted_tree()
354
356
        self.assertRaises(msgeditor.BadCommitMessageEncoding,
355
357
                          msgeditor.edit_commit_message, '')
356
358
 
357
359
    def test_set_commit_message_no_hooks(self):
358
360
        commit_obj = commit.Commit()
359
361
        self.assertIs(None,
360
 
                      msgeditor.set_commit_message(commit_obj))
 
362
            msgeditor.set_commit_message(commit_obj))
361
363
 
362
364
    def test_set_commit_message_hook(self):
363
365
        msgeditor.hooks.install_named_hook("set_commit_message",
364
 
                                           lambda commit_obj, existing_message: "save me some typing\n", None)
 
366
                lambda commit_obj, existing_message: "save me some typing\n", None)
365
367
        commit_obj = commit.Commit()
366
368
        self.assertEqual("save me some typing\n",
367
 
                         msgeditor.set_commit_message(commit_obj))
 
369
            msgeditor.set_commit_message(commit_obj))
368
370
 
369
371
    def test_generate_commit_message_template_no_hooks(self):
370
372
        commit_obj = commit.Commit()
371
373
        self.assertIs(None,
372
 
                      msgeditor.generate_commit_message_template(commit_obj))
 
374
            msgeditor.generate_commit_message_template(commit_obj))
373
375
 
374
376
    def test_generate_commit_message_template_hook(self):
375
377
        msgeditor.hooks.install_named_hook("commit_message_template",
376
 
                                           lambda commit_obj, msg: "save me some typing\n", None)
 
378
                lambda commit_obj, msg: "save me some typing\n", None)
377
379
        commit_obj = commit.Commit()
378
380
        self.assertEqual("save me some typing\n",
379
 
                         msgeditor.generate_commit_message_template(commit_obj))
 
381
            msgeditor.generate_commit_message_template(commit_obj))
380
382
 
381
383
 
382
384
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
386
388
    def test_subprocess_call_bad_file(self):
387
389
        if sys.platform != "win32":
388
390
            raise TestNotApplicable("Workarounds for windows only")
389
 
        import subprocess
390
 
        import errno
 
391
        import subprocess, errno
391
392
        ERROR_BAD_EXE_FORMAT = 193
392
 
        open("textfile.txt", "w").close()
 
393
        file("textfile.txt", "w").close()
393
394
        e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")
394
395
        self.assertEqual(e.errno, errno.ENOEXEC)
395
396
        self.assertEqual(e.winerror, ERROR_BAD_EXE_FORMAT)