~bzr-pqm/bzr/bzr.dev

2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
16
17
"""Tests for different inventory implementations"""
18
19
# NOTE: Don't import Inventory here, to make sure that we don't accidentally
20
# hardcode that when we should be using self.make_inventory
21
22
from bzrlib import (
23
        errors,
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
24
        inventory,
25
        osutils,
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
26
        )
27
28
from bzrlib.inventory import (
29
        InventoryDirectory,
30
        InventoryEntry,
31
        InventoryFile,
32
        InventoryLink,
33
        TreeReference,
34
        )
35
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
36
from bzrlib.tests.per_inventory import TestCaseWithInventory
37
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
38
from bzrlib.symbol_versioning import (
39
    deprecated_in,
40
    )
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
41
42
43
class TestInventory(TestCaseWithInventory):
44
45
    def make_init_inventory(self):
46
        inv = inventory.Inventory('tree-root')
47
        inv.revision = 'initial-rev'
48
        inv.root.revision = 'initial-rev'
49
        return self.inv_to_test_inv(inv)
50
51
    def make_file(self, file_id, name, parent_id, content='content\n',
52
                  revision='new-test-rev'):
53
        ie = InventoryFile(file_id, name, parent_id)
54
        ie.text_sha1 = osutils.sha_string(content)
55
        ie.text_size = len(content)
56
        ie.revision = revision
57
        return ie
58
59
    def make_link(self, file_id, name, parent_id, target='link-target\n'):
60
        ie = InventoryLink(file_id, name, parent_id)
61
        ie.symlink_target = target
62
        return ie
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
63
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
64
    def prepare_inv_with_nested_dirs(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
65
        inv = inventory.Inventory('tree-root')
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
66
        for args in [('src', 'directory', 'src-id'),
67
                     ('doc', 'directory', 'doc-id'),
68
                     ('src/hello.c', 'file', 'hello-id'),
69
                     ('src/bye.c', 'file', 'bye-id'),
70
                     ('zz', 'file', 'zz-id'),
71
                     ('src/sub/', 'directory', 'sub-id'),
72
                     ('src/zz.c', 'file', 'zzc-id'),
73
                     ('src/sub/a', 'file', 'a-id'),
74
                     ('Makefile', 'file', 'makefile-id')]:
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
75
            ie = inv.add_path(*args)
76
            if args[1] == 'file':
77
                ie.text_sha1 = osutils.sha_string('content\n')
78
                ie.text_size = len('content\n')
79
        return self.inv_to_test_inv(inv)
80
81
82
class TestInventoryCreateByApplyDelta(TestInventory):
4505.5.8 by Robert Collins
Fix fallout from the delta checking work, don't error on deltas containing the root inventory item in CHK delta application, and clean up Inventory docs.
83
    """A subset of the inventory delta application tests.
84
85
    See test_inv which has comprehensive delta application tests for
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
86
    inventories, dirstate, and repository based inventories.
4505.5.8 by Robert Collins
Fix fallout from the delta checking work, don't error on deltas containing the root inventory item in CHK delta application, and clean up Inventory docs.
87
    """
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
88
    def test_add(self):
89
        inv = self.make_init_inventory()
90
        inv = inv.create_by_apply_delta([
91
            (None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
92
            ], 'new-test-rev')
93
        self.assertEqual('a', inv.id2path('a-id'))
94
95
    def test_delete(self):
96
        inv = self.make_init_inventory()
97
        inv = inv.create_by_apply_delta([
98
            (None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
99
            ], 'new-rev-1')
100
        self.assertEqual('a', inv.id2path('a-id'))
101
        inv = inv.create_by_apply_delta([
102
            ("a", None, "a-id", None),
103
            ], 'new-rev-2')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
104
        self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
105
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
106
    def test_rename(self):
107
        inv = self.make_init_inventory()
108
        inv = inv.create_by_apply_delta([
109
            (None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
110
            ], 'new-rev-1')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
111
        self.assertEqual('a', inv.id2path('a-id'))
112
        a_ie = inv['a-id']
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
113
        b_ie = self.make_file(a_ie.file_id, "b", a_ie.parent_id)
114
        inv = inv.create_by_apply_delta([("a", "b", "a-id", b_ie)], 'new-rev-2')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
115
        self.assertEqual("b", inv.id2path('a-id'))
116
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
117
    def test_illegal(self):
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
118
        # A file-id cannot appear in a delta more than once
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
119
        inv = self.make_init_inventory()
120
        self.assertRaises(errors.InconsistentDelta, inv.create_by_apply_delta, [
121
            (None, "a", "id-1", self.make_file('id-1', 'a', 'tree-root')),
122
            (None, "b", "id-1", self.make_file('id-1', 'b', 'tree-root')),
123
            ], 'new-rev-1')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
124
125
126
class TestInventoryReads(TestInventory):
127
128
    def test_is_root(self):
129
        """Ensure our root-checking code is accurate."""
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
130
        inv = self.make_init_inventory()
131
        self.assertTrue(inv.is_root('tree-root'))
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
132
        self.assertFalse(inv.is_root('booga'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
133
        ie = inv['tree-root'].copy()
134
        ie.file_id = 'booga'
135
        inv = inv.create_by_apply_delta([("", None, "tree-root", None),
136
                                         (None, "", "booga", ie)], 'new-rev-2')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
137
        self.assertFalse(inv.is_root('TREE_ROOT'))
138
        self.assertTrue(inv.is_root('booga'))
139
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
140
    def test_ids(self):
141
        """Test detection of files within selected directories."""
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
142
        inv = inventory.Inventory('TREE_ROOT')
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
143
        for args in [('src', 'directory', 'src-id'),
144
                     ('doc', 'directory', 'doc-id'),
145
                     ('src/hello.c', 'file'),
146
                     ('src/bye.c', 'file', 'bye-id'),
147
                     ('Makefile', 'file')]:
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
148
            ie = inv.add_path(*args)
149
            if args[1] == 'file':
150
                ie.text_sha1 = osutils.sha_string('content\n')
151
                ie.text_size = len('content\n')
152
        inv = self.inv_to_test_inv(inv)
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
153
        self.assertEqual(inv.path2id('src'), 'src-id')
154
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
155
156
    def test_non_directory_children(self):
157
        """Test path2id when a parent directory has no children"""
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
158
        inv = inventory.Inventory('tree-root')
159
        inv.add(self.make_file('file-id','file', 'tree-root'))
160
        inv.add(self.make_link('link-id','link', 'tree-root'))
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
161
        self.assertIs(None, inv.path2id('file/subfile'))
162
        self.assertIs(None, inv.path2id('link/subfile'))
163
164
    def test_iter_entries(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
165
        inv = self.prepare_inv_with_nested_dirs()
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
166
167
        # Test all entries
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
168
        self.assertEqual([
169
            ('', 'tree-root'),
170
            ('Makefile', 'makefile-id'),
171
            ('doc', 'doc-id'),
172
            ('src', 'src-id'),
173
            ('src/bye.c', 'bye-id'),
174
            ('src/hello.c', 'hello-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
175
            ('src/sub', 'sub-id'),
176
            ('src/sub/a', 'a-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
177
            ('src/zz.c', 'zzc-id'),
178
            ('zz', 'zz-id'),
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
179
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
180
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
181
        # Test a subdirectory
182
        self.assertEqual([
183
            ('bye.c', 'bye-id'),
184
            ('hello.c', 'hello-id'),
185
            ('sub', 'sub-id'),
186
            ('sub/a', 'a-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
187
            ('zz.c', 'zzc-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
188
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
189
            from_dir='src-id')])
190
191
        # Test not recursing at the root level
192
        self.assertEqual([
193
            ('', 'tree-root'),
194
            ('Makefile', 'makefile-id'),
195
            ('doc', 'doc-id'),
196
            ('src', 'src-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
197
            ('zz', 'zz-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
198
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
199
            recursive=False)])
200
201
        # Test not recursing at a subdirectory level
202
        self.assertEqual([
203
            ('bye.c', 'bye-id'),
204
            ('hello.c', 'hello-id'),
205
            ('sub', 'sub-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
206
            ('zz.c', 'zzc-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
207
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
208
            from_dir='src-id', recursive=False)])
209
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
210
    def test_iter_just_entries(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
211
        inv = self.prepare_inv_with_nested_dirs()
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
212
        self.assertEqual([
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
213
            'a-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
214
            'bye-id',
215
            'doc-id',
216
            'hello-id',
217
            'makefile-id',
218
            'src-id',
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
219
            'sub-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
220
            'tree-root',
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
221
            'zz-id',
222
            'zzc-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
223
            ], sorted([ie.file_id for ie in inv.iter_just_entries()]))
224
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
225
    def test_iter_entries_by_dir(self):
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
226
        inv = self. prepare_inv_with_nested_dirs()
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
227
        self.assertEqual([
228
            ('', 'tree-root'),
229
            ('Makefile', 'makefile-id'),
230
            ('doc', 'doc-id'),
231
            ('src', 'src-id'),
232
            ('zz', 'zz-id'),
233
            ('src/bye.c', 'bye-id'),
234
            ('src/hello.c', 'hello-id'),
235
            ('src/sub', 'sub-id'),
236
            ('src/zz.c', 'zzc-id'),
237
            ('src/sub/a', 'a-id'),
238
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
239
        self.assertEqual([
240
            ('', 'tree-root'),
241
            ('Makefile', 'makefile-id'),
242
            ('doc', 'doc-id'),
243
            ('src', 'src-id'),
244
            ('zz', 'zz-id'),
245
            ('src/bye.c', 'bye-id'),
246
            ('src/hello.c', 'hello-id'),
247
            ('src/sub', 'sub-id'),
248
            ('src/zz.c', 'zzc-id'),
249
            ('src/sub/a', 'a-id'),
250
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
251
                specific_file_ids=('a-id', 'zzc-id', 'doc-id', 'tree-root',
252
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
253
                'sub-id'))])
254
255
        self.assertEqual([
256
            ('Makefile', 'makefile-id'),
257
            ('doc', 'doc-id'),
258
            ('zz', 'zz-id'),
259
            ('src/bye.c', 'bye-id'),
260
            ('src/hello.c', 'hello-id'),
261
            ('src/zz.c', 'zzc-id'),
262
            ('src/sub/a', 'a-id'),
263
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
264
                specific_file_ids=('a-id', 'zzc-id', 'doc-id',
265
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
266
267
        self.assertEqual([
268
            ('Makefile', 'makefile-id'),
269
            ('src/bye.c', 'bye-id'),
270
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
271
                specific_file_ids=('bye-id', 'makefile-id'))])
272
273
        self.assertEqual([
274
            ('Makefile', 'makefile-id'),
275
            ('src/bye.c', 'bye-id'),
276
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
277
                specific_file_ids=('bye-id', 'makefile-id'))])
278
279
        self.assertEqual([
280
            ('src/bye.c', 'bye-id'),
281
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
282
                specific_file_ids=('bye-id',))])
283
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
284
        self.assertEqual([
285
            ('', 'tree-root'),
286
            ('src', 'src-id'),
287
            ('src/bye.c', 'bye-id'),
288
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
289
                specific_file_ids=('bye-id',), yield_parents=True)])
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
290
 
291
292
class TestInventoryFiltering(TestInventory):
293
294
    def test_inv_filter_empty(self):
295
        inv = self.prepare_inv_with_nested_dirs()
296
        new_inv = inv.filter([])
297
        self.assertEqual([
298
            ('', 'tree-root'),
299
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
300
    
301
    def test_inv_filter_files(self):
302
        inv = self.prepare_inv_with_nested_dirs()
303
        new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
304
        self.assertEqual([
305
            ('', 'tree-root'),
306
            ('src', 'src-id'),
307
            ('src/hello.c', 'hello-id'),
308
            ('src/sub', 'sub-id'),
309
            ('src/sub/a', 'a-id'),
310
            ('zz', 'zz-id'),
311
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
312
    
313
    def test_inv_filter_dirs(self):
314
        inv = self.prepare_inv_with_nested_dirs()
315
        new_inv = inv.filter(['doc-id', 'sub-id'])
316
        self.assertEqual([
317
            ('', 'tree-root'),
318
            ('doc', 'doc-id'),
319
            ('src', 'src-id'),
320
            ('src/sub', 'sub-id'),
321
            ('src/sub/a', 'a-id'),
322
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
323
324
    def test_inv_filter_files_and_dirs(self):
325
        inv = self.prepare_inv_with_nested_dirs()
326
        new_inv = inv.filter(['makefile-id', 'src-id'])
327
        self.assertEqual([
328
            ('', 'tree-root'),
329
            ('Makefile', 'makefile-id'),
330
            ('src', 'src-id'),
331
            ('src/bye.c', 'bye-id'),
332
            ('src/hello.c', 'hello-id'),
333
            ('src/sub', 'sub-id'),
334
            ('src/sub/a', 'a-id'),
335
            ('src/zz.c', 'zzc-id'),
336
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
4634.51.11 by John Arbash Meinel
Handle the case when the specific_fileids don't exist in this revision.
337
338
    def test_inv_filter_entry_not_present(self):
339
        inv = self.prepare_inv_with_nested_dirs()
340
        new_inv = inv.filter(['not-present-id'])
341
        self.assertEqual([
342
            ('', 'tree-root'),
343
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])