/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2729.2.5 by Martin Pool
Move per-inventory tests from test_inv to tests.inventory_implementations
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
963 by Martin Pool
- add the start of a test for inventory file-id matching
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
963 by Martin Pool
- add the start of a test for inventory file-id matching
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
963 by Martin Pool
- add the start of a test for inventory file-id matching
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
963 by Martin Pool
- add the start of a test for inventory file-id matching
16
2729.2.5 by Martin Pool
Move per-inventory tests from test_inv to tests.inventory_implementations
17
4241.6.5 by Robert Collins, John Arbash Meinel
CHKInventory support from brisbane-core.
18
from bzrlib import errors, chk_map, inventory, osutils
19
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
20
    InventoryDirectory, InventoryEntry, TreeReference)
4241.6.5 by Robert Collins, John Arbash Meinel
CHKInventory support from brisbane-core.
21
from bzrlib.tests import TestCase, TestCaseWithTransport
963 by Martin Pool
- add the start of a test for inventory file-id matching
22
969 by Martin Pool
- Add less-sucky is_within_any
23
1407 by Robert Collins
define some expected behaviour for inventory_entry.snapshot
24
class TestInventoryEntry(TestCase):
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
25
26
    def test_file_kind_character(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
27
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
28
        self.assertEqual(file.kind_character(), '')
29
30
    def test_dir_kind_character(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
31
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
32
        self.assertEqual(dir.kind_character(), '/')
33
34
    def test_link_kind_character(self):
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
35
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
36
        self.assertEqual(dir.kind_character(), '')
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
37
38
    def test_dir_detect_changes(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
39
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
40
        left.text_sha1 = 123
41
        left.executable = True
42
        left.symlink_target='foo'
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
43
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
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):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
50
        left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
51
        left.text_sha1 = 123
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
52
        right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
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):
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
64
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
65
        left.text_sha1 = 123
66
        left.executable = True
67
        left.symlink_target='foo'
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
68
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
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))
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
76
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
77
    def test_file_has_text(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
78
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
79
        self.failUnless(file.has_text())
80
81
    def test_directory_has_text(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
82
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
83
        self.failIf(dir.has_text())
84
85
    def test_link_has_text(self):
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
86
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
87
        self.failIf(link.has_text())
88
1713.1.11 by Robert Collins
refactor smart_add to pass around the parent inventory entry and use that, resulting in another 100bzrlib/inventory.py performance improvement, and making inventory writing the dominating factory in add. (Robert Collins)
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)
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
96
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
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
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
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
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
143
        # in this case it's both renamed and modified; show a rename and
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
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)
4241.6.5 by Robert Collins, John Arbash Meinel
CHKInventory support from brisbane-core.
169
170
171
class TestCHKInventory(TestCaseWithTransport):
172
173
    def get_chk_bytes(self):
174
        # The easiest way to get a CHK store is a development5 repository and
175
        # then work with the chk_bytes attribute directly.
176
        from bzrlib.tests import TestSkipped
177
        raise TestSkipped("Cannot execut CHKInventory tests yet.")
178
        repo = self.make_repository(".", format="development5")
179
        repo.lock_write()
180
        self.addCleanup(repo.unlock)
181
        repo.start_write_group()
182
        self.addCleanup(repo.abort_write_group)
183
        return repo.chk_bytes
184
185
    def read_bytes(self, chk_bytes, key):
186
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
187
        return stream.next().get_bytes_as("fulltext")
188
189
    def test_deserialise_gives_CHKInventory(self):
190
        inv = Inventory()
191
        inv.revision_id = "revid"
192
        inv.root.revision = "rootrev"
193
        chk_bytes = self.get_chk_bytes()
194
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
195
        bytes = ''.join(chk_inv.to_lines())
196
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
197
        self.assertEqual("revid", new_inv.revision_id)
198
        self.assertEqual("directory", new_inv.root.kind)
199
        self.assertEqual(inv.root.file_id, new_inv.root.file_id)
200
        self.assertEqual(inv.root.parent_id, new_inv.root.parent_id)
201
        self.assertEqual(inv.root.name, new_inv.root.name)
202
        self.assertEqual("rootrev", new_inv.root.revision)
203
        self.assertEqual('plain', new_inv._search_key_name)
204
205
    def test_deserialise_wrong_revid(self):
206
        inv = Inventory()
207
        inv.revision_id = "revid"
208
        inv.root.revision = "rootrev"
209
        chk_bytes = self.get_chk_bytes()
210
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
211
        bytes = ''.join(chk_inv.to_lines())
212
        self.assertRaises(ValueError, CHKInventory.deserialise, chk_bytes,
213
            bytes, ("revid2",))
214
215
    def test_captures_rev_root_byid(self):
216
        inv = Inventory()
217
        inv.revision_id = "foo"
218
        inv.root.revision = "bar"
219
        chk_bytes = self.get_chk_bytes()
220
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
221
        lines = chk_inv.to_lines()
222
        self.assertEqual([
223
            'chkinventory:\n',
224
            'revision_id: foo\n',
225
            'root_id: TREE_ROOT\n',
226
            'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
227
            'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
228
            ], lines)
229
        chk_inv = CHKInventory.deserialise(chk_bytes, ''.join(lines), ('foo',))
230
        self.assertEqual('plain', chk_inv._search_key_name)
231
232
    def test_captures_parent_id_basename_index(self):
233
        inv = Inventory()
234
        inv.revision_id = "foo"
235
        inv.root.revision = "bar"
236
        chk_bytes = self.get_chk_bytes()
237
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
238
        lines = chk_inv.to_lines()
239
        self.assertEqual([
240
            'chkinventory:\n',
241
            'revision_id: foo\n',
242
            'root_id: TREE_ROOT\n',
243
            'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
244
            'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
245
            ], lines)
246
        chk_inv = CHKInventory.deserialise(chk_bytes, ''.join(lines), ('foo',))
247
        self.assertEqual('plain', chk_inv._search_key_name)
248
249
    def test_captures_search_key_name(self):
250
        inv = Inventory()
251
        inv.revision_id = "foo"
252
        inv.root.revision = "bar"
253
        chk_bytes = self.get_chk_bytes()
254
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv,
255
                                              search_key_name='hash-16-way')
256
        lines = chk_inv.to_lines()
257
        self.assertEqual([
258
            'chkinventory:\n',
259
            'search_key_name: hash-16-way\n',
260
            'root_id: TREE_ROOT\n',
261
            'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
262
            'revision_id: foo\n',
263
            'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
264
            ], lines)
265
        chk_inv = CHKInventory.deserialise(chk_bytes, ''.join(lines), ('foo',))
266
        self.assertEqual('hash-16-way', chk_inv._search_key_name)
267
268
    def test_directory_children_on_demand(self):
269
        inv = Inventory()
270
        inv.revision_id = "revid"
271
        inv.root.revision = "rootrev"
272
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
273
        inv["fileid"].revision = "filerev"
274
        inv["fileid"].executable = True
275
        inv["fileid"].text_sha1 = "ffff"
276
        inv["fileid"].text_size = 1
277
        chk_bytes = self.get_chk_bytes()
278
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
279
        bytes = ''.join(chk_inv.to_lines())
280
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
281
        root_entry = new_inv[inv.root.file_id]
282
        self.assertEqual(None, root_entry._children)
283
        self.assertEqual(['file'], root_entry.children.keys())
284
        file_direct = new_inv["fileid"]
285
        file_found = root_entry.children['file']
286
        self.assertEqual(file_direct.kind, file_found.kind)
287
        self.assertEqual(file_direct.file_id, file_found.file_id)
288
        self.assertEqual(file_direct.parent_id, file_found.parent_id)
289
        self.assertEqual(file_direct.name, file_found.name)
290
        self.assertEqual(file_direct.revision, file_found.revision)
291
        self.assertEqual(file_direct.text_sha1, file_found.text_sha1)
292
        self.assertEqual(file_direct.text_size, file_found.text_size)
293
        self.assertEqual(file_direct.executable, file_found.executable)
294
295
    def test_from_inventory_maximum_size(self):
296
        # from_inventory supports the maximum_size parameter.
297
        inv = Inventory()
298
        inv.revision_id = "revid"
299
        inv.root.revision = "rootrev"
300
        chk_bytes = self.get_chk_bytes()
301
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv, 120)
302
        self.assertEqual(120, chk_inv.id_to_entry._root_node.maximum_size)
303
304
    def test___iter__(self):
305
        inv = Inventory()
306
        inv.revision_id = "revid"
307
        inv.root.revision = "rootrev"
308
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
309
        inv["fileid"].revision = "filerev"
310
        inv["fileid"].executable = True
311
        inv["fileid"].text_sha1 = "ffff"
312
        inv["fileid"].text_size = 1
313
        chk_bytes = self.get_chk_bytes()
314
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
315
        bytes = ''.join(chk_inv.to_lines())
316
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
317
        fileids = list(new_inv.__iter__())
318
        fileids.sort()
319
        self.assertEqual([inv.root.file_id, "fileid"], fileids)
320
321
    def test__len__(self):
322
        inv = Inventory()
323
        inv.revision_id = "revid"
324
        inv.root.revision = "rootrev"
325
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
326
        inv["fileid"].revision = "filerev"
327
        inv["fileid"].executable = True
328
        inv["fileid"].text_sha1 = "ffff"
329
        inv["fileid"].text_size = 1
330
        chk_bytes = self.get_chk_bytes()
331
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
332
        self.assertEqual(2, len(chk_inv))
333
334
    def test___getitem__(self):
335
        inv = Inventory()
336
        inv.revision_id = "revid"
337
        inv.root.revision = "rootrev"
338
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
339
        inv["fileid"].revision = "filerev"
340
        inv["fileid"].executable = True
341
        inv["fileid"].text_sha1 = "ffff"
342
        inv["fileid"].text_size = 1
343
        chk_bytes = self.get_chk_bytes()
344
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
345
        bytes = ''.join(chk_inv.to_lines())
346
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
347
        root_entry = new_inv[inv.root.file_id]
348
        file_entry = new_inv["fileid"]
349
        self.assertEqual("directory", root_entry.kind)
350
        self.assertEqual(inv.root.file_id, root_entry.file_id)
351
        self.assertEqual(inv.root.parent_id, root_entry.parent_id)
352
        self.assertEqual(inv.root.name, root_entry.name)
353
        self.assertEqual("rootrev", root_entry.revision)
354
        self.assertEqual("file", file_entry.kind)
355
        self.assertEqual("fileid", file_entry.file_id)
356
        self.assertEqual(inv.root.file_id, file_entry.parent_id)
357
        self.assertEqual("file", file_entry.name)
358
        self.assertEqual("filerev", file_entry.revision)
359
        self.assertEqual("ffff", file_entry.text_sha1)
360
        self.assertEqual(1, file_entry.text_size)
361
        self.assertEqual(True, file_entry.executable)
362
        self.assertRaises(errors.NoSuchId, new_inv.__getitem__, 'missing')
363
364
    def test_has_id_true(self):
365
        inv = Inventory()
366
        inv.revision_id = "revid"
367
        inv.root.revision = "rootrev"
368
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
369
        inv["fileid"].revision = "filerev"
370
        inv["fileid"].executable = True
371
        inv["fileid"].text_sha1 = "ffff"
372
        inv["fileid"].text_size = 1
373
        chk_bytes = self.get_chk_bytes()
374
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
375
        self.assertTrue(chk_inv.has_id('fileid'))
376
        self.assertTrue(chk_inv.has_id(inv.root.file_id))
377
378
    def test_has_id_not(self):
379
        inv = Inventory()
380
        inv.revision_id = "revid"
381
        inv.root.revision = "rootrev"
382
        chk_bytes = self.get_chk_bytes()
383
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
384
        self.assertFalse(chk_inv.has_id('fileid'))
385
386
    def test_id2path(self):
387
        inv = Inventory()
388
        inv.revision_id = "revid"
389
        inv.root.revision = "rootrev"
390
        direntry = InventoryDirectory("dirid", "dir", inv.root.file_id)
391
        fileentry = InventoryFile("fileid", "file", "dirid")
392
        inv.add(direntry)
393
        inv.add(fileentry)
394
        inv["fileid"].revision = "filerev"
395
        inv["fileid"].executable = True
396
        inv["fileid"].text_sha1 = "ffff"
397
        inv["fileid"].text_size = 1
398
        inv["dirid"].revision = "filerev"
399
        chk_bytes = self.get_chk_bytes()
400
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
401
        bytes = ''.join(chk_inv.to_lines())
402
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
403
        self.assertEqual('', new_inv.id2path(inv.root.file_id))
404
        self.assertEqual('dir', new_inv.id2path('dirid'))
405
        self.assertEqual('dir/file', new_inv.id2path('fileid'))
406
407
    def test_path2id(self):
408
        inv = Inventory()
409
        inv.revision_id = "revid"
410
        inv.root.revision = "rootrev"
411
        direntry = InventoryDirectory("dirid", "dir", inv.root.file_id)
412
        fileentry = InventoryFile("fileid", "file", "dirid")
413
        inv.add(direntry)
414
        inv.add(fileentry)
415
        inv["fileid"].revision = "filerev"
416
        inv["fileid"].executable = True
417
        inv["fileid"].text_sha1 = "ffff"
418
        inv["fileid"].text_size = 1
419
        inv["dirid"].revision = "filerev"
420
        chk_bytes = self.get_chk_bytes()
421
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
422
        bytes = ''.join(chk_inv.to_lines())
423
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
424
        self.assertEqual(inv.root.file_id, new_inv.path2id(''))
425
        self.assertEqual('dirid', new_inv.path2id('dir'))
426
        self.assertEqual('fileid', new_inv.path2id('dir/file'))
427
428
    def test_create_by_apply_delta_sets_root(self):
429
        inv = Inventory()
430
        inv.revision_id = "revid"
431
        chk_bytes = self.get_chk_bytes()
432
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
433
        inv.add_path("", "directory", "myrootid", None)
434
        inv.revision_id = "expectedid"
435
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
436
        delta = [(None, "",  "myrootid", inv.root)]
437
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
438
        self.assertEquals(reference_inv.root, new_inv.root)
439
440
    def test_create_by_apply_delta_empty_add_child(self):
441
        inv = Inventory()
442
        inv.revision_id = "revid"
443
        inv.root.revision = "rootrev"
444
        chk_bytes = self.get_chk_bytes()
445
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
446
        a_entry = InventoryFile("A-id", "A", inv.root.file_id)
447
        a_entry.revision = "filerev"
448
        a_entry.executable = True
449
        a_entry.text_sha1 = "ffff"
450
        a_entry.text_size = 1
451
        inv.add(a_entry)
452
        inv.revision_id = "expectedid"
453
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
454
        delta = [(None, "A",  "A-id", a_entry)]
455
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
456
        # new_inv should be the same as reference_inv.
457
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
458
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
459
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
460
            new_inv.id_to_entry._root_node._key)
461
462
    def test_create_by_apply_delta_empty_add_child_updates_parent_id(self):
463
        inv = Inventory()
464
        inv.revision_id = "revid"
465
        inv.root.revision = "rootrev"
466
        chk_bytes = self.get_chk_bytes()
467
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
468
        a_entry = InventoryFile("A-id", "A", inv.root.file_id)
469
        a_entry.revision = "filerev"
470
        a_entry.executable = True
471
        a_entry.text_sha1 = "ffff"
472
        a_entry.text_size = 1
473
        inv.add(a_entry)
474
        inv.revision_id = "expectedid"
475
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
476
        delta = [(None, "A",  "A-id", a_entry)]
477
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
478
        # new_inv should be the same as reference_inv.
479
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
480
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
481
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
482
            new_inv.id_to_entry._root_node._key)
483
        self.assertEqual(reference_inv.parent_id_basename_to_file_id._root_node._key,
484
            new_inv.parent_id_basename_to_file_id._root_node._key)
485
486
    def test_iter_changes(self):
487
        # Low level bootstrapping smoke test; comprehensive generic tests via
488
        # InterTree are coming.
489
        inv = Inventory()
490
        inv.revision_id = "revid"
491
        inv.root.revision = "rootrev"
492
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
493
        inv["fileid"].revision = "filerev"
494
        inv["fileid"].executable = True
495
        inv["fileid"].text_sha1 = "ffff"
496
        inv["fileid"].text_size = 1
497
        inv2 = Inventory()
498
        inv2.revision_id = "revid2"
499
        inv2.root.revision = "rootrev"
500
        inv2.add(InventoryFile("fileid", "file", inv.root.file_id))
501
        inv2["fileid"].revision = "filerev2"
502
        inv2["fileid"].executable = False
503
        inv2["fileid"].text_sha1 = "bbbb"
504
        inv2["fileid"].text_size = 2
505
        # get fresh objects.
506
        chk_bytes = self.get_chk_bytes()
507
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
508
        bytes = ''.join(chk_inv.to_lines())
509
        inv_1 = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
510
        chk_inv2 = CHKInventory.from_inventory(chk_bytes, inv2)
511
        bytes = ''.join(chk_inv2.to_lines())
512
        inv_2 = CHKInventory.deserialise(chk_bytes, bytes, ("revid2",))
513
        self.assertEqual([('fileid', (u'file', u'file'), True, (True, True),
514
            ('TREE_ROOT', 'TREE_ROOT'), (u'file', u'file'), ('file', 'file'),
515
            (False, True))],
516
            list(inv_1.iter_changes(inv_2)))
517
518
    def test_parent_id_basename_to_file_id_index_enabled(self):
519
        inv = Inventory()
520
        inv.revision_id = "revid"
521
        inv.root.revision = "rootrev"
522
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
523
        inv["fileid"].revision = "filerev"
524
        inv["fileid"].executable = True
525
        inv["fileid"].text_sha1 = "ffff"
526
        inv["fileid"].text_size = 1
527
        # get fresh objects.
528
        chk_bytes = self.get_chk_bytes()
529
        tmp_inv = CHKInventory.from_inventory(chk_bytes, inv)
530
        bytes = ''.join(tmp_inv.to_lines())
531
        chk_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
532
        self.assertIsInstance(chk_inv.parent_id_basename_to_file_id, chk_map.CHKMap)
533
        self.assertEqual(
534
            {('', ''): 'TREE_ROOT', ('TREE_ROOT', 'file'): 'fileid'},
535
            dict(chk_inv.parent_id_basename_to_file_id.iteritems()))
536
537
    def test_file_entry_to_bytes(self):
538
        inv = CHKInventory(None)
539
        ie = inventory.InventoryFile('file-id', 'filename', 'parent-id')
540
        ie.executable = True
541
        ie.revision = 'file-rev-id'
542
        ie.text_sha1 = 'abcdefgh'
543
        ie.text_size = 100
544
        bytes = inv._entry_to_bytes(ie)
545
        self.assertEqual('file: file-id\nparent-id\nfilename\n'
546
                         'file-rev-id\nabcdefgh\n100\nY', bytes)
547
        ie2 = inv._bytes_to_entry(bytes)
548
        self.assertEqual(ie, ie2)
549
        self.assertIsInstance(ie2.name, unicode)
550
        self.assertEqual(('filename', 'file-id', 'file-rev-id'),
551
                         inv._bytes_to_utf8name_key(bytes))
552
553
    def test_file2_entry_to_bytes(self):
554
        inv = CHKInventory(None)
555
        # \u30a9 == 'omega'
556
        ie = inventory.InventoryFile('file-id', u'\u03a9name', 'parent-id')
557
        ie.executable = False
558
        ie.revision = 'file-rev-id'
559
        ie.text_sha1 = '123456'
560
        ie.text_size = 25
561
        bytes = inv._entry_to_bytes(ie)
562
        self.assertEqual('file: file-id\nparent-id\n\xce\xa9name\n'
563
                         'file-rev-id\n123456\n25\nN', bytes)
564
        ie2 = inv._bytes_to_entry(bytes)
565
        self.assertEqual(ie, ie2)
566
        self.assertIsInstance(ie2.name, unicode)
567
        self.assertEqual(('\xce\xa9name', 'file-id', 'file-rev-id'),
568
                         inv._bytes_to_utf8name_key(bytes))
569
570
    def test_dir_entry_to_bytes(self):
571
        inv = CHKInventory(None)
572
        ie = inventory.InventoryDirectory('dir-id', 'dirname', 'parent-id')
573
        ie.revision = 'dir-rev-id'
574
        bytes = inv._entry_to_bytes(ie)
575
        self.assertEqual('dir: dir-id\nparent-id\ndirname\ndir-rev-id', bytes)
576
        ie2 = inv._bytes_to_entry(bytes)
577
        self.assertEqual(ie, ie2)
578
        self.assertIsInstance(ie2.name, unicode)
579
        self.assertEqual(('dirname', 'dir-id', 'dir-rev-id'),
580
                         inv._bytes_to_utf8name_key(bytes))
581
582
    def test_dir2_entry_to_bytes(self):
583
        inv = CHKInventory(None)
584
        ie = inventory.InventoryDirectory('dir-id', u'dir\u03a9name',
585
                                          None)
586
        ie.revision = 'dir-rev-id'
587
        bytes = inv._entry_to_bytes(ie)
588
        self.assertEqual('dir: dir-id\n\ndir\xce\xa9name\n'
589
                         'dir-rev-id', bytes)
590
        ie2 = inv._bytes_to_entry(bytes)
591
        self.assertEqual(ie, ie2)
592
        self.assertIsInstance(ie2.name, unicode)
593
        self.assertIs(ie2.parent_id, None)
594
        self.assertEqual(('dir\xce\xa9name', 'dir-id', 'dir-rev-id'),
595
                         inv._bytes_to_utf8name_key(bytes))
596
597
    def test_symlink_entry_to_bytes(self):
598
        inv = CHKInventory(None)
599
        ie = inventory.InventoryLink('link-id', 'linkname', 'parent-id')
600
        ie.revision = 'link-rev-id'
601
        ie.symlink_target = u'target/path'
602
        bytes = inv._entry_to_bytes(ie)
603
        self.assertEqual('symlink: link-id\nparent-id\nlinkname\n'
604
                         'link-rev-id\ntarget/path', bytes)
605
        ie2 = inv._bytes_to_entry(bytes)
606
        self.assertEqual(ie, ie2)
607
        self.assertIsInstance(ie2.name, unicode)
608
        self.assertIsInstance(ie2.symlink_target, unicode)
609
        self.assertEqual(('linkname', 'link-id', 'link-rev-id'),
610
                         inv._bytes_to_utf8name_key(bytes))
611
612
    def test_symlink2_entry_to_bytes(self):
613
        inv = CHKInventory(None)
614
        ie = inventory.InventoryLink('link-id', u'link\u03a9name', 'parent-id')
615
        ie.revision = 'link-rev-id'
616
        ie.symlink_target = u'target/\u03a9path'
617
        bytes = inv._entry_to_bytes(ie)
618
        self.assertEqual('symlink: link-id\nparent-id\nlink\xce\xa9name\n'
619
                         'link-rev-id\ntarget/\xce\xa9path', bytes)
620
        ie2 = inv._bytes_to_entry(bytes)
621
        self.assertEqual(ie, ie2)
622
        self.assertIsInstance(ie2.name, unicode)
623
        self.assertIsInstance(ie2.symlink_target, unicode)
624
        self.assertEqual(('link\xce\xa9name', 'link-id', 'link-rev-id'),
625
                         inv._bytes_to_utf8name_key(bytes))
626
627
    def test_tree_reference_entry_to_bytes(self):
628
        inv = CHKInventory(None)
629
        ie = inventory.TreeReference('tree-root-id', u'tree\u03a9name',
630
                                     'parent-id')
631
        ie.revision = 'tree-rev-id'
632
        ie.reference_revision = 'ref-rev-id'
633
        bytes = inv._entry_to_bytes(ie)
634
        self.assertEqual('tree: tree-root-id\nparent-id\ntree\xce\xa9name\n'
635
                         'tree-rev-id\nref-rev-id', bytes)
636
        ie2 = inv._bytes_to_entry(bytes)
637
        self.assertEqual(ie, ie2)
638
        self.assertIsInstance(ie2.name, unicode)
639
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
640
                         inv._bytes_to_utf8name_key(bytes))