~bzr/ubuntu/lucid/bzr/beta-ppa

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_commit.py

  • Committer: Martin Pool
  • Date: 2010-08-18 04:26:39 UTC
  • mfrom: (129.1.8 packaging-karmic)
  • Revision ID: mbp@sourcefrog.net-20100818042639-mjoxtngyjwiu05fo
* PPA rebuild for lucid.
* PPA rebuild for karmic.
* PPA rebuild onto jaunty.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Tests for the commit CLI of bzr."""
19
19
 
20
20
import os
 
21
import re
21
22
import sys
22
23
 
23
24
from bzrlib import (
 
25
    bzrdir,
24
26
    osutils,
25
27
    ignores,
26
28
    msgeditor,
32
34
    probe_bad_non_ascii,
33
35
    TestSkipped,
34
36
    )
35
 
from bzrlib.tests.blackbox import ExternalBase
36
 
 
37
 
 
38
 
class TestCommit(ExternalBase):
 
37
from bzrlib.tests import TestCaseWithTransport
 
38
 
 
39
 
 
40
class TestCommit(TestCaseWithTransport):
39
41
 
40
42
    def test_05_empty_commit(self):
41
43
        """Commit of tree with no versioned files should fail"""
107
109
                              'modified hello\.txt\n'
108
110
                              'Committed revision 2\.\n$')
109
111
 
 
112
    def test_unicode_commit_message_is_filename(self):
 
113
        """Unicode commit message same as a filename (Bug #563646).
 
114
        """
 
115
        file_name = u'\N{euro sign}'
 
116
        self.run_bzr(['init'])
 
117
        open(file_name, 'w').write('hello world')
 
118
        self.run_bzr(['add'])
 
119
        out, err = self.run_bzr(['commit', '-m', file_name])
 
120
        reflags = re.MULTILINE|re.DOTALL|re.UNICODE
 
121
        te = osutils.get_terminal_encoding()
 
122
        self.assertContainsRe(err.decode(te),
 
123
            u'The commit message is a file name:',
 
124
            flags=reflags)
 
125
 
 
126
        # Run same test with a filename that causes encode
 
127
        # error for the terminal encoding. We do this
 
128
        # by forcing terminal encoding of ascii for
 
129
        # osutils.get_terminal_encoding which is used
 
130
        # by ui.text.show_warning
 
131
        default_get_terminal_enc = osutils.get_terminal_encoding
 
132
        try:
 
133
            osutils.get_terminal_encoding = lambda trace=None: 'ascii'
 
134
            file_name = u'foo\u1234'
 
135
            open(file_name, 'w').write('hello world')
 
136
            self.run_bzr(['add'])
 
137
            out, err = self.run_bzr(['commit', '-m', file_name])
 
138
            reflags = re.MULTILINE|re.DOTALL|re.UNICODE
 
139
            te = osutils.get_terminal_encoding()
 
140
            self.assertContainsRe(err.decode(te, 'replace'),
 
141
                u'The commit message is a file name:',
 
142
                flags=reflags)
 
143
        finally:
 
144
            osutils.get_terminal_encoding = default_get_terminal_enc
 
145
 
110
146
    def test_warn_about_forgotten_commit_message(self):
111
147
        """Test that the lack of -m parameter is caught"""
112
148
        wt = self.make_branch_and_tree('.')
343
379
        trunk = self.make_branch_and_tree('trunk')
344
380
 
345
381
        u1 = trunk.branch.create_checkout('u1')
346
 
        self.build_tree_contents([('u1/hosts', 'initial contents')])
 
382
        self.build_tree_contents([('u1/hosts', 'initial contents\n')])
347
383
        u1.add('hosts')
348
384
        self.run_bzr('commit -m add-hosts u1')
349
385
 
350
386
        u2 = trunk.branch.create_checkout('u2')
351
 
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
 
387
        self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
352
388
        self.run_bzr('commit -m checkin-from-u2 u2')
353
389
 
354
390
        # make an offline commits
355
 
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
 
391
        self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
356
392
        self.run_bzr('commit -m checkin-offline --local u1')
357
393
 
358
394
        # now try to pull in online work from u2, and then commit our offline
359
395
        # work as a merge
360
396
        # retcode 1 as we expect a text conflict
361
397
        self.run_bzr('update u1', retcode=1)
 
398
        self.assertFileEqual('''\
 
399
<<<<<<< TREE
 
400
first offline change in u1
 
401
=======
 
402
altered in u2
 
403
>>>>>>> MERGE-SOURCE
 
404
''',
 
405
                             'u1/hosts')
 
406
 
362
407
        self.run_bzr('resolved u1/hosts')
363
408
        # add a text change here to represent resolving the merge conflicts in
364
409
        # favour of a new version of the file not identical to either the u1
654
699
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
655
700
 
656
701
    def test_commit_readonly_checkout(self):
657
 
        # https://bugs.edge.launchpad.net/bzr/+bug/129701
 
702
        # https://bugs.launchpad.net/bzr/+bug/129701
658
703
        # "UnlockableTransport error trying to commit in checkout of readonly
659
704
        # branch"
660
705
        self.make_branch('master')
666
711
        self.assertContainsRe(err,
667
712
            r'^bzr: ERROR: Cannot lock.*readonly transport')
668
713
 
669
 
    def test_commit_hook_template(self):
 
714
    def setup_editor(self):
670
715
        # Test that commit template hooks work
671
 
        def restoreDefaults():
672
 
            msgeditor.hooks['commit_message_template'] = []
673
 
            osutils.set_or_unset_env('BZR_EDITOR', default_editor)
674
716
        if sys.platform == "win32":
675
717
            f = file('fed.bat', 'w')
676
718
            f.write('@rem dummy fed')
677
719
            f.close()
678
 
            default_editor = osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
 
720
            osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
679
721
        else:
680
722
            f = file('fed.sh', 'wb')
681
723
            f.write('#!/bin/sh\n')
682
724
            f.close()
683
725
            os.chmod('fed.sh', 0755)
684
 
            default_editor = osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
685
 
        self.addCleanup(restoreDefaults)
 
726
            osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
 
727
 
 
728
    def setup_commit_with_template(self):
 
729
        self.setup_editor()
686
730
        msgeditor.hooks.install_named_hook("commit_message_template",
687
731
                lambda commit_obj, msg: "save me some typing\n", None)
688
732
        tree = self.make_branch_and_tree('tree')
689
733
        self.build_tree(['tree/hello.txt'])
690
734
        tree.add('hello.txt')
691
 
        out, err = self.run_bzr("commit tree/hello.txt")
 
735
        return tree
 
736
 
 
737
    def test_commit_hook_template_accepted(self):
 
738
        tree = self.setup_commit_with_template()
 
739
        out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
692
740
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
693
741
        self.assertEqual('save me some typing\n', last_rev.message)
 
742
 
 
743
    def test_commit_hook_template_rejected(self):
 
744
        tree = self.setup_commit_with_template()
 
745
        expected = tree.last_revision()
 
746
        out, err = self.run_bzr_error(["empty commit message"],
 
747
            "commit tree/hello.txt", stdin="n\n")
 
748
        self.assertEqual(expected, tree.last_revision())
 
749
 
 
750
    def test_commit_without_username(self):
 
751
        """Ensure commit error if username is not set.
 
752
        """
 
753
        self.run_bzr(['init', 'foo'])
 
754
        os.chdir('foo')
 
755
        open('foo.txt', 'w').write('hello')
 
756
        self.run_bzr(['add'])
 
757
        osutils.set_or_unset_env('EMAIL', None)
 
758
        osutils.set_or_unset_env('BZR_EMAIL', None)
 
759
        out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
 
760
        self.assertContainsRe(err, 'Unable to determine your name')
 
761
 
 
762
    def test_commit_recursive_checkout(self):
 
763
        """Ensure that a commit to a recursive checkout fails cleanly.
 
764
        """
 
765
        self.run_bzr(['init', 'test_branch'])
 
766
        self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
 
767
        os.chdir('test_checkout')
 
768
        self.run_bzr(['bind', '.']) # bind to self
 
769
        open('foo.txt', 'w').write('hello')
 
770
        self.run_bzr(['add'])
 
771
        out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
 
772
        self.assertEqual(out, '')
 
773
        self.assertContainsRe(err,
 
774
            'Branch.*test_checkout.*appears to be bound to itself')
 
775