/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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
22
from breezy import (
6670.4.1 by Jelmer Vernooij
Update imports.
23
    errors,
24
    osutils,
25
    )
26
from breezy.bzr import (
27
    inventory,
28
    )
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
29
6670.4.1 by Jelmer Vernooij
Update imports.
30
from breezy.bzr.inventory import (
31
    InventoryDirectory,
32
    InventoryEntry,
33
    InventoryFile,
34
    InventoryLink,
35
    TreeReference,
36
    )
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
37
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
38
from breezy.tests.per_inventory import TestCaseWithInventory
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
39
40
41
class TestInventory(TestCaseWithInventory):
42
43
    def make_init_inventory(self):
44
        inv = inventory.Inventory('tree-root')
45
        inv.revision = 'initial-rev'
46
        inv.root.revision = 'initial-rev'
47
        return self.inv_to_test_inv(inv)
48
49
    def make_file(self, file_id, name, parent_id, content='content\n',
50
                  revision='new-test-rev'):
51
        ie = InventoryFile(file_id, name, parent_id)
52
        ie.text_sha1 = osutils.sha_string(content)
53
        ie.text_size = len(content)
54
        ie.revision = revision
55
        return ie
56
57
    def make_link(self, file_id, name, parent_id, target='link-target\n'):
58
        ie = InventoryLink(file_id, name, parent_id)
59
        ie.symlink_target = target
60
        return ie
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
61
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
62
    def prepare_inv_with_nested_dirs(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
63
        inv = inventory.Inventory('tree-root')
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
64
        for args in [('src', 'directory', 'src-id'),
65
                     ('doc', 'directory', 'doc-id'),
66
                     ('src/hello.c', 'file', 'hello-id'),
67
                     ('src/bye.c', 'file', 'bye-id'),
68
                     ('zz', 'file', 'zz-id'),
69
                     ('src/sub/', 'directory', 'sub-id'),
70
                     ('src/zz.c', 'file', 'zzc-id'),
71
                     ('src/sub/a', 'file', 'a-id'),
72
                     ('Makefile', 'file', 'makefile-id')]:
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
73
            ie = inv.add_path(*args)
74
            if args[1] == 'file':
75
                ie.text_sha1 = osutils.sha_string('content\n')
76
                ie.text_size = len('content\n')
77
        return self.inv_to_test_inv(inv)
78
79
80
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.
81
    """A subset of the inventory delta application tests.
82
83
    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.
84
    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.
85
    """
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
86
    def test_add(self):
87
        inv = self.make_init_inventory()
88
        inv = inv.create_by_apply_delta([
89
            (None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
90
            ], 'new-test-rev')
91
        self.assertEqual('a', inv.id2path('a-id'))
92
93
    def test_delete(self):
94
        inv = self.make_init_inventory()
95
        inv = inv.create_by_apply_delta([
96
            (None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
97
            ], 'new-rev-1')
98
        self.assertEqual('a', inv.id2path('a-id'))
99
        inv = inv.create_by_apply_delta([
100
            ("a", None, "a-id", None),
101
            ], 'new-rev-2')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
102
        self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
103
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
104
    def test_rename(self):
105
        inv = self.make_init_inventory()
106
        inv = inv.create_by_apply_delta([
107
            (None, "a", "a-id", self.make_file('a-id', 'a', 'tree-root')),
108
            ], 'new-rev-1')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
109
        self.assertEqual('a', inv.id2path('a-id'))
110
        a_ie = inv['a-id']
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
111
        b_ie = self.make_file(a_ie.file_id, "b", a_ie.parent_id)
112
        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()
113
        self.assertEqual("b", inv.id2path('a-id'))
114
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
115
    def test_illegal(self):
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
116
        # 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.
117
        inv = self.make_init_inventory()
118
        self.assertRaises(errors.InconsistentDelta, inv.create_by_apply_delta, [
119
            (None, "a", "id-1", self.make_file('id-1', 'a', 'tree-root')),
120
            (None, "b", "id-1", self.make_file('id-1', 'b', 'tree-root')),
121
            ], 'new-rev-1')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
122
123
124
class TestInventoryReads(TestInventory):
125
126
    def test_is_root(self):
127
        """Ensure our root-checking code is accurate."""
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
128
        inv = self.make_init_inventory()
129
        self.assertTrue(inv.is_root('tree-root'))
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
130
        self.assertFalse(inv.is_root('booga'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
131
        ie = inv['tree-root'].copy()
132
        ie.file_id = 'booga'
133
        inv = inv.create_by_apply_delta([("", None, "tree-root", None),
134
                                         (None, "", "booga", ie)], 'new-rev-2')
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
135
        self.assertFalse(inv.is_root('TREE_ROOT'))
136
        self.assertTrue(inv.is_root('booga'))
137
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
138
    def test_ids(self):
139
        """Test detection of files within selected directories."""
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
140
        inv = inventory.Inventory('TREE_ROOT')
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
141
        for args in [('src', 'directory', 'src-id'),
142
                     ('doc', 'directory', 'doc-id'),
143
                     ('src/hello.c', 'file'),
144
                     ('src/bye.c', 'file', 'bye-id'),
145
                     ('Makefile', 'file')]:
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
146
            ie = inv.add_path(*args)
147
            if args[1] == 'file':
148
                ie.text_sha1 = osutils.sha_string('content\n')
149
                ie.text_size = len('content\n')
150
        inv = self.inv_to_test_inv(inv)
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
151
        self.assertEqual(inv.path2id('src'), 'src-id')
152
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
153
154
    def test_non_directory_children(self):
155
        """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.
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'))
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
159
        self.assertIs(None, inv.path2id('file/subfile'))
160
        self.assertIs(None, inv.path2id('link/subfile'))
161
162
    def test_iter_entries(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
163
        inv = self.prepare_inv_with_nested_dirs()
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
164
165
        # Test all entries
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
166
        self.assertEqual([
167
            ('', 'tree-root'),
168
            ('Makefile', 'makefile-id'),
169
            ('doc', 'doc-id'),
170
            ('src', 'src-id'),
171
            ('src/bye.c', 'bye-id'),
172
            ('src/hello.c', 'hello-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
173
            ('src/sub', 'sub-id'),
174
            ('src/sub/a', 'a-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
175
            ('src/zz.c', 'zzc-id'),
176
            ('zz', 'zz-id'),
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
177
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
178
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
179
        # Test a subdirectory
180
        self.assertEqual([
181
            ('bye.c', 'bye-id'),
182
            ('hello.c', 'hello-id'),
183
            ('sub', 'sub-id'),
184
            ('sub/a', 'a-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
185
            ('zz.c', 'zzc-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
186
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
187
            from_dir='src-id')])
188
189
        # Test not recursing at the root level
190
        self.assertEqual([
191
            ('', 'tree-root'),
192
            ('Makefile', 'makefile-id'),
193
            ('doc', 'doc-id'),
194
            ('src', 'src-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
195
            ('zz', 'zz-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
196
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
197
            recursive=False)])
198
199
        # Test not recursing at a subdirectory level
200
        self.assertEqual([
201
            ('bye.c', 'bye-id'),
202
            ('hello.c', 'hello-id'),
203
            ('sub', 'sub-id'),
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
204
            ('zz.c', 'zzc-id'),
4370.5.1 by Ian Clatworthy
add recursive parameter to iter_entries()
205
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
206
            from_dir='src-id', recursive=False)])
207
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
208
    def test_iter_just_entries(self):
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
209
        inv = self.prepare_inv_with_nested_dirs()
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
210
        self.assertEqual([
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
211
            'a-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
212
            'bye-id',
213
            'doc-id',
214
            'hello-id',
215
            'makefile-id',
216
            'src-id',
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
217
            'sub-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
218
            'tree-root',
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
219
            'zz-id',
220
            'zzc-id',
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
221
            ], sorted([ie.file_id for ie in inv.iter_just_entries()]))
222
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
223
    def test_iter_entries_by_dir(self):
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
224
        inv = self. prepare_inv_with_nested_dirs()
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
225
        self.assertEqual([
226
            ('', 'tree-root'),
227
            ('Makefile', 'makefile-id'),
228
            ('doc', 'doc-id'),
229
            ('src', 'src-id'),
230
            ('zz', 'zz-id'),
231
            ('src/bye.c', 'bye-id'),
232
            ('src/hello.c', 'hello-id'),
233
            ('src/sub', 'sub-id'),
234
            ('src/zz.c', 'zzc-id'),
235
            ('src/sub/a', 'a-id'),
236
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
237
        self.assertEqual([
238
            ('', 'tree-root'),
239
            ('Makefile', 'makefile-id'),
240
            ('doc', 'doc-id'),
241
            ('src', 'src-id'),
242
            ('zz', 'zz-id'),
243
            ('src/bye.c', 'bye-id'),
244
            ('src/hello.c', 'hello-id'),
245
            ('src/sub', 'sub-id'),
246
            ('src/zz.c', 'zzc-id'),
247
            ('src/sub/a', 'a-id'),
248
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
249
                specific_file_ids=('a-id', 'zzc-id', 'doc-id', 'tree-root',
250
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
251
                'sub-id'))])
252
253
        self.assertEqual([
254
            ('Makefile', 'makefile-id'),
255
            ('doc', 'doc-id'),
256
            ('zz', 'zz-id'),
257
            ('src/bye.c', 'bye-id'),
258
            ('src/hello.c', 'hello-id'),
259
            ('src/zz.c', 'zzc-id'),
260
            ('src/sub/a', 'a-id'),
261
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
262
                specific_file_ids=('a-id', 'zzc-id', 'doc-id',
263
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
264
265
        self.assertEqual([
266
            ('Makefile', 'makefile-id'),
267
            ('src/bye.c', 'bye-id'),
268
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
269
                specific_file_ids=('bye-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
            ('src/bye.c', 'bye-id'),
279
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
280
                specific_file_ids=('bye-id',))])
281
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
282
        self.assertEqual([
283
            ('', 'tree-root'),
284
            ('src', 'src-id'),
285
            ('src/bye.c', 'bye-id'),
286
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
287
                specific_file_ids=('bye-id',), yield_parents=True)])
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
288
 
289
290
class TestInventoryFiltering(TestInventory):
291
292
    def test_inv_filter_empty(self):
293
        inv = self.prepare_inv_with_nested_dirs()
294
        new_inv = inv.filter([])
295
        self.assertEqual([
296
            ('', 'tree-root'),
297
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
298
    
299
    def test_inv_filter_files(self):
300
        inv = self.prepare_inv_with_nested_dirs()
301
        new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
302
        self.assertEqual([
303
            ('', 'tree-root'),
304
            ('src', 'src-id'),
305
            ('src/hello.c', 'hello-id'),
306
            ('src/sub', 'sub-id'),
307
            ('src/sub/a', 'a-id'),
308
            ('zz', 'zz-id'),
309
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
310
    
311
    def test_inv_filter_dirs(self):
312
        inv = self.prepare_inv_with_nested_dirs()
313
        new_inv = inv.filter(['doc-id', 'sub-id'])
314
        self.assertEqual([
315
            ('', 'tree-root'),
316
            ('doc', 'doc-id'),
317
            ('src', 'src-id'),
318
            ('src/sub', 'sub-id'),
319
            ('src/sub/a', 'a-id'),
320
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
321
322
    def test_inv_filter_files_and_dirs(self):
323
        inv = self.prepare_inv_with_nested_dirs()
324
        new_inv = inv.filter(['makefile-id', 'src-id'])
325
        self.assertEqual([
326
            ('', 'tree-root'),
327
            ('Makefile', 'makefile-id'),
328
            ('src', 'src-id'),
329
            ('src/bye.c', 'bye-id'),
330
            ('src/hello.c', 'hello-id'),
331
            ('src/sub', 'sub-id'),
332
            ('src/sub/a', 'a-id'),
333
            ('src/zz.c', 'zzc-id'),
334
            ], [(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.
335
336
    def test_inv_filter_entry_not_present(self):
337
        inv = self.prepare_inv_with_nested_dirs()
338
        new_inv = inv.filter(['not-present-id'])
339
        self.assertEqual([
340
            ('', 'tree-root'),
341
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])