/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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
45
46
class TestInventoryUpdates(TestInventory):
47
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
48
    def test_creation_from_root_id(self):
49
        # iff a root id is passed to the constructor, a root directory is made
50
        inv = self.make_inventory(root_id='tree-root')
51
        self.assertNotEqual(None, inv.root)
52
        self.assertEqual('tree-root', inv.root.file_id)
53
54
    def test_add_path_of_root(self):
55
        # if no root id is given at creation time, there is no root directory
56
        inv = self.make_inventory(root_id=None)
57
        self.assertIs(None, inv.root)
58
        # add a root entry by adding its path
59
        ie = inv.add_path("", "directory", "my-root")
60
        self.assertEqual("my-root", ie.file_id)
61
        self.assertIs(ie, inv.root)
62
63
    def test_add_path(self):
64
        inv = self.make_inventory(root_id='tree_root')
65
        ie = inv.add_path('hello', 'file', 'hello-id')
66
        self.assertEqual('hello-id', ie.file_id)
67
        self.assertEqual('file', ie.kind)
68
2935.2.1 by Jelmer Vernooij
Fix Inventory.copy() and add a test for it.
69
    def test_copy(self):
70
        """Make sure copy() works and creates a deep copy."""
71
        inv = self.make_inventory(root_id='some-tree-root')
72
        ie = inv.add_path('hello', 'file', 'hello-id')
73
        inv2 = inv.copy()
74
        inv.root.file_id = 'some-new-root'
75
        ie.name = 'file2'
76
        self.assertEqual('some-tree-root', inv2.root.file_id)
77
        self.assertEqual('hello', inv2['hello-id'].name)
78
2938.2.1 by Jelmer Vernooij
Handle empty inventories in Inventory.copy().
79
    def test_copy_empty(self):
80
        """Make sure an empty inventory can be copied."""
81
        inv = self.make_inventory(root_id=None)
82
        inv2 = inv.copy()
83
        self.assertIs(None, inv2.root)
84
3616.1.1 by Jelmer Vernooij
Fix copying of root revision in inventory.
85
    def test_copy_copies_root_revision(self):
86
        """Make sure the revision of the root gets copied."""
87
        inv = self.make_inventory(root_id='someroot')
88
        inv.root.revision = 'therev'
89
        inv2 = inv.copy()
90
        self.assertEquals('someroot', inv2.root.file_id)
91
        self.assertEquals('therev', inv2.root.revision)
92
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
93
    def test_create_tree_reference(self):
94
        inv = self.make_inventory('tree-root-123')
95
        inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
96
                              revision='rev', reference_revision='rev2'))
97
98
    def test_error_encoding(self):
99
        inv = self.make_inventory('tree-root')
100
        inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
101
        try:
102
            inv.add(InventoryFile('b-id', u'\u1234', 'tree-root'))
103
        except errors.BzrError, e:
104
            self.assertContainsRe(str(e), u'\u1234'.encode('utf-8'))
105
        else:
106
            self.fail('BzrError not raised')
107
4090.3.1 by Ian Clatworthy
check delta is legal in Inventory.apply_delta()
108
    def test_add_recursive(self):
109
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
110
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
111
        parent.children[child.file_id] = child
112
        inv = self.make_inventory('tree-root')
113
        inv.add(parent)
114
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
115
116
117
class TestInventoryApplyDelta(TestInventory):
118
119
    def test_apply_delta_add(self):
120
        inv = self.make_inventory('tree-root')
121
        inv.apply_delta([
122
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
123
            ])
124
        self.assertEqual('a', inv.id2path('a-id'))
125
126
    def test_apply_delta_delete(self):
127
        inv = self.make_inventory('tree-root')
128
        inv.apply_delta([
129
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
130
            ])
131
        self.assertEqual('a', inv.id2path('a-id'))
132
        a_ie = inv['a-id']
133
        inv.apply_delta([("a", None, "a-id", a_ie)])
134
        self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
135
136
    def test_apply_delta_rename(self):
137
        inv = self.make_inventory('tree-root')
138
        inv.apply_delta([
139
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
140
            ])
141
        self.assertEqual('a', inv.id2path('a-id'))
142
        a_ie = inv['a-id']
143
        b_ie = InventoryFile(a_ie.file_id, "b", a_ie.parent_id)
144
        inv.apply_delta([("a", "b", "a-id", b_ie)])
145
        self.assertEqual("b", inv.id2path('a-id'))
146
147
    def test_apply_delta_illegal(self):
148
        # A file-id cannot appear in a delta more than once
149
        inv = self.make_inventory('tree-root')
150
        self.assertRaises(AssertionError, inv.apply_delta, [
151
            ("a", "a", "id-1", InventoryFile('id-1', 'a', 'tree-root')),
152
            ("a", "b", "id-1", InventoryFile('id-1', 'b', 'tree-root')),
153
            ])
154
155
156
class TestInventoryReads(TestInventory):
157
158
    def test_is_root(self):
159
        """Ensure our root-checking code is accurate."""
160
        inv = self.make_inventory('TREE_ROOT')
161
        self.assertTrue(inv.is_root('TREE_ROOT'))
162
        self.assertFalse(inv.is_root('booga'))
163
        inv.root.file_id = 'booga'
164
        self.assertFalse(inv.is_root('TREE_ROOT'))
165
        self.assertTrue(inv.is_root('booga'))
166
        # works properly even if no root is set
167
        inv.root = None
168
        self.assertFalse(inv.is_root('TREE_ROOT'))
169
        self.assertFalse(inv.is_root('booga'))
170
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
171
    def test_ids(self):
172
        """Test detection of files within selected directories."""
173
        inv = self.make_inventory(ROOT_ID)
174
        for args in [('src', 'directory', 'src-id'),
175
                     ('doc', 'directory', 'doc-id'),
176
                     ('src/hello.c', 'file'),
177
                     ('src/bye.c', 'file', 'bye-id'),
178
                     ('Makefile', 'file')]:
179
            inv.add_path(*args)
180
        self.assertEqual(inv.path2id('src'), 'src-id')
181
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
182
        self.assert_('src-id' in inv)
183
184
    def test_non_directory_children(self):
185
        """Test path2id when a parent directory has no children"""
186
        inv = self.make_inventory('tree_root')
187
        inv.add(InventoryFile('file-id','file',
188
                                        parent_id='tree_root'))
189
        inv.add(InventoryLink('link-id','link',
190
                                        parent_id='tree_root'))
191
        self.assertIs(None, inv.path2id('file/subfile'))
192
        self.assertIs(None, inv.path2id('link/subfile'))
193
194
    def test_iter_entries(self):
195
        inv = self.make_inventory('tree-root')
196
        for args in [('src', 'directory', 'src-id'),
197
                     ('doc', 'directory', 'doc-id'),
198
                     ('src/hello.c', 'file', 'hello-id'),
199
                     ('src/bye.c', 'file', 'bye-id'),
200
                     ('Makefile', 'file', 'makefile-id')]:
201
            inv.add_path(*args)
202
        self.assertEqual([
203
            ('', 'tree-root'),
204
            ('Makefile', 'makefile-id'),
205
            ('doc', 'doc-id'),
206
            ('src', 'src-id'),
207
            ('src/bye.c', 'bye-id'),
208
            ('src/hello.c', 'hello-id'),
209
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
210
211
    def test_iter_entries_by_dir(self):
212
        inv = self.make_inventory('tree-root')
213
        for args in [('src', 'directory', 'src-id'),
214
                     ('doc', 'directory', 'doc-id'),
215
                     ('src/hello.c', 'file', 'hello-id'),
216
                     ('src/bye.c', 'file', 'bye-id'),
217
                     ('zz', 'file', 'zz-id'),
218
                     ('src/sub/', 'directory', 'sub-id'),
219
                     ('src/zz.c', 'file', 'zzc-id'),
220
                     ('src/sub/a', 'file', 'a-id'),
221
                     ('Makefile', 'file', 'makefile-id')]:
222
            inv.add_path(*args)
223
        self.assertEqual([
224
            ('', 'tree-root'),
225
            ('Makefile', 'makefile-id'),
226
            ('doc', 'doc-id'),
227
            ('src', 'src-id'),
228
            ('zz', 'zz-id'),
229
            ('src/bye.c', 'bye-id'),
230
            ('src/hello.c', 'hello-id'),
231
            ('src/sub', 'sub-id'),
232
            ('src/zz.c', 'zzc-id'),
233
            ('src/sub/a', 'a-id'),
234
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
235
        self.assertEqual([
236
            ('', 'tree-root'),
237
            ('Makefile', 'makefile-id'),
238
            ('doc', 'doc-id'),
239
            ('src', 'src-id'),
240
            ('zz', 'zz-id'),
241
            ('src/bye.c', 'bye-id'),
242
            ('src/hello.c', 'hello-id'),
243
            ('src/sub', 'sub-id'),
244
            ('src/zz.c', 'zzc-id'),
245
            ('src/sub/a', 'a-id'),
246
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
247
                specific_file_ids=('a-id', 'zzc-id', 'doc-id', 'tree-root',
248
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
249
                'sub-id'))])
250
251
        self.assertEqual([
252
            ('Makefile', 'makefile-id'),
253
            ('doc', 'doc-id'),
254
            ('zz', 'zz-id'),
255
            ('src/bye.c', 'bye-id'),
256
            ('src/hello.c', 'hello-id'),
257
            ('src/zz.c', 'zzc-id'),
258
            ('src/sub/a', 'a-id'),
259
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
260
                specific_file_ids=('a-id', 'zzc-id', 'doc-id',
261
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
262
263
        self.assertEqual([
264
            ('Makefile', 'makefile-id'),
265
            ('src/bye.c', 'bye-id'),
266
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
267
                specific_file_ids=('bye-id', 'makefile-id'))])
268
269
        self.assertEqual([
270
            ('Makefile', 'makefile-id'),
271
            ('src/bye.c', 'bye-id'),
272
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
273
                specific_file_ids=('bye-id', 'makefile-id'))])
274
275
        self.assertEqual([
276
            ('src/bye.c', 'bye-id'),
277
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
278
                specific_file_ids=('bye-id',))])
279
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
280
        self.assertEqual([
281
            ('', 'tree-root'),
282
            ('src', 'src-id'),
283
            ('src/bye.c', 'bye-id'),
284
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
285
                specific_file_ids=('bye-id',), yield_parents=True)])