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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-03-09 21:27:15 UTC
  • mfrom: (2328.1.1 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070309212715-07aacbc78b3e2dc0
(Dmitry Vasiliev) Improvements to osutils functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
import os
19
19
 
20
20
import bzrlib
21
 
from bzrlib.tests import TestCaseWithTransport
 
21
from bzrlib import (
 
22
    errors,
 
23
    lockdir,
 
24
    osutils,
 
25
    tests,
 
26
    )
22
27
from bzrlib.branch import Branch
23
28
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
24
 
from bzrlib.workingtree import WorkingTree
25
 
from bzrlib.commit import Commit
 
29
from bzrlib.commit import Commit, NullCommitReporter
26
30
from bzrlib.config import BranchConfig
27
31
from bzrlib.errors import (PointlessCommit, BzrError, SigningFailed, 
28
32
                           LockContention)
 
33
from bzrlib.tests import TestCaseWithTransport
 
34
from bzrlib.workingtree import WorkingTree
29
35
 
30
36
 
31
37
# TODO: Test commit with some added, and added-but-missing files
45
51
        return "bzrlib.ahook bzrlib.ahook"
46
52
 
47
53
 
 
54
class CapturingReporter(NullCommitReporter):
 
55
    """This reporter captures the calls made to it for evaluation later."""
 
56
 
 
57
    def __init__(self):
 
58
        # a list of the calls this received
 
59
        self.calls = []
 
60
 
 
61
    def snapshot_change(self, change, path):
 
62
        self.calls.append(('change', change, path))
 
63
 
 
64
    def deleted(self, file_id):
 
65
        self.calls.append(('deleted', file_id))
 
66
 
 
67
    def missing(self, path):
 
68
        self.calls.append(('missing', path))
 
69
 
 
70
    def renamed(self, change, old_path, new_path):
 
71
        self.calls.append(('renamed', change, old_path, new_path))
 
72
 
 
73
 
48
74
class TestCommit(TestCaseWithTransport):
49
75
 
50
76
    def test_simple_commit(self):
197
223
        wt.move(['hello'], 'a')
198
224
        r2 = 'test@rev-2'
199
225
        wt.commit('two', rev_id=r2, allow_pointless=False)
200
 
        self.check_inventory_shape(wt.read_working_inventory(),
201
 
                                   ['a', 'a/hello', 'b'])
 
226
        wt.lock_read()
 
227
        try:
 
228
            self.check_inventory_shape(wt.read_working_inventory(),
 
229
                                       ['a', 'a/hello', 'b'])
 
230
        finally:
 
231
            wt.unlock()
202
232
 
203
233
        wt.move(['b'], 'a')
204
234
        r3 = 'test@rev-3'
205
235
        wt.commit('three', rev_id=r3, allow_pointless=False)
206
 
        self.check_inventory_shape(wt.read_working_inventory(),
207
 
                                   ['a', 'a/hello', 'a/b'])
208
 
        self.check_inventory_shape(b.repository.get_revision_inventory(r3),
209
 
                                   ['a', 'a/hello', 'a/b'])
 
236
        wt.lock_read()
 
237
        try:
 
238
            self.check_inventory_shape(wt.read_working_inventory(),
 
239
                                       ['a', 'a/hello', 'a/b'])
 
240
            self.check_inventory_shape(b.repository.get_revision_inventory(r3),
 
241
                                       ['a', 'a/hello', 'a/b'])
 
242
        finally:
 
243
            wt.unlock()
210
244
 
211
245
        wt.move(['a/hello'], 'a/b')
212
246
        r4 = 'test@rev-4'
213
247
        wt.commit('four', rev_id=r4, allow_pointless=False)
214
 
        self.check_inventory_shape(wt.read_working_inventory(),
215
 
                                   ['a', 'a/b/hello', 'a/b'])
 
248
        wt.lock_read()
 
249
        try:
 
250
            self.check_inventory_shape(wt.read_working_inventory(),
 
251
                                       ['a', 'a/b/hello', 'a/b'])
 
252
        finally:
 
253
            wt.unlock()
216
254
 
217
255
        inv = b.repository.get_revision_inventory(r4)
218
256
        eq(inv['hello-id'].revision, r4)
219
257
        eq(inv['a-id'].revision, r1)
220
258
        eq(inv['b-id'].revision, r3)
221
 
        
 
259
 
222
260
    def test_removed_commit(self):
223
261
        """Commit with a removed file"""
224
262
        wt = self.make_branch_and_tree('.')
387
425
        bound = master.sprout('bound')
388
426
        wt = bound.open_workingtree()
389
427
        wt.branch.set_bound_location(os.path.realpath('master'))
 
428
 
 
429
        orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
390
430
        master_branch.lock_write()
391
431
        try:
 
432
            lockdir._DEFAULT_TIMEOUT_SECONDS = 1
392
433
            self.assertRaises(LockContention, wt.commit, 'silly')
393
434
        finally:
 
435
            lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
394
436
            master_branch.unlock()
 
437
 
 
438
    def test_commit_bound_merge(self):
 
439
        # see bug #43959; commit of a merge in a bound branch fails to push
 
440
        # the new commit into the master
 
441
        master_branch = self.make_branch('master')
 
442
        bound_tree = self.make_branch_and_tree('bound')
 
443
        bound_tree.branch.bind(master_branch)
 
444
 
 
445
        self.build_tree_contents([('bound/content_file', 'initial contents\n')])
 
446
        bound_tree.add(['content_file'])
 
447
        bound_tree.commit(message='woo!')
 
448
 
 
449
        other_bzrdir = master_branch.bzrdir.sprout('other')
 
450
        other_tree = other_bzrdir.open_workingtree()
 
451
 
 
452
        # do a commit to the the other branch changing the content file so
 
453
        # that our commit after merging will have a merged revision in the
 
454
        # content file history.
 
455
        self.build_tree_contents([('other/content_file', 'change in other\n')])
 
456
        other_tree.commit('change in other')
 
457
 
 
458
        # do a merge into the bound branch from other, and then change the
 
459
        # content file locally to force a new revision (rather than using the
 
460
        # revision from other). This forces extra processing in commit.
 
461
        bound_tree.merge_from_branch(other_tree.branch)
 
462
        self.build_tree_contents([('bound/content_file', 'change in bound\n')])
 
463
 
 
464
        # before #34959 was fixed, this failed with 'revision not present in
 
465
        # weave' when trying to implicitly push from the bound branch to the master
 
466
        bound_tree.commit(message='commit of merge in bound tree')
 
467
 
 
468
    def test_commit_reporting_after_merge(self):
 
469
        # when doing a commit of a merge, the reporter needs to still 
 
470
        # be called for each item that is added/removed/deleted.
 
471
        this_tree = self.make_branch_and_tree('this')
 
472
        # we need a bunch of files and dirs, to perform one action on each.
 
473
        self.build_tree([
 
474
            'this/dirtorename/',
 
475
            'this/dirtoreparent/',
 
476
            'this/dirtoleave/',
 
477
            'this/dirtoremove/',
 
478
            'this/filetoreparent',
 
479
            'this/filetorename',
 
480
            'this/filetomodify',
 
481
            'this/filetoremove',
 
482
            'this/filetoleave']
 
483
            )
 
484
        this_tree.add([
 
485
            'dirtorename',
 
486
            'dirtoreparent',
 
487
            'dirtoleave',
 
488
            'dirtoremove',
 
489
            'filetoreparent',
 
490
            'filetorename',
 
491
            'filetomodify',
 
492
            'filetoremove',
 
493
            'filetoleave']
 
494
            )
 
495
        this_tree.commit('create_files')
 
496
        other_dir = this_tree.bzrdir.sprout('other')
 
497
        other_tree = other_dir.open_workingtree()
 
498
        other_tree.lock_write()
 
499
        # perform the needed actions on the files and dirs.
 
500
        try:
 
501
            other_tree.rename_one('dirtorename', 'renameddir')
 
502
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
 
503
            other_tree.rename_one('filetorename', 'renamedfile')
 
504
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
 
505
            other_tree.remove(['dirtoremove', 'filetoremove'])
 
506
            self.build_tree_contents([
 
507
                ('other/newdir/', ),
 
508
                ('other/filetomodify', 'new content'),
 
509
                ('other/newfile', 'new file content')])
 
510
            other_tree.add('newfile')
 
511
            other_tree.add('newdir/')
 
512
            other_tree.commit('modify all sample files and dirs.')
 
513
        finally:
 
514
            other_tree.unlock()
 
515
        this_tree.merge_from_branch(other_tree.branch)
 
516
        reporter = CapturingReporter()
 
517
        this_tree.commit('do the commit', reporter=reporter)
 
518
        self.assertEqual([
 
519
            ('change', 'unchanged', ''),
 
520
            ('change', 'unchanged', 'dirtoleave'),
 
521
            ('change', 'unchanged', 'filetoleave'),
 
522
            ('change', 'modified', 'filetomodify'),
 
523
            ('change', 'added', 'newdir'),
 
524
            ('change', 'added', 'newfile'),
 
525
            ('renamed', 'renamed', 'dirtorename', 'renameddir'),
 
526
            ('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
 
527
            ('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
 
528
            ('renamed', 'renamed', 'filetorename', 'renamedfile'),
 
529
            ('deleted', 'dirtoremove'),
 
530
            ('deleted', 'filetoremove'),
 
531
            ],
 
532
            reporter.calls)
 
533
 
 
534
    def test_commit_removals_respects_filespec(self):
 
535
        """Commit respects the specified_files for removals."""
 
536
        tree = self.make_branch_and_tree('.')
 
537
        self.build_tree(['a', 'b'])
 
538
        tree.add(['a', 'b'])
 
539
        tree.commit('added a, b')
 
540
        tree.remove(['a', 'b'])
 
541
        tree.commit('removed a', specific_files='a')
 
542
        basis = tree.basis_tree()
 
543
        tree.lock_read()
 
544
        try:
 
545
            self.assertIs(None, basis.path2id('a'))
 
546
            self.assertFalse(basis.path2id('b') is None)
 
547
        finally:
 
548
            tree.unlock()
 
549
 
 
550
    def test_commit_saves_1ms_timestamp(self):
 
551
        """Passing in a timestamp is saved with 1ms resolution"""
 
552
        tree = self.make_branch_and_tree('.')
 
553
        self.build_tree(['a'])
 
554
        tree.add('a')
 
555
        tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
 
556
                    rev_id='a1')
 
557
 
 
558
        rev = tree.branch.repository.get_revision('a1')
 
559
        self.assertEqual(1153248633.419, rev.timestamp)
 
560
 
 
561
    def test_commit_has_1ms_resolution(self):
 
562
        """Allowing commit to generate the timestamp also has 1ms resolution"""
 
563
        tree = self.make_branch_and_tree('.')
 
564
        self.build_tree(['a'])
 
565
        tree.add('a')
 
566
        tree.commit('added a', rev_id='a1')
 
567
 
 
568
        rev = tree.branch.repository.get_revision('a1')
 
569
        timestamp = rev.timestamp
 
570
        timestamp_1ms = round(timestamp, 3)
 
571
        self.assertEqual(timestamp_1ms, timestamp)
 
572
 
 
573
    def assertBasisTreeKind(self, kind, tree, file_id):
 
574
        basis = tree.basis_tree()
 
575
        basis.lock_read()
 
576
        try:
 
577
            self.assertEqual(kind, basis.kind(file_id))
 
578
        finally:
 
579
            basis.unlock()
 
580
 
 
581
    def test_commit_kind_changes(self):
 
582
        if not osutils.has_symlinks():
 
583
            raise tests.TestSkipped('Test requires symlink support')
 
584
        tree = self.make_branch_and_tree('.')
 
585
        os.symlink('target', 'name')
 
586
        tree.add('name', 'a-file-id')
 
587
        tree.commit('Added a symlink')
 
588
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
 
589
 
 
590
        os.unlink('name')
 
591
        self.build_tree(['name'])
 
592
        tree.commit('Changed symlink to file')
 
593
        self.assertBasisTreeKind('file', tree, 'a-file-id')
 
594
 
 
595
        os.unlink('name')
 
596
        os.symlink('target', 'name')
 
597
        tree.commit('file to symlink')
 
598
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
 
599
 
 
600
        os.unlink('name')
 
601
        os.mkdir('name')
 
602
        tree.commit('symlink to directory')
 
603
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
 
604
 
 
605
        os.rmdir('name')
 
606
        os.symlink('target', 'name')
 
607
        tree.commit('directory to symlink')
 
608
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
 
609
 
 
610
        # prepare for directory <-> file tests
 
611
        os.unlink('name')
 
612
        os.mkdir('name')
 
613
        tree.commit('symlink to directory')
 
614
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
 
615
 
 
616
        os.rmdir('name')
 
617
        self.build_tree(['name'])
 
618
        tree.commit('Changed directory to file')
 
619
        self.assertBasisTreeKind('file', tree, 'a-file-id')
 
620
 
 
621
        os.unlink('name')
 
622
        os.mkdir('name')
 
623
        tree.commit('file to directory')
 
624
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
 
625
 
 
626
    def test_commit_unversioned_specified(self):
 
627
        """Commit should raise if specified files isn't in basis or worktree"""
 
628
        tree = self.make_branch_and_tree('.')
 
629
        self.assertRaises(errors.PathsNotVersionedError, tree.commit, 
 
630
                          'message', specific_files=['bogus'])
 
631
 
 
632
    class Callback(object):
 
633
        
 
634
        def __init__(self, message, testcase):
 
635
            self.called = False
 
636
            self.message = message
 
637
            self.testcase = testcase
 
638
 
 
639
        def __call__(self, commit_obj):
 
640
            self.called = True
 
641
            self.testcase.assertTrue(isinstance(commit_obj, Commit))
 
642
            return self.message
 
643
 
 
644
    def test_commit_callback(self):
 
645
        """Commit should invoke a callback to get the message"""
 
646
 
 
647
        tree = self.make_branch_and_tree('.')
 
648
        try:
 
649
            tree.commit()
 
650
        except Exception, e:
 
651
            self.assertTrue(isinstance(e, BzrError))
 
652
            self.assertEqual('The message or message_callback keyword'
 
653
                             ' parameter is required for commit().', str(e))
 
654
        else:
 
655
            self.fail('exception not raised')
 
656
        cb = self.Callback(u'commit 1', self)
 
657
        tree.commit(message_callback=cb)
 
658
        self.assertTrue(cb.called)
 
659
        repository = tree.branch.repository
 
660
        message = repository.get_revision(tree.last_revision()).message
 
661
        self.assertEqual('commit 1', message)
 
662
 
 
663
    def test_no_callback_pointless(self):
 
664
        """Callback should not be invoked for pointless commit"""
 
665
        tree = self.make_branch_and_tree('.')
 
666
        cb = self.Callback(u'commit 2', self)
 
667
        self.assertRaises(PointlessCommit, tree.commit, message_callback=cb, 
 
668
                          allow_pointless=False)
 
669
        self.assertFalse(cb.called)
 
670
 
 
671
    def test_no_callback_netfailure(self):
 
672
        """Callback should not be invoked if connectivity fails"""
 
673
        tree = self.make_branch_and_tree('.')
 
674
        cb = self.Callback(u'commit 2', self)
 
675
        repository = tree.branch.repository
 
676
        # simulate network failure
 
677
        def raise_(self, arg, arg2):
 
678
            raise errors.NoSuchFile('foo')
 
679
        repository.add_inventory = raise_
 
680
        self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
 
681
        self.assertFalse(cb.called)