~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__dirstate_helpers.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-06 06:48:25 UTC
  • mfrom: (4070.8.6 debug-config)
  • Revision ID: pqm@pqm.ubuntu.com-20090306064825-kbpwggw21dygeix6
(mbp) debug_flags configuration option

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 for the compiled dirstate helpers."""
18
18
 
23
23
from bzrlib import (
24
24
    dirstate,
25
25
    errors,
26
 
    osutils,
27
26
    tests,
28
27
    )
29
28
from bzrlib.tests import (
1021
1020
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1022
1021
                         state._dirblock_state)
1023
1022
 
1024
 
    def test_update_entry_tree_reference(self):
1025
 
        self.set_update_entry()
1026
 
        state = test_dirstate.InstrumentedDirState.initialize('dirstate')
1027
 
        self.addCleanup(state.unlock)
1028
 
        state.add('r', 'r-id', 'tree-reference', None, '')
1029
 
        self.build_tree(['r/'])
1030
 
        entry = state._get_entry(0, path_utf8='r')
1031
 
        self.do_update_entry(state, entry, 'r')
1032
 
        entry = state._get_entry(0, path_utf8='r')
1033
 
        self.assertEqual('t', entry[1][0][0])
1034
 
 
1035
1023
    def create_and_test_file(self, state, entry):
1036
1024
        """Create a file at 'a' and verify the state finds it during update.
1037
1025
 
1174
1162
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1175
1163
            entry[1])
1176
1164
 
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(dirstate.SHA1Provider):
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
 
 
1230
1165
 
1231
1166
class TestCompiledUpdateEntry(TestUpdateEntry):
1232
1167
    """Test the pyrex implementation of _read_dirblocks"""
1236
1171
    def set_update_entry(self):
1237
1172
        from bzrlib._dirstate_helpers_c import update_entry
1238
1173
        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