~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Andrew Bennetts
  • Date: 2008-03-12 20:13:07 UTC
  • mfrom: (3267 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20080312201307-ngd5bynt2nvhnlb7
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
from bzrlib.merge import Merge3Merger
44
44
from bzrlib.tests import (
45
45
    CaseInsensitiveFilesystemFeature,
 
46
    HardlinkFeature,
46
47
    SymlinkFeature,
47
48
    TestCase,
48
49
    TestCaseInTempDir,
134
135
        transform.finalize()
135
136
        transform.finalize()
136
137
 
 
138
    def test_hardlink(self):
 
139
        self.requireFeature(HardlinkFeature)
 
140
        transform, root = self.get_transform()
 
141
        transform.new_file('file1', root, 'contents')
 
142
        transform.apply()
 
143
        target = self.make_branch_and_tree('target')
 
144
        target_transform = TreeTransform(target)
 
145
        trans_id = target_transform.create_path('file1', target_transform.root)
 
146
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
 
147
        target_transform.apply()
 
148
        self.failUnlessExists('target/file1')
 
149
        source_stat = os.stat(self.wt.abspath('file1'))
 
150
        target_stat = os.stat('target/file1')
 
151
        self.assertEqual(source_stat, target_stat)
 
152
 
137
153
    def test_convenience(self):
138
154
        transform, root = self.get_transform()
139
155
        self.wt.lock_tree_write()
820
836
        transform.apply()
821
837
        transform, root = self.get_transform()
822
838
        try:
823
 
            self.assertEqual([], list(transform._iter_changes()))
 
839
            self.assertEqual([], list(transform.iter_changes()))
824
840
            old = transform.trans_id_tree_file_id('id-1')
825
841
            transform.unversion_file(old)
826
842
            self.assertEqual([('id-1', ('old', None), False, (True, False),
827
843
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
828
 
                (True, True))], list(transform._iter_changes()))
 
844
                (True, True))], list(transform.iter_changes()))
829
845
            transform.new_directory('new', root, 'id-1')
830
846
            self.assertEqual([('id-1', ('old', 'new'), True, (True, True),
831
847
                ('eert_toor', 'eert_toor'), ('old', 'new'),
832
848
                ('file', 'directory'),
833
 
                (True, False))], list(transform._iter_changes()))
 
849
                (True, False))], list(transform.iter_changes()))
834
850
        finally:
835
851
            transform.finalize()
836
852
 
845
861
            transform.version_file('id-1', old)
846
862
            self.assertEqual([('id-1', (None, 'old'), False, (False, True),
847
863
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
848
 
                (False, False))], list(transform._iter_changes()))
 
864
                (False, False))], list(transform.iter_changes()))
849
865
        finally:
850
866
            transform.finalize()
851
867
 
861
877
            old = transform.trans_id_tree_path('old')
862
878
            subdir = transform.trans_id_tree_file_id('subdir-id')
863
879
            new = transform.trans_id_tree_path('new')
864
 
            self.assertEqual([], list(transform._iter_changes()))
 
880
            self.assertEqual([], list(transform.iter_changes()))
865
881
 
866
882
            #content deletion
867
883
            transform.delete_contents(old)
868
884
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
869
885
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', None),
870
 
                (False, False))], list(transform._iter_changes()))
 
886
                (False, False))], list(transform.iter_changes()))
871
887
 
872
888
            #content change
873
889
            transform.create_file('blah', old)
874
890
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
875
891
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
876
 
                (False, False))], list(transform._iter_changes()))
 
892
                (False, False))], list(transform.iter_changes()))
877
893
            transform.cancel_deletion(old)
878
894
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
879
895
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
880
 
                (False, False))], list(transform._iter_changes()))
 
896
                (False, False))], list(transform.iter_changes()))
881
897
            transform.cancel_creation(old)
882
898
 
883
899
            # move file_id to a different file
884
 
            self.assertEqual([], list(transform._iter_changes()))
 
900
            self.assertEqual([], list(transform.iter_changes()))
885
901
            transform.unversion_file(old)
886
902
            transform.version_file('id-1', new)
887
903
            transform.adjust_path('old', root, new)
888
904
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
889
905
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
890
 
                (False, False))], list(transform._iter_changes()))
 
906
                (False, False))], list(transform.iter_changes()))
891
907
            transform.cancel_versioning(new)
892
908
            transform._removed_id = set()
893
909
 
894
910
            #execute bit
895
 
            self.assertEqual([], list(transform._iter_changes()))
 
911
            self.assertEqual([], list(transform.iter_changes()))
896
912
            transform.set_executability(True, old)
897
913
            self.assertEqual([('id-1', ('old', 'old'), False, (True, True),
898
914
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
899
 
                (False, True))], list(transform._iter_changes()))
 
915
                (False, True))], list(transform.iter_changes()))
900
916
            transform.set_executability(None, old)
901
917
 
902
918
            # filename
903
 
            self.assertEqual([], list(transform._iter_changes()))
 
919
            self.assertEqual([], list(transform.iter_changes()))
904
920
            transform.adjust_path('new', root, old)
905
921
            transform._new_parent = {}
906
922
            self.assertEqual([('id-1', ('old', 'new'), False, (True, True),
907
923
                ('eert_toor', 'eert_toor'), ('old', 'new'), ('file', 'file'),
908
 
                (False, False))], list(transform._iter_changes()))
 
924
                (False, False))], list(transform.iter_changes()))
909
925
            transform._new_name = {}
910
926
 
911
927
            # parent directory
912
 
            self.assertEqual([], list(transform._iter_changes()))
 
928
            self.assertEqual([], list(transform.iter_changes()))
913
929
            transform.adjust_path('new', subdir, old)
914
930
            transform._new_name = {}
915
931
            self.assertEqual([('id-1', ('old', 'subdir/old'), False,
916
932
                (True, True), ('eert_toor', 'subdir-id'), ('old', 'old'),
917
933
                ('file', 'file'), (False, False))],
918
 
                list(transform._iter_changes()))
 
934
                list(transform.iter_changes()))
919
935
            transform._new_path = {}
920
936
 
921
937
        finally:
943
959
                ('id-2', (u'file2', u'file2'), False, (True, True),
944
960
                ('eert_toor', 'eert_toor'), ('file2', u'file2'),
945
961
                ('file', 'file'), (False, True))],
946
 
                list(transform._iter_changes()))
 
962
                list(transform.iter_changes()))
947
963
        finally:
948
964
            transform.finalize()
949
965
 
963
979
            transform.adjust_path('flitter', root, floater)
964
980
            self.assertEqual([('floater-id', ('floater', 'flitter'), False,
965
981
            (True, True), ('toor_eert', 'toor_eert'), ('floater', 'flitter'),
966
 
            (None, None), (False, False))], list(transform._iter_changes()))
 
982
            (None, None), (False, False))], list(transform.iter_changes()))
967
983
        finally:
968
984
            transform.finalize()
969
985
 
978
994
        try:
979
995
            old = transform.trans_id_tree_path('old')
980
996
            subdir = transform.trans_id_tree_file_id('subdir-id')
981
 
            self.assertEqual([], list(transform._iter_changes()))
 
997
            self.assertEqual([], list(transform.iter_changes()))
982
998
            transform.delete_contents(subdir)
983
999
            transform.create_directory(subdir)
984
1000
            transform.set_executability(False, old)
985
1001
            transform.unversion_file(old)
986
1002
            transform.version_file('id-1', old)
987
1003
            transform.adjust_path('old', root, old)
988
 
            self.assertEqual([], list(transform._iter_changes()))
 
1004
            self.assertEqual([], list(transform.iter_changes()))
989
1005
        finally:
990
1006
            transform.finalize()
991
1007
 
1573
1589
        # children of non-root directories should not be renamed
1574
1590
        self.assertEqual(2, transform_result.rename_count)
1575
1591
 
 
1592
    def create_ab_tree(self):
 
1593
        """Create a committed test tree with two files"""
 
1594
        source = self.make_branch_and_tree('source')
 
1595
        self.build_tree_contents([('source/file1', 'A')])
 
1596
        self.build_tree_contents([('source/file2', 'B')])
 
1597
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
1598
        source.commit('commit files')
 
1599
        source.lock_write()
 
1600
        self.addCleanup(source.unlock)
 
1601
        return source
 
1602
 
1576
1603
    def test_build_tree_accelerator_tree(self):
1577
 
        source = self.make_branch_and_tree('source')
1578
 
        self.build_tree_contents([('source/file1', 'A')])
1579
 
        self.build_tree_contents([('source/file2', 'B')])
1580
 
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
1581
 
        source.commit('commit files')
 
1604
        source = self.create_ab_tree()
1582
1605
        self.build_tree_contents([('source/file2', 'C')])
1583
1606
        calls = []
1584
1607
        real_source_get_file = source.get_file
1586
1609
            calls.append(file_id)
1587
1610
            return real_source_get_file(file_id, path)
1588
1611
        source.get_file = get_file
1589
 
        source.lock_read()
1590
 
        self.addCleanup(source.unlock)
1591
1612
        target = self.make_branch_and_tree('target')
1592
1613
        revision_tree = source.basis_tree()
1593
1614
        revision_tree.lock_read()
1596
1617
        self.assertEqual(['file1-id'], calls)
1597
1618
        target.lock_read()
1598
1619
        self.addCleanup(target.unlock)
1599
 
        self.assertEqual([], list(target._iter_changes(revision_tree)))
 
1620
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1600
1621
 
1601
1622
    def test_build_tree_accelerator_tree_missing_file(self):
1602
 
        source = self.make_branch_and_tree('source')
1603
 
        self.build_tree_contents([('source/file1', 'A')])
1604
 
        self.build_tree_contents([('source/file2', 'B')])
1605
 
        source.add(['file1', 'file2'])
1606
 
        source.commit('commit files')
 
1623
        source = self.create_ab_tree()
1607
1624
        os.unlink('source/file1')
1608
1625
        source.remove(['file2'])
1609
1626
        target = self.make_branch_and_tree('target')
1613
1630
        build_tree(revision_tree, target, source)
1614
1631
        target.lock_read()
1615
1632
        self.addCleanup(target.unlock)
1616
 
        self.assertEqual([], list(target._iter_changes(revision_tree)))
 
1633
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1617
1634
 
1618
1635
    def test_build_tree_accelerator_wrong_kind(self):
1619
1636
        self.requireFeature(SymlinkFeature)
1632
1649
            calls.append(file_id)
1633
1650
            return real_source_get_file(file_id, path)
1634
1651
        source.get_file = get_file
1635
 
        source.lock_read()
1636
 
        self.addCleanup(source.unlock)
1637
1652
        target = self.make_branch_and_tree('target')
1638
1653
        revision_tree = source.basis_tree()
1639
1654
        revision_tree.lock_read()
1642
1657
        self.assertEqual([], calls)
1643
1658
        target.lock_read()
1644
1659
        self.addCleanup(target.unlock)
1645
 
        self.assertEqual([], list(target._iter_changes(revision_tree)))
 
1660
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1661
 
 
1662
    def test_build_tree_hardlink(self):
 
1663
        self.requireFeature(HardlinkFeature)
 
1664
        source = self.create_ab_tree()
 
1665
        target = self.make_branch_and_tree('target')
 
1666
        revision_tree = source.basis_tree()
 
1667
        revision_tree.lock_read()
 
1668
        self.addCleanup(revision_tree.unlock)
 
1669
        build_tree(revision_tree, target, source, hardlink=True)
 
1670
        target.lock_read()
 
1671
        self.addCleanup(target.unlock)
 
1672
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1673
        source_stat = os.stat('source/file1')
 
1674
        target_stat = os.stat('target/file1')
 
1675
        self.assertEqual(source_stat, target_stat)
 
1676
 
 
1677
        # Explicitly disallowing hardlinks should prevent them.
 
1678
        target2 = self.make_branch_and_tree('target2')
 
1679
        build_tree(revision_tree, target2, source, hardlink=False)
 
1680
        target2.lock_read()
 
1681
        self.addCleanup(target2.unlock)
 
1682
        self.assertEqual([], list(target2.iter_changes(revision_tree)))
 
1683
        source_stat = os.stat('source/file1')
 
1684
        target2_stat = os.stat('target2/file1')
 
1685
        self.assertNotEqual(source_stat, target2_stat)
1646
1686
 
1647
1687
    def test_build_tree_accelerator_tree_moved(self):
1648
1688
        source = self.make_branch_and_tree('source')
1659
1699
        build_tree(revision_tree, target, source)
1660
1700
        target.lock_read()
1661
1701
        self.addCleanup(target.unlock)
1662
 
        self.assertEqual([], list(target._iter_changes(revision_tree)))
 
1702
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1703
 
 
1704
    def test_build_tree_hardlinks_preserve_execute(self):
 
1705
        self.requireFeature(HardlinkFeature)
 
1706
        source = self.create_ab_tree()
 
1707
        tt = TreeTransform(source)
 
1708
        trans_id = tt.trans_id_tree_file_id('file1-id')
 
1709
        tt.set_executability(True, trans_id)
 
1710
        tt.apply()
 
1711
        self.assertTrue(source.is_executable('file1-id'))
 
1712
        target = self.make_branch_and_tree('target')
 
1713
        revision_tree = source.basis_tree()
 
1714
        revision_tree.lock_read()
 
1715
        self.addCleanup(revision_tree.unlock)
 
1716
        build_tree(revision_tree, target, source, hardlink=True)
 
1717
        target.lock_read()
 
1718
        self.addCleanup(target.unlock)
 
1719
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1720
        self.assertTrue(source.is_executable('file1-id'))
1663
1721
 
1664
1722
 
1665
1723
class MockTransform(object):
1897
1955
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
1898
1956
                          (root, root), ('a', 'a'), ('file', 'file'),
1899
1957
                          (False, False))],
1900
 
                          list(preview_tree._iter_changes(revision_tree)))
 
1958
                          list(preview_tree.iter_changes(revision_tree)))
1901
1959
 
1902
1960
    def test_wrong_tree_value_error(self):
1903
1961
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1904
 
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1962
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
1905
1963
                              preview_tree)
1906
1964
        self.assertEqual('from_tree must be transform source tree.', str(e))
1907
1965
 
1908
1966
    def test_include_unchanged_value_error(self):
1909
1967
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1910
 
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1968
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
1911
1969
                              revision_tree, include_unchanged=True)
1912
1970
        self.assertEqual('include_unchanged is not supported', str(e))
1913
1971
 
1914
1972
    def test_specific_files(self):
1915
1973
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1916
 
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1974
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
1917
1975
                              revision_tree, specific_files=['pete'])
1918
1976
        self.assertEqual('specific_files is not supported', str(e))
1919
1977
 
1920
1978
    def test_want_unversioned_value_error(self):
1921
1979
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1922
 
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1980
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
1923
1981
                              revision_tree, want_unversioned=True)
1924
1982
        self.assertEqual('want_unversioned is not supported', str(e))
1925
1983
 
1927
1985
        # extra_trees is harmless without specific_files, so we'll silently
1928
1986
        # accept it, even though we won't use it.
1929
1987
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1930
 
        preview_tree._iter_changes(revision_tree, extra_trees=[preview_tree])
 
1988
        preview_tree.iter_changes(revision_tree, extra_trees=[preview_tree])
1931
1989
 
1932
1990
    def test_ignore_require_versioned_no_specific_files(self):
1933
1991
        # require_versioned is meaningless without specific_files.
1934
1992
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1935
 
        preview_tree._iter_changes(revision_tree, require_versioned=False)
 
1993
        preview_tree.iter_changes(revision_tree, require_versioned=False)
1936
1994
 
1937
1995
    def test_ignore_pb(self):
1938
1996
        # pb could be supported, but TT.iter_changes doesn't support it.
1939
1997
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
1940
 
        preview_tree._iter_changes(revision_tree, pb=progress.DummyProgress())
 
1998
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
1941
1999
 
1942
2000
    def test_kind(self):
1943
2001
        revision_tree = self.create_tree()
1967
2025
            self.assertEqual('contents', tree_file.read())
1968
2026
        finally:
1969
2027
            tree_file.close()
 
2028
 
 
2029
    def test_get_symlink_target(self):
 
2030
        self.requireFeature(SymlinkFeature)
 
2031
        preview = self.get_empty_preview()
 
2032
        preview.new_symlink('symlink', preview.root, 'target', 'symlink-id')
 
2033
        preview_tree = preview.get_preview_tree()
 
2034
        self.assertEqual('target',
 
2035
                         preview_tree.get_symlink_target('symlink-id'))