/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/blackbox/test_commit.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-20 09:46:28 UTC
  • mfrom: (5609.33.4 2.3)
  • mto: (5609.33.5 2.3)
  • mto: This revision was merged to the branch mainline in revision 5811.
  • Revision ID: john@arbash-meinel.com-20110420094628-l0bafq1lwb6ib1v2
Merge lp:bzr/2.3 @ 5640 so we can update the release notes (aka NEWS)

Show diffs side-by-side

added added

removed removed

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