~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2009-06-16 13:57:14 UTC
  • mto: This revision was merged to the branch mainline in revision 4449.
  • Revision ID: john@arbash-meinel.com-20090616135714-8o7jdtqqsfuv914z
The new code removes a get_parent_map call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
22
21
import sys
23
22
 
24
23
from bzrlib import (
25
 
    bzrdir,
26
24
    osutils,
27
25
    ignores,
28
26
    msgeditor,
29
27
    osutils,
30
 
    tests,
31
28
    )
32
29
from bzrlib.bzrdir import BzrDir
33
30
from bzrlib.tests import (
34
31
    probe_bad_non_ascii,
35
32
    TestSkipped,
36
 
    UnicodeFilenameFeature,
37
33
    )
38
 
from bzrlib.tests import TestCaseWithTransport
39
 
 
40
 
 
41
 
class TestCommit(TestCaseWithTransport):
 
34
from bzrlib.tests.blackbox import ExternalBase
 
35
 
 
36
 
 
37
class TestCommit(ExternalBase):
42
38
 
43
39
    def test_05_empty_commit(self):
44
40
        """Commit of tree with no versioned files should fail"""
110
106
                              'modified hello\.txt\n'
111
107
                              'Committed revision 2\.\n$')
112
108
 
113
 
    def test_unicode_commit_message_is_filename(self):
114
 
        """Unicode commit message same as a filename (Bug #563646).
115
 
        """
116
 
        self.requireFeature(UnicodeFilenameFeature)
117
 
        file_name = u'\N{euro sign}'
118
 
        self.run_bzr(['init'])
119
 
        open(file_name, 'w').write('hello world')
120
 
        self.run_bzr(['add'])
121
 
        out, err = self.run_bzr(['commit', '-m', file_name])
122
 
        reflags = re.MULTILINE|re.DOTALL|re.UNICODE
123
 
        te = osutils.get_terminal_encoding()
124
 
        self.assertContainsRe(err.decode(te),
125
 
            u'The commit message is a file name:',
126
 
            flags=reflags)
127
 
 
128
 
        # Run same test with a filename that causes encode
129
 
        # error for the terminal encoding. We do this
130
 
        # by forcing terminal encoding of ascii for
131
 
        # osutils.get_terminal_encoding which is used
132
 
        # by ui.text.show_warning
133
 
        default_get_terminal_enc = osutils.get_terminal_encoding
134
 
        try:
135
 
            osutils.get_terminal_encoding = lambda trace=None: 'ascii'
136
 
            file_name = u'foo\u1234'
137
 
            open(file_name, 'w').write('hello world')
138
 
            self.run_bzr(['add'])
139
 
            out, err = self.run_bzr(['commit', '-m', file_name])
140
 
            reflags = re.MULTILINE|re.DOTALL|re.UNICODE
141
 
            te = osutils.get_terminal_encoding()
142
 
            self.assertContainsRe(err.decode(te, 'replace'),
143
 
                u'The commit message is a file name:',
144
 
                flags=reflags)
145
 
        finally:
146
 
            osutils.get_terminal_encoding = default_get_terminal_enc
147
 
 
148
 
    def test_warn_about_forgotten_commit_message(self):
149
 
        """Test that the lack of -m parameter is caught"""
150
 
        wt = self.make_branch_and_tree('.')
151
 
        self.build_tree(['one', 'two'])
152
 
        wt.add(['two'])
153
 
        out, err = self.run_bzr('commit -m one two')
154
 
        self.assertContainsRe(err, "The commit message is a file name")
155
 
 
156
109
    def test_verbose_commit_renamed(self):
157
110
        # Verbose commit of renamed file should say so
158
111
        wt = self.prepare_simple_history()
217
170
        self.assertEqual(err, 'Committing to: %s\n'
218
171
                         'Committed revision 2.\n' % expected)
219
172
 
220
 
    def test_commit_sanitizes_CR_in_message(self):
221
 
        # See bug #433779, basically Emacs likes to pass '\r\n' style line
222
 
        # endings to 'bzr commit -m ""' which breaks because we don't allow
223
 
        # '\r' in commit messages. (Mostly because of issues where XML style
224
 
        # formats arbitrarily strip it out of the data while parsing.)
225
 
        # To make life easier for users, we just always translate '\r\n' =>
226
 
        # '\n'. And '\r' => '\n'.
227
 
        a_tree = self.make_branch_and_tree('a')
228
 
        self.build_tree(['a/b'])
229
 
        a_tree.add('b')
230
 
        self.run_bzr(['commit',
231
 
                      '-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
232
 
                     working_dir='a')
233
 
        rev_id = a_tree.branch.last_revision()
234
 
        rev = a_tree.branch.repository.get_revision(rev_id)
235
 
        self.assertEqualDiff('a string\n\nwith mixed\n\nendings\n',
236
 
                             rev.message)
237
 
 
238
173
    def test_commit_merge_reports_all_modified_files(self):
239
174
        # the commit command should show all the files that are shown by
240
175
        # bzr diff or bzr status when committing, even when they were not
310
245
        self.run_bzr('commit -m ""', retcode=3)
311
246
 
312
247
    def test_unsupported_encoding_commit_message(self):
313
 
        if sys.platform == 'win32':
314
 
            raise tests.TestNotApplicable('Win32 parses arguments directly'
315
 
                ' as Unicode, so we can\'t pass invalid non-ascii')
316
248
        tree = self.make_branch_and_tree('.')
317
249
        self.build_tree_contents([('foo.c', 'int main() {}')])
318
250
        tree.add('foo.c')
337
269
        self.build_tree_contents([
338
270
            ('branch/foo.c', 'int main() {}'),
339
271
            ('branch/bar.c', 'int main() {}')])
340
 
        inner_tree.add(['foo.c', 'bar.c'])
 
272
        inner_tree.add('foo.c')
 
273
        inner_tree.add('bar.c')
341
274
        # can't commit files in different trees; sane error
342
275
        self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
343
 
        # can commit to branch - records foo.c only
344
276
        self.run_bzr('commit -m newstuff branch/foo.c')
345
 
        # can commit to branch - records bar.c
346
277
        self.run_bzr('commit -m newstuff branch')
347
 
        # No changes left
348
 
        self.run_bzr_error(["No changes to commit"], 'commit -m newstuff branch')
 
278
        self.run_bzr('commit -m newstuff branch', retcode=3)
349
279
 
350
280
    def test_out_of_date_tree_commit(self):
351
281
        # check we get an error code and a clear message committing with an out
381
311
        trunk = self.make_branch_and_tree('trunk')
382
312
 
383
313
        u1 = trunk.branch.create_checkout('u1')
384
 
        self.build_tree_contents([('u1/hosts', 'initial contents\n')])
 
314
        self.build_tree_contents([('u1/hosts', 'initial contents')])
385
315
        u1.add('hosts')
386
316
        self.run_bzr('commit -m add-hosts u1')
387
317
 
388
318
        u2 = trunk.branch.create_checkout('u2')
389
 
        self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
 
319
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
390
320
        self.run_bzr('commit -m checkin-from-u2 u2')
391
321
 
392
322
        # make an offline commits
393
 
        self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
 
323
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
394
324
        self.run_bzr('commit -m checkin-offline --local u1')
395
325
 
396
326
        # now try to pull in online work from u2, and then commit our offline
397
327
        # work as a merge
398
328
        # retcode 1 as we expect a text conflict
399
329
        self.run_bzr('update u1', retcode=1)
400
 
        self.assertFileEqual('''\
401
 
<<<<<<< TREE
402
 
first offline change in u1
403
 
=======
404
 
altered in u2
405
 
>>>>>>> MERGE-SOURCE
406
 
''',
407
 
                             'u1/hosts')
408
 
 
409
330
        self.run_bzr('resolved u1/hosts')
410
331
        # add a text change here to represent resolving the merge conflicts in
411
332
        # favour of a new version of the file not identical to either the u1
663
584
        properties = last_rev.properties
664
585
        self.assertEqual('John Doe\nJane Rey', properties['authors'])
665
586
 
666
 
    def test_commit_time(self):
667
 
        tree = self.make_branch_and_tree('tree')
668
 
        self.build_tree(['tree/hello.txt'])
669
 
        tree.add('hello.txt')
670
 
        out, err = self.run_bzr("commit -m hello "
671
 
            "--commit-time='2009-10-10 08:00:00 +0100' tree/hello.txt")
672
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
673
 
        self.assertEqual(
674
 
            'Sat 2009-10-10 08:00:00 +0100',
675
 
            osutils.format_date(last_rev.timestamp, last_rev.timezone))
676
 
        
677
 
    def test_commit_time_bad_time(self):
678
 
        tree = self.make_branch_and_tree('tree')
679
 
        self.build_tree(['tree/hello.txt'])
680
 
        tree.add('hello.txt')
681
 
        out, err = self.run_bzr("commit -m hello "
682
 
            "--commit-time='NOT A TIME' tree/hello.txt", retcode=3)
683
 
        self.assertStartsWith(
684
 
            err, "bzr: ERROR: Could not parse --commit-time:")
685
 
 
686
587
    def test_partial_commit_with_renames_in_tree(self):
687
588
        # this test illustrates bug #140419
688
589
        t = self.make_branch_and_tree('.')
701
602
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
702
603
 
703
604
    def test_commit_readonly_checkout(self):
704
 
        # https://bugs.launchpad.net/bzr/+bug/129701
 
605
        # https://bugs.edge.launchpad.net/bzr/+bug/129701
705
606
        # "UnlockableTransport error trying to commit in checkout of readonly
706
607
        # branch"
707
608
        self.make_branch('master')
713
614
        self.assertContainsRe(err,
714
615
            r'^bzr: ERROR: Cannot lock.*readonly transport')
715
616
 
716
 
    def setup_editor(self):
 
617
    def test_commit_hook_template(self):
717
618
        # Test that commit template hooks work
 
619
        def restoreDefaults():
 
620
            msgeditor.hooks['commit_message_template'] = []
 
621
            osutils.set_or_unset_env('BZR_EDITOR', default_editor)
718
622
        if sys.platform == "win32":
719
623
            f = file('fed.bat', 'w')
720
624
            f.write('@rem dummy fed')
721
625
            f.close()
722
 
            osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
 
626
            default_editor = osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
723
627
        else:
724
628
            f = file('fed.sh', 'wb')
725
629
            f.write('#!/bin/sh\n')
726
630
            f.close()
727
631
            os.chmod('fed.sh', 0755)
728
 
            osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
729
 
 
730
 
    def setup_commit_with_template(self):
731
 
        self.setup_editor()
 
632
            default_editor = osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
 
633
        self.addCleanup(restoreDefaults)
732
634
        msgeditor.hooks.install_named_hook("commit_message_template",
733
635
                lambda commit_obj, msg: "save me some typing\n", None)
734
636
        tree = self.make_branch_and_tree('tree')
735
637
        self.build_tree(['tree/hello.txt'])
736
638
        tree.add('hello.txt')
737
 
        return tree
738
 
 
739
 
    def test_commit_hook_template_accepted(self):
740
 
        tree = self.setup_commit_with_template()
741
 
        out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
 
639
        out, err = self.run_bzr("commit tree/hello.txt")
742
640
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
743
641
        self.assertEqual('save me some typing\n', last_rev.message)
744
 
 
745
 
    def test_commit_hook_template_rejected(self):
746
 
        tree = self.setup_commit_with_template()
747
 
        expected = tree.last_revision()
748
 
        out, err = self.run_bzr_error(["empty commit message"],
749
 
            "commit tree/hello.txt", stdin="n\n")
750
 
        self.assertEqual(expected, tree.last_revision())
751
 
 
752
 
    def test_commit_without_username(self):
753
 
        """Ensure commit error if username is not set.
754
 
        """
755
 
        self.run_bzr(['init', 'foo'])
756
 
        os.chdir('foo')
757
 
        open('foo.txt', 'w').write('hello')
758
 
        self.run_bzr(['add'])
759
 
        osutils.set_or_unset_env('EMAIL', None)
760
 
        osutils.set_or_unset_env('BZR_EMAIL', None)
761
 
        out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
762
 
        self.assertContainsRe(err, 'Unable to determine your name')
763
 
 
764
 
    def test_commit_recursive_checkout(self):
765
 
        """Ensure that a commit to a recursive checkout fails cleanly.
766
 
        """
767
 
        self.run_bzr(['init', 'test_branch'])
768
 
        self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
769
 
        os.chdir('test_checkout')
770
 
        self.run_bzr(['bind', '.']) # bind to self
771
 
        open('foo.txt', 'w').write('hello')
772
 
        self.run_bzr(['add'])
773
 
        out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
774
 
        self.assertEqual(out, '')
775
 
        self.assertContainsRe(err,
776
 
            'Branch.*test_checkout.*appears to be bound to itself')
777