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