1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
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.
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.
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
17
"""Tests for different inventory implementations"""
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
26
from bzrlib.inventory import (
35
from bzrlib.tests import (
37
multiply_tests_from_modules,
41
class TestInventoryBasics(TestCase):
42
# Most of these were moved the rather old bzrlib.tests.test_inv module
44
def make_inventory(self, root_id):
45
return self.inventory_class(root_id=root_id)
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)
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)
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)
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')
73
inv.root.file_id = 'some-new-root'
75
self.assertEqual('some-tree-root', inv2.root.file_id)
76
self.assertEqual('hello', inv2['hello-id'].name)
78
def test_copy_empty(self):
79
"""Make sure an empty inventory can be copied."""
80
inv = self.make_inventory(root_id=None)
82
self.assertIs(None, inv2.root)
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'
89
self.assertEquals('someroot', inv2.root.file_id)
90
self.assertEquals('therev', inv2.root.revision)
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
102
self.assertFalse(inv.is_root('TREE_ROOT'))
103
self.assertFalse(inv.is_root('booga'))
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'))
110
def test_error_encoding(self):
111
inv = self.make_inventory('tree-root')
112
inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
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'))
118
self.fail('BzrError not raised')
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')]:
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)
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'))
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')]:
153
('Makefile', 'makefile-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()])
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')]:
174
('Makefile', 'makefile-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()])
186
('Makefile', 'makefile-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',
201
('Makefile', 'makefile-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'))])
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'))])
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'))])
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',))])
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)])
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')
242
self.assertEqual('src/hello.c', inv.id2path('hello-id'))