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