/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-11-16 19:47:19 UTC
  • mfrom: (7178 work)
  • mto: This revision was merged to the branch mainline in revision 7179.
  • Revision ID: jelmer@jelmer.uk-20181116194719-m5ut2wfuze5x9s1p
Merge trunk.

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(standard_tests,
 
51
    to_adapt, result = split_suite_by_re(
 
52
        standard_tests,
52
53
        "test__create_temp_file_with_commit_template_in_unicode_dir")
53
54
    return multiply_tests(to_adapt, encoding_scenarios, result)
54
55
 
58
59
    def make_uncommitted_tree(self):
59
60
        """Build a branch with uncommitted unicode named changes in the cwd."""
60
61
        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
 
 
125
124
    def test_commit_template_and_diff(self):
126
125
        """Test building a commit message template"""
127
126
        working_tree = self.make_uncommitted_tree()
163
162
        See <https://bugs.launchpad.net/bzr/+bug/220331>
164
163
        """
165
164
        self.overrideEnv('BRZ_EDITOR',
166
 
            '"%s"' % self.make_do_nothing_editor('name with spaces'))
167
 
        self.assertEqual(True, msgeditor._run_editor('a_filename'))    
 
165
                         '"%s"' % self.make_do_nothing_editor('name with spaces'))
 
166
        self.assertEqual(True, msgeditor._run_editor('a_filename'))
168
167
 
169
168
    def make_fake_editor(self, message='test message from fed\n'):
170
169
        """Set up environment so that an editor will be a known script.
201
200
            self.overrideEnv('BRZ_EDITOR', './fed.py')
202
201
 
203
202
    def test_edit_commit_message_without_infotext(self):
204
 
        working_tree = self.make_uncommitted_tree()
 
203
        self.make_uncommitted_tree()
205
204
        self.make_fake_editor()
206
205
 
207
206
        mutter('edit_commit_message without infotext')
209
208
                         msgeditor.edit_commit_message(''))
210
209
 
211
210
    def test_edit_commit_message_with_ascii_infotext(self):
212
 
        working_tree = self.make_uncommitted_tree()
 
211
        self.make_uncommitted_tree()
213
212
        self.make_fake_editor()
214
213
 
215
214
        mutter('edit_commit_message with ascii string infotext')
217
216
                         msgeditor.edit_commit_message('spam'))
218
217
 
219
218
    def test_edit_commit_message_with_unicode_infotext(self):
220
 
        working_tree = self.make_uncommitted_tree()
 
219
        self.make_uncommitted_tree()
221
220
        self.make_fake_editor()
222
221
 
223
222
        mutter('edit_commit_message with unicode infotext')
237
236
        self.make_uncommitted_tree()
238
237
        self.make_fake_editor()
239
238
        self.assertEqual('test message from fed\nstart message\n',
240
 
                         msgeditor.edit_commit_message('',
241
 
                                              start_message='start message\n'))
 
239
                         msgeditor.edit_commit_message(
 
240
                             '', start_message='start message\n'))
242
241
        self.assertEqual('test message from fed\n',
243
 
                         msgeditor.edit_commit_message('',
244
 
                                              start_message=''))
 
242
                         msgeditor.edit_commit_message(
 
243
                             '', start_message=''))
245
244
 
246
245
    def test_deleted_commit_message(self):
247
 
        working_tree = self.make_uncommitted_tree()
 
246
        self.make_uncommitted_tree()
248
247
 
249
248
        if sys.platform == 'win32':
250
249
            editor = 'cmd.exe /c del'
253
252
        self.overrideEnv('BRZ_EDITOR', editor)
254
253
 
255
254
        self.assertRaises((EnvironmentError, errors.NoSuchFile),
256
 
                msgeditor.edit_commit_message, '')
 
255
                          msgeditor.edit_commit_message, '')
257
256
 
258
257
    def test__get_editor(self):
259
258
        self.overrideEnv('BRZ_EDITOR', 'bzr_editor')
275
274
            self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'],
276
275
                             editors[4:])
277
276
 
278
 
 
279
277
    def test__run_editor_EACCES(self):
280
278
        """If running a configured editor raises EACESS, the user is warned."""
281
279
        self.overrideEnv('BRZ_EDITOR', 'eacces.py')
288
286
        self.overrideEnv('EDITOR', self.make_do_nothing_editor())
289
287
        # Call _run_editor, capturing mutter.warning calls.
290
288
        warnings = []
 
289
 
291
290
        def warning(*args):
292
291
            if len(args) > 1:
293
292
                warnings.append(args[0] % args[1:])
299
298
            msgeditor._run_editor('')
300
299
        finally:
301
300
            trace.warning = _warning
302
 
        self.assertStartsWith(warnings[0], 'Could not start editor "eacces.py"')
 
301
        self.assertStartsWith(
 
302
            warnings[0], 'Could not start editor "eacces.py"')
303
303
 
304
304
    def test__create_temp_file_with_commit_template(self):
305
305
        # check that commit template written properly
306
306
        # and has platform native line-endings (CRLF on win32)
307
307
        create_file = msgeditor._create_temp_file_with_commit_template
308
 
        msgfilename, hasinfo = create_file(b'infotext', '----', b'start message')
 
308
        msgfilename, hasinfo = create_file(
 
309
            b'infotext', '----', b'start message')
309
310
        self.assertNotEqual(None, msgfilename)
310
311
        self.assertTrue(hasinfo)
311
312
        expected = os.linesep.join(['start message',
343
344
        # in default user encoding
344
345
        char = probe_bad_non_ascii(osutils.get_user_encoding())
345
346
        if char is None:
346
 
            self.skipTest('Cannot find suitable non-ascii character '
 
347
            self.skipTest(
 
348
                'Cannot find suitable non-ascii character '
347
349
                'for user_encoding (%s)' % osutils.get_user_encoding())
348
350
 
349
351
        self.make_fake_editor(message=char)
350
352
 
351
 
        working_tree = self.make_uncommitted_tree()
 
353
        self.make_uncommitted_tree()
352
354
        self.assertRaises(msgeditor.BadCommitMessageEncoding,
353
355
                          msgeditor.edit_commit_message, '')
354
356
 
355
357
    def test_set_commit_message_no_hooks(self):
356
358
        commit_obj = commit.Commit()
357
359
        self.assertIs(None,
358
 
            msgeditor.set_commit_message(commit_obj))
 
360
                      msgeditor.set_commit_message(commit_obj))
359
361
 
360
362
    def test_set_commit_message_hook(self):
361
363
        msgeditor.hooks.install_named_hook("set_commit_message",
362
 
                lambda commit_obj, existing_message: "save me some typing\n", None)
 
364
                                           lambda commit_obj, existing_message: "save me some typing\n", None)
363
365
        commit_obj = commit.Commit()
364
366
        self.assertEqual("save me some typing\n",
365
 
            msgeditor.set_commit_message(commit_obj))
 
367
                         msgeditor.set_commit_message(commit_obj))
366
368
 
367
369
    def test_generate_commit_message_template_no_hooks(self):
368
370
        commit_obj = commit.Commit()
369
371
        self.assertIs(None,
370
 
            msgeditor.generate_commit_message_template(commit_obj))
 
372
                      msgeditor.generate_commit_message_template(commit_obj))
371
373
 
372
374
    def test_generate_commit_message_template_hook(self):
373
375
        msgeditor.hooks.install_named_hook("commit_message_template",
374
 
                lambda commit_obj, msg: "save me some typing\n", None)
 
376
                                           lambda commit_obj, msg: "save me some typing\n", None)
375
377
        commit_obj = commit.Commit()
376
378
        self.assertEqual("save me some typing\n",
377
 
            msgeditor.generate_commit_message_template(commit_obj))
 
379
                         msgeditor.generate_commit_message_template(commit_obj))
378
380
 
379
381
 
380
382
# GZ 2009-11-17: This wants moving to osutils when the errno checking code is
384
386
    def test_subprocess_call_bad_file(self):
385
387
        if sys.platform != "win32":
386
388
            raise TestNotApplicable("Workarounds for windows only")
387
 
        import subprocess, errno
 
389
        import subprocess
 
390
        import errno
388
391
        ERROR_BAD_EXE_FORMAT = 193
389
392
        open("textfile.txt", "w").close()
390
393
        e = self.assertRaises(WindowsError, subprocess.call, "textfile.txt")