/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

Implement _bisect_recursive, which uses multiple bisect calls to
handle renames and finding entries in subdirs.
As is, this could be hooked into paths2ids() if the dirstate has not been loaded yet.
However, it doesn't quite provide enough, since the parsed entries would probably not
be saved. Further, the multiple bisect calls are less efficient then they could be,
because they do not remember the last bisect call.
We should explore switching to a caching structure, which maintains all records that
have been processed, in a structure that can be in-memory searched before going back
to disk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1032
1032
        This takes the basic dirstate, and moves the paths around.
1033
1033
        """
1034
1034
        tree, state, expected = self.create_basic_dirstate()
 
1035
        # Rename a file
1035
1036
        tree.rename_one('a', 'b/g')
 
1037
        # And a directory
 
1038
        tree.rename_one('b/d', 'h')
1036
1039
 
1037
1040
        old_a = expected['a']
1038
1041
        expected['a'] = (old_a[0], [('r', 'b/g', 0, False, ''), old_a[1][1]])
1039
1042
        expected['b/g'] = (('b', 'g', 'a-id'), [old_a[1][0],
1040
1043
                                                ('r', 'a', 0, False, '')])
 
1044
        old_d = expected['b/d']
 
1045
        expected['b/d'] = (old_d[0], [('r', 'h', 0, False, ''), old_d[1][1]])
 
1046
        expected['h'] = (('', 'h', 'd-id'), [old_d[1][0],
 
1047
                                             ('r', 'b/d', 0, False, '')])
 
1048
 
 
1049
        old_e = expected['b/d/e']
 
1050
        expected['b/d/e'] = (old_e[0], [('r', 'h/e', 0, False, ''),
 
1051
                             old_e[1][1]])
 
1052
        expected['h/e'] = (('h', 'e', 'e-id'), [old_e[1][0],
 
1053
                                                ('r', 'b/d/e', 0, False, '')])
 
1054
 
1041
1055
        state.unlock()
1042
1056
        try:
1043
1057
            new_state = dirstate.DirState.from_tree(tree, 'dirstate')
1082
1096
    def assertBisectDirBlocks(self, expected_map, map_keys, state, paths):
1083
1097
        """Assert that bisecting for dirbblocks returns the right result.
1084
1098
 
1085
 
        :param expected: A map from path => expected values
 
1099
        :param expected_map: A map from key => expected values
1086
1100
        :param map_keys: A nested list of paths we expect to be returned.
1087
1101
            Something like [['a', 'b', 'f'], ['b/c', 'b/d']]
1088
1102
        :param state: The DirState object.
1102
1116
 
1103
1117
        self.assertEqual(expected, result)
1104
1118
 
 
1119
    def assertBisectRecursive(self, expected_map, map_keys, state, paths):
 
1120
        """Assert the return value of a recursive bisection.
 
1121
 
 
1122
        :param expected_map: A map from key => entry value
 
1123
        :param map_keys: A list of paths we expect to be returned.
 
1124
            Something like ['a', 'b', 'f', 'b/d', 'b/d2']
 
1125
        :param state: The DirState object.
 
1126
        :param paths: A list of files and directories. It will be broken up
 
1127
            into (dir, name) pairs and sorted before calling _bisect_recursive.
 
1128
        """
 
1129
        expected = {}
 
1130
        for key in map_keys:
 
1131
            entry = expected_map[key]
 
1132
            dir_name_id, trees_info = entry
 
1133
            expected[dir_name_id] = trees_info
 
1134
 
 
1135
        dir_names = sorted(osutils.split(p) for p in paths)
 
1136
        result = state._bisect_recursive(dir_names)
 
1137
 
 
1138
        self.assertEqual(expected, result)
 
1139
 
1105
1140
    def test_bisect_each(self):
1106
1141
        """Find a single record using bisect."""
1107
1142
        tree, state, expected = self.create_basic_dirstate()
1185
1220
        # Search for the pre and post renamed entries
1186
1221
        self.assertBisect(expected, [['a']], state, ['a'])
1187
1222
        self.assertBisect(expected, [['b/g']], state, ['b/g'])
 
1223
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
 
1224
        self.assertBisect(expected, [['h']], state, ['h'])
 
1225
 
 
1226
        # What about b/d/e? shouldn't that also get 2 directory entries?
 
1227
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
 
1228
        self.assertBisect(expected, [['h/e']], state, ['h/e'])
1188
1229
 
1189
1230
    def test_bisect_dirblocks(self):
1190
1231
        tree, state, expected = self.create_duplicated_dirstate()
1210
1251
        self.assertBisectDirBlocks(expected, [None], state, ['c'])
1211
1252
        self.assertBisectDirBlocks(expected, [None], state, ['b/d/e'])
1212
1253
        self.assertBisectDirBlocks(expected, [None], state, ['f'])
 
1254
 
 
1255
    def test_bisect_recursive_each(self):
 
1256
        tree, state, expected = self.create_basic_dirstate()
 
1257
        self.assertBisectRecursive(expected, ['a'], state, ['a'])
 
1258
        self.assertBisectRecursive(expected, ['b/c'], state, ['b/c'])
 
1259
        self.assertBisectRecursive(expected, ['b/d/e'], state, ['b/d/e'])
 
1260
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
 
1261
                                   state, ['b/d'])
 
1262
        self.assertBisectRecursive(expected, ['b', 'b/c', 'b/d', 'b/d/e'],
 
1263
                                   state, ['b'])
 
1264
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'f', 'b/c',
 
1265
                                              'b/d', 'b/d/e'],
 
1266
                                   state, [''])
 
1267
 
 
1268
    def test_bisect_recursive_multiple(self):
 
1269
        tree, state, expected = self.create_basic_dirstate()
 
1270
        self.assertBisectRecursive(expected, ['a', 'b/c'], state, ['a', 'b/c'])
 
1271
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
 
1272
                                   state, ['b/d', 'b/d/e'])
 
1273
 
 
1274
    def test_bisect_recursive_missing(self):
 
1275
        tree, state, expected = self.create_basic_dirstate()
 
1276
        self.assertBisectRecursive(expected, [], state, ['d'])
 
1277
        self.assertBisectRecursive(expected, [], state, ['b/e'])
 
1278
        self.assertBisectRecursive(expected, [], state, ['g'])
 
1279
        self.assertBisectRecursive(expected, ['a'], state, ['a', 'g'])
 
1280
 
 
1281
    def test_bisect_recursive_renamed(self):
 
1282
        tree, state, expected = self.create_renamed_dirstate()
 
1283
 
 
1284
        # Looking for either renamed item should find the other
 
1285
        self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['a'])
 
1286
        self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['b/g'])
 
1287
        # Looking in the containing directory should find the rename target,
 
1288
        # and anything in a subdir of the renamed target.
 
1289
        self.assertBisectRecursive(expected, ['a', 'b', 'b/c', 'b/d',
 
1290
                                              'b/d/e', 'b/g', 'h', 'h/e'],
 
1291
                                   state, ['b'])
 
1292