~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree_4.py

  • Committer: Robert Collins
  • Date: 2008-09-22 05:15:20 UTC
  • mto: (3696.5.1 commit-updates)
  • mto: This revision was merged to the branch mainline in revision 3741.
  • Revision ID: robertc@robertcollins.net-20080922051520-uhr3pn61w141kagv
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.

Show diffs side-by-side

added added

removed removed

Lines of Context:
599
599
    def test_observed_sha1_cachable(self):
600
600
        tree = self.get_tree_with_cachable_file_foo()
601
601
        expected_sha1 = osutils.sha_file_by_name('foo')
 
602
        statvalue = os.lstat("foo")
602
603
        tree.lock_write()
603
604
        try:
604
 
            tree._observed_sha1("foo-id", "foo", expected_sha1)
 
605
            tree._observed_sha1("foo-id", "foo", (expected_sha1, statvalue))
605
606
            self.assertEqual(expected_sha1,
606
607
                tree._get_entry(path="foo")[1][0][1])
607
608
        finally:
623
624
        tree.lock_write()
624
625
        try:
625
626
            tree._observed_sha1("foo-id", "foo",
626
 
                osutils.sha_file_by_name('foo'))
 
627
                (osutils.sha_file_by_name('foo'), os.lstat("foo")))
627
628
            # Must not have changed
628
629
            self.assertEqual(current_sha1,
629
630
                tree._get_entry(path="foo")[1][0][1])
630
631
        finally:
631
632
            tree.unlock()
 
633
 
 
634
    def test_get_file_with_stat_id_only(self):
 
635
        # Explicit test to ensure we get a lstat value from WT4 trees.
 
636
        tree = self.make_branch_and_tree('.')
 
637
        self.build_tree(['foo'])
 
638
        tree.add(['foo'], ['foo-id'])
 
639
        tree.lock_read()
 
640
        self.addCleanup(tree.unlock)
 
641
        file_obj, statvalue = tree.get_file_with_stat('foo-id')
 
642
        expected = os.lstat('foo')
 
643
        self.assertEqualStat(expected, statvalue)
 
644
        self.assertEqual(["contents of foo\n"], file_obj.readlines())
 
645
 
 
646
 
 
647
class TestCorruptDirstate(TestCaseWithTransport):
 
648
    """Tests for how we handle when the dirstate has been corrupted."""
 
649
 
 
650
    def create_wt4(self):
 
651
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
652
        control.create_repository()
 
653
        control.create_branch()
 
654
        tree = workingtree_4.WorkingTreeFormat4().initialize(control)
 
655
        return tree
 
656
 
 
657
    def test_invalid_rename(self):
 
658
        tree = self.create_wt4()
 
659
        # Create a corrupted dirstate
 
660
        tree.lock_write()
 
661
        try:
 
662
            tree.commit('init') # We need a parent, or we always compare with NULL
 
663
            state = tree.current_dirstate()
 
664
            state._read_dirblocks_if_needed()
 
665
            # Now add in an invalid entry, a rename with a dangling pointer
 
666
            state._dirblocks[1][1].append((('', 'foo', 'foo-id'),
 
667
                                            [('f', '', 0, False, ''),
 
668
                                             ('r', 'bar', 0 , False, '')]))
 
669
            self.assertListRaises(errors.CorruptDirstate,
 
670
                                  tree.iter_changes, tree.basis_tree())
 
671
        finally:
 
672
            tree.unlock()
 
673
 
 
674
    def get_simple_dirblocks(self, state):
 
675
        """Extract the simple information from the DirState.
 
676
 
 
677
        This returns the dirblocks, only with the sha1sum and stat details
 
678
        filtered out.
 
679
        """
 
680
        simple_blocks = []
 
681
        for block in state._dirblocks:
 
682
            simple_block = (block[0], [])
 
683
            for entry in block[1]:
 
684
                # Include the key for each entry, and for each parent include
 
685
                # just the minikind, so we know if it was
 
686
                # present/absent/renamed/etc
 
687
                simple_block[1].append((entry[0], [i[0] for i in entry[1]]))
 
688
            simple_blocks.append(simple_block)
 
689
        return simple_blocks
 
690
 
 
691
    def test_update_basis_with_invalid_delta(self):
 
692
        """When given an invalid delta, it should abort, and not be saved."""
 
693
        self.build_tree(['dir/', 'dir/file'])
 
694
        tree = self.create_wt4()
 
695
        tree.lock_write()
 
696
        self.addCleanup(tree.unlock)
 
697
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
698
        first_revision_id = tree.commit('init')
 
699
 
 
700
        root_id = tree.path2id('')
 
701
        state = tree.current_dirstate()
 
702
        state._read_dirblocks_if_needed()
 
703
        self.assertEqual([
 
704
            ('', [(('', '', root_id), ['d', 'd'])]),
 
705
            ('', [(('', 'dir', 'dir-id'), ['d', 'd'])]),
 
706
            ('dir', [(('dir', 'file', 'file-id'), ['f', 'f'])]),
 
707
        ],  self.get_simple_dirblocks(state))
 
708
 
 
709
        tree.remove(['dir/file'])
 
710
        self.assertEqual([
 
711
            ('', [(('', '', root_id), ['d', 'd'])]),
 
712
            ('', [(('', 'dir', 'dir-id'), ['d', 'd'])]),
 
713
            ('dir', [(('dir', 'file', 'file-id'), ['a', 'f'])]),
 
714
        ],  self.get_simple_dirblocks(state))
 
715
        # Make sure the removal is written to disk
 
716
        tree.flush()
 
717
 
 
718
        # self.assertRaises(Exception, tree.update_basis_by_delta,
 
719
        new_dir = inventory.InventoryDirectory('dir-id', 'new-dir', root_id)
 
720
        new_dir.revision = 'new-revision-id'
 
721
        new_file = inventory.InventoryFile('file-id', 'new-file', root_id)
 
722
        new_file.revision = 'new-revision-id'
 
723
        self.assertRaises(errors.InconsistentDelta,
 
724
            tree.update_basis_by_delta, 'new-revision-id',
 
725
            [('dir', 'new-dir', 'dir-id', new_dir),
 
726
             ('dir/file', 'new-dir/new-file', 'file-id', new_file),
 
727
            ])
 
728
        del state
 
729
 
 
730
        # Now when we re-read the file it should not have been modified
 
731
        tree.unlock()
 
732
        tree.lock_read()
 
733
        self.assertEqual(first_revision_id, tree.last_revision())
 
734
        state = tree.current_dirstate()
 
735
        state._read_dirblocks_if_needed()
 
736
        self.assertEqual([
 
737
            ('', [(('', '', root_id), ['d', 'd'])]),
 
738
            ('', [(('', 'dir', 'dir-id'), ['d', 'd'])]),
 
739
            ('dir', [(('dir', 'file', 'file-id'), ['a', 'f'])]),
 
740
        ],  self.get_simple_dirblocks(state))