~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,
24
        )
25
26
from bzrlib.inventory import (
27
        InventoryDirectory,
28
        InventoryEntry,
29
        InventoryFile,
30
        InventoryLink,
31
        ROOT_ID,
32
        TreeReference,
33
        )
34
35
from bzrlib.tests import (
36
        TestCase,
37
        )
38
39
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
40
class TestInventory(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
41
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
42
    def make_inventory(self, root_id):
43
        return self.inventory_class(root_id=root_id)
44
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
45
    def prepare_inv_with_nested_dirs(self):
46
        inv = self.make_inventory('tree-root')
47
        for args in [('src', 'directory', 'src-id'),
48
                     ('doc', 'directory', 'doc-id'),
49
                     ('src/hello.c', 'file', 'hello-id'),
50
                     ('src/bye.c', 'file', 'bye-id'),
51
                     ('zz', 'file', 'zz-id'),
52
                     ('src/sub/', 'directory', 'sub-id'),
53
                     ('src/zz.c', 'file', 'zzc-id'),
54
                     ('src/sub/a', 'file', 'a-id'),
55
                     ('Makefile', 'file', 'makefile-id')]:
56
            inv.add_path(*args)
57
        return inv
58
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
59
60
class TestInventoryUpdates(TestInventory):
61
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
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)
67
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)
76
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)
82
2935.2.1 by Jelmer Vernooij
Fix Inventory.copy() and add a test for it.
83
    def test_copy(self):
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')
87
        inv2 = inv.copy()
88
        inv.root.file_id = 'some-new-root'
89
        ie.name = 'file2'
90
        self.assertEqual('some-tree-root', inv2.root.file_id)
91
        self.assertEqual('hello', inv2['hello-id'].name)
92
2938.2.1 by Jelmer Vernooij
Handle empty inventories in Inventory.copy().
93
    def test_copy_empty(self):
94
        """Make sure an empty inventory can be copied."""
95
        inv = self.make_inventory(root_id=None)
96
        inv2 = inv.copy()
97
        self.assertIs(None, inv2.root)
98
3616.1.1 by Jelmer Vernooij
Fix copying of root revision in inventory.
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'
103
        inv2 = inv.copy()
104
        self.assertEquals('someroot', inv2.root.file_id)
105
        self.assertEquals('therev', inv2.root.revision)
106
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
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'))
111
112
    def test_error_encoding(self):
113
        inv = self.make_inventory('tree-root')
114
        inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
115
        try:
116
            inv.add(InventoryFile('b-id', u'\u1234', 'tree-root'))
117
        except errors.BzrError, e:
118
            self.assertContainsRe(str(e), u'\u1234'.encode('utf-8'))
119
        else:
120
            self.fail('BzrError not raised')
121
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
122
    def test_add_recursive(self):
123
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
124
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
125
        parent.children[child.file_id] = child
126
        inv = self.make_inventory('tree-root')
127
        inv.add(parent)
128
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
129
130
131
class TestInventoryApplyDelta(TestInventory):
132
133
    def test_apply_delta_add(self):
134
        inv = self.make_inventory('tree-root')
135
        inv.apply_delta([
136
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
137
            ])
138
        self.assertEqual('a', inv.id2path('a-id'))
139
140
    def test_apply_delta_delete(self):
141
        inv = self.make_inventory('tree-root')
142
        inv.apply_delta([
143
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
144
            ])
145
        self.assertEqual('a', inv.id2path('a-id'))
146
        a_ie = inv['a-id']
147
        inv.apply_delta([("a", None, "a-id", a_ie)])
148
        self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
149
150
    def test_apply_delta_rename(self):
151
        inv = self.make_inventory('tree-root')
152
        inv.apply_delta([
153
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
154
            ])
155
        self.assertEqual('a', inv.id2path('a-id'))
156
        a_ie = inv['a-id']
157
        b_ie = InventoryFile(a_ie.file_id, "b", a_ie.parent_id)
158
        inv.apply_delta([("a", "b", "a-id", b_ie)])
159
        self.assertEqual("b", inv.id2path('a-id'))
160
161
    def test_apply_delta_illegal(self):
162
        # A file-id cannot appear in a delta more than once
163
        inv = self.make_inventory('tree-root')
164
        self.assertRaises(AssertionError, inv.apply_delta, [
165
            ("a", "a", "id-1", InventoryFile('id-1', 'a', 'tree-root')),
166
            ("a", "b", "id-1", InventoryFile('id-1', 'b', 'tree-root')),
167
            ])
168
169
170
class TestInventoryReads(TestInventory):
171
172
    def test_is_root(self):
173
        """Ensure our root-checking code is accurate."""
174
        inv = self.make_inventory('TREE_ROOT')
175
        self.assertTrue(inv.is_root('TREE_ROOT'))
176
        self.assertFalse(inv.is_root('booga'))
177
        inv.root.file_id = 'booga'
178
        self.assertFalse(inv.is_root('TREE_ROOT'))
179
        self.assertTrue(inv.is_root('booga'))
180
        # works properly even if no root is set
181
        inv.root = None
182
        self.assertFalse(inv.is_root('TREE_ROOT'))
183
        self.assertFalse(inv.is_root('booga'))
184
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
185
    def test_ids(self):
186
        """Test detection of files within selected directories."""
187
        inv = self.make_inventory(ROOT_ID)
188
        for args in [('src', 'directory', 'src-id'),
189
                     ('doc', 'directory', 'doc-id'),
190
                     ('src/hello.c', 'file'),
191
                     ('src/bye.c', 'file', 'bye-id'),
192
                     ('Makefile', 'file')]:
193
            inv.add_path(*args)
194
        self.assertEqual(inv.path2id('src'), 'src-id')
195
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
196
        self.assert_('src-id' in inv)
197
198
    def test_non_directory_children(self):
199
        """Test path2id when a parent directory has no children"""
200
        inv = self.make_inventory('tree_root')
201
        inv.add(InventoryFile('file-id','file',
202
                                        parent_id='tree_root'))
203
        inv.add(InventoryLink('link-id','link',
204
                                        parent_id='tree_root'))
205
        self.assertIs(None, inv.path2id('file/subfile'))
206
        self.assertIs(None, inv.path2id('link/subfile'))
207
208
    def test_iter_entries(self):
209
        inv = self.make_inventory('tree-root')
210
        for args in [('src', 'directory', 'src-id'),
211
                     ('doc', 'directory', 'doc-id'),
212
                     ('src/hello.c', 'file', 'hello-id'),
213
                     ('src/bye.c', 'file', 'bye-id'),
214
                     ('Makefile', 'file', 'makefile-id')]:
215
            inv.add_path(*args)
216
        self.assertEqual([
217
            ('', 'tree-root'),
218
            ('Makefile', 'makefile-id'),
219
            ('doc', 'doc-id'),
220
            ('src', 'src-id'),
221
            ('src/bye.c', 'bye-id'),
222
            ('src/hello.c', 'hello-id'),
223
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
224
3735.2.155 by Ian Clatworthy
Inventory.iter_just_entries() API & test
225
    def test_iter_just_entries(self):
226
        inv = self.make_inventory('tree-root')
227
        for args in [('src', 'directory', 'src-id'),
228
                     ('doc', 'directory', 'doc-id'),
229
                     ('src/hello.c', 'file', 'hello-id'),
230
                     ('src/bye.c', 'file', 'bye-id'),
231
                     ('Makefile', 'file', 'makefile-id')]:
232
            inv.add_path(*args)
233
        self.assertEqual([
234
            'bye-id',
235
            'doc-id',
236
            'hello-id',
237
            'makefile-id',
238
            'src-id',
239
            'tree-root',
240
            ], sorted([ie.file_id for ie in inv.iter_just_entries()]))
241
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
242
    def test_iter_entries_by_dir(self):
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
243
        inv = self. prepare_inv_with_nested_dirs()
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
244
        self.assertEqual([
245
            ('', 'tree-root'),
246
            ('Makefile', 'makefile-id'),
247
            ('doc', 'doc-id'),
248
            ('src', 'src-id'),
249
            ('zz', 'zz-id'),
250
            ('src/bye.c', 'bye-id'),
251
            ('src/hello.c', 'hello-id'),
252
            ('src/sub', 'sub-id'),
253
            ('src/zz.c', 'zzc-id'),
254
            ('src/sub/a', 'a-id'),
255
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
256
        self.assertEqual([
257
            ('', 'tree-root'),
258
            ('Makefile', 'makefile-id'),
259
            ('doc', 'doc-id'),
260
            ('src', 'src-id'),
261
            ('zz', 'zz-id'),
262
            ('src/bye.c', 'bye-id'),
263
            ('src/hello.c', 'hello-id'),
264
            ('src/sub', 'sub-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', 'tree-root',
269
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
270
                'sub-id'))])
271
272
        self.assertEqual([
273
            ('Makefile', 'makefile-id'),
274
            ('doc', 'doc-id'),
275
            ('zz', 'zz-id'),
276
            ('src/bye.c', 'bye-id'),
277
            ('src/hello.c', 'hello-id'),
278
            ('src/zz.c', 'zzc-id'),
279
            ('src/sub/a', 'a-id'),
280
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
281
                specific_file_ids=('a-id', 'zzc-id', 'doc-id',
282
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
283
284
        self.assertEqual([
285
            ('Makefile', 'makefile-id'),
286
            ('src/bye.c', 'bye-id'),
287
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
288
                specific_file_ids=('bye-id', 'makefile-id'))])
289
290
        self.assertEqual([
291
            ('Makefile', 'makefile-id'),
292
            ('src/bye.c', 'bye-id'),
293
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
294
                specific_file_ids=('bye-id', 'makefile-id'))])
295
296
        self.assertEqual([
297
            ('src/bye.c', 'bye-id'),
298
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
299
                specific_file_ids=('bye-id',))])
300
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
301
        self.assertEqual([
302
            ('', 'tree-root'),
303
            ('src', 'src-id'),
304
            ('src/bye.c', 'bye-id'),
305
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
306
                specific_file_ids=('bye-id',), yield_parents=True)])
4137.3.1 by Ian Clatworthy
Inventory.filter() API with tests
307
 
308
309
class TestInventoryFiltering(TestInventory):
310
311
    def test_inv_filter_empty(self):
312
        inv = self.prepare_inv_with_nested_dirs()
313
        new_inv = inv.filter([])
314
        self.assertEqual([
315
            ('', 'tree-root'),
316
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
317
    
318
    def test_inv_filter_files(self):
319
        inv = self.prepare_inv_with_nested_dirs()
320
        new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
321
        self.assertEqual([
322
            ('', 'tree-root'),
323
            ('src', 'src-id'),
324
            ('src/hello.c', 'hello-id'),
325
            ('src/sub', 'sub-id'),
326
            ('src/sub/a', 'a-id'),
327
            ('zz', 'zz-id'),
328
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
329
    
330
    def test_inv_filter_dirs(self):
331
        inv = self.prepare_inv_with_nested_dirs()
332
        new_inv = inv.filter(['doc-id', 'sub-id'])
333
        self.assertEqual([
334
            ('', 'tree-root'),
335
            ('doc', 'doc-id'),
336
            ('src', 'src-id'),
337
            ('src/sub', 'sub-id'),
338
            ('src/sub/a', 'a-id'),
339
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
340
341
    def test_inv_filter_files_and_dirs(self):
342
        inv = self.prepare_inv_with_nested_dirs()
343
        new_inv = inv.filter(['makefile-id', 'src-id'])
344
        self.assertEqual([
345
            ('', 'tree-root'),
346
            ('Makefile', 'makefile-id'),
347
            ('src', 'src-id'),
348
            ('src/bye.c', 'bye-id'),
349
            ('src/hello.c', 'hello-id'),
350
            ('src/sub', 'sub-id'),
351
            ('src/sub/a', 'a-id'),
352
            ('src/zz.c', 'zzc-id'),
353
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])