/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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')
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
155
        self.assertTrue(
156
            self.applyDeprecated(
157
                deprecated_in((2, 4, 0)),
158
                inv.__contains__, 'src-id'))
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
159
160
    def test_non_directory_children(self):
161
        """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.
162
        inv = inventory.Inventory('tree-root')
163
        inv.add(self.make_file('file-id','file', 'tree-root'))
164
        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)
165
        self.assertIs(None, inv.path2id('file/subfile'))
166
        self.assertIs(None, inv.path2id('link/subfile'))
167
168
    def test_iter_entries(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
169
        inv = self.prepare_inv_with_nested_dirs()
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
170
171
        # Test all entries
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
172
        self.assertEqual([
173
            ('', 'tree-root'),
174
            ('Makefile', 'makefile-id'),
175
            ('doc', 'doc-id'),
176
            ('src', 'src-id'),
177
            ('src/bye.c', 'bye-id'),
178
            ('src/hello.c', 'hello-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
179
            ('src/sub', 'sub-id'),
180
            ('src/sub/a', 'a-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
181
            ('src/zz.c', 'zzc-id'),
182
            ('zz', 'zz-id'),
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
183
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
184
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
185
        # Test a subdirectory
186
        self.assertEqual([
187
            ('bye.c', 'bye-id'),
188
            ('hello.c', 'hello-id'),
189
            ('sub', 'sub-id'),
190
            ('sub/a', 'a-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
191
            ('zz.c', 'zzc-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
192
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
193
            from_dir='src-id')])
194
195
        # Test not recursing at the root level
196
        self.assertEqual([
197
            ('', 'tree-root'),
198
            ('Makefile', 'makefile-id'),
199
            ('doc', 'doc-id'),
200
            ('src', 'src-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
201
            ('zz', 'zz-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
202
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
203
            recursive=False)])
204
205
        # Test not recursing at a subdirectory level
206
        self.assertEqual([
207
            ('bye.c', 'bye-id'),
208
            ('hello.c', 'hello-id'),
209
            ('sub', 'sub-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
210
            ('zz.c', 'zzc-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
211
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
212
            from_dir='src-id', recursive=False)])
213
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
214
    def test_iter_just_entries(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
215
        inv = self.prepare_inv_with_nested_dirs()
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
216
        self.assertEqual([
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
217
            'a-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
218
            'bye-id',
219
            'doc-id',
220
            'hello-id',
221
            'makefile-id',
222
            'src-id',
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
223
            'sub-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
224
            'tree-root',
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
225
            'zz-id',
226
            'zzc-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
227
            ], sorted([ie.file_id for ie in inv.iter_just_entries()]))
228
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
229
    def test_iter_entries_by_dir(self):
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
230
        inv = self. prepare_inv_with_nested_dirs()
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
231
        self.assertEqual([
232
            ('', 'tree-root'),
233
            ('Makefile', 'makefile-id'),
234
            ('doc', 'doc-id'),
235
            ('src', 'src-id'),
236
            ('zz', 'zz-id'),
237
            ('src/bye.c', 'bye-id'),
238
            ('src/hello.c', 'hello-id'),
239
            ('src/sub', 'sub-id'),
240
            ('src/zz.c', 'zzc-id'),
241
            ('src/sub/a', 'a-id'),
242
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
243
        self.assertEqual([
244
            ('', 'tree-root'),
245
            ('Makefile', 'makefile-id'),
246
            ('doc', 'doc-id'),
247
            ('src', 'src-id'),
248
            ('zz', 'zz-id'),
249
            ('src/bye.c', 'bye-id'),
250
            ('src/hello.c', 'hello-id'),
251
            ('src/sub', 'sub-id'),
252
            ('src/zz.c', 'zzc-id'),
253
            ('src/sub/a', 'a-id'),
254
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
255
                specific_file_ids=('a-id', 'zzc-id', 'doc-id', 'tree-root',
256
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
257
                'sub-id'))])
258
259
        self.assertEqual([
260
            ('Makefile', 'makefile-id'),
261
            ('doc', 'doc-id'),
262
            ('zz', 'zz-id'),
263
            ('src/bye.c', 'bye-id'),
264
            ('src/hello.c', 'hello-id'),
265
            ('src/zz.c', 'zzc-id'),
266
            ('src/sub/a', 'a-id'),
267
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
268
                specific_file_ids=('a-id', 'zzc-id', 'doc-id',
269
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
270
271
        self.assertEqual([
272
            ('Makefile', 'makefile-id'),
273
            ('src/bye.c', 'bye-id'),
274
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
275
                specific_file_ids=('bye-id', 'makefile-id'))])
276
277
        self.assertEqual([
278
            ('Makefile', 'makefile-id'),
279
            ('src/bye.c', 'bye-id'),
280
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
281
                specific_file_ids=('bye-id', 'makefile-id'))])
282
283
        self.assertEqual([
284
            ('src/bye.c', 'bye-id'),
285
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
286
                specific_file_ids=('bye-id',))])
287
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
288
        self.assertEqual([
289
            ('', 'tree-root'),
290
            ('src', 'src-id'),
291
            ('src/bye.c', 'bye-id'),
292
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
293
                specific_file_ids=('bye-id',), yield_parents=True)])
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
294
 
295
296
class TestInventoryFiltering(TestInventory):
297
298
    def test_inv_filter_empty(self):
299
        inv = self.prepare_inv_with_nested_dirs()
300
        new_inv = inv.filter([])
301
        self.assertEqual([
302
            ('', 'tree-root'),
303
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
304
    
305
    def test_inv_filter_files(self):
306
        inv = self.prepare_inv_with_nested_dirs()
307
        new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
308
        self.assertEqual([
309
            ('', 'tree-root'),
310
            ('src', 'src-id'),
311
            ('src/hello.c', 'hello-id'),
312
            ('src/sub', 'sub-id'),
313
            ('src/sub/a', 'a-id'),
314
            ('zz', 'zz-id'),
315
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
316
    
317
    def test_inv_filter_dirs(self):
318
        inv = self.prepare_inv_with_nested_dirs()
319
        new_inv = inv.filter(['doc-id', 'sub-id'])
320
        self.assertEqual([
321
            ('', 'tree-root'),
322
            ('doc', 'doc-id'),
323
            ('src', 'src-id'),
324
            ('src/sub', 'sub-id'),
325
            ('src/sub/a', 'a-id'),
326
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
327
328
    def test_inv_filter_files_and_dirs(self):
329
        inv = self.prepare_inv_with_nested_dirs()
330
        new_inv = inv.filter(['makefile-id', 'src-id'])
331
        self.assertEqual([
332
            ('', 'tree-root'),
333
            ('Makefile', 'makefile-id'),
334
            ('src', 'src-id'),
335
            ('src/bye.c', 'bye-id'),
336
            ('src/hello.c', 'hello-id'),
337
            ('src/sub', 'sub-id'),
338
            ('src/sub/a', 'a-id'),
339
            ('src/zz.c', 'zzc-id'),
340
            ], [(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.
341
342
    def test_inv_filter_entry_not_present(self):
343
        inv = self.prepare_inv_with_nested_dirs()
344
        new_inv = inv.filter(['not-present-id'])
345
        self.assertEqual([
346
            ('', 'tree-root'),
347
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])