~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2010-02-25 06:17:27 UTC
  • mfrom: (5055 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5057.
  • Revision ID: mbp@sourcefrog.net-20100225061727-4sd9lt0qmdc6087t
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
21
20
import os
22
 
import re
23
21
import sys
24
22
 
25
 
from testtools.matchers import DocTestMatches
26
 
 
27
23
from bzrlib import (
28
 
    config,
29
24
    osutils,
30
25
    ignores,
31
26
    msgeditor,
 
27
    osutils,
32
28
    tests,
33
29
    )
34
30
from bzrlib.bzrdir import BzrDir
35
31
from bzrlib.tests import (
36
32
    probe_bad_non_ascii,
37
 
    test_foreign,
38
33
    TestSkipped,
39
 
    UnicodeFilenameFeature,
40
34
    )
41
 
from bzrlib.tests import TestCaseWithTransport
42
 
 
43
 
 
44
 
class TestCommit(TestCaseWithTransport):
 
35
from bzrlib.tests.blackbox import ExternalBase
 
36
 
 
37
 
 
38
class TestCommit(ExternalBase):
45
39
 
46
40
    def test_05_empty_commit(self):
47
41
        """Commit of tree with no versioned files should fail"""
50
44
        self.build_tree(['hello.txt'])
51
45
        out,err = self.run_bzr('commit -m empty', retcode=3)
52
46
        self.assertEqual('', out)
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
 
aborting commit write group: PointlessCommit(No changes to commit)
61
 
bzr: ERROR: No changes to commit.\
62
 
 Please 'bzr add' the files you want to commit,\
63
 
 or use --unchanged to force an empty commit.
64
 
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
 
47
        self.assertContainsRe(err, 'bzr: ERROR: No changes to commit\.'
 
48
                                  ' Use --unchanged to commit anyhow.\n')
65
49
 
66
50
    def test_commit_success(self):
67
51
        """Successful commit should not leave behind a bzr-commit-* file"""
73
57
        self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
74
58
        self.assertEqual('', self.run_bzr('unknowns')[0])
75
59
 
76
 
    def test_commit_lossy_native(self):
77
 
        """A --lossy option to commit is supported."""
78
 
        self.make_branch_and_tree('.')
79
 
        self.run_bzr('commit --lossy --unchanged -m message')
80
 
        self.assertEqual('', self.run_bzr('unknowns')[0])
81
 
 
82
 
    def test_commit_lossy_foreign(self):
83
 
        test_foreign.register_dummy_foreign_for_test(self)
84
 
        self.make_branch_and_tree('.',
85
 
            format=test_foreign.DummyForeignVcsDirFormat())
86
 
        self.run_bzr('commit --lossy --unchanged -m message')
87
 
        output = self.run_bzr('revision-info')[0]
88
 
        self.assertTrue(output.startswith('1 dummy-'))
89
 
 
90
60
    def test_commit_with_path(self):
91
61
        """Commit tree with path of root specified"""
92
62
        a_tree = self.make_branch_and_tree('a')
106
76
        self.run_bzr('resolved b/a_file')
107
77
        self.run_bzr(['commit', '-m', 'merge into b', 'b'])
108
78
 
 
79
 
109
80
    def test_10_verbose_commit(self):
110
81
        """Add one file and examine verbose commit output"""
111
82
        tree = self.make_branch_and_tree('.')
136
107
                              'modified hello\.txt\n'
137
108
                              'Committed revision 2\.\n$')
138
109
 
139
 
    def test_unicode_commit_message_is_filename(self):
140
 
        """Unicode commit message same as a filename (Bug #563646).
141
 
        """
142
 
        self.requireFeature(UnicodeFilenameFeature)
143
 
        file_name = u'\N{euro sign}'
144
 
        self.run_bzr(['init'])
145
 
        open(file_name, 'w').write('hello world')
146
 
        self.run_bzr(['add'])
147
 
        out, err = self.run_bzr(['commit', '-m', file_name])
148
 
        reflags = re.MULTILINE|re.DOTALL|re.UNICODE
149
 
        te = osutils.get_terminal_encoding()
150
 
        self.assertContainsRe(err.decode(te),
151
 
            u'The commit message is a file name:',
152
 
            flags=reflags)
153
 
 
154
 
        # Run same test with a filename that causes encode
155
 
        # error for the terminal encoding. We do this
156
 
        # by forcing terminal encoding of ascii for
157
 
        # osutils.get_terminal_encoding which is used
158
 
        # by ui.text.show_warning
159
 
        default_get_terminal_enc = osutils.get_terminal_encoding
160
 
        try:
161
 
            osutils.get_terminal_encoding = lambda trace=None: 'ascii'
162
 
            file_name = u'foo\u1234'
163
 
            open(file_name, 'w').write('hello world')
164
 
            self.run_bzr(['add'])
165
 
            out, err = self.run_bzr(['commit', '-m', file_name])
166
 
            reflags = re.MULTILINE|re.DOTALL|re.UNICODE
167
 
            te = osutils.get_terminal_encoding()
168
 
            self.assertContainsRe(err.decode(te, 'replace'),
169
 
                u'The commit message is a file name:',
170
 
                flags=reflags)
171
 
        finally:
172
 
            osutils.get_terminal_encoding = default_get_terminal_enc
173
 
 
174
110
    def test_warn_about_forgotten_commit_message(self):
175
111
        """Test that the lack of -m parameter is caught"""
176
112
        wt = self.make_branch_and_tree('.')
335
271
        tree.add('foo.c')
336
272
        self.run_bzr('commit -m ""', retcode=3)
337
273
 
 
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
 
338
294
    def test_other_branch_commit(self):
339
295
        # this branch is to ensure consistent behaviour, whether we're run
340
296
        # inside a branch, or not.
707
663
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
708
664
 
709
665
    def test_commit_readonly_checkout(self):
710
 
        # https://bugs.launchpad.net/bzr/+bug/129701
 
666
        # https://bugs.edge.launchpad.net/bzr/+bug/129701
711
667
        # "UnlockableTransport error trying to commit in checkout of readonly
712
668
        # branch"
713
669
        self.make_branch('master')
719
675
        self.assertContainsRe(err,
720
676
            r'^bzr: ERROR: Cannot lock.*readonly transport')
721
677
 
722
 
    def setup_editor(self):
 
678
    def test_commit_hook_template(self):
723
679
        # Test that commit template hooks work
724
680
        if sys.platform == "win32":
725
681
            f = file('fed.bat', 'w')
726
682
            f.write('@rem dummy fed')
727
683
            f.close()
728
 
            self.overrideEnv('BZR_EDITOR', "fed.bat")
 
684
            osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
729
685
        else:
730
686
            f = file('fed.sh', 'wb')
731
687
            f.write('#!/bin/sh\n')
732
688
            f.close()
733
689
            os.chmod('fed.sh', 0755)
734
 
            self.overrideEnv('BZR_EDITOR', "./fed.sh")
735
 
 
736
 
    def setup_commit_with_template(self):
737
 
        self.setup_editor()
 
690
            osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
738
691
        msgeditor.hooks.install_named_hook("commit_message_template",
739
692
                lambda commit_obj, msg: "save me some typing\n", None)
740
693
        tree = self.make_branch_and_tree('tree')
741
694
        self.build_tree(['tree/hello.txt'])
742
695
        tree.add('hello.txt')
743
 
        return tree
744
 
 
745
 
    def test_commit_hook_template_accepted(self):
746
 
        tree = self.setup_commit_with_template()
747
 
        out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
 
696
        out, err = self.run_bzr("commit tree/hello.txt")
748
697
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
749
698
        self.assertEqual('save me some typing\n', last_rev.message)
750
 
 
751
 
    def test_commit_hook_template_rejected(self):
752
 
        tree = self.setup_commit_with_template()
753
 
        expected = tree.last_revision()
754
 
        out, err = self.run_bzr_error(["empty commit message"],
755
 
            "commit tree/hello.txt", stdin="n\n")
756
 
        self.assertEqual(expected, tree.last_revision())
757
 
 
758
 
    def test_commit_without_username(self):
759
 
        """Ensure commit error if username is not set.
760
 
        """
761
 
        self.run_bzr(['init', 'foo'])
762
 
        os.chdir('foo')
763
 
        open('foo.txt', 'w').write('hello')
764
 
        self.run_bzr(['add'])
765
 
        self.overrideEnv('EMAIL', None)
766
 
        self.overrideEnv('BZR_EMAIL', None)
767
 
        # Also, make sure that it's not inferred from mailname.
768
 
        self.overrideAttr(config, '_auto_user_id',
769
 
            lambda: (None, None))
770
 
        out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
771
 
        self.assertContainsRe(err, 'Unable to determine your name')
772
 
 
773
 
    def test_commit_recursive_checkout(self):
774
 
        """Ensure that a commit to a recursive checkout fails cleanly.
775
 
        """
776
 
        self.run_bzr(['init', 'test_branch'])
777
 
        self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
778
 
        os.chdir('test_checkout')
779
 
        self.run_bzr(['bind', '.']) # bind to self
780
 
        open('foo.txt', 'w').write('hello')
781
 
        self.run_bzr(['add'])
782
 
        out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
783
 
        self.assertEqual(out, '')
784
 
        self.assertContainsRe(err,
785
 
            'Branch.*test_checkout.*appears to be bound to itself')
786