~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-17 18:13:57 UTC
  • mfrom: (5268.7.29 transport-segments)
  • Revision ID: pqm@pqm.ubuntu.com-20110817181357-y5q5eth1hk8bl3om
(jelmer) Allow specifying the colocated branch to use in the branch URL,
 and retrieving the branch name using ControlDir._get_selected_branch.
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2009 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
17
17
 
18
18
"""Tests for the commit CLI of bzr."""
19
19
 
 
20
import doctest
20
21
import os
 
22
import re
21
23
import sys
22
24
 
 
25
from testtools.matchers import DocTestMatches
 
26
 
23
27
from bzrlib import (
 
28
    config,
24
29
    osutils,
25
30
    ignores,
26
31
    msgeditor,
27
 
    osutils,
28
32
    tests,
29
33
    )
30
34
from bzrlib.bzrdir import BzrDir
31
35
from bzrlib.tests import (
32
36
    probe_bad_non_ascii,
 
37
    test_foreign,
33
38
    TestSkipped,
 
39
    features,
34
40
    )
35
 
from bzrlib.tests.blackbox import ExternalBase
36
 
 
37
 
 
38
 
class TestCommit(ExternalBase):
 
41
from bzrlib.tests import TestCaseWithTransport
 
42
 
 
43
 
 
44
class TestCommit(TestCaseWithTransport):
39
45
 
40
46
    def test_05_empty_commit(self):
41
47
        """Commit of tree with no versioned files should fail"""
44
50
        self.build_tree(['hello.txt'])
45
51
        out,err = self.run_bzr('commit -m empty', retcode=3)
46
52
        self.assertEqual('', out)
47
 
        self.assertContainsRe(err, 'bzr: ERROR: No changes to commit\.'
48
 
                                  ' Use --unchanged to commit anyhow.\n')
 
53
        # Two ugly bits here.
 
54
        # 1) We really don't want 'aborting commit write group' anymore.
 
55
        # 2) bzr: ERROR: is a really long line, so we wrap it with '\'
 
56
        self.assertThat(
 
57
            err,
 
58
            DocTestMatches("""\
 
59
Committing to: ...
 
60
bzr: ERROR: No changes to commit.\
 
61
 Please 'bzr add' the files you want to commit,\
 
62
 or use --unchanged to force an empty commit.
 
63
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
49
64
 
50
65
    def test_commit_success(self):
51
66
        """Successful commit should not leave behind a bzr-commit-* file"""
57
72
        self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
58
73
        self.assertEqual('', self.run_bzr('unknowns')[0])
59
74
 
 
75
    def test_commit_lossy_native(self):
 
76
        """A --lossy option to commit is supported."""
 
77
        self.make_branch_and_tree('.')
 
78
        self.run_bzr('commit --lossy --unchanged -m message')
 
79
        self.assertEqual('', self.run_bzr('unknowns')[0])
 
80
 
 
81
    def test_commit_lossy_foreign(self):
 
82
        test_foreign.register_dummy_foreign_for_test(self)
 
83
        self.make_branch_and_tree('.',
 
84
            format=test_foreign.DummyForeignVcsDirFormat())
 
85
        self.run_bzr('commit --lossy --unchanged -m message')
 
86
        output = self.run_bzr('revision-info')[0]
 
87
        self.assertTrue(output.startswith('1 dummy-'))
 
88
 
60
89
    def test_commit_with_path(self):
61
90
        """Commit tree with path of root specified"""
62
91
        a_tree = self.make_branch_and_tree('a')
76
105
        self.run_bzr('resolved b/a_file')
77
106
        self.run_bzr(['commit', '-m', 'merge into b', 'b'])
78
107
 
79
 
 
80
108
    def test_10_verbose_commit(self):
81
109
        """Add one file and examine verbose commit output"""
82
110
        tree = self.make_branch_and_tree('.')
107
135
                              'modified hello\.txt\n'
108
136
                              'Committed revision 2\.\n$')
109
137
 
 
138
    def test_unicode_commit_message_is_filename(self):
 
139
        """Unicode commit message same as a filename (Bug #563646).
 
140
        """
 
141
        self.requireFeature(features.UnicodeFilenameFeature)
 
142
        file_name = u'\N{euro sign}'
 
143
        self.run_bzr(['init'])
 
144
        open(file_name, 'w').write('hello world')
 
145
        self.run_bzr(['add'])
 
146
        out, err = self.run_bzr(['commit', '-m', file_name])
 
147
        reflags = re.MULTILINE|re.DOTALL|re.UNICODE
 
148
        te = osutils.get_terminal_encoding()
 
149
        self.assertContainsRe(err.decode(te),
 
150
            u'The commit message is a file name:',
 
151
            flags=reflags)
 
152
 
 
153
        # Run same test with a filename that causes encode
 
154
        # error for the terminal encoding. We do this
 
155
        # by forcing terminal encoding of ascii for
 
156
        # osutils.get_terminal_encoding which is used
 
157
        # by ui.text.show_warning
 
158
        default_get_terminal_enc = osutils.get_terminal_encoding
 
159
        try:
 
160
            osutils.get_terminal_encoding = lambda trace=None: 'ascii'
 
161
            file_name = u'foo\u1234'
 
162
            open(file_name, 'w').write('hello world')
 
163
            self.run_bzr(['add'])
 
164
            out, err = self.run_bzr(['commit', '-m', file_name])
 
165
            reflags = re.MULTILINE|re.DOTALL|re.UNICODE
 
166
            te = osutils.get_terminal_encoding()
 
167
            self.assertContainsRe(err.decode(te, 'replace'),
 
168
                u'The commit message is a file name:',
 
169
                flags=reflags)
 
170
        finally:
 
171
            osutils.get_terminal_encoding = default_get_terminal_enc
 
172
 
110
173
    def test_warn_about_forgotten_commit_message(self):
111
174
        """Test that the lack of -m parameter is caught"""
112
175
        wt = self.make_branch_and_tree('.')
271
334
        tree.add('foo.c')
272
335
        self.run_bzr('commit -m ""', retcode=3)
273
336
 
274
 
    def test_unsupported_encoding_commit_message(self):
275
 
        if sys.platform == 'win32':
276
 
            raise tests.TestNotApplicable('Win32 parses arguments directly'
277
 
                ' as Unicode, so we can\'t pass invalid non-ascii')
278
 
        tree = self.make_branch_and_tree('.')
279
 
        self.build_tree_contents([('foo.c', 'int main() {}')])
280
 
        tree.add('foo.c')
281
 
        # LANG env variable has no effect on Windows
282
 
        # but some characters anyway cannot be represented
283
 
        # in default user encoding
284
 
        char = probe_bad_non_ascii(osutils.get_user_encoding())
285
 
        if char is None:
286
 
            raise TestSkipped('Cannot find suitable non-ascii character'
287
 
                'for user_encoding (%s)' % osutils.get_user_encoding())
288
 
        out,err = self.run_bzr_subprocess('commit -m "%s"' % char,
289
 
                                          retcode=1,
290
 
                                          env_changes={'LANG': 'C'})
291
 
        self.assertContainsRe(err, r'bzrlib.errors.BzrError: Parameter.*is '
292
 
                                    'unsupported by the current encoding.')
293
 
 
294
337
    def test_other_branch_commit(self):
295
338
        # this branch is to ensure consistent behaviour, whether we're run
296
339
        # inside a branch, or not.
343
386
        trunk = self.make_branch_and_tree('trunk')
344
387
 
345
388
        u1 = trunk.branch.create_checkout('u1')
346
 
        self.build_tree_contents([('u1/hosts', 'initial contents')])
 
389
        self.build_tree_contents([('u1/hosts', 'initial contents\n')])
347
390
        u1.add('hosts')
348
391
        self.run_bzr('commit -m add-hosts u1')
349
392
 
350
393
        u2 = trunk.branch.create_checkout('u2')
351
 
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
 
394
        self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
352
395
        self.run_bzr('commit -m checkin-from-u2 u2')
353
396
 
354
397
        # make an offline commits
355
 
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
 
398
        self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
356
399
        self.run_bzr('commit -m checkin-offline --local u1')
357
400
 
358
401
        # now try to pull in online work from u2, and then commit our offline
359
402
        # work as a merge
360
403
        # retcode 1 as we expect a text conflict
361
404
        self.run_bzr('update u1', retcode=1)
 
405
        self.assertFileEqual('''\
 
406
<<<<<<< TREE
 
407
first offline change in u1
 
408
=======
 
409
altered in u2
 
410
>>>>>>> MERGE-SOURCE
 
411
''',
 
412
                             'u1/hosts')
 
413
 
362
414
        self.run_bzr('resolved u1/hosts')
363
415
        # add a text change here to represent resolving the merge conflicts in
364
416
        # favour of a new version of the file not identical to either the u1
654
706
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
655
707
 
656
708
    def test_commit_readonly_checkout(self):
657
 
        # https://bugs.edge.launchpad.net/bzr/+bug/129701
 
709
        # https://bugs.launchpad.net/bzr/+bug/129701
658
710
        # "UnlockableTransport error trying to commit in checkout of readonly
659
711
        # branch"
660
712
        self.make_branch('master')
666
718
        self.assertContainsRe(err,
667
719
            r'^bzr: ERROR: Cannot lock.*readonly transport')
668
720
 
669
 
    def test_commit_hook_template(self):
 
721
    def setup_editor(self):
670
722
        # Test that commit template hooks work
671
723
        if sys.platform == "win32":
672
724
            f = file('fed.bat', 'w')
673
725
            f.write('@rem dummy fed')
674
726
            f.close()
675
 
            osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
 
727
            self.overrideEnv('BZR_EDITOR', "fed.bat")
676
728
        else:
677
729
            f = file('fed.sh', 'wb')
678
730
            f.write('#!/bin/sh\n')
679
731
            f.close()
680
732
            os.chmod('fed.sh', 0755)
681
 
            osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
 
733
            self.overrideEnv('BZR_EDITOR', "./fed.sh")
 
734
 
 
735
    def setup_commit_with_template(self):
 
736
        self.setup_editor()
682
737
        msgeditor.hooks.install_named_hook("commit_message_template",
683
738
                lambda commit_obj, msg: "save me some typing\n", None)
684
739
        tree = self.make_branch_and_tree('tree')
685
740
        self.build_tree(['tree/hello.txt'])
686
741
        tree.add('hello.txt')
 
742
        return tree
 
743
 
 
744
    def test_commit_hook_template_accepted(self):
 
745
        tree = self.setup_commit_with_template()
 
746
        out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
 
747
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
 
748
        self.assertEqual('save me some typing\n', last_rev.message)
 
749
 
 
750
    def test_commit_hook_template_rejected(self):
 
751
        tree = self.setup_commit_with_template()
 
752
        expected = tree.last_revision()
 
753
        out, err = self.run_bzr_error(["Empty commit message specified."
 
754
                  " Please specify a commit message with either"
 
755
                  " --message or --file or leave a blank message"
 
756
                  " with --message \"\"."],
 
757
            "commit tree/hello.txt", stdin="n\n")
 
758
        self.assertEqual(expected, tree.last_revision())
 
759
 
 
760
    def test_set_commit_message(self):
 
761
        msgeditor.hooks.install_named_hook("set_commit_message",
 
762
                lambda commit_obj, msg: "save me some typing\n", None)
 
763
        tree = self.make_branch_and_tree('tree')
 
764
        self.build_tree(['tree/hello.txt'])
 
765
        tree.add('hello.txt')
687
766
        out, err = self.run_bzr("commit tree/hello.txt")
688
767
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
689
768
        self.assertEqual('save me some typing\n', last_rev.message)
 
769
 
 
770
    def test_commit_without_username(self):
 
771
        """Ensure commit error if username is not set.
 
772
        """
 
773
        self.run_bzr(['init', 'foo'])
 
774
        os.chdir('foo')
 
775
        open('foo.txt', 'w').write('hello')
 
776
        self.run_bzr(['add'])
 
777
        self.overrideEnv('EMAIL', None)
 
778
        self.overrideEnv('BZR_EMAIL', None)
 
779
        # Also, make sure that it's not inferred from mailname.
 
780
        self.overrideAttr(config, '_auto_user_id',
 
781
            lambda: (None, None))
 
782
        out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
 
783
        self.assertContainsRe(err, 'Unable to determine your name')
 
784
 
 
785
    def test_commit_recursive_checkout(self):
 
786
        """Ensure that a commit to a recursive checkout fails cleanly.
 
787
        """
 
788
        self.run_bzr(['init', 'test_branch'])
 
789
        self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
 
790
        os.chdir('test_checkout')
 
791
        self.run_bzr(['bind', '.']) # bind to self
 
792
        open('foo.txt', 'w').write('hello')
 
793
        self.run_bzr(['add'])
 
794
        out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
 
795
        self.assertEqual(out, '')
 
796
        self.assertContainsRe(err,
 
797
            'Branch.*test_checkout.*appears to be bound to itself')