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)
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
78
self.assertFalse(inv.is_root('TREE_ROOT'))
79
self.assertFalse(inv.is_root('booga'))
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'))
86
def test_error_encoding(self):
87
inv = self.make_inventory('tree-root')
88
inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
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'))
94
self.fail('BzrError not raised')
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')]:
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)
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'))
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')]:
129
('Makefile', 'makefile-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()])
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')]:
150
('Makefile', 'makefile-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()])
162
('Makefile', 'makefile-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',
177
('Makefile', 'makefile-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'))])
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'))])
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'))])
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',))])
205
def test_add_recursive(self):
206
parent = InventoryDirectory('src-id', 'src', 'tree-root')
207
child = InventoryFile('hello-id', 'hello.c', 'src-id')
208
parent.children[child.file_id] = child
209
inv = self.make_inventory('tree-root')
211
self.assertEqual('src/hello.c', inv.id2path('hello-id'))