14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
from bzrlib import errors, inventory, osutils
18
19
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
19
20
InventoryDirectory, InventoryEntry, TreeReference)
20
from bzrlib.osutils import (pathjoin
22
21
from bzrlib.tests import TestCase
25
class TestInventory(TestCase):
28
"""Test detection of files within selected directories."""
31
for args in [('src', 'directory', 'src-id'),
32
('doc', 'directory', 'doc-id'),
33
('src/hello.c', 'file'),
34
('src/bye.c', 'file', 'bye-id'),
35
('Makefile', 'file')]:
38
self.assertEqual(inv.path2id('src'), 'src-id')
39
self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
41
self.assert_('src-id' in inv)
43
def test_non_directory_children(self):
44
"""Test path2id when a parent directory has no children"""
45
inv = inventory.Inventory('tree_root')
46
inv.add(inventory.InventoryFile('file-id','file',
47
parent_id='tree_root'))
48
inv.add(inventory.InventoryLink('link-id','link',
49
parent_id='tree_root'))
50
self.assertIs(None, inv.path2id('file/subfile'))
51
self.assertIs(None, inv.path2id('link/subfile'))
53
def test_iter_entries(self):
56
for args in [('src', 'directory', 'src-id'),
57
('doc', 'directory', 'doc-id'),
58
('src/hello.c', 'file', 'hello-id'),
59
('src/bye.c', 'file', 'bye-id'),
60
('Makefile', 'file', 'makefile-id')]:
65
('Makefile', 'makefile-id'),
68
('src/bye.c', 'bye-id'),
69
('src/hello.c', 'hello-id'),
70
], [(path, ie.file_id) for path, ie in inv.iter_entries()])
72
def test_iter_entries_by_dir(self):
75
for args in [('src', 'directory', 'src-id'),
76
('doc', 'directory', 'doc-id'),
77
('src/hello.c', 'file', 'hello-id'),
78
('src/bye.c', 'file', 'bye-id'),
79
('zz', 'file', 'zz-id'),
80
('src/sub/', 'directory', 'sub-id'),
81
('src/zz.c', 'file', 'zzc-id'),
82
('src/sub/a', 'file', 'a-id'),
83
('Makefile', 'file', 'makefile-id')]:
88
('Makefile', 'makefile-id'),
92
('src/bye.c', 'bye-id'),
93
('src/hello.c', 'hello-id'),
94
('src/sub', 'sub-id'),
95
('src/zz.c', 'zzc-id'),
96
('src/sub/a', 'a-id'),
97
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
101
('Makefile', 'makefile-id'),
105
('src/bye.c', 'bye-id'),
106
('src/hello.c', 'hello-id'),
107
('src/sub', 'sub-id'),
108
('src/zz.c', 'zzc-id'),
109
('src/sub/a', 'a-id'),
110
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
111
specific_file_ids=('a-id', 'zzc-id', 'doc-id', ROOT_ID,
112
'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id',
116
('Makefile', 'makefile-id'),
119
('src/bye.c', 'bye-id'),
120
('src/hello.c', 'hello-id'),
121
('src/zz.c', 'zzc-id'),
122
('src/sub/a', 'a-id'),
123
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
124
specific_file_ids=('a-id', 'zzc-id', 'doc-id',
125
'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
128
('Makefile', 'makefile-id'),
129
('src/bye.c', 'bye-id'),
130
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
131
specific_file_ids=('bye-id', 'makefile-id'))])
134
('Makefile', 'makefile-id'),
135
('src/bye.c', 'bye-id'),
136
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
137
specific_file_ids=('bye-id', 'makefile-id'))])
140
('src/bye.c', 'bye-id'),
141
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
142
specific_file_ids=('bye-id',))])
144
def test_add_recursive(self):
145
parent = InventoryDirectory('src-id', 'src', ROOT_ID)
146
child = InventoryFile('hello-id', 'hello.c', 'src-id')
147
parent.children[child.file_id] = child
150
self.assertEqual('src/hello.c', inv.id2path('hello-id'))
153
24
class TestInventoryEntry(TestCase):
155
26
def test_file_kind_character(self):
295
166
def assertChangeDescription(self, expected_change, old_ie, new_ie):
296
167
change = InventoryEntry.describe_change(old_ie, new_ie)
297
168
self.assertEqual(expected_change, change)
300
class TestIsRoot(TestCase):
301
"""Ensure our root-checking code is accurate."""
303
def test_is_root(self):
304
inv = Inventory('TREE_ROOT')
305
self.assertTrue(inv.is_root('TREE_ROOT'))
306
self.assertFalse(inv.is_root('booga'))
307
inv.root.file_id = 'booga'
308
self.assertFalse(inv.is_root('TREE_ROOT'))
309
self.assertTrue(inv.is_root('booga'))
310
# works properly even if no root is set
312
self.assertFalse(inv.is_root('TREE_ROOT'))
313
self.assertFalse(inv.is_root('booga'))
316
class TestTreeReference(TestCase):
318
def test_create(self):
319
inv = Inventory('tree-root-123')
320
inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
321
revision='rev', reference_revision='rev2'))
324
class TestEncoding(TestCase):
326
def test_error_encoding(self):
327
inv = Inventory('tree-root')
328
inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
330
inv.add(InventoryFile('b-id', u'\u1234', 'tree-root'))
331
except errors.BzrError, e:
332
self.assertContainsRe(str(e), u'\u1234'.encode('utf-8'))
334
self.fail('BzrError not raised')