~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__dirstate_helpers.py

make sha1_provider a parameter to DirState()

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib import (
24
24
    dirstate,
25
25
    errors,
 
26
    osutils,
26
27
    tests,
27
28
    )
28
29
from bzrlib.tests import (
1173
1174
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1174
1175
            entry[1])
1175
1176
 
 
1177
    def _prepare_tree(self):
 
1178
        # Create a tree
 
1179
        text = 'Hello World\n'
 
1180
        tree = self.make_branch_and_tree('tree')
 
1181
        self.build_tree_contents([('tree/a file', text)])
 
1182
        tree.add('a file', 'a-file-id')
 
1183
        # Note: dirstate does not sha prior to the first commit
 
1184
        # so commit now in order for the test to work
 
1185
        tree.commit('first')
 
1186
        return tree, text
 
1187
 
 
1188
    def test_sha1provider_sha1_used(self):
 
1189
        tree, text = self._prepare_tree()
 
1190
        state = dirstate.DirState.from_tree(tree, 'dirstate',
 
1191
            UppercaseSHA1Provider())
 
1192
        self.addCleanup(state.unlock)
 
1193
        expected_sha = osutils.sha_string(text.upper() + "foo")
 
1194
        entry = state._get_entry(0, path_utf8='a file')
 
1195
        state._sha_cutoff_time()
 
1196
        state._cutoff_time += 10
 
1197
        sha1 = dirstate.update_entry(state, entry, 'tree/a file',
 
1198
            os.lstat('tree/a file'))
 
1199
        self.assertEqual(expected_sha, sha1)
 
1200
 
 
1201
    def test_sha1provider_stat_and_sha1_used(self):
 
1202
        tree, text = self._prepare_tree()
 
1203
        tree.lock_write()
 
1204
        self.addCleanup(tree.unlock)
 
1205
        state = tree._current_dirstate()
 
1206
        state._sha1_provider = UppercaseSHA1Provider()
 
1207
        # If we used the standard provider, it would look like nothing has
 
1208
        # changed
 
1209
        file_ids_changed = [change[0] for change 
 
1210
                in tree.iter_changes(tree.basis_tree())]
 
1211
        self.assertEqual(['a-file-id'], file_ids_changed)
 
1212
 
 
1213
 
 
1214
class UppercaseSHA1Provider():
 
1215
    """A custom SHA1Provider."""
 
1216
 
 
1217
    def sha1(self, abspath):
 
1218
        return self.stat_and_sha1(abspath)[1]
 
1219
 
 
1220
    def stat_and_sha1(self, abspath):
 
1221
        file_obj = file(abspath, 'rb')
 
1222
        try:
 
1223
            statvalue = os.fstat(file_obj.fileno())
 
1224
            text = ''.join(file_obj.readlines())
 
1225
            sha1 = osutils.sha_string(text.upper() + "foo")
 
1226
        finally:
 
1227
            file_obj.close()
 
1228
        return statvalue, sha1
 
1229
 
1176
1230
 
1177
1231
class TestCompiledUpdateEntry(TestUpdateEntry):
1178
1232
    """Test the pyrex implementation of _read_dirblocks"""
1182
1236
    def set_update_entry(self):
1183
1237
        from bzrlib._dirstate_helpers_c import update_entry
1184
1238
        self.update_entry = update_entry
 
1239
 
 
1240
 
 
1241
class TestProcessEntryPython(test_dirstate.TestCaseWithDirState):
 
1242
 
 
1243
    def setUp(self):
 
1244
        super(TestProcessEntryPython, self).setUp()
 
1245
        self.setup_process_entry()
 
1246
 
 
1247
    def setup_process_entry(self):
 
1248
        from bzrlib import dirstate
 
1249
        orig = dirstate._process_entry
 
1250
        def cleanup():
 
1251
            dirstate._process_entry = orig
 
1252
        self.addCleanup(cleanup)
 
1253
        dirstate._process_entry = dirstate.ProcessEntryPython
 
1254
 
 
1255
    def assertChangedFileIds(self, expected, tree):
 
1256
        tree.lock_read()
 
1257
        try:
 
1258
            file_ids = [info[0] for info
 
1259
                        in tree.iter_changes(tree.basis_tree())]
 
1260
        finally:
 
1261
            tree.unlock()
 
1262
        self.assertEqual(sorted(expected), sorted(file_ids))
 
1263
 
 
1264
    def test_simple_changes(self):
 
1265
        tree = self.make_branch_and_tree('tree')
 
1266
        self.build_tree(['tree/file'])
 
1267
        tree.add(['file'], ['file-id'])
 
1268
        self.assertChangedFileIds([tree.get_root_id(), 'file-id'], tree)
 
1269
        tree.commit('one')
 
1270
        self.assertChangedFileIds([], tree)
 
1271
 
 
1272
    def test_sha1provider_stat_and_sha1_used(self):
 
1273
        tree = self.make_branch_and_tree('tree')
 
1274
        self.build_tree(['tree/file'])
 
1275
        tree.add(['file'], ['file-id'])
 
1276
        tree.commit('one')
 
1277
        tree.lock_write()
 
1278
        self.addCleanup(tree.unlock)
 
1279
        state = tree._current_dirstate()
 
1280
        state._sha1_provider = UppercaseSHA1Provider()
 
1281
        self.assertChangedFileIds(['file-id'], tree)
 
1282
 
 
1283
 
 
1284
class TestProcessEntryC(TestProcessEntryPython):
 
1285
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
1286
 
 
1287
    def setup_process_entry(self):
 
1288
        from bzrlib import _dirstate_helpers_c
 
1289
        orig = dirstate._process_entry
 
1290
        def cleanup():
 
1291
            dirstate._process_entry = orig
 
1292
        self.addCleanup(cleanup)
 
1293
        dirstate._process_entry = _dirstate_helpers_c.ProcessEntryC
 
1294