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