53
69
('src/zz.c', 'file', 'zzc-id'),
54
70
('src/sub/a', 'file', 'a-id'),
55
71
('Makefile', 'file', 'makefile-id')]:
60
class TestInventoryUpdates(TestInventory):
62
def test_creation_from_root_id(self):
63
# iff a root id is passed to the constructor, a root directory is made
64
inv = self.make_inventory(root_id='tree-root')
65
self.assertNotEqual(None, inv.root)
66
self.assertEqual('tree-root', inv.root.file_id)
68
def test_add_path_of_root(self):
69
# if no root id is given at creation time, there is no root directory
70
inv = self.make_inventory(root_id=None)
71
self.assertIs(None, inv.root)
72
# add a root entry by adding its path
73
ie = inv.add_path("", "directory", "my-root")
74
self.assertEqual("my-root", ie.file_id)
75
self.assertIs(ie, inv.root)
77
def test_add_path(self):
78
inv = self.make_inventory(root_id='tree_root')
79
ie = inv.add_path('hello', 'file', 'hello-id')
80
self.assertEqual('hello-id', ie.file_id)
81
self.assertEqual('file', ie.kind)
84
"""Make sure copy() works and creates a deep copy."""
85
inv = self.make_inventory(root_id='some-tree-root')
86
ie = inv.add_path('hello', 'file', 'hello-id')
88
inv.root.file_id = 'some-new-root'
90
self.assertEqual('some-tree-root', inv2.root.file_id)
91
self.assertEqual('hello', inv2['hello-id'].name)
93
def test_copy_empty(self):
94
"""Make sure an empty inventory can be copied."""
95
inv = self.make_inventory(root_id=None)
97
self.assertIs(None, inv2.root)
99
def test_copy_copies_root_revision(self):
100
"""Make sure the revision of the root gets copied."""
101
inv = self.make_inventory(root_id='someroot')
102
inv.root.revision = 'therev'
104
self.assertEquals('someroot', inv2.root.file_id)
105
self.assertEquals('therev', inv2.root.revision)
107
def test_create_tree_reference(self):
108
inv = self.make_inventory('tree-root-123')
109
inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
110
revision='rev', reference_revision='rev2'))
112
def test_error_encoding(self):
113
inv = self.make_inventory('tree-root')
114
inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
115
e = self.assertRaises(errors.InconsistentDelta, inv.add,
116
InventoryFile('b-id', u'\u1234', 'tree-root'))
117
self.assertContainsRe(str(e), r'\\u1234')
119
def test_add_recursive(self):
120
parent = InventoryDirectory('src-id', 'src', 'tree-root')
121
child = InventoryFile('hello-id', 'hello.c', 'src-id')
122
parent.children[child.file_id] = child
123
inv = self.make_inventory('tree-root')
125
self.assertEqual('src/hello.c', inv.id2path('hello-id'))
128
class TestInventoryApplyDelta(TestInventory):
72
ie = inv.add_path(*args)
74
ie.text_sha1 = osutils.sha_string('content\n')
75
ie.text_size = len('content\n')
76
return self.inv_to_test_inv(inv)
79
class TestInventoryCreateByApplyDelta(TestInventory):
129
80
"""A subset of the inventory delta application tests.
131
82
See test_inv which has comprehensive delta application tests for
132
inventories, dirstate, and repository based inventories, unlike the tests
133
here which only test in-memory implementations that can support a plain
83
inventories, dirstate, and repository based inventories.
137
def test_apply_delta_add(self):
138
inv = self.make_inventory('tree-root')
140
(None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
142
self.assertEqual('a', inv.id2path('a-id'))
144
def test_apply_delta_delete(self):
145
inv = self.make_inventory('tree-root')
147
(None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
149
self.assertEqual('a', inv.id2path('a-id'))
151
inv.apply_delta([("a", None, "a-id", None)])
86
inv = self.make_init_inventory()
87
inv = inv.create_by_apply_delta([
88
(None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
90
self.assertEqual('a', inv.id2path('a-id'))
92
def test_delete(self):
93
inv = self.make_init_inventory()
94
inv = inv.create_by_apply_delta([
95
(None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
97
self.assertEqual('a', inv.id2path('a-id'))
98
inv = inv.create_by_apply_delta([
99
("a", None, "a-id", None),
152
101
self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
154
def test_apply_delta_rename(self):
155
inv = self.make_inventory('tree-root')
157
(None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
103
def test_rename(self):
104
inv = self.make_init_inventory()
105
inv = inv.create_by_apply_delta([
106
(None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
159
108
self.assertEqual('a', inv.id2path('a-id'))
160
109
a_ie = inv['a-id']
161
b_ie = InventoryFile(a_ie.file_id, "b", a_ie.parent_id)
162
inv.apply_delta([("a", "b", "a-id", b_ie)])
110
b_ie = self.make_file(a_ie.file_id, "b", a_ie.parent_id)
111
inv = inv.create_by_apply_delta([("a", "b", "a-id", b_ie)], 'new-rev-2')
163
112
self.assertEqual("b", inv.id2path('a-id'))
165
def test_apply_delta_illegal(self):
114
def test_illegal(self):
166
115
# A file-id cannot appear in a delta more than once
167
inv = self.make_inventory('tree-root')
168
self.assertRaises(errors.InconsistentDelta, inv.apply_delta, [
169
("a", "a", "id-1", InventoryFile('id-1', 'a', 'tree-root')),
170
("a", "b", "id-1", InventoryFile('id-1', 'b', 'tree-root')),
116
inv = self.make_init_inventory()
117
self.assertRaises(errors.InconsistentDelta, inv.create_by_apply_delta, [
118
(None, "a", "id-1", self.make_file('id-1', 'a', 'tree-root')),
119
(None, "b", "id-1", self.make_file('id-1', 'b', 'tree-root')),
174
123
class TestInventoryReads(TestInventory):
176
125
def test_is_root(self):
177
126
"""Ensure our root-checking code is accurate."""
178
inv = self.make_inventory('TREE_ROOT')
179
self.assertTrue(inv.is_root('TREE_ROOT'))
127
inv = self.make_init_inventory()
128
self.assertTrue(inv.is_root('tree-root'))
180
129
self.assertFalse(inv.is_root('booga'))
181
inv.root.file_id = 'booga'
130
ie = inv['tree-root'].copy()
132
inv = inv.create_by_apply_delta([("", None, "tree-root", None),
133
(None, "", "booga", ie)], 'new-rev-2')
182
134
self.assertFalse(inv.is_root('TREE_ROOT'))
183
135
self.assertTrue(inv.is_root('booga'))
184
# works properly even if no root is set
186
self.assertFalse(inv.is_root('TREE_ROOT'))
187
self.assertFalse(inv.is_root('booga'))
189
137
def test_ids(self):
190
138
"""Test detection of files within selected directories."""
191
inv = self.make_inventory(ROOT_ID)
139
inv = inventory.Inventory('TREE_ROOT')
192
140
for args in [('src', 'directory', 'src-id'),
193
141
('doc', 'directory', 'doc-id'),
194
142
('src/hello.c', 'file'),
195
143
('src/bye.c', 'file', 'bye-id'),
196
144
('Makefile', 'file')]:
145
ie = inv.add_path(*args)
146
if args[1] == 'file':
147
ie.text_sha1 = osutils.sha_string('content\n')
148
ie.text_size = len('content\n')
149
inv = self.inv_to_test_inv(inv)
198
150
self.assertEqual(inv.path2id('src'), 'src-id')
199
151
self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
200
self.assert_('src-id' in inv)
152
self.assertTrue('src-id' in inv)
202
154
def test_non_directory_children(self):
203
155
"""Test path2id when a parent directory has no children"""
204
inv = self.make_inventory('tree_root')
205
inv.add(InventoryFile('file-id','file',
206
parent_id='tree_root'))
207
inv.add(InventoryLink('link-id','link',
208
parent_id='tree_root'))
156
inv = inventory.Inventory('tree-root')
157
inv.add(self.make_file('file-id','file', 'tree-root'))
158
inv.add(self.make_link('link-id','link', 'tree-root'))
209
159
self.assertIs(None, inv.path2id('file/subfile'))
210
160
self.assertIs(None, inv.path2id('link/subfile'))
212
162
def test_iter_entries(self):
213
inv = self.make_inventory('tree-root')
214
for args in [('src', 'directory', 'src-id'),
215
('doc', 'directory', 'doc-id'),
216
('src/hello.c', 'file', 'hello-id'),
217
('src/bye.c', 'file', 'bye-id'),
218
('src/sub', 'directory', 'sub-id'),
219
('src/sub/a', 'file', 'a-id'),
220
('Makefile', 'file', 'makefile-id')]:
163
inv = self.prepare_inv_with_nested_dirs()
223
165
# Test all entries
224
166
self.assertEqual([