44
44
# set_path_id setting id when state is in memory modified
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)
57
47
class TestCaseWithDirState(tests.TestCaseWithTransport):
58
48
"""Helper functions for creating DirState objects with various content."""
61
_dir_reader_class = None
62
_native_to_unicode = None # Not used yet
65
tests.TestCaseWithTransport.setUp(self)
67
self.overrideAttr(osutils,
68
'_selected_dir_reader', self._dir_reader_class())
70
50
def create_empty_dirstate(self):
71
51
"""Return a locked but empty dirstate"""
72
52
state = dirstate.DirState.initialize('dirstate')
413
393
(('', '', tree.get_root_id()), # common details
414
394
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
415
395
('d', '', 0, False, rev_id), # first parent details
416
('d', '', 0, False, rev_id), # second parent details
396
('d', '', 0, False, rev_id2), # second parent details
418
398
state = dirstate.DirState.from_tree(tree, 'dirstate')
419
399
self.check_state_with_reopen(expected_result, state)
494
474
(('', '', tree.get_root_id()), # common details
495
475
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
496
476
('d', '', 0, False, rev_id), # first parent details
497
('d', '', 0, False, rev_id), # second parent details
477
('d', '', 0, False, rev_id2), # second parent details
499
479
(('', 'a file', 'a-file-id'), # common
500
480
[('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
731
711
class TestDirStateManipulations(TestCaseWithDirState):
733
def test_update_minimal_updates_id_index(self):
734
state = self.create_dirstate_with_root_and_subdir()
735
self.addCleanup(state.unlock)
736
id_index = state._get_id_index()
737
self.assertEqual(['a-root-value', 'subdir-id'], sorted(id_index))
738
state.add('file-name', 'file-id', 'file', None, '')
739
self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
741
state.update_minimal(('', 'new-name', 'file-id'), 'f',
742
path_utf8='new-name')
743
self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
745
self.assertEqual([('', 'new-name', 'file-id')],
746
sorted(id_index['file-id']))
749
713
def test_set_state_from_inventory_no_content_no_parents(self):
750
714
# setting the current inventory is a slow but important api to support.
751
715
tree1 = self.make_branch_and_memory_tree('tree1')
883
848
state = dirstate.DirState.initialize('dirstate')
885
850
# check precondition to be sure the state does change appropriately.
886
root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
887
self.assertEqual([root_entry], list(state._iter_entries()))
888
self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
889
self.assertEqual(root_entry,
890
state._get_entry(0, fileid_utf8='TREE_ROOT'))
891
self.assertEqual((None, None),
892
state._get_entry(0, fileid_utf8='second-root-id'))
893
state.set_path_id('', 'second-root-id')
894
new_root_entry = (('', '', 'second-root-id'),
895
[('d', '', 0, False, 'x'*32)])
896
expected_rows = [new_root_entry]
852
[(('', '', 'TREE_ROOT'), [('d', '', 0, False,
853
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
854
list(state._iter_entries()))
855
state.set_path_id('', 'foobarbaz')
857
(('', '', 'foobarbaz'), [('d', '', 0, False,
858
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
897
859
self.assertEqual(expected_rows, list(state._iter_entries()))
898
self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
899
self.assertEqual(new_root_entry,
900
state._get_entry(0, fileid_utf8='second-root-id'))
901
self.assertEqual((None, None),
902
state._get_entry(0, fileid_utf8='TREE_ROOT'))
903
860
# should work across save too
923
880
state._validate()
925
882
state.set_parent_trees([('parent-revid', rt)], ghosts=[])
926
root_entry = (('', '', 'TREE_ROOT'),
927
[('d', '', 0, False, 'x'*32),
928
('d', '', 0, False, 'parent-revid')])
929
self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
930
self.assertEqual(root_entry,
931
state._get_entry(0, fileid_utf8='TREE_ROOT'))
932
self.assertEqual((None, None),
933
state._get_entry(0, fileid_utf8='Asecond-root-id'))
934
state.set_path_id('', 'Asecond-root-id')
883
state.set_path_id('', 'foobarbaz')
935
884
state._validate()
936
885
# now see that it is what we expected
937
old_root_entry = (('', '', 'TREE_ROOT'),
938
[('a', '', 0, False, ''),
939
('d', '', 0, False, 'parent-revid')])
940
new_root_entry = (('', '', 'Asecond-root-id'),
941
[('d', '', 0, False, ''),
942
('a', '', 0, False, '')])
943
expected_rows = [new_root_entry, old_root_entry]
887
(('', '', 'TREE_ROOT'),
888
[('a', '', 0, False, ''),
889
('d', '', 0, False, 'parent-revid'),
891
(('', '', 'foobarbaz'),
892
[('d', '', 0, False, ''),
893
('a', '', 0, False, ''),
944
896
state._validate()
945
897
self.assertEqual(expected_rows, list(state._iter_entries()))
946
self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
947
self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
948
self.assertEqual((None, None),
949
state._get_entry(0, fileid_utf8='TREE_ROOT'))
950
self.assertEqual(old_root_entry,
951
state._get_entry(1, fileid_utf8='TREE_ROOT'))
952
self.assertEqual(new_root_entry,
953
state._get_entry(0, fileid_utf8='Asecond-root-id'))
954
self.assertEqual((None, None),
955
state._get_entry(1, fileid_utf8='Asecond-root-id'))
956
898
# should work across save too
1033
976
[(('', '', root_id), [
1034
977
('d', '', 0, False, dirstate.DirState.NULLSTAT),
1035
978
('d', '', 0, False, revid1),
1036
('d', '', 0, False, revid1)
979
('d', '', 0, False, revid2)
1038
981
list(state._iter_entries()))
1067
1010
(('', '', root_id), [
1068
1011
('d', '', 0, False, dirstate.DirState.NULLSTAT),
1069
1012
('d', '', 0, False, revid1.encode('utf8')),
1070
('d', '', 0, False, revid1.encode('utf8'))
1013
('d', '', 0, False, revid2.encode('utf8'))
1072
1015
(('', 'a file', 'file-id'), [
1073
1016
('a', '', 0, False, ''),
1163
1106
state._validate()
1164
1107
self.assertEqual(expected_entries, list(state._iter_entries()))
1166
def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
1109
def test_add_symlink_to_root_no_parents_all_data(self):
1167
1110
# The most trivial addition of a symlink when there are no parents and
1168
1111
# its in the root and all data about the file is supplied
1169
1112
# bzr doesn't support fake symlinks on windows, yet.
1170
1113
self.requireFeature(tests.SymlinkFeature)
1171
os.symlink(target, link_name)
1172
stat = os.lstat(link_name)
1114
os.symlink('target', 'a link')
1115
stat = os.lstat('a link')
1173
1116
expected_entries = [
1174
1117
(('', '', 'TREE_ROOT'), [
1175
1118
('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1177
(('', link_name.encode('UTF-8'), 'a link id'), [
1178
('l', target.encode('UTF-8'), stat[6],
1179
False, dirstate.pack_stat(stat)), # current tree
1120
(('', 'a link', 'a link id'), [
1121
('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1182
1124
state = dirstate.DirState.initialize('dirstate')
1184
state.add(link_name, 'a link id', 'symlink', stat,
1185
target.encode('UTF-8'))
1126
state.add('a link', 'a link id', 'symlink', stat, 'target')
1186
1127
# having added it, it should be in the output of iter_entries.
1187
1128
self.assertEqual(expected_entries, list(state._iter_entries()))
1188
1129
# saving and reloading should not affect this.
1194
1135
self.addCleanup(state.unlock)
1195
1136
self.assertEqual(expected_entries, list(state._iter_entries()))
1197
def test_add_symlink_to_root_no_parents_all_data(self):
1198
self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1200
def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1201
self.requireFeature(tests.UnicodeFilenameFeature)
1202
self._test_add_symlink_to_root_no_parents_all_data(
1203
u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
1205
1138
def test_add_directory_and_child_no_parents_all_data(self):
1206
1139
# after adding a directory, we should be able to add children to it.
1207
1140
self.build_tree(['a dir/', 'a dir/a file'])
1269
1202
self.assertRaises(errors.BzrError,
1270
1203
state.add, '..', 'ass-id', 'directory', None, None)
1272
def test_set_state_with_rename_b_a_bug_395556(self):
1273
# bug 395556 uncovered a bug where the dirstate ends up with a false
1274
# relocation record - in a tree with no parents there should be no
1275
# absent or relocated records. This then leads to further corruption
1276
# when a commit occurs, as the incorrect relocation gathers an
1277
# incorrect absent in tree 1, and future changes go to pot.
1278
tree1 = self.make_branch_and_tree('tree1')
1279
self.build_tree(['tree1/b'])
1282
tree1.add(['b'], ['b-id'])
1283
root_id = tree1.get_root_id()
1284
inv = tree1.inventory
1285
state = dirstate.DirState.initialize('dirstate')
1287
# Set the initial state with 'b'
1288
state.set_state_from_inventory(inv)
1289
inv.rename('b-id', root_id, 'a')
1290
# Set the new state with 'a', which currently corrupts.
1291
state.set_state_from_inventory(inv)
1292
expected_result1 = [('', '', root_id, 'd'),
1293
('', 'a', 'b-id', 'f'),
1296
for entry in state._iter_entries():
1297
values.append(entry[0] + entry[1][0][:1])
1298
self.assertEqual(expected_result1, values)
1305
1206
class TestGetLines(TestCaseWithDirState):
2303
2204
self.assertIsInstance(tree_data, str)
2305
2206
def test_unicode_symlink(self):
2306
inv_entry = inventory.InventoryLink('link-file-id',
2307
u'nam\N{Euro Sign}e',
2207
# In general, the code base doesn't support a target that contains
2208
# non-ascii characters. So we just assert tha
2209
inv_entry = inventory.InventoryLink('link-file-id', 'name',
2308
2210
'link-parent-id')
2309
2211
inv_entry.revision = 'link-revision-id'
2310
target = u'link-targ\N{Euro Sign}t'
2311
inv_entry.symlink_target = target
2312
self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2313
'link-revision-id'), inv_entry)
2212
inv_entry.symlink_target = u'link-target'
2213
details = self.assertDetails(('l', 'link-target', 0, False,
2214
'link-revision-id'), inv_entry)
2316
2217
class TestSHA1Provider(tests.TestCaseInTempDir):