~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-05-12 05:23:58 UTC
  • mfrom: (3350.4.4 remove-deprecations)
  • Revision ID: pqm@pqm.ubuntu.com-20080512052358-68borsc013lplzz2
(robertc) Remove many deprecated methods from VersionedFile,
        Repository and bzrlib.revision. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 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
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
18
18
 
19
19
import bisect
20
20
import os
 
21
import time
21
22
 
22
23
from bzrlib import (
23
24
    dirstate,
24
25
    errors,
25
 
    inventory,
26
 
    memorytree,
27
26
    osutils,
28
 
    revision as _mod_revision,
29
 
    tests,
30
27
    )
31
 
from bzrlib.tests import test_osutils
 
28
from bzrlib.memorytree import MemoryTree
 
29
from bzrlib.tests import (
 
30
        SymlinkFeature,
 
31
        TestCase,
 
32
        TestCaseWithTransport,
 
33
        )
32
34
 
33
35
 
34
36
# TODO:
44
46
# set_path_id  setting id when state is in memory modified
45
47
 
46
48
 
47
 
def load_tests(basic_tests, module, loader):
48
 
    suite = loader.suiteClass()
49
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
50
 
        basic_tests, tests.condition_isinstance(TestCaseWithDirState))
51
 
    tests.multiply_tests(dir_reader_tests,
52
 
                         test_osutils.dir_reader_scenarios(), suite)
53
 
    suite.addTest(remaining_tests)
54
 
    return suite
55
 
 
56
 
 
57
 
class TestCaseWithDirState(tests.TestCaseWithTransport):
 
49
class TestCaseWithDirState(TestCaseWithTransport):
58
50
    """Helper functions for creating DirState objects with various content."""
59
51
 
60
 
    # Set by load_tests
61
 
    _dir_reader_class = None
62
 
    _native_to_unicode = None # Not used yet
63
 
 
64
 
    def setUp(self):
65
 
        tests.TestCaseWithTransport.setUp(self)
66
 
 
67
 
        self.overrideAttr(osutils,
68
 
                          '_selected_dir_reader', self._dir_reader_class())
69
 
 
70
52
    def create_empty_dirstate(self):
71
53
        """Return a locked but empty dirstate"""
72
54
        state = dirstate.DirState.initialize('dirstate')
413
395
            (('', '', tree.get_root_id()), # common details
414
396
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
415
397
              ('d', '', 0, False, rev_id), # first parent details
416
 
              ('d', '', 0, False, rev_id), # second parent details
 
398
              ('d', '', 0, False, rev_id2), # second parent details
417
399
             ])])
418
400
        state = dirstate.DirState.from_tree(tree, 'dirstate')
419
401
        self.check_state_with_reopen(expected_result, state)
494
476
            (('', '', tree.get_root_id()), # common details
495
477
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
496
478
              ('d', '', 0, False, rev_id), # first parent details
497
 
              ('d', '', 0, False, rev_id), # second parent details
 
479
              ('d', '', 0, False, rev_id2), # second parent details
498
480
             ]),
499
481
            (('', 'a file', 'a-file-id'), # common
500
482
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
580
562
        state.lock_read()
581
563
        try:
582
564
            entry = state._get_entry(0, path_utf8='a-file')
583
 
            # The current size should be 0 (default)
584
 
            self.assertEqual(0, entry[1][0][2])
 
565
            # The current sha1 sum should be empty
 
566
            self.assertEqual('', entry[1][0][1])
585
567
            # We should have a real entry.
586
568
            self.assertNotEqual((None, None), entry)
587
569
            # Make sure everything is old enough
588
570
            state._sha_cutoff_time()
589
571
            state._cutoff_time += 10
590
 
            # Change the file length
591
 
            self.build_tree_contents([('a-file', 'shorter')])
592
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
593
 
                os.lstat('a-file'))
594
 
            # new file, no cached sha:
595
 
            self.assertEqual(None, sha1sum)
 
572
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
573
            # We should have gotten a real sha1
 
574
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
575
                             sha1sum)
596
576
 
597
577
            # The dirblock has been updated
598
 
            self.assertEqual(7, entry[1][0][2])
 
578
            self.assertEqual(sha1sum, entry[1][0][1])
599
579
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
600
580
                             state._dirblock_state)
601
581
 
611
591
        state.lock_read()
612
592
        try:
613
593
            entry = state._get_entry(0, path_utf8='a-file')
614
 
            self.assertEqual(7, entry[1][0][2])
 
594
            self.assertEqual(sha1sum, entry[1][0][1])
615
595
        finally:
616
596
            state.unlock()
617
597
 
630
610
        state.lock_read()
631
611
        try:
632
612
            entry = state._get_entry(0, path_utf8='a-file')
633
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
634
 
                os.lstat('a-file'))
635
 
            # No sha - too new
636
 
            self.assertEqual(None, sha1sum)
 
613
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
614
            # We should have gotten a real sha1
 
615
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
616
                             sha1sum)
637
617
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
638
618
                             state._dirblock_state)
639
619
 
656
636
                state2.unlock()
657
637
        finally:
658
638
            state.unlock()
659
 
 
 
639
        
660
640
        # The file on disk should not be modified.
661
641
        state = dirstate.DirState.on_file('dirstate')
662
642
        state.lock_read()
730
710
 
731
711
class TestDirStateManipulations(TestCaseWithDirState):
732
712
 
733
 
    def test_update_minimal_updates_id_index(self):
734
 
        state = self.create_dirstate_with_root_and_subdir()
735
 
        self.addCleanup(state.unlock)
736
 
        id_index = state._get_id_index()
737
 
        self.assertEqual(['a-root-value', 'subdir-id'], sorted(id_index))
738
 
        state.add('file-name', 'file-id', 'file', None, '')
739
 
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
740
 
                         sorted(id_index))
741
 
        state.update_minimal(('', 'new-name', 'file-id'), 'f',
742
 
                             path_utf8='new-name')
743
 
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
744
 
                         sorted(id_index))
745
 
        self.assertEqual([('', 'new-name', 'file-id')],
746
 
                         sorted(id_index['file-id']))
747
 
        state._validate()
748
 
 
749
713
    def test_set_state_from_inventory_no_content_no_parents(self):
750
714
        # setting the current inventory is a slow but important api to support.
751
715
        tree1 = self.make_branch_and_memory_tree('tree1')
778
742
        # https://bugs.launchpad.net/bzr/+bug/146176
779
743
        # set_state_from_inventory should preserve the stat and hash value for
780
744
        # workingtree files that are not changed by the inventory.
781
 
 
 
745
       
782
746
        tree = self.make_branch_and_tree('.')
783
747
        # depends on the default format using dirstate...
784
748
        tree.lock_write()
785
749
        try:
786
 
            # make a dirstate with some valid hashcache data
 
750
            # make a dirstate with some valid hashcache data 
787
751
            # file on disk, but that's not needed for this test
788
752
            foo_contents = 'contents of foo'
789
753
            self.build_tree_contents([('foo', foo_contents)])
809
773
                (('', 'foo', 'foo-id',),
810
774
                 [('f', foo_sha, foo_size, False, foo_packed)]),
811
775
                tree._dirstate._get_entry(0, 'foo-id'))
812
 
 
 
776
           
813
777
            # extract the inventory, and add something to it
814
778
            inv = tree._get_inventory()
815
779
            # should see the file we poked in...
837
801
        finally:
838
802
            tree.unlock()
839
803
 
 
804
 
840
805
    def test_set_state_from_inventory_mixed_paths(self):
841
806
        tree1 = self.make_branch_and_tree('tree1')
842
807
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
883
848
        state = dirstate.DirState.initialize('dirstate')
884
849
        try:
885
850
            # check precondition to be sure the state does change appropriately.
886
 
            root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
887
 
            self.assertEqual([root_entry], list(state._iter_entries()))
888
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
889
 
            self.assertEqual(root_entry,
890
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
891
 
            self.assertEqual((None, None),
892
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
893
 
            state.set_path_id('', 'second-root-id')
894
 
            new_root_entry = (('', '', 'second-root-id'),
895
 
                              [('d', '', 0, False, 'x'*32)])
896
 
            expected_rows = [new_root_entry]
 
851
            self.assertEqual(
 
852
                [(('', '', 'TREE_ROOT'), [('d', '', 0, False,
 
853
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
 
854
                list(state._iter_entries()))
 
855
            state.set_path_id('', 'foobarbaz')
 
856
            expected_rows = [
 
857
                (('', '', 'foobarbaz'), [('d', '', 0, False,
 
858
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
897
859
            self.assertEqual(expected_rows, list(state._iter_entries()))
898
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
899
 
            self.assertEqual(new_root_entry, 
900
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
901
 
            self.assertEqual((None, None),
902
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
903
860
            # should work across save too
904
861
            state.save()
905
862
        finally:
923
880
        state._validate()
924
881
        try:
925
882
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
926
 
            root_entry = (('', '', 'TREE_ROOT'),
927
 
                          [('d', '', 0, False, 'x'*32),
928
 
                           ('d', '', 0, False, 'parent-revid')])
929
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
930
 
            self.assertEqual(root_entry,
931
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
932
 
            self.assertEqual((None, None),
933
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
934
 
            state.set_path_id('', 'Asecond-root-id')
 
883
            state.set_path_id('', 'foobarbaz')
935
884
            state._validate()
936
885
            # now see that it is what we expected
937
 
            old_root_entry = (('', '', 'TREE_ROOT'),
938
 
                              [('a', '', 0, False, ''),
939
 
                               ('d', '', 0, False, 'parent-revid')])
940
 
            new_root_entry = (('', '', 'Asecond-root-id'),
941
 
                              [('d', '', 0, False, ''),
942
 
                               ('a', '', 0, False, '')])
943
 
            expected_rows = [new_root_entry, old_root_entry]
 
886
            expected_rows = [
 
887
                (('', '', 'TREE_ROOT'),
 
888
                    [('a', '', 0, False, ''),
 
889
                     ('d', '', 0, False, 'parent-revid'),
 
890
                     ]),
 
891
                (('', '', 'foobarbaz'),
 
892
                    [('d', '', 0, False, ''),
 
893
                     ('a', '', 0, False, ''),
 
894
                     ]),
 
895
                ]
944
896
            state._validate()
945
897
            self.assertEqual(expected_rows, list(state._iter_entries()))
946
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
947
 
            self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
948
 
            self.assertEqual((None, None),
949
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
950
 
            self.assertEqual(old_root_entry,
951
 
                             state._get_entry(1, fileid_utf8='TREE_ROOT'))
952
 
            self.assertEqual(new_root_entry,
953
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
954
 
            self.assertEqual((None, None),
955
 
                             state._get_entry(1, fileid_utf8='Asecond-root-id'))
956
898
            # should work across save too
957
899
            state.save()
958
900
        finally:
974
916
        finally:
975
917
            state.unlock()
976
918
 
 
919
 
977
920
    def test_set_parent_trees_no_content(self):
978
921
        # set_parent_trees is a slow but important api to support.
979
922
        tree1 = self.make_branch_and_memory_tree('tree1')
984
927
        finally:
985
928
            tree1.unlock()
986
929
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
987
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
930
        tree2 = MemoryTree.create_on_branch(branch2)
988
931
        tree2.lock_write()
989
932
        try:
990
933
            revid2 = tree2.commit('foo')
1022
965
            state.set_parent_trees(
1023
966
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
1024
967
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
1025
 
                 ('ghost-rev', tree2.branch.repository.revision_tree(
1026
 
                                   _mod_revision.NULL_REVISION))),
 
968
                 ('ghost-rev', tree2.branch.repository.revision_tree(None))),
1027
969
                ['ghost-rev'])
1028
970
            self.assertEqual([revid1, revid2, 'ghost-rev'],
1029
971
                             state.get_parent_ids())
1033
975
                [(('', '', root_id), [
1034
976
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1035
977
                  ('d', '', 0, False, revid1),
1036
 
                  ('d', '', 0, False, revid1)
 
978
                  ('d', '', 0, False, revid2)
1037
979
                  ])],
1038
980
                list(state._iter_entries()))
1039
981
        finally:
1054
996
        finally:
1055
997
            tree1.unlock()
1056
998
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1057
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
999
        tree2 = MemoryTree.create_on_branch(branch2)
1058
1000
        tree2.lock_write()
1059
1001
        try:
1060
1002
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1067
1009
            (('', '', root_id), [
1068
1010
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1069
1011
             ('d', '', 0, False, revid1.encode('utf8')),
1070
 
             ('d', '', 0, False, revid1.encode('utf8'))
 
1012
             ('d', '', 0, False, revid2.encode('utf8'))
1071
1013
             ]),
1072
1014
            (('', 'a file', 'file-id'), [
1073
1015
             ('a', '', 0, False, ''),
1119
1061
            state.unlock()
1120
1062
        state = dirstate.DirState.on_file('dirstate')
1121
1063
        state.lock_read()
1122
 
        self.addCleanup(state.unlock)
1123
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1064
        try:
 
1065
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1066
        finally:
 
1067
            state.unlock()
1124
1068
 
1125
1069
    def test_add_path_to_unversioned_directory(self):
1126
1070
        """Adding a path to an unversioned directory should error.
1131
1075
        """
1132
1076
        self.build_tree(['unversioned/', 'unversioned/a file'])
1133
1077
        state = dirstate.DirState.initialize('dirstate')
1134
 
        self.addCleanup(state.unlock)
1135
 
        self.assertRaises(errors.NotVersionedError, state.add,
1136
 
                          'unversioned/a file', 'a-file-id', 'file', None, None)
 
1078
        try:
 
1079
            self.assertRaises(errors.NotVersionedError, state.add,
 
1080
                'unversioned/a file', 'a-file-id', 'file', None, None)
 
1081
        finally:
 
1082
            state.unlock()
1137
1083
 
1138
1084
    def test_add_directory_to_root_no_parents_all_data(self):
1139
1085
        # The most trivial addition of a dir is when there are no parents and
1159
1105
            state.unlock()
1160
1106
        state = dirstate.DirState.on_file('dirstate')
1161
1107
        state.lock_read()
1162
 
        self.addCleanup(state.unlock)
1163
1108
        state._validate()
1164
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1109
        try:
 
1110
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1111
        finally:
 
1112
            state.unlock()
1165
1113
 
1166
 
    def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
 
1114
    def test_add_symlink_to_root_no_parents_all_data(self):
1167
1115
        # The most trivial addition of a symlink when there are no parents and
1168
1116
        # its in the root and all data about the file is supplied
1169
1117
        # bzr doesn't support fake symlinks on windows, yet.
1170
 
        self.requireFeature(tests.SymlinkFeature)
1171
 
        os.symlink(target, link_name)
1172
 
        stat = os.lstat(link_name)
 
1118
        self.requireFeature(SymlinkFeature)
 
1119
        os.symlink('target', 'a link')
 
1120
        stat = os.lstat('a link')
1173
1121
        expected_entries = [
1174
1122
            (('', '', 'TREE_ROOT'), [
1175
1123
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1176
1124
             ]),
1177
 
            (('', link_name.encode('UTF-8'), 'a link id'), [
1178
 
             ('l', target.encode('UTF-8'), stat[6],
1179
 
              False, dirstate.pack_stat(stat)), # current tree
 
1125
            (('', 'a link', 'a link id'), [
 
1126
             ('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1180
1127
             ]),
1181
1128
            ]
1182
1129
        state = dirstate.DirState.initialize('dirstate')
1183
1130
        try:
1184
 
            state.add(link_name, 'a link id', 'symlink', stat,
1185
 
                      target.encode('UTF-8'))
 
1131
            state.add('a link', 'a link id', 'symlink', stat, 'target')
1186
1132
            # having added it, it should be in the output of iter_entries.
1187
1133
            self.assertEqual(expected_entries, list(state._iter_entries()))
1188
1134
            # saving and reloading should not affect this.
1191
1137
            state.unlock()
1192
1138
        state = dirstate.DirState.on_file('dirstate')
1193
1139
        state.lock_read()
1194
 
        self.addCleanup(state.unlock)
1195
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
1196
 
 
1197
 
    def test_add_symlink_to_root_no_parents_all_data(self):
1198
 
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1199
 
 
1200
 
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1201
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1202
 
        self._test_add_symlink_to_root_no_parents_all_data(
1203
 
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
 
1140
        try:
 
1141
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1142
        finally:
 
1143
            state.unlock()
1204
1144
 
1205
1145
    def test_add_directory_and_child_no_parents_all_data(self):
1206
1146
        # after adding a directory, we should be able to add children to it.
1231
1171
            state.unlock()
1232
1172
        state = dirstate.DirState.on_file('dirstate')
1233
1173
        state.lock_read()
1234
 
        self.addCleanup(state.unlock)
1235
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1174
        try:
 
1175
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1176
        finally:
 
1177
            state.unlock()
1236
1178
 
1237
1179
    def test_add_tree_reference(self):
1238
1180
        # make a dirstate and add a tree reference
1252
1194
            state.unlock()
1253
1195
        # now check we can read it back
1254
1196
        state.lock_read()
1255
 
        self.addCleanup(state.unlock)
1256
1197
        state._validate()
1257
 
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1258
 
        self.assertEqual(entry, entry2)
1259
 
        self.assertEqual(entry, expected_entry)
1260
 
        # and lookup by id should work too
1261
 
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1262
 
        self.assertEqual(entry, expected_entry)
 
1198
        try:
 
1199
            entry2 = state._get_entry(0, 'subdir-id', 'subdir')
 
1200
            self.assertEqual(entry, entry2)
 
1201
            self.assertEqual(entry, expected_entry)
 
1202
            # and lookup by id should work too
 
1203
            entry2 = state._get_entry(0, fileid_utf8='subdir-id')
 
1204
            self.assertEqual(entry, expected_entry)
 
1205
        finally:
 
1206
            state.unlock()
1263
1207
 
1264
1208
    def test_add_forbidden_names(self):
1265
1209
        state = dirstate.DirState.initialize('dirstate')
1269
1213
        self.assertRaises(errors.BzrError,
1270
1214
            state.add, '..', 'ass-id', 'directory', None, None)
1271
1215
 
1272
 
    def test_set_state_with_rename_b_a_bug_395556(self):
1273
 
        # bug 395556 uncovered a bug where the dirstate ends up with a false
1274
 
        # relocation record - in a tree with no parents there should be no
1275
 
        # absent or relocated records. This then leads to further corruption
1276
 
        # when a commit occurs, as the incorrect relocation gathers an
1277
 
        # incorrect absent in tree 1, and future changes go to pot.
1278
 
        tree1 = self.make_branch_and_tree('tree1')
1279
 
        self.build_tree(['tree1/b'])
1280
 
        tree1.lock_write()
1281
 
        try:
1282
 
            tree1.add(['b'], ['b-id'])
1283
 
            root_id = tree1.get_root_id()
1284
 
            inv = tree1.inventory
1285
 
            state = dirstate.DirState.initialize('dirstate')
1286
 
            try:
1287
 
                # Set the initial state with 'b'
1288
 
                state.set_state_from_inventory(inv)
1289
 
                inv.rename('b-id', root_id, 'a')
1290
 
                # Set the new state with 'a', which currently corrupts.
1291
 
                state.set_state_from_inventory(inv)
1292
 
                expected_result1 = [('', '', root_id, 'd'),
1293
 
                                    ('', 'a', 'b-id', 'f'),
1294
 
                                   ]
1295
 
                values = []
1296
 
                for entry in state._iter_entries():
1297
 
                    values.append(entry[0] + entry[1][0][:1])
1298
 
                self.assertEqual(expected_result1, values)
1299
 
            finally:
1300
 
                state.unlock()
1301
 
        finally:
1302
 
            tree1.unlock()
1303
 
 
1304
1216
 
1305
1217
class TestGetLines(TestCaseWithDirState):
1306
1218
 
1539
1451
        There is one parent tree, which has the same shape with the following variations:
1540
1452
        b/g in the parent is gone.
1541
1453
        b/h in the parent has a different id
1542
 
        b/i is new in the parent
 
1454
        b/i is new in the parent 
1543
1455
        c is renamed to b/j in the parent
1544
1456
 
1545
1457
        :return: The dirstate, still write-locked.
1635
1547
            list(state._iter_child_entries(1, '')))
1636
1548
 
1637
1549
 
1638
 
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1550
class TestDirstateSortOrder(TestCaseWithTransport):
1639
1551
    """Test that DirState adds entries in the right order."""
1640
1552
 
1641
1553
    def test_add_sorting(self):
1690
1602
 
1691
1603
        # *really* cheesy way to just get an empty tree
1692
1604
        repo = self.make_repository('repo')
1693
 
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1605
        empty_tree = repo.revision_tree(None)
1694
1606
        state.set_parent_trees([('null:', empty_tree)], [])
1695
1607
 
1696
1608
        dirblock_names = [d[0] for d in state._dirblocks]
1700
1612
class InstrumentedDirState(dirstate.DirState):
1701
1613
    """An DirState with instrumented sha1 functionality."""
1702
1614
 
1703
 
    def __init__(self, path, sha1_provider):
1704
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
 
1615
    def __init__(self, path):
 
1616
        super(InstrumentedDirState, self).__init__(path)
1705
1617
        self._time_offset = 0
1706
1618
        self._log = []
1707
1619
        # member is dynamically set in DirState.__init__ to turn on trace
1708
 
        self._sha1_provider = sha1_provider
1709
1620
        self._sha1_file = self._sha1_file_and_log
1710
1621
 
1711
1622
    def _sha_cutoff_time(self):
1714
1625
 
1715
1626
    def _sha1_file_and_log(self, abspath):
1716
1627
        self._log.append(('sha1', abspath))
1717
 
        return self._sha1_provider.sha1(abspath)
 
1628
        return osutils.sha_file_by_name(abspath)
1718
1629
 
1719
1630
    def _read_link(self, abspath, old_link):
1720
1631
        self._log.append(('read_link', abspath, old_link))
1751
1662
        self.st_ino = ino
1752
1663
        self.st_mode = mode
1753
1664
 
1754
 
    @staticmethod
1755
 
    def from_stat(st):
1756
 
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1757
 
            st.st_ino, st.st_mode)
1758
 
 
1759
 
 
1760
 
class TestPackStat(tests.TestCaseWithTransport):
 
1665
 
 
1666
class TestUpdateEntry(TestCaseWithDirState):
 
1667
    """Test the DirState.update_entry functions"""
 
1668
 
 
1669
    def get_state_with_a(self):
 
1670
        """Create a DirState tracking a single object named 'a'"""
 
1671
        state = InstrumentedDirState.initialize('dirstate')
 
1672
        self.addCleanup(state.unlock)
 
1673
        state.add('a', 'a-id', 'file', None, '')
 
1674
        entry = state._get_entry(0, path_utf8='a')
 
1675
        return state, entry
 
1676
 
 
1677
    def test_update_entry(self):
 
1678
        state, entry = self.get_state_with_a()
 
1679
        self.build_tree(['a'])
 
1680
        # Add one where we don't provide the stat or sha already
 
1681
        self.assertEqual(('', 'a', 'a-id'), entry[0])
 
1682
        self.assertEqual([('f', '', 0, False, dirstate.DirState.NULLSTAT)],
 
1683
                         entry[1])
 
1684
        # Flush the buffers to disk
 
1685
        state.save()
 
1686
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1687
                         state._dirblock_state)
 
1688
 
 
1689
        stat_value = os.lstat('a')
 
1690
        packed_stat = dirstate.pack_stat(stat_value)
 
1691
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1692
                                          stat_value=stat_value)
 
1693
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1694
                         link_or_sha1)
 
1695
 
 
1696
        # The dirblock entry should not cache the file's sha1
 
1697
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
 
1698
                         entry[1])
 
1699
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1700
                         state._dirblock_state)
 
1701
        mode = stat_value.st_mode
 
1702
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False)], state._log)
 
1703
 
 
1704
        state.save()
 
1705
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1706
                         state._dirblock_state)
 
1707
 
 
1708
        # If we do it again right away, we don't know if the file has changed
 
1709
        # so we will re-read the file. Roll the clock back so the file is
 
1710
        # guaranteed to look too new.
 
1711
        state.adjust_time(-10)
 
1712
 
 
1713
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1714
                                          stat_value=stat_value)
 
1715
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1716
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1717
                         ], state._log)
 
1718
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1719
                         link_or_sha1)
 
1720
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1721
                         state._dirblock_state)
 
1722
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
 
1723
                         entry[1])
 
1724
        state.save()
 
1725
 
 
1726
        # However, if we move the clock forward so the file is considered
 
1727
        # "stable", it should just cache the value.
 
1728
        state.adjust_time(+20)
 
1729
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1730
                                          stat_value=stat_value)
 
1731
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1732
                         link_or_sha1)
 
1733
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1734
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1735
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1736
                         ], state._log)
 
1737
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1738
                         entry[1])
 
1739
 
 
1740
        # Subsequent calls will just return the cached value
 
1741
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1742
                                          stat_value=stat_value)
 
1743
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1744
                         link_or_sha1)
 
1745
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1746
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1747
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1748
                         ], state._log)
 
1749
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1750
                         entry[1])
 
1751
 
 
1752
    def test_update_entry_symlink(self):
 
1753
        """Update entry should read symlinks."""
 
1754
        self.requireFeature(SymlinkFeature)
 
1755
        state, entry = self.get_state_with_a()
 
1756
        state.save()
 
1757
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1758
                         state._dirblock_state)
 
1759
        os.symlink('target', 'a')
 
1760
 
 
1761
        state.adjust_time(-10) # Make the symlink look new
 
1762
        stat_value = os.lstat('a')
 
1763
        packed_stat = dirstate.pack_stat(stat_value)
 
1764
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1765
                                          stat_value=stat_value)
 
1766
        self.assertEqual('target', link_or_sha1)
 
1767
        self.assertEqual([('read_link', 'a', '')], state._log)
 
1768
        # Dirblock is not updated (the link is too new)
 
1769
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
 
1770
                         entry[1])
 
1771
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1772
                         state._dirblock_state)
 
1773
 
 
1774
        # Because the stat_value looks new, we should re-read the target
 
1775
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1776
                                          stat_value=stat_value)
 
1777
        self.assertEqual('target', link_or_sha1)
 
1778
        self.assertEqual([('read_link', 'a', ''),
 
1779
                          ('read_link', 'a', ''),
 
1780
                         ], state._log)
 
1781
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
 
1782
                         entry[1])
 
1783
        state.adjust_time(+20) # Skip into the future, all files look old
 
1784
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1785
                                          stat_value=stat_value)
 
1786
        self.assertEqual('target', link_or_sha1)
 
1787
        # We need to re-read the link because only now can we cache it
 
1788
        self.assertEqual([('read_link', 'a', ''),
 
1789
                          ('read_link', 'a', ''),
 
1790
                          ('read_link', 'a', ''),
 
1791
                         ], state._log)
 
1792
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
 
1793
                         entry[1])
 
1794
 
 
1795
        # Another call won't re-read the link
 
1796
        self.assertEqual([('read_link', 'a', ''),
 
1797
                          ('read_link', 'a', ''),
 
1798
                          ('read_link', 'a', ''),
 
1799
                         ], state._log)
 
1800
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1801
                                          stat_value=stat_value)
 
1802
        self.assertEqual('target', link_or_sha1)
 
1803
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
 
1804
                         entry[1])
 
1805
 
 
1806
    def do_update_entry(self, state, entry, abspath):
 
1807
        stat_value = os.lstat(abspath)
 
1808
        return state.update_entry(entry, abspath, stat_value)
 
1809
 
 
1810
    def test_update_entry_dir(self):
 
1811
        state, entry = self.get_state_with_a()
 
1812
        self.build_tree(['a/'])
 
1813
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1814
 
 
1815
    def test_update_entry_dir_unchanged(self):
 
1816
        state, entry = self.get_state_with_a()
 
1817
        self.build_tree(['a/'])
 
1818
        state.adjust_time(+20)
 
1819
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1820
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1821
                         state._dirblock_state)
 
1822
        state.save()
 
1823
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1824
                         state._dirblock_state)
 
1825
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1826
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1827
                         state._dirblock_state)
 
1828
 
 
1829
    def test_update_entry_file_unchanged(self):
 
1830
        state, entry = self.get_state_with_a()
 
1831
        self.build_tree(['a'])
 
1832
        sha1sum = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1833
        state.adjust_time(+20)
 
1834
        self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
 
1835
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1836
                         state._dirblock_state)
 
1837
        state.save()
 
1838
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1839
                         state._dirblock_state)
 
1840
        self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
 
1841
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1842
                         state._dirblock_state)
 
1843
 
 
1844
    def create_and_test_file(self, state, entry):
 
1845
        """Create a file at 'a' and verify the state finds it.
 
1846
 
 
1847
        The state should already be versioning *something* at 'a'. This makes
 
1848
        sure that state.update_entry recognizes it as a file.
 
1849
        """
 
1850
        self.build_tree(['a'])
 
1851
        stat_value = os.lstat('a')
 
1852
        packed_stat = dirstate.pack_stat(stat_value)
 
1853
 
 
1854
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
 
1855
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1856
                         link_or_sha1)
 
1857
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1858
                         entry[1])
 
1859
        return packed_stat
 
1860
 
 
1861
    def create_and_test_dir(self, state, entry):
 
1862
        """Create a directory at 'a' and verify the state finds it.
 
1863
 
 
1864
        The state should already be versioning *something* at 'a'. This makes
 
1865
        sure that state.update_entry recognizes it as a directory.
 
1866
        """
 
1867
        self.build_tree(['a/'])
 
1868
        stat_value = os.lstat('a')
 
1869
        packed_stat = dirstate.pack_stat(stat_value)
 
1870
 
 
1871
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
 
1872
        self.assertIs(None, link_or_sha1)
 
1873
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
 
1874
 
 
1875
        return packed_stat
 
1876
 
 
1877
    def create_and_test_symlink(self, state, entry):
 
1878
        """Create a symlink at 'a' and verify the state finds it.
 
1879
 
 
1880
        The state should already be versioning *something* at 'a'. This makes
 
1881
        sure that state.update_entry recognizes it as a symlink.
 
1882
 
 
1883
        This should not be called if this platform does not have symlink
 
1884
        support.
 
1885
        """
 
1886
        # caller should care about skipping test on platforms without symlinks
 
1887
        os.symlink('path/to/foo', 'a')
 
1888
 
 
1889
        stat_value = os.lstat('a')
 
1890
        packed_stat = dirstate.pack_stat(stat_value)
 
1891
 
 
1892
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
 
1893
        self.assertEqual('path/to/foo', link_or_sha1)
 
1894
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
 
1895
                         entry[1])
 
1896
        return packed_stat
 
1897
 
 
1898
    def test_update_file_to_dir(self):
 
1899
        """If a file changes to a directory we return None for the sha.
 
1900
        We also update the inventory record.
 
1901
        """
 
1902
        state, entry = self.get_state_with_a()
 
1903
        # The file sha1 won't be cached unless the file is old
 
1904
        state.adjust_time(+10)
 
1905
        self.create_and_test_file(state, entry)
 
1906
        os.remove('a')
 
1907
        self.create_and_test_dir(state, entry)
 
1908
 
 
1909
    def test_update_file_to_symlink(self):
 
1910
        """File becomes a symlink"""
 
1911
        self.requireFeature(SymlinkFeature)
 
1912
        state, entry = self.get_state_with_a()
 
1913
        # The file sha1 won't be cached unless the file is old
 
1914
        state.adjust_time(+10)
 
1915
        self.create_and_test_file(state, entry)
 
1916
        os.remove('a')
 
1917
        self.create_and_test_symlink(state, entry)
 
1918
 
 
1919
    def test_update_dir_to_file(self):
 
1920
        """Directory becoming a file updates the entry."""
 
1921
        state, entry = self.get_state_with_a()
 
1922
        # The file sha1 won't be cached unless the file is old
 
1923
        state.adjust_time(+10)
 
1924
        self.create_and_test_dir(state, entry)
 
1925
        os.rmdir('a')
 
1926
        self.create_and_test_file(state, entry)
 
1927
 
 
1928
    def test_update_dir_to_symlink(self):
 
1929
        """Directory becomes a symlink"""
 
1930
        self.requireFeature(SymlinkFeature)
 
1931
        state, entry = self.get_state_with_a()
 
1932
        # The symlink target won't be cached if it isn't old
 
1933
        state.adjust_time(+10)
 
1934
        self.create_and_test_dir(state, entry)
 
1935
        os.rmdir('a')
 
1936
        self.create_and_test_symlink(state, entry)
 
1937
 
 
1938
    def test_update_symlink_to_file(self):
 
1939
        """Symlink becomes a file"""
 
1940
        self.requireFeature(SymlinkFeature)
 
1941
        state, entry = self.get_state_with_a()
 
1942
        # The symlink and file info won't be cached unless old
 
1943
        state.adjust_time(+10)
 
1944
        self.create_and_test_symlink(state, entry)
 
1945
        os.remove('a')
 
1946
        self.create_and_test_file(state, entry)
 
1947
 
 
1948
    def test_update_symlink_to_dir(self):
 
1949
        """Symlink becomes a directory"""
 
1950
        self.requireFeature(SymlinkFeature)
 
1951
        state, entry = self.get_state_with_a()
 
1952
        # The symlink target won't be cached if it isn't old
 
1953
        state.adjust_time(+10)
 
1954
        self.create_and_test_symlink(state, entry)
 
1955
        os.remove('a')
 
1956
        self.create_and_test_dir(state, entry)
 
1957
 
 
1958
    def test__is_executable_win32(self):
 
1959
        state, entry = self.get_state_with_a()
 
1960
        self.build_tree(['a'])
 
1961
 
 
1962
        # Make sure we are using the win32 implementation of _is_executable
 
1963
        state._is_executable = state._is_executable_win32
 
1964
 
 
1965
        # The file on disk is not executable, but we are marking it as though
 
1966
        # it is. With _is_executable_win32 we ignore what is on disk.
 
1967
        entry[1][0] = ('f', '', 0, True, dirstate.DirState.NULLSTAT)
 
1968
 
 
1969
        stat_value = os.lstat('a')
 
1970
        packed_stat = dirstate.pack_stat(stat_value)
 
1971
 
 
1972
        state.adjust_time(-10) # Make sure everything is new
 
1973
        state.update_entry(entry, abspath='a', stat_value=stat_value)
 
1974
 
 
1975
        # The row is updated, but the executable bit stays set.
 
1976
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
 
1977
                         entry[1])
 
1978
 
 
1979
        # Make the disk object look old enough to cache
 
1980
        state.adjust_time(+20)
 
1981
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1982
        state.update_entry(entry, abspath='a', stat_value=stat_value)
 
1983
        self.assertEqual([('f', digest, 14, True, packed_stat)], entry[1])
 
1984
 
 
1985
 
 
1986
class TestPackStat(TestCaseWithTransport):
1761
1987
 
1762
1988
    def assertPackStat(self, expected, stat_value):
1763
1989
        """Check the packed and serialized form of a stat value."""
2288
2514
        state._discard_merge_parents()
2289
2515
        state._validate()
2290
2516
        self.assertEqual(exp_dirblocks, state._dirblocks)
2291
 
 
2292
 
 
2293
 
class Test_InvEntryToDetails(tests.TestCase):
2294
 
 
2295
 
    def assertDetails(self, expected, inv_entry):
2296
 
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2297
 
        self.assertEqual(expected, details)
2298
 
        # details should always allow join() and always be a plain str when
2299
 
        # finished
2300
 
        (minikind, fingerprint, size, executable, tree_data) = details
2301
 
        self.assertIsInstance(minikind, str)
2302
 
        self.assertIsInstance(fingerprint, str)
2303
 
        self.assertIsInstance(tree_data, str)
2304
 
 
2305
 
    def test_unicode_symlink(self):
2306
 
        inv_entry = inventory.InventoryLink('link-file-id',
2307
 
                                            u'nam\N{Euro Sign}e',
2308
 
                                            'link-parent-id')
2309
 
        inv_entry.revision = 'link-revision-id'
2310
 
        target = u'link-targ\N{Euro Sign}t'
2311
 
        inv_entry.symlink_target = target
2312
 
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2313
 
                            'link-revision-id'), inv_entry)
2314
 
 
2315
 
 
2316
 
class TestSHA1Provider(tests.TestCaseInTempDir):
2317
 
 
2318
 
    def test_sha1provider_is_an_interface(self):
2319
 
        p = dirstate.SHA1Provider()
2320
 
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2321
 
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2322
 
 
2323
 
    def test_defaultsha1provider_sha1(self):
2324
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2325
 
        self.build_tree_contents([('foo', text)])
2326
 
        expected_sha = osutils.sha_string(text)
2327
 
        p = dirstate.DefaultSHA1Provider()
2328
 
        self.assertEqual(expected_sha, p.sha1('foo'))
2329
 
 
2330
 
    def test_defaultsha1provider_stat_and_sha1(self):
2331
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2332
 
        self.build_tree_contents([('foo', text)])
2333
 
        expected_sha = osutils.sha_string(text)
2334
 
        p = dirstate.DefaultSHA1Provider()
2335
 
        statvalue, sha1 = p.stat_and_sha1('foo')
2336
 
        self.assertTrue(len(statvalue) >= 10)
2337
 
        self.assertEqual(len(text), statvalue.st_size)
2338
 
        self.assertEqual(expected_sha, sha1)