/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
        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
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
78
    def test_is_root(self):
79
        """Ensure our root-checking code is accurate."""
80
        inv = self.make_inventory('TREE_ROOT')
81
        self.assertTrue(inv.is_root('TREE_ROOT'))
82
        self.assertFalse(inv.is_root('booga'))
83
        inv.root.file_id = 'booga'
84
        self.assertFalse(inv.is_root('TREE_ROOT'))
85
        self.assertTrue(inv.is_root('booga'))
86
        # works properly even if no root is set
87
        inv.root = None
88
        self.assertFalse(inv.is_root('TREE_ROOT'))
89
        self.assertFalse(inv.is_root('booga'))
90
91
    def test_create_tree_reference(self):
92
        inv = self.make_inventory('tree-root-123')
93
        inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
94
                              revision='rev', reference_revision='rev2'))
95
96
    def test_error_encoding(self):
97
        inv = self.make_inventory('tree-root')
98
        inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
99
        try:
100
            inv.add(InventoryFile('b-id', u'\u1234', 'tree-root'))
101
        except errors.BzrError, e:
102
            self.assertContainsRe(str(e), u'\u1234'.encode('utf-8'))
103
        else:
104
            self.fail('BzrError not raised')
105
106
    def test_ids(self):
107
        """Test detection of files within selected directories."""
108
        inv = self.make_inventory(ROOT_ID)
109
        for args in [('src', 'directory', 'src-id'),
110
                     ('doc', 'directory', 'doc-id'),
111
                     ('src/hello.c', 'file'),
112
                     ('src/bye.c', 'file', 'bye-id'),
113
                     ('Makefile', 'file')]:
114
            inv.add_path(*args)
115
        self.assertEqual(inv.path2id('src'), 'src-id')
116
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
117
        self.assert_('src-id' in inv)
118
119
    def test_non_directory_children(self):
120
        """Test path2id when a parent directory has no children"""
121
        inv = self.make_inventory('tree_root')
122
        inv.add(InventoryFile('file-id','file',
123
                                        parent_id='tree_root'))
124
        inv.add(InventoryLink('link-id','link',
125
                                        parent_id='tree_root'))
126
        self.assertIs(None, inv.path2id('file/subfile'))
127
        self.assertIs(None, inv.path2id('link/subfile'))
128
129
    def test_iter_entries(self):
130
        inv = self.make_inventory('tree-root')
131
        for args in [('src', 'directory', 'src-id'),
132
                     ('doc', 'directory', 'doc-id'),
133
                     ('src/hello.c', 'file', 'hello-id'),
134
                     ('src/bye.c', 'file', 'bye-id'),
135
                     ('Makefile', 'file', 'makefile-id')]:
136
            inv.add_path(*args)
137
        self.assertEqual([
138
            ('', 'tree-root'),
139
            ('Makefile', 'makefile-id'),
140
            ('doc', 'doc-id'),
141
            ('src', 'src-id'),
142
            ('src/bye.c', 'bye-id'),
143
            ('src/hello.c', 'hello-id'),
144
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
145
146
    def test_iter_entries_by_dir(self):
147
        inv = self.make_inventory('tree-root')
148
        for args in [('src', 'directory', 'src-id'),
149
                     ('doc', 'directory', 'doc-id'),
150
                     ('src/hello.c', 'file', 'hello-id'),
151
                     ('src/bye.c', 'file', 'bye-id'),
152
                     ('zz', 'file', 'zz-id'),
153
                     ('src/sub/', 'directory', 'sub-id'),
154
                     ('src/zz.c', 'file', 'zzc-id'),
155
                     ('src/sub/a', 'file', 'a-id'),
156
                     ('Makefile', 'file', 'makefile-id')]:
157
            inv.add_path(*args)
158
        self.assertEqual([
159
            ('', 'tree-root'),
160
            ('Makefile', 'makefile-id'),
161
            ('doc', 'doc-id'),
162
            ('src', 'src-id'),
163
            ('zz', 'zz-id'),
164
            ('src/bye.c', 'bye-id'),
165
            ('src/hello.c', 'hello-id'),
166
            ('src/sub', 'sub-id'),
167
            ('src/zz.c', 'zzc-id'),
168
            ('src/sub/a', 'a-id'),
169
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
170
        self.assertEqual([
171
            ('', 'tree-root'),
172
            ('Makefile', 'makefile-id'),
173
            ('doc', 'doc-id'),
174
            ('src', 'src-id'),
175
            ('zz', 'zz-id'),
176
            ('src/bye.c', 'bye-id'),
177
            ('src/hello.c', 'hello-id'),
178
            ('src/sub', 'sub-id'),
179
            ('src/zz.c', 'zzc-id'),
180
            ('src/sub/a', 'a-id'),
181
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
182
                specific_file_ids=('a-id', 'zzc-id', 'doc-id', 'tree-root',
183
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
184
                'sub-id'))])
185
186
        self.assertEqual([
187
            ('Makefile', 'makefile-id'),
188
            ('doc', 'doc-id'),
189
            ('zz', 'zz-id'),
190
            ('src/bye.c', 'bye-id'),
191
            ('src/hello.c', 'hello-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',
196
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
197
198
        self.assertEqual([
199
            ('Makefile', 'makefile-id'),
200
            ('src/bye.c', 'bye-id'),
201
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
202
                specific_file_ids=('bye-id', 'makefile-id'))])
203
204
        self.assertEqual([
205
            ('Makefile', 'makefile-id'),
206
            ('src/bye.c', 'bye-id'),
207
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
208
                specific_file_ids=('bye-id', 'makefile-id'))])
209
210
        self.assertEqual([
211
            ('src/bye.c', 'bye-id'),
212
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
213
                specific_file_ids=('bye-id',))])
214
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
215
        self.assertEqual([
216
            ('', 'tree-root'),
217
            ('src', 'src-id'),
218
            ('src/bye.c', 'bye-id'),
219
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
220
                specific_file_ids=('bye-id',), yield_parents=True)])
221
2729.2.9 by Martin Pool
Move actual tests from inventory_implementations __init__ into basics.py (Aaron)
222
    def test_add_recursive(self):
223
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
224
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
225
        parent.children[child.file_id] = child
226
        inv = self.make_inventory('tree-root')
227
        inv.add(parent)
228
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))