~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-05-06 04:07:37 UTC
  • mfrom: (4332.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090506040737-tdiqyojy2uia33qb
Make it easier to blackbox test rules (Marius Kruger)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
from bzrlib.selftest import TestBase
18
 
 
19
 
from bzrlib.inventory import Inventory, InventoryEntry
20
 
 
21
 
class TestIsWithin(TestBase):
22
 
    def runTest(self):
23
 
        """Test detection of files within selected directories."""
24
 
        inv = Inventory()
25
 
        
26
 
        for args in [('src', 'directory', 'src-id'), 
27
 
                     ('doc', 'directory', 'doc-id'), 
28
 
                     ('src/hello.c', 'file'),
29
 
                     ('src/bye.c', 'file', 'bye-id'),
30
 
                     ('Makefile', 'file')]:
31
 
            inv.add_path(*args)
32
 
            
33
 
        self.assertEqual(inv.path2id('src'), 'src-id')
34
 
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
35
 
        
36
 
        self.assert_('src-id' in inv)
37
 
        
38
 
        
 
 
b'\\ No newline at end of file'
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
from bzrlib import errors, chk_map, inventory, osutils
 
19
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
 
20
    InventoryDirectory, InventoryEntry, TreeReference)
 
21
from bzrlib.tests import TestCase, TestCaseWithTransport
 
22
 
 
23
 
 
24
class TestInventoryEntry(TestCase):
 
25
 
 
26
    def test_file_kind_character(self):
 
27
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
28
        self.assertEqual(file.kind_character(), '')
 
29
 
 
30
    def test_dir_kind_character(self):
 
31
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
32
        self.assertEqual(dir.kind_character(), '/')
 
33
 
 
34
    def test_link_kind_character(self):
 
35
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
36
        self.assertEqual(dir.kind_character(), '')
 
37
 
 
38
    def test_dir_detect_changes(self):
 
39
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
40
        left.text_sha1 = 123
 
41
        left.executable = True
 
42
        left.symlink_target='foo'
 
43
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
44
        right.text_sha1 = 321
 
45
        right.symlink_target='bar'
 
46
        self.assertEqual((False, False), left.detect_changes(right))
 
47
        self.assertEqual((False, False), right.detect_changes(left))
 
48
 
 
49
    def test_file_detect_changes(self):
 
50
        left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
51
        left.text_sha1 = 123
 
52
        right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
53
        right.text_sha1 = 123
 
54
        self.assertEqual((False, False), left.detect_changes(right))
 
55
        self.assertEqual((False, False), right.detect_changes(left))
 
56
        left.executable = True
 
57
        self.assertEqual((False, True), left.detect_changes(right))
 
58
        self.assertEqual((False, True), right.detect_changes(left))
 
59
        right.text_sha1 = 321
 
60
        self.assertEqual((True, True), left.detect_changes(right))
 
61
        self.assertEqual((True, True), right.detect_changes(left))
 
62
 
 
63
    def test_symlink_detect_changes(self):
 
64
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
65
        left.text_sha1 = 123
 
66
        left.executable = True
 
67
        left.symlink_target='foo'
 
68
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
69
        right.text_sha1 = 321
 
70
        right.symlink_target='foo'
 
71
        self.assertEqual((False, False), left.detect_changes(right))
 
72
        self.assertEqual((False, False), right.detect_changes(left))
 
73
        left.symlink_target = 'different'
 
74
        self.assertEqual((True, False), left.detect_changes(right))
 
75
        self.assertEqual((True, False), right.detect_changes(left))
 
76
 
 
77
    def test_file_has_text(self):
 
78
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
79
        self.failUnless(file.has_text())
 
80
 
 
81
    def test_directory_has_text(self):
 
82
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
83
        self.failIf(dir.has_text())
 
84
 
 
85
    def test_link_has_text(self):
 
86
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
87
        self.failIf(link.has_text())
 
88
 
 
89
    def test_make_entry(self):
 
90
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
 
91
            inventory.InventoryFile)
 
92
        self.assertIsInstance(inventory.make_entry("symlink", "name", ROOT_ID),
 
93
            inventory.InventoryLink)
 
94
        self.assertIsInstance(inventory.make_entry("directory", "name", ROOT_ID),
 
95
            inventory.InventoryDirectory)
 
96
 
 
97
    def test_make_entry_non_normalized(self):
 
98
        orig_normalized_filename = osutils.normalized_filename
 
99
 
 
100
        try:
 
101
            osutils.normalized_filename = osutils._accessible_normalized_filename
 
102
            entry = inventory.make_entry("file", u'a\u030a', ROOT_ID)
 
103
            self.assertEqual(u'\xe5', entry.name)
 
104
            self.assertIsInstance(entry, inventory.InventoryFile)
 
105
 
 
106
            osutils.normalized_filename = osutils._inaccessible_normalized_filename
 
107
            self.assertRaises(errors.InvalidNormalization,
 
108
                    inventory.make_entry, 'file', u'a\u030a', ROOT_ID)
 
109
        finally:
 
110
            osutils.normalized_filename = orig_normalized_filename
 
111
 
 
112
 
 
113
class TestDescribeChanges(TestCase):
 
114
 
 
115
    def test_describe_change(self):
 
116
        # we need to test the following change combinations:
 
117
        # rename
 
118
        # reparent
 
119
        # modify
 
120
        # gone
 
121
        # added
 
122
        # renamed/reparented and modified
 
123
        # change kind (perhaps can't be done yet?)
 
124
        # also, merged in combination with all of these?
 
125
        old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
126
        old_a.text_sha1 = '123132'
 
127
        old_a.text_size = 0
 
128
        new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
129
        new_a.text_sha1 = '123132'
 
130
        new_a.text_size = 0
 
131
 
 
132
        self.assertChangeDescription('unchanged', old_a, new_a)
 
133
 
 
134
        new_a.text_size = 10
 
135
        new_a.text_sha1 = 'abcabc'
 
136
        self.assertChangeDescription('modified', old_a, new_a)
 
137
 
 
138
        self.assertChangeDescription('added', None, new_a)
 
139
        self.assertChangeDescription('removed', old_a, None)
 
140
        # perhaps a bit questionable but seems like the most reasonable thing...
 
141
        self.assertChangeDescription('unchanged', None, None)
 
142
 
 
143
        # in this case it's both renamed and modified; show a rename and
 
144
        # modification:
 
145
        new_a.name = 'newfilename'
 
146
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
147
 
 
148
        # reparenting is 'renaming'
 
149
        new_a.name = old_a.name
 
150
        new_a.parent_id = 'somedir-id'
 
151
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
152
 
 
153
        # reset the content values so its not modified
 
154
        new_a.text_size = old_a.text_size
 
155
        new_a.text_sha1 = old_a.text_sha1
 
156
        new_a.name = old_a.name
 
157
 
 
158
        new_a.name = 'newfilename'
 
159
        self.assertChangeDescription('renamed', old_a, new_a)
 
160
 
 
161
        # reparenting is 'renaming'
 
162
        new_a.name = old_a.name
 
163
        new_a.parent_id = 'somedir-id'
 
164
        self.assertChangeDescription('renamed', old_a, new_a)
 
165
 
 
166
    def assertChangeDescription(self, expected_change, old_ie, new_ie):
 
167
        change = InventoryEntry.describe_change(old_ie, new_ie)
 
168
        self.assertEqual(expected_change, change)
 
169
 
 
170
 
 
171
class TestCHKInventory(TestCaseWithTransport):
 
172
 
 
173
    def get_chk_bytes(self):
 
174
        # The easiest way to get a CHK store is a development6 repository and
 
175
        # then work with the chk_bytes attribute directly.
 
176
        repo = self.make_repository(".", format="development6-rich-root")
 
177
        repo.lock_write()
 
178
        self.addCleanup(repo.unlock)
 
179
        repo.start_write_group()
 
180
        self.addCleanup(repo.abort_write_group)
 
181
        return repo.chk_bytes
 
182
 
 
183
    def read_bytes(self, chk_bytes, key):
 
184
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
 
185
        return stream.next().get_bytes_as("fulltext")
 
186
 
 
187
    def test_deserialise_gives_CHKInventory(self):
 
188
        inv = Inventory()
 
189
        inv.revision_id = "revid"
 
190
        inv.root.revision = "rootrev"
 
191
        chk_bytes = self.get_chk_bytes()
 
192
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
193
        bytes = ''.join(chk_inv.to_lines())
 
194
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
195
        self.assertEqual("revid", new_inv.revision_id)
 
196
        self.assertEqual("directory", new_inv.root.kind)
 
197
        self.assertEqual(inv.root.file_id, new_inv.root.file_id)
 
198
        self.assertEqual(inv.root.parent_id, new_inv.root.parent_id)
 
199
        self.assertEqual(inv.root.name, new_inv.root.name)
 
200
        self.assertEqual("rootrev", new_inv.root.revision)
 
201
        self.assertEqual('plain', new_inv._search_key_name)
 
202
 
 
203
    def test_deserialise_wrong_revid(self):
 
204
        inv = Inventory()
 
205
        inv.revision_id = "revid"
 
206
        inv.root.revision = "rootrev"
 
207
        chk_bytes = self.get_chk_bytes()
 
208
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
209
        bytes = ''.join(chk_inv.to_lines())
 
210
        self.assertRaises(ValueError, CHKInventory.deserialise, chk_bytes,
 
211
            bytes, ("revid2",))
 
212
 
 
213
    def test_captures_rev_root_byid(self):
 
214
        inv = Inventory()
 
215
        inv.revision_id = "foo"
 
216
        inv.root.revision = "bar"
 
217
        chk_bytes = self.get_chk_bytes()
 
218
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
219
        lines = chk_inv.to_lines()
 
220
        self.assertEqual([
 
221
            'chkinventory:\n',
 
222
            'revision_id: foo\n',
 
223
            'root_id: TREE_ROOT\n',
 
224
            'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
 
225
            'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
 
226
            ], lines)
 
227
        chk_inv = CHKInventory.deserialise(chk_bytes, ''.join(lines), ('foo',))
 
228
        self.assertEqual('plain', chk_inv._search_key_name)
 
229
 
 
230
    def test_captures_parent_id_basename_index(self):
 
231
        inv = Inventory()
 
232
        inv.revision_id = "foo"
 
233
        inv.root.revision = "bar"
 
234
        chk_bytes = self.get_chk_bytes()
 
235
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
236
        lines = chk_inv.to_lines()
 
237
        self.assertEqual([
 
238
            'chkinventory:\n',
 
239
            'revision_id: foo\n',
 
240
            'root_id: TREE_ROOT\n',
 
241
            'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
 
242
            'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
 
243
            ], lines)
 
244
        chk_inv = CHKInventory.deserialise(chk_bytes, ''.join(lines), ('foo',))
 
245
        self.assertEqual('plain', chk_inv._search_key_name)
 
246
 
 
247
    def test_captures_search_key_name(self):
 
248
        inv = Inventory()
 
249
        inv.revision_id = "foo"
 
250
        inv.root.revision = "bar"
 
251
        chk_bytes = self.get_chk_bytes()
 
252
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv,
 
253
                                              search_key_name='hash-16-way')
 
254
        lines = chk_inv.to_lines()
 
255
        self.assertEqual([
 
256
            'chkinventory:\n',
 
257
            'search_key_name: hash-16-way\n',
 
258
            'root_id: TREE_ROOT\n',
 
259
            'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
 
260
            'revision_id: foo\n',
 
261
            'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
 
262
            ], lines)
 
263
        chk_inv = CHKInventory.deserialise(chk_bytes, ''.join(lines), ('foo',))
 
264
        self.assertEqual('hash-16-way', chk_inv._search_key_name)
 
265
 
 
266
    def test_directory_children_on_demand(self):
 
267
        inv = Inventory()
 
268
        inv.revision_id = "revid"
 
269
        inv.root.revision = "rootrev"
 
270
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
271
        inv["fileid"].revision = "filerev"
 
272
        inv["fileid"].executable = True
 
273
        inv["fileid"].text_sha1 = "ffff"
 
274
        inv["fileid"].text_size = 1
 
275
        chk_bytes = self.get_chk_bytes()
 
276
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
277
        bytes = ''.join(chk_inv.to_lines())
 
278
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
279
        root_entry = new_inv[inv.root.file_id]
 
280
        self.assertEqual(None, root_entry._children)
 
281
        self.assertEqual(['file'], root_entry.children.keys())
 
282
        file_direct = new_inv["fileid"]
 
283
        file_found = root_entry.children['file']
 
284
        self.assertEqual(file_direct.kind, file_found.kind)
 
285
        self.assertEqual(file_direct.file_id, file_found.file_id)
 
286
        self.assertEqual(file_direct.parent_id, file_found.parent_id)
 
287
        self.assertEqual(file_direct.name, file_found.name)
 
288
        self.assertEqual(file_direct.revision, file_found.revision)
 
289
        self.assertEqual(file_direct.text_sha1, file_found.text_sha1)
 
290
        self.assertEqual(file_direct.text_size, file_found.text_size)
 
291
        self.assertEqual(file_direct.executable, file_found.executable)
 
292
 
 
293
    def test_from_inventory_maximum_size(self):
 
294
        # from_inventory supports the maximum_size parameter.
 
295
        inv = Inventory()
 
296
        inv.revision_id = "revid"
 
297
        inv.root.revision = "rootrev"
 
298
        chk_bytes = self.get_chk_bytes()
 
299
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv, 120)
 
300
        self.assertEqual(120, chk_inv.id_to_entry._root_node.maximum_size)
 
301
 
 
302
    def test___iter__(self):
 
303
        inv = Inventory()
 
304
        inv.revision_id = "revid"
 
305
        inv.root.revision = "rootrev"
 
306
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
307
        inv["fileid"].revision = "filerev"
 
308
        inv["fileid"].executable = True
 
309
        inv["fileid"].text_sha1 = "ffff"
 
310
        inv["fileid"].text_size = 1
 
311
        chk_bytes = self.get_chk_bytes()
 
312
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
313
        bytes = ''.join(chk_inv.to_lines())
 
314
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
315
        fileids = list(new_inv.__iter__())
 
316
        fileids.sort()
 
317
        self.assertEqual([inv.root.file_id, "fileid"], fileids)
 
318
 
 
319
    def test__len__(self):
 
320
        inv = Inventory()
 
321
        inv.revision_id = "revid"
 
322
        inv.root.revision = "rootrev"
 
323
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
324
        inv["fileid"].revision = "filerev"
 
325
        inv["fileid"].executable = True
 
326
        inv["fileid"].text_sha1 = "ffff"
 
327
        inv["fileid"].text_size = 1
 
328
        chk_bytes = self.get_chk_bytes()
 
329
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
330
        self.assertEqual(2, len(chk_inv))
 
331
 
 
332
    def test___getitem__(self):
 
333
        inv = Inventory()
 
334
        inv.revision_id = "revid"
 
335
        inv.root.revision = "rootrev"
 
336
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
337
        inv["fileid"].revision = "filerev"
 
338
        inv["fileid"].executable = True
 
339
        inv["fileid"].text_sha1 = "ffff"
 
340
        inv["fileid"].text_size = 1
 
341
        chk_bytes = self.get_chk_bytes()
 
342
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
343
        bytes = ''.join(chk_inv.to_lines())
 
344
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
345
        root_entry = new_inv[inv.root.file_id]
 
346
        file_entry = new_inv["fileid"]
 
347
        self.assertEqual("directory", root_entry.kind)
 
348
        self.assertEqual(inv.root.file_id, root_entry.file_id)
 
349
        self.assertEqual(inv.root.parent_id, root_entry.parent_id)
 
350
        self.assertEqual(inv.root.name, root_entry.name)
 
351
        self.assertEqual("rootrev", root_entry.revision)
 
352
        self.assertEqual("file", file_entry.kind)
 
353
        self.assertEqual("fileid", file_entry.file_id)
 
354
        self.assertEqual(inv.root.file_id, file_entry.parent_id)
 
355
        self.assertEqual("file", file_entry.name)
 
356
        self.assertEqual("filerev", file_entry.revision)
 
357
        self.assertEqual("ffff", file_entry.text_sha1)
 
358
        self.assertEqual(1, file_entry.text_size)
 
359
        self.assertEqual(True, file_entry.executable)
 
360
        self.assertRaises(errors.NoSuchId, new_inv.__getitem__, 'missing')
 
361
 
 
362
    def test_has_id_true(self):
 
363
        inv = Inventory()
 
364
        inv.revision_id = "revid"
 
365
        inv.root.revision = "rootrev"
 
366
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
367
        inv["fileid"].revision = "filerev"
 
368
        inv["fileid"].executable = True
 
369
        inv["fileid"].text_sha1 = "ffff"
 
370
        inv["fileid"].text_size = 1
 
371
        chk_bytes = self.get_chk_bytes()
 
372
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
373
        self.assertTrue(chk_inv.has_id('fileid'))
 
374
        self.assertTrue(chk_inv.has_id(inv.root.file_id))
 
375
 
 
376
    def test_has_id_not(self):
 
377
        inv = Inventory()
 
378
        inv.revision_id = "revid"
 
379
        inv.root.revision = "rootrev"
 
380
        chk_bytes = self.get_chk_bytes()
 
381
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
382
        self.assertFalse(chk_inv.has_id('fileid'))
 
383
 
 
384
    def test_id2path(self):
 
385
        inv = Inventory()
 
386
        inv.revision_id = "revid"
 
387
        inv.root.revision = "rootrev"
 
388
        direntry = InventoryDirectory("dirid", "dir", inv.root.file_id)
 
389
        fileentry = InventoryFile("fileid", "file", "dirid")
 
390
        inv.add(direntry)
 
391
        inv.add(fileentry)
 
392
        inv["fileid"].revision = "filerev"
 
393
        inv["fileid"].executable = True
 
394
        inv["fileid"].text_sha1 = "ffff"
 
395
        inv["fileid"].text_size = 1
 
396
        inv["dirid"].revision = "filerev"
 
397
        chk_bytes = self.get_chk_bytes()
 
398
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
399
        bytes = ''.join(chk_inv.to_lines())
 
400
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
401
        self.assertEqual('', new_inv.id2path(inv.root.file_id))
 
402
        self.assertEqual('dir', new_inv.id2path('dirid'))
 
403
        self.assertEqual('dir/file', new_inv.id2path('fileid'))
 
404
 
 
405
    def test_path2id(self):
 
406
        inv = Inventory()
 
407
        inv.revision_id = "revid"
 
408
        inv.root.revision = "rootrev"
 
409
        direntry = InventoryDirectory("dirid", "dir", inv.root.file_id)
 
410
        fileentry = InventoryFile("fileid", "file", "dirid")
 
411
        inv.add(direntry)
 
412
        inv.add(fileentry)
 
413
        inv["fileid"].revision = "filerev"
 
414
        inv["fileid"].executable = True
 
415
        inv["fileid"].text_sha1 = "ffff"
 
416
        inv["fileid"].text_size = 1
 
417
        inv["dirid"].revision = "filerev"
 
418
        chk_bytes = self.get_chk_bytes()
 
419
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
420
        bytes = ''.join(chk_inv.to_lines())
 
421
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
422
        self.assertEqual(inv.root.file_id, new_inv.path2id(''))
 
423
        self.assertEqual('dirid', new_inv.path2id('dir'))
 
424
        self.assertEqual('fileid', new_inv.path2id('dir/file'))
 
425
 
 
426
    def test_create_by_apply_delta_sets_root(self):
 
427
        inv = Inventory()
 
428
        inv.revision_id = "revid"
 
429
        chk_bytes = self.get_chk_bytes()
 
430
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
431
        inv.add_path("", "directory", "myrootid", None)
 
432
        inv.revision_id = "expectedid"
 
433
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
434
        delta = [(None, "",  "myrootid", inv.root)]
 
435
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
 
436
        self.assertEquals(reference_inv.root, new_inv.root)
 
437
 
 
438
    def test_create_by_apply_delta_empty_add_child(self):
 
439
        inv = Inventory()
 
440
        inv.revision_id = "revid"
 
441
        inv.root.revision = "rootrev"
 
442
        chk_bytes = self.get_chk_bytes()
 
443
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
444
        a_entry = InventoryFile("A-id", "A", inv.root.file_id)
 
445
        a_entry.revision = "filerev"
 
446
        a_entry.executable = True
 
447
        a_entry.text_sha1 = "ffff"
 
448
        a_entry.text_size = 1
 
449
        inv.add(a_entry)
 
450
        inv.revision_id = "expectedid"
 
451
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
452
        delta = [(None, "A",  "A-id", a_entry)]
 
453
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
 
454
        # new_inv should be the same as reference_inv.
 
455
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
 
456
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
 
457
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
 
458
            new_inv.id_to_entry._root_node._key)
 
459
 
 
460
    def test_create_by_apply_delta_empty_add_child_updates_parent_id(self):
 
461
        inv = Inventory()
 
462
        inv.revision_id = "revid"
 
463
        inv.root.revision = "rootrev"
 
464
        chk_bytes = self.get_chk_bytes()
 
465
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
466
        a_entry = InventoryFile("A-id", "A", inv.root.file_id)
 
467
        a_entry.revision = "filerev"
 
468
        a_entry.executable = True
 
469
        a_entry.text_sha1 = "ffff"
 
470
        a_entry.text_size = 1
 
471
        inv.add(a_entry)
 
472
        inv.revision_id = "expectedid"
 
473
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
474
        delta = [(None, "A",  "A-id", a_entry)]
 
475
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
 
476
        # new_inv should be the same as reference_inv.
 
477
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
 
478
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
 
479
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
 
480
            new_inv.id_to_entry._root_node._key)
 
481
        self.assertEqual(reference_inv.parent_id_basename_to_file_id._root_node._key,
 
482
            new_inv.parent_id_basename_to_file_id._root_node._key)
 
483
 
 
484
    def test_iter_changes(self):
 
485
        # Low level bootstrapping smoke test; comprehensive generic tests via
 
486
        # InterTree are coming.
 
487
        inv = Inventory()
 
488
        inv.revision_id = "revid"
 
489
        inv.root.revision = "rootrev"
 
490
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
491
        inv["fileid"].revision = "filerev"
 
492
        inv["fileid"].executable = True
 
493
        inv["fileid"].text_sha1 = "ffff"
 
494
        inv["fileid"].text_size = 1
 
495
        inv2 = Inventory()
 
496
        inv2.revision_id = "revid2"
 
497
        inv2.root.revision = "rootrev"
 
498
        inv2.add(InventoryFile("fileid", "file", inv.root.file_id))
 
499
        inv2["fileid"].revision = "filerev2"
 
500
        inv2["fileid"].executable = False
 
501
        inv2["fileid"].text_sha1 = "bbbb"
 
502
        inv2["fileid"].text_size = 2
 
503
        # get fresh objects.
 
504
        chk_bytes = self.get_chk_bytes()
 
505
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
506
        bytes = ''.join(chk_inv.to_lines())
 
507
        inv_1 = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
508
        chk_inv2 = CHKInventory.from_inventory(chk_bytes, inv2)
 
509
        bytes = ''.join(chk_inv2.to_lines())
 
510
        inv_2 = CHKInventory.deserialise(chk_bytes, bytes, ("revid2",))
 
511
        self.assertEqual([('fileid', (u'file', u'file'), True, (True, True),
 
512
            ('TREE_ROOT', 'TREE_ROOT'), (u'file', u'file'), ('file', 'file'),
 
513
            (False, True))],
 
514
            list(inv_1.iter_changes(inv_2)))
 
515
 
 
516
    def test_parent_id_basename_to_file_id_index_enabled(self):
 
517
        inv = Inventory()
 
518
        inv.revision_id = "revid"
 
519
        inv.root.revision = "rootrev"
 
520
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
 
521
        inv["fileid"].revision = "filerev"
 
522
        inv["fileid"].executable = True
 
523
        inv["fileid"].text_sha1 = "ffff"
 
524
        inv["fileid"].text_size = 1
 
525
        # get fresh objects.
 
526
        chk_bytes = self.get_chk_bytes()
 
527
        tmp_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
528
        bytes = ''.join(tmp_inv.to_lines())
 
529
        chk_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
530
        self.assertIsInstance(chk_inv.parent_id_basename_to_file_id, chk_map.CHKMap)
 
531
        self.assertEqual(
 
532
            {('', ''): 'TREE_ROOT', ('TREE_ROOT', 'file'): 'fileid'},
 
533
            dict(chk_inv.parent_id_basename_to_file_id.iteritems()))
 
534
 
 
535
    def test_file_entry_to_bytes(self):
 
536
        inv = CHKInventory(None)
 
537
        ie = inventory.InventoryFile('file-id', 'filename', 'parent-id')
 
538
        ie.executable = True
 
539
        ie.revision = 'file-rev-id'
 
540
        ie.text_sha1 = 'abcdefgh'
 
541
        ie.text_size = 100
 
542
        bytes = inv._entry_to_bytes(ie)
 
543
        self.assertEqual('file: file-id\nparent-id\nfilename\n'
 
544
                         'file-rev-id\nabcdefgh\n100\nY', bytes)
 
545
        ie2 = inv._bytes_to_entry(bytes)
 
546
        self.assertEqual(ie, ie2)
 
547
        self.assertIsInstance(ie2.name, unicode)
 
548
        self.assertEqual(('filename', 'file-id', 'file-rev-id'),
 
549
                         inv._bytes_to_utf8name_key(bytes))
 
550
 
 
551
    def test_file2_entry_to_bytes(self):
 
552
        inv = CHKInventory(None)
 
553
        # \u30a9 == 'omega'
 
554
        ie = inventory.InventoryFile('file-id', u'\u03a9name', 'parent-id')
 
555
        ie.executable = False
 
556
        ie.revision = 'file-rev-id'
 
557
        ie.text_sha1 = '123456'
 
558
        ie.text_size = 25
 
559
        bytes = inv._entry_to_bytes(ie)
 
560
        self.assertEqual('file: file-id\nparent-id\n\xce\xa9name\n'
 
561
                         'file-rev-id\n123456\n25\nN', bytes)
 
562
        ie2 = inv._bytes_to_entry(bytes)
 
563
        self.assertEqual(ie, ie2)
 
564
        self.assertIsInstance(ie2.name, unicode)
 
565
        self.assertEqual(('\xce\xa9name', 'file-id', 'file-rev-id'),
 
566
                         inv._bytes_to_utf8name_key(bytes))
 
567
 
 
568
    def test_dir_entry_to_bytes(self):
 
569
        inv = CHKInventory(None)
 
570
        ie = inventory.InventoryDirectory('dir-id', 'dirname', 'parent-id')
 
571
        ie.revision = 'dir-rev-id'
 
572
        bytes = inv._entry_to_bytes(ie)
 
573
        self.assertEqual('dir: dir-id\nparent-id\ndirname\ndir-rev-id', bytes)
 
574
        ie2 = inv._bytes_to_entry(bytes)
 
575
        self.assertEqual(ie, ie2)
 
576
        self.assertIsInstance(ie2.name, unicode)
 
577
        self.assertEqual(('dirname', 'dir-id', 'dir-rev-id'),
 
578
                         inv._bytes_to_utf8name_key(bytes))
 
579
 
 
580
    def test_dir2_entry_to_bytes(self):
 
581
        inv = CHKInventory(None)
 
582
        ie = inventory.InventoryDirectory('dir-id', u'dir\u03a9name',
 
583
                                          None)
 
584
        ie.revision = 'dir-rev-id'
 
585
        bytes = inv._entry_to_bytes(ie)
 
586
        self.assertEqual('dir: dir-id\n\ndir\xce\xa9name\n'
 
587
                         'dir-rev-id', bytes)
 
588
        ie2 = inv._bytes_to_entry(bytes)
 
589
        self.assertEqual(ie, ie2)
 
590
        self.assertIsInstance(ie2.name, unicode)
 
591
        self.assertIs(ie2.parent_id, None)
 
592
        self.assertEqual(('dir\xce\xa9name', 'dir-id', 'dir-rev-id'),
 
593
                         inv._bytes_to_utf8name_key(bytes))
 
594
 
 
595
    def test_symlink_entry_to_bytes(self):
 
596
        inv = CHKInventory(None)
 
597
        ie = inventory.InventoryLink('link-id', 'linkname', 'parent-id')
 
598
        ie.revision = 'link-rev-id'
 
599
        ie.symlink_target = u'target/path'
 
600
        bytes = inv._entry_to_bytes(ie)
 
601
        self.assertEqual('symlink: link-id\nparent-id\nlinkname\n'
 
602
                         'link-rev-id\ntarget/path', bytes)
 
603
        ie2 = inv._bytes_to_entry(bytes)
 
604
        self.assertEqual(ie, ie2)
 
605
        self.assertIsInstance(ie2.name, unicode)
 
606
        self.assertIsInstance(ie2.symlink_target, unicode)
 
607
        self.assertEqual(('linkname', 'link-id', 'link-rev-id'),
 
608
                         inv._bytes_to_utf8name_key(bytes))
 
609
 
 
610
    def test_symlink2_entry_to_bytes(self):
 
611
        inv = CHKInventory(None)
 
612
        ie = inventory.InventoryLink('link-id', u'link\u03a9name', 'parent-id')
 
613
        ie.revision = 'link-rev-id'
 
614
        ie.symlink_target = u'target/\u03a9path'
 
615
        bytes = inv._entry_to_bytes(ie)
 
616
        self.assertEqual('symlink: link-id\nparent-id\nlink\xce\xa9name\n'
 
617
                         'link-rev-id\ntarget/\xce\xa9path', bytes)
 
618
        ie2 = inv._bytes_to_entry(bytes)
 
619
        self.assertEqual(ie, ie2)
 
620
        self.assertIsInstance(ie2.name, unicode)
 
621
        self.assertIsInstance(ie2.symlink_target, unicode)
 
622
        self.assertEqual(('link\xce\xa9name', 'link-id', 'link-rev-id'),
 
623
                         inv._bytes_to_utf8name_key(bytes))
 
624
 
 
625
    def test_tree_reference_entry_to_bytes(self):
 
626
        inv = CHKInventory(None)
 
627
        ie = inventory.TreeReference('tree-root-id', u'tree\u03a9name',
 
628
                                     'parent-id')
 
629
        ie.revision = 'tree-rev-id'
 
630
        ie.reference_revision = 'ref-rev-id'
 
631
        bytes = inv._entry_to_bytes(ie)
 
632
        self.assertEqual('tree: tree-root-id\nparent-id\ntree\xce\xa9name\n'
 
633
                         'tree-rev-id\nref-rev-id', bytes)
 
634
        ie2 = inv._bytes_to_entry(bytes)
 
635
        self.assertEqual(ie, ie2)
 
636
        self.assertIsInstance(ie2.name, unicode)
 
637
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
 
638
                         inv._bytes_to_utf8name_key(bytes))