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