23
23
from bzrlib import (
28
29
from bzrlib.tests import (
31
from bzrlib.tests import test_dirstate
35
from bzrlib import _dirstate_helpers_c
36
has_dirstate_helpers_c = True
38
has_dirstate_helpers_c = False
34
41
class _CompiledDirstateHelpersFeature(tests.Feature):
37
import bzrlib._dirstate_helpers_c
43
return has_dirstate_helpers_c
42
45
def feature_name(self):
43
46
return 'bzrlib._dirstate_helpers_c'
45
47
CompiledDirstateHelpersFeature = _CompiledDirstateHelpersFeature()
50
def load_tests(basic_tests, module, loader):
51
# FIXME: we should also parametrize against SHA1Provider !
52
suite = loader.suiteClass()
53
remaining_tests = basic_tests
55
dir_reader_scenarios = test_osutils.dir_reader_scenarios()
57
ue_scenarios = [('dirstate_Python',
58
{'update_entry': dirstate.py_update_entry})]
59
if has_dirstate_helpers_c:
60
c_scenario = ('dirstate_C',
61
{'update_entry': _dirstate_helpers_c.update_entry})
62
ue_scenarios.append(c_scenario)
63
process_entry_tests, remaining_tests = tests.split_suite_by_condition(
64
remaining_tests, tests.condition_isinstance(TestUpdateEntry))
65
tests.multiply_tests(process_entry_tests,
66
tests.multiply_scenarios(dir_reader_scenarios,
70
pe_scenarios = [('dirstate_Python',
71
{'_process_entry': dirstate.ProcessEntryPython})]
72
if has_dirstate_helpers_c:
73
c_scenario = ('dirstate_C',
74
{'_process_entry': _dirstate_helpers_c.ProcessEntryC})
75
pe_scenarios.append(c_scenario)
76
process_entry_tests, remaining_tests = tests.split_suite_by_condition(
77
remaining_tests, tests.condition_isinstance(TestProcessEntry))
78
tests.multiply_tests(process_entry_tests,
79
tests.multiply_scenarios(dir_reader_scenarios,
83
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
84
remaining_tests, tests.condition_isinstance(
85
test_dirstate.TestCaseWithDirState))
86
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios, suite)
87
suite.addTest(remaining_tests)
48
92
class TestBisectPathMixin(object):
49
93
"""Test that _bisect_path_*() returns the expected values.
789
833
class TestUpdateEntry(test_dirstate.TestCaseWithDirState):
790
834
"""Test the DirState.update_entry functions"""
840
super(TestUpdateEntry, self).setUp()
841
orig = dirstate.update_entry
843
dirstate.update_entry = orig
844
self.addCleanup(cleanup)
845
dirstate.update_entry = self.update_entry
792
847
def get_state_with_a(self):
793
848
"""Create a DirState tracking a single object named 'a'"""
794
849
state = test_dirstate.InstrumentedDirState.initialize('dirstate')
795
850
self.addCleanup(state.unlock)
796
851
state.add('a', 'a-id', 'file', None, '')
797
852
entry = state._get_entry(0, path_utf8='a')
798
self.set_update_entry()
799
853
return state, entry
801
def set_update_entry(self):
802
self.update_entry = dirstate.py_update_entry
804
855
def test_observed_sha1_cachable(self):
805
856
state, entry = self.get_state_with_a()
806
857
atime = time.time() - 10
1165
1216
self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1168
# Make the disk object look old enough to cache (but it won't cache the sha
1169
# as it is a new file).
1219
# Make the disk object look old enough to cache (but it won't cache the
1220
# sha as it is a new file).
1170
1221
state.adjust_time(+20)
1171
1222
digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
1172
1223
self.update_entry(state, entry, abspath='a', stat_value=stat_value)
1173
1224
self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1177
class TestCompiledUpdateEntry(TestUpdateEntry):
1178
"""Test the pyrex implementation of _read_dirblocks"""
1180
_test_needs_features = [CompiledDirstateHelpersFeature]
1182
def set_update_entry(self):
1183
from bzrlib._dirstate_helpers_c import update_entry
1184
self.update_entry = update_entry
1227
def _prepare_tree(self):
1229
text = 'Hello World\n'
1230
tree = self.make_branch_and_tree('tree')
1231
self.build_tree_contents([('tree/a file', text)])
1232
tree.add('a file', 'a-file-id')
1233
# Note: dirstate does not sha prior to the first commit
1234
# so commit now in order for the test to work
1235
tree.commit('first')
1238
def test_sha1provider_sha1_used(self):
1239
tree, text = self._prepare_tree()
1240
state = dirstate.DirState.from_tree(tree, 'dirstate',
1241
UppercaseSHA1Provider())
1242
self.addCleanup(state.unlock)
1243
expected_sha = osutils.sha_string(text.upper() + "foo")
1244
entry = state._get_entry(0, path_utf8='a file')
1245
state._sha_cutoff_time()
1246
state._cutoff_time += 10
1247
sha1 = self.update_entry(state, entry, 'tree/a file',
1248
os.lstat('tree/a file'))
1249
self.assertEqual(expected_sha, sha1)
1251
def test_sha1provider_stat_and_sha1_used(self):
1252
tree, text = self._prepare_tree()
1254
self.addCleanup(tree.unlock)
1255
state = tree._current_dirstate()
1256
state._sha1_provider = UppercaseSHA1Provider()
1257
# If we used the standard provider, it would look like nothing has
1259
file_ids_changed = [change[0] for change
1260
in tree.iter_changes(tree.basis_tree())]
1261
self.assertEqual(['a-file-id'], file_ids_changed)
1264
class UppercaseSHA1Provider(dirstate.SHA1Provider):
1265
"""A custom SHA1Provider."""
1267
def sha1(self, abspath):
1268
return self.stat_and_sha1(abspath)[1]
1270
def stat_and_sha1(self, abspath):
1271
file_obj = file(abspath, 'rb')
1273
statvalue = os.fstat(file_obj.fileno())
1274
text = ''.join(file_obj.readlines())
1275
sha1 = osutils.sha_string(text.upper() + "foo")
1278
return statvalue, sha1
1281
class TestProcessEntry(test_dirstate.TestCaseWithDirState):
1284
_process_entry = None
1287
super(TestProcessEntry, self).setUp()
1288
orig = dirstate._process_entry
1290
dirstate._process_entry = orig
1291
self.addCleanup(cleanup)
1292
dirstate._process_entry = self._process_entry
1294
def assertChangedFileIds(self, expected, tree):
1297
file_ids = [info[0] for info
1298
in tree.iter_changes(tree.basis_tree())]
1301
self.assertEqual(sorted(expected), sorted(file_ids))
1303
def test_simple_changes(self):
1304
tree = self.make_branch_and_tree('tree')
1305
self.build_tree(['tree/file'])
1306
tree.add(['file'], ['file-id'])
1307
self.assertChangedFileIds([tree.get_root_id(), 'file-id'], tree)
1309
self.assertChangedFileIds([], tree)
1311
def test_sha1provider_stat_and_sha1_used(self):
1312
tree = self.make_branch_and_tree('tree')
1313
self.build_tree(['tree/file'])
1314
tree.add(['file'], ['file-id'])
1317
self.addCleanup(tree.unlock)
1318
state = tree._current_dirstate()
1319
state._sha1_provider = UppercaseSHA1Provider()
1320
self.assertChangedFileIds(['file-id'], tree)