~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Ian Clatworthy
  • Date: 2008-03-27 07:51:10 UTC
  • mto: (3311.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3312.
  • Revision ID: ian.clatworthy@canonical.com-20080327075110-afgd7x03ybju06ez
Reduce evangelism in the User Guide

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')
180
162
        """
181
163
        # The state should already be write locked, since we just had to do
182
164
        # some operation to get here.
183
 
        self.assertTrue(state._lock_token is not None)
 
165
        assert state._lock_token is not None
184
166
        try:
185
167
            self.assertEqual(expected_result[0],  state.get_parent_ids())
186
168
            # there should be no ghosts in this tree.
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()
762
742
        # https://bugs.launchpad.net/bzr/+bug/146176
763
743
        # set_state_from_inventory should preserve the stat and hash value for
764
744
        # workingtree files that are not changed by the inventory.
765
 
 
 
745
       
766
746
        tree = self.make_branch_and_tree('.')
767
747
        # depends on the default format using dirstate...
768
748
        tree.lock_write()
769
749
        try:
770
 
            # make a dirstate with some valid hashcache data
 
750
            # make a dirstate with some valid hashcache data 
771
751
            # file on disk, but that's not needed for this test
772
752
            foo_contents = 'contents of foo'
773
753
            self.build_tree_contents([('foo', foo_contents)])
793
773
                (('', 'foo', 'foo-id',),
794
774
                 [('f', foo_sha, foo_size, False, foo_packed)]),
795
775
                tree._dirstate._get_entry(0, 'foo-id'))
796
 
 
 
776
           
797
777
            # extract the inventory, and add something to it
798
778
            inv = tree._get_inventory()
799
779
            # should see the file we poked in...
821
801
        finally:
822
802
            tree.unlock()
823
803
 
 
804
 
824
805
    def test_set_state_from_inventory_mixed_paths(self):
825
806
        tree1 = self.make_branch_and_tree('tree1')
826
807
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
867
848
        state = dirstate.DirState.initialize('dirstate')
868
849
        try:
869
850
            # check precondition to be sure the state does change appropriately.
870
 
            root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
871
 
            self.assertEqual([root_entry], list(state._iter_entries()))
872
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
873
 
            self.assertEqual(root_entry,
874
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
875
 
            self.assertEqual((None, None),
876
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
877
 
            state.set_path_id('', 'second-root-id')
878
 
            new_root_entry = (('', '', 'second-root-id'),
879
 
                              [('d', '', 0, False, 'x'*32)])
880
 
            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')])]
881
859
            self.assertEqual(expected_rows, list(state._iter_entries()))
882
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
883
 
            self.assertEqual(new_root_entry, 
884
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
885
 
            self.assertEqual((None, None),
886
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
887
860
            # should work across save too
888
861
            state.save()
889
862
        finally:
907
880
        state._validate()
908
881
        try:
909
882
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
910
 
            root_entry = (('', '', 'TREE_ROOT'),
911
 
                          [('d', '', 0, False, 'x'*32),
912
 
                           ('d', '', 0, False, 'parent-revid')])
913
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
914
 
            self.assertEqual(root_entry,
915
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
916
 
            self.assertEqual((None, None),
917
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
918
 
            state.set_path_id('', 'Asecond-root-id')
 
883
            state.set_path_id('', 'foobarbaz')
919
884
            state._validate()
920
885
            # now see that it is what we expected
921
 
            old_root_entry = (('', '', 'TREE_ROOT'),
922
 
                              [('a', '', 0, False, ''),
923
 
                               ('d', '', 0, False, 'parent-revid')])
924
 
            new_root_entry = (('', '', 'Asecond-root-id'),
925
 
                              [('d', '', 0, False, ''),
926
 
                               ('a', '', 0, False, '')])
927
 
            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
                ]
928
896
            state._validate()
929
897
            self.assertEqual(expected_rows, list(state._iter_entries()))
930
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
931
 
            self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
932
 
            self.assertEqual((None, None),
933
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
934
 
            self.assertEqual(old_root_entry,
935
 
                             state._get_entry(1, fileid_utf8='TREE_ROOT'))
936
 
            self.assertEqual(new_root_entry,
937
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
938
 
            self.assertEqual((None, None),
939
 
                             state._get_entry(1, fileid_utf8='Asecond-root-id'))
940
898
            # should work across save too
941
899
            state.save()
942
900
        finally:
958
916
        finally:
959
917
            state.unlock()
960
918
 
 
919
 
961
920
    def test_set_parent_trees_no_content(self):
962
921
        # set_parent_trees is a slow but important api to support.
963
922
        tree1 = self.make_branch_and_memory_tree('tree1')
968
927
        finally:
969
928
            tree1.unlock()
970
929
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
971
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
930
        tree2 = MemoryTree.create_on_branch(branch2)
972
931
        tree2.lock_write()
973
932
        try:
974
933
            revid2 = tree2.commit('foo')
1006
965
            state.set_parent_trees(
1007
966
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
1008
967
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
1009
 
                 ('ghost-rev', tree2.branch.repository.revision_tree(
1010
 
                                   _mod_revision.NULL_REVISION))),
 
968
                 ('ghost-rev', tree2.branch.repository.revision_tree(None))),
1011
969
                ['ghost-rev'])
1012
970
            self.assertEqual([revid1, revid2, 'ghost-rev'],
1013
971
                             state.get_parent_ids())
1017
975
                [(('', '', root_id), [
1018
976
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1019
977
                  ('d', '', 0, False, revid1),
1020
 
                  ('d', '', 0, False, revid1)
 
978
                  ('d', '', 0, False, revid2)
1021
979
                  ])],
1022
980
                list(state._iter_entries()))
1023
981
        finally:
1038
996
        finally:
1039
997
            tree1.unlock()
1040
998
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1041
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
999
        tree2 = MemoryTree.create_on_branch(branch2)
1042
1000
        tree2.lock_write()
1043
1001
        try:
1044
1002
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1051
1009
            (('', '', root_id), [
1052
1010
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1053
1011
             ('d', '', 0, False, revid1.encode('utf8')),
1054
 
             ('d', '', 0, False, revid1.encode('utf8'))
 
1012
             ('d', '', 0, False, revid2.encode('utf8'))
1055
1013
             ]),
1056
1014
            (('', 'a file', 'file-id'), [
1057
1015
             ('a', '', 0, False, ''),
1103
1061
            state.unlock()
1104
1062
        state = dirstate.DirState.on_file('dirstate')
1105
1063
        state.lock_read()
1106
 
        self.addCleanup(state.unlock)
1107
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1064
        try:
 
1065
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1066
        finally:
 
1067
            state.unlock()
1108
1068
 
1109
1069
    def test_add_path_to_unversioned_directory(self):
1110
1070
        """Adding a path to an unversioned directory should error.
1115
1075
        """
1116
1076
        self.build_tree(['unversioned/', 'unversioned/a file'])
1117
1077
        state = dirstate.DirState.initialize('dirstate')
1118
 
        self.addCleanup(state.unlock)
1119
 
        self.assertRaises(errors.NotVersionedError, state.add,
1120
 
                          '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()
1121
1083
 
1122
1084
    def test_add_directory_to_root_no_parents_all_data(self):
1123
1085
        # The most trivial addition of a dir is when there are no parents and
1143
1105
            state.unlock()
1144
1106
        state = dirstate.DirState.on_file('dirstate')
1145
1107
        state.lock_read()
1146
 
        self.addCleanup(state.unlock)
1147
1108
        state._validate()
1148
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1109
        try:
 
1110
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1111
        finally:
 
1112
            state.unlock()
1149
1113
 
1150
 
    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):
1151
1115
        # The most trivial addition of a symlink when there are no parents and
1152
1116
        # its in the root and all data about the file is supplied
1153
1117
        # bzr doesn't support fake symlinks on windows, yet.
1154
 
        self.requireFeature(tests.SymlinkFeature)
1155
 
        os.symlink(target, link_name)
1156
 
        stat = os.lstat(link_name)
 
1118
        self.requireFeature(SymlinkFeature)
 
1119
        os.symlink('target', 'a link')
 
1120
        stat = os.lstat('a link')
1157
1121
        expected_entries = [
1158
1122
            (('', '', 'TREE_ROOT'), [
1159
1123
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1160
1124
             ]),
1161
 
            (('', link_name.encode('UTF-8'), 'a link id'), [
1162
 
             ('l', target.encode('UTF-8'), stat[6],
1163
 
              False, dirstate.pack_stat(stat)), # current tree
 
1125
            (('', 'a link', 'a link id'), [
 
1126
             ('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1164
1127
             ]),
1165
1128
            ]
1166
1129
        state = dirstate.DirState.initialize('dirstate')
1167
1130
        try:
1168
 
            state.add(link_name, 'a link id', 'symlink', stat,
1169
 
                      target.encode('UTF-8'))
 
1131
            state.add('a link', 'a link id', 'symlink', stat, 'target')
1170
1132
            # having added it, it should be in the output of iter_entries.
1171
1133
            self.assertEqual(expected_entries, list(state._iter_entries()))
1172
1134
            # saving and reloading should not affect this.
1175
1137
            state.unlock()
1176
1138
        state = dirstate.DirState.on_file('dirstate')
1177
1139
        state.lock_read()
1178
 
        self.addCleanup(state.unlock)
1179
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
1180
 
 
1181
 
    def test_add_symlink_to_root_no_parents_all_data(self):
1182
 
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1183
 
 
1184
 
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1185
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1186
 
        self._test_add_symlink_to_root_no_parents_all_data(
1187
 
            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()
1188
1144
 
1189
1145
    def test_add_directory_and_child_no_parents_all_data(self):
1190
1146
        # after adding a directory, we should be able to add children to it.
1215
1171
            state.unlock()
1216
1172
        state = dirstate.DirState.on_file('dirstate')
1217
1173
        state.lock_read()
1218
 
        self.addCleanup(state.unlock)
1219
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1174
        try:
 
1175
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1176
        finally:
 
1177
            state.unlock()
1220
1178
 
1221
1179
    def test_add_tree_reference(self):
1222
1180
        # make a dirstate and add a tree reference
1236
1194
            state.unlock()
1237
1195
        # now check we can read it back
1238
1196
        state.lock_read()
1239
 
        self.addCleanup(state.unlock)
1240
1197
        state._validate()
1241
 
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1242
 
        self.assertEqual(entry, entry2)
1243
 
        self.assertEqual(entry, expected_entry)
1244
 
        # and lookup by id should work too
1245
 
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1246
 
        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()
1247
1207
 
1248
1208
    def test_add_forbidden_names(self):
1249
1209
        state = dirstate.DirState.initialize('dirstate')
1253
1213
        self.assertRaises(errors.BzrError,
1254
1214
            state.add, '..', 'ass-id', 'directory', None, None)
1255
1215
 
1256
 
    def test_set_state_with_rename_b_a_bug_395556(self):
1257
 
        # bug 395556 uncovered a bug where the dirstate ends up with a false
1258
 
        # relocation record - in a tree with no parents there should be no
1259
 
        # absent or relocated records. This then leads to further corruption
1260
 
        # when a commit occurs, as the incorrect relocation gathers an
1261
 
        # incorrect absent in tree 1, and future changes go to pot.
1262
 
        tree1 = self.make_branch_and_tree('tree1')
1263
 
        self.build_tree(['tree1/b'])
1264
 
        tree1.lock_write()
1265
 
        try:
1266
 
            tree1.add(['b'], ['b-id'])
1267
 
            root_id = tree1.get_root_id()
1268
 
            inv = tree1.inventory
1269
 
            state = dirstate.DirState.initialize('dirstate')
1270
 
            try:
1271
 
                # Set the initial state with 'b'
1272
 
                state.set_state_from_inventory(inv)
1273
 
                inv.rename('b-id', root_id, 'a')
1274
 
                # Set the new state with 'a', which currently corrupts.
1275
 
                state.set_state_from_inventory(inv)
1276
 
                expected_result1 = [('', '', root_id, 'd'),
1277
 
                                    ('', 'a', 'b-id', 'f'),
1278
 
                                   ]
1279
 
                values = []
1280
 
                for entry in state._iter_entries():
1281
 
                    values.append(entry[0] + entry[1][0][:1])
1282
 
                self.assertEqual(expected_result1, values)
1283
 
            finally:
1284
 
                state.unlock()
1285
 
        finally:
1286
 
            tree1.unlock()
1287
 
 
1288
1216
 
1289
1217
class TestGetLines(TestCaseWithDirState):
1290
1218
 
1523
1451
        There is one parent tree, which has the same shape with the following variations:
1524
1452
        b/g in the parent is gone.
1525
1453
        b/h in the parent has a different id
1526
 
        b/i is new in the parent
 
1454
        b/i is new in the parent 
1527
1455
        c is renamed to b/j in the parent
1528
1456
 
1529
1457
        :return: The dirstate, still write-locked.
1619
1547
            list(state._iter_child_entries(1, '')))
1620
1548
 
1621
1549
 
1622
 
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1550
class TestDirstateSortOrder(TestCaseWithTransport):
1623
1551
    """Test that DirState adds entries in the right order."""
1624
1552
 
1625
1553
    def test_add_sorting(self):
1674
1602
 
1675
1603
        # *really* cheesy way to just get an empty tree
1676
1604
        repo = self.make_repository('repo')
1677
 
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1605
        empty_tree = repo.revision_tree(None)
1678
1606
        state.set_parent_trees([('null:', empty_tree)], [])
1679
1607
 
1680
1608
        dirblock_names = [d[0] for d in state._dirblocks]
1684
1612
class InstrumentedDirState(dirstate.DirState):
1685
1613
    """An DirState with instrumented sha1 functionality."""
1686
1614
 
1687
 
    def __init__(self, path, sha1_provider):
1688
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
 
1615
    def __init__(self, path):
 
1616
        super(InstrumentedDirState, self).__init__(path)
1689
1617
        self._time_offset = 0
1690
1618
        self._log = []
1691
1619
        # member is dynamically set in DirState.__init__ to turn on trace
1692
 
        self._sha1_provider = sha1_provider
1693
1620
        self._sha1_file = self._sha1_file_and_log
1694
1621
 
1695
1622
    def _sha_cutoff_time(self):
1698
1625
 
1699
1626
    def _sha1_file_and_log(self, abspath):
1700
1627
        self._log.append(('sha1', abspath))
1701
 
        return self._sha1_provider.sha1(abspath)
 
1628
        return osutils.sha_file_by_name(abspath)
1702
1629
 
1703
1630
    def _read_link(self, abspath, old_link):
1704
1631
        self._log.append(('read_link', abspath, old_link))
1735
1662
        self.st_ino = ino
1736
1663
        self.st_mode = mode
1737
1664
 
1738
 
    @staticmethod
1739
 
    def from_stat(st):
1740
 
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1741
 
            st.st_ino, st.st_mode)
1742
 
 
1743
 
 
1744
 
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):
1745
1987
 
1746
1988
    def assertPackStat(self, expected, stat_value):
1747
1989
        """Check the packed and serialized form of a stat value."""
1812
2054
        # the end it would still be fairly arbitrary, and we don't want the
1813
2055
        # extra overhead if we can avoid it. So sort everything to make sure
1814
2056
        # equality is true
1815
 
        self.assertEqual(len(map_keys), len(paths))
 
2057
        assert len(map_keys) == len(paths)
1816
2058
        expected = {}
1817
2059
        for path, keys in zip(paths, map_keys):
1818
2060
            if keys is None:
1837
2079
        :param paths: A list of directories
1838
2080
        """
1839
2081
        result = state._bisect_dirblocks(paths)
1840
 
        self.assertEqual(len(map_keys), len(paths))
 
2082
        assert len(map_keys) == len(paths)
 
2083
 
1841
2084
        expected = {}
1842
2085
        for path, keys in zip(paths, map_keys):
1843
2086
            if keys is None:
2272
2515
        state._discard_merge_parents()
2273
2516
        state._validate()
2274
2517
        self.assertEqual(exp_dirblocks, state._dirblocks)
2275
 
 
2276
 
 
2277
 
class Test_InvEntryToDetails(tests.TestCase):
2278
 
 
2279
 
    def assertDetails(self, expected, inv_entry):
2280
 
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2281
 
        self.assertEqual(expected, details)
2282
 
        # details should always allow join() and always be a plain str when
2283
 
        # finished
2284
 
        (minikind, fingerprint, size, executable, tree_data) = details
2285
 
        self.assertIsInstance(minikind, str)
2286
 
        self.assertIsInstance(fingerprint, str)
2287
 
        self.assertIsInstance(tree_data, str)
2288
 
 
2289
 
    def test_unicode_symlink(self):
2290
 
        inv_entry = inventory.InventoryLink('link-file-id',
2291
 
                                            u'nam\N{Euro Sign}e',
2292
 
                                            'link-parent-id')
2293
 
        inv_entry.revision = 'link-revision-id'
2294
 
        target = u'link-targ\N{Euro Sign}t'
2295
 
        inv_entry.symlink_target = target
2296
 
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2297
 
                            'link-revision-id'), inv_entry)
2298
 
 
2299
 
 
2300
 
class TestSHA1Provider(tests.TestCaseInTempDir):
2301
 
 
2302
 
    def test_sha1provider_is_an_interface(self):
2303
 
        p = dirstate.SHA1Provider()
2304
 
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2305
 
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2306
 
 
2307
 
    def test_defaultsha1provider_sha1(self):
2308
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2309
 
        self.build_tree_contents([('foo', text)])
2310
 
        expected_sha = osutils.sha_string(text)
2311
 
        p = dirstate.DefaultSHA1Provider()
2312
 
        self.assertEqual(expected_sha, p.sha1('foo'))
2313
 
 
2314
 
    def test_defaultsha1provider_stat_and_sha1(self):
2315
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2316
 
        self.build_tree_contents([('foo', text)])
2317
 
        expected_sha = osutils.sha_string(text)
2318
 
        p = dirstate.DefaultSHA1Provider()
2319
 
        statvalue, sha1 = p.stat_and_sha1('foo')
2320
 
        self.assertTrue(len(statvalue) >= 10)
2321
 
        self.assertEqual(len(text), statvalue.st_size)
2322
 
        self.assertEqual(expected_sha, sha1)