/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005-2012, 2016 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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
18
from .. import (
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
19
    errors,
20
    osutils,
21
    repository,
22
    revision,
4634.51.1 by John Arbash Meinel
Switch away from creating a whole repository just to get a VF.
23
    tests,
5662.3.1 by Jelmer Vernooij
Add WorkingTreeFormatRegistry.
24
    workingtree,
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
25
    )
6973.6.2 by Jelmer Vernooij
Fix more tests.
26
from ..sixish import text_type
6670.4.1 by Jelmer Vernooij
Update imports.
27
from ..bzr import (
28
    chk_map,
29
    groupcompress,
30
    inventory,
31
    )
32
from ..bzr.inventory import (
5579.3.1 by Jelmer Vernooij
Remove unused imports.
33
    CHKInventory,
34
    Inventory,
35
    ROOT_ID,
36
    InventoryFile,
37
    InventoryDirectory,
38
    InventoryEntry,
39
    TreeReference,
5802.1.2 by Jelmer Vernooij
Add test for mutable_inventory_from_tree.
40
    mutable_inventory_from_tree,
5579.3.1 by Jelmer Vernooij
Remove unused imports.
41
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
42
from . import (
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
43
    TestCase,
44
    TestCaseWithTransport,
45
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
46
from .scenarios import load_tests_apply_scenarios
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
47
48
49
load_tests = load_tests_apply_scenarios
50
51
52
def delta_application_scenarios():
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
53
    scenarios = [
54
        ('Inventory', {'apply_delta':apply_inventory_Inventory}),
55
        ]
56
    # Working tree basis delta application
57
    # Repository add_inv_by_delta.
58
    # Reduce form of the per_repository test logic - that logic needs to be
59
    # be able to get /just/ repositories whereas these tests are fine with
60
    # just creating trees.
61
    formats = set()
62
    for _, format in repository.format_registry.iteritems():
5718.3.1 by Jelmer Vernooij
Skip more tests for repository formats that don't support the full
63
        if format.supports_full_versioned_files:
64
            scenarios.append((str(format.__name__), {
65
                'apply_delta':apply_inventory_Repository_add_inventory_by_delta,
66
                'format':format}))
7002.1.1 by Martin
Avoid import error from test_inv without dulwich
67
    for getter in workingtree.format_registry._get_all_lazy():
68
        try:
69
            format = getter()
70
            if callable(format):
71
                format = format()
72
        except ImportError:
73
            pass  # Format with unmet dependency
6746.2.1 by Jelmer Vernooij
Rename matchingbzrdir to matchingcontroldir.
74
        repo_fmt = format._matchingcontroldir.repository_format
5718.3.1 by Jelmer Vernooij
Skip more tests for repository formats that don't support the full
75
        if not repo_fmt.supports_full_versioned_files:
76
            continue
4526.9.2 by Robert Collins
Handle deltas with new paths not matching the actual path.
77
        scenarios.append(
78
            (str(format.__class__.__name__) + ".update_basis_by_delta", {
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
79
            'apply_delta':apply_inventory_WT_basis,
80
            'format':format}))
4526.9.2 by Robert Collins
Handle deltas with new paths not matching the actual path.
81
        scenarios.append(
82
            (str(format.__class__.__name__) + ".apply_inventory_delta", {
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
83
            'apply_delta':apply_inventory_WT,
84
            'format':format}))
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
85
    return scenarios
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
86
87
4634.35.19 by Andrew Bennetts
Fix test_inv.
88
def create_texts_for_inv(repo, inv):
89
    for path, ie in inv.iter_entries():
90
        if ie.text_size:
6973.11.6 by Jelmer Vernooij
Fix more http tests.
91
            lines = [b'a' * ie.text_size]
4634.35.19 by Andrew Bennetts
Fix test_inv.
92
        else:
93
            lines = []
94
        repo.texts.add_lines((ie.file_id, ie.revision), [], lines)
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
95
5718.3.1 by Jelmer Vernooij
Skip more tests for repository formats that don't support the full
96
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
97
def apply_inventory_Inventory(self, basis, delta, invalid_delta=True):
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
98
    """Apply delta to basis and return the result.
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
99
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
100
    :param basis: An inventory to be used as the basis.
101
    :param delta: The inventory delta to apply:
102
    :return: An inventory resulting from the application.
103
    """
104
    basis.apply_delta(delta)
105
    return basis
106
107
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
108
def apply_inventory_WT(self, basis, delta, invalid_delta=True):
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
109
    """Apply delta to basis and return the result.
110
111
    This sets the tree state to be basis, and then calls apply_inventory_delta.
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
112
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
113
    :param basis: An inventory to be used as the basis.
114
    :param delta: The inventory delta to apply:
115
    :return: An inventory resulting from the application.
116
    """
6746.2.1 by Jelmer Vernooij
Rename matchingbzrdir to matchingcontroldir.
117
    control = self.make_controldir('tree', format=self.format._matchingcontroldir)
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
118
    control.create_repository()
119
    control.create_branch()
120
    tree = self.format.initialize(control)
121
    tree.lock_write()
122
    try:
123
        tree._write_inventory(basis)
124
    finally:
125
        tree.unlock()
126
    # Fresh object, reads disk again.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
127
    tree = tree.controldir.open_workingtree()
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
128
    tree.lock_write()
129
    try:
130
        tree.apply_inventory_delta(delta)
131
    finally:
132
        tree.unlock()
133
    # reload tree - ensure we get what was written.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
134
    tree = tree.controldir.open_workingtree()
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
135
    tree.lock_read()
136
    self.addCleanup(tree.unlock)
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
137
    if not invalid_delta:
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
138
        tree._validate()
6405.2.9 by Jelmer Vernooij
More test fixes.
139
    return tree.root_inventory
4526.9.1 by Robert Collins
Add WorkingTree.apply_inventory_delta to the set of delta implementations interface tested.
140
141
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
142
def _create_repo_revisions(repo, basis, delta, invalid_delta):
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
143
    repo.start_write_group()
144
    try:
6973.11.6 by Jelmer Vernooij
Fix more http tests.
145
        rev = revision.Revision(b'basis', timestamp=0, timezone=None,
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
146
            message="", committer="foo@example.com")
6973.11.6 by Jelmer Vernooij
Fix more http tests.
147
        basis.revision_id = b'basis'
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
148
        create_texts_for_inv(repo, basis)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
149
        repo.add_revision(b'basis', rev, basis)
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
150
        if invalid_delta:
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
151
            # We don't want to apply the delta to the basis, because we expect
152
            # the delta is invalid.
153
            result_inv = basis
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
154
            result_inv.revision_id = b'result'
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
155
            target_entries = None
156
        else:
6973.11.6 by Jelmer Vernooij
Fix more http tests.
157
            result_inv = basis.create_by_apply_delta(delta, b'result')
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
158
            create_texts_for_inv(repo, result_inv)
159
            target_entries = list(result_inv.iter_entries_by_dir())
6973.11.6 by Jelmer Vernooij
Fix more http tests.
160
        rev = revision.Revision(b'result', timestamp=0, timezone=None,
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
161
            message="", committer="foo@example.com")
6973.11.6 by Jelmer Vernooij
Fix more http tests.
162
        repo.add_revision(b'result', rev, result_inv)
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
163
        repo.commit_write_group()
164
    except:
165
        repo.abort_write_group()
166
        raise
167
    return target_entries
168
169
170
def _get_basis_entries(tree):
171
    basis_tree = tree.basis_tree()
172
    basis_tree.lock_read()
173
    basis_tree_entries = list(basis_tree.inventory.iter_entries_by_dir())
174
    basis_tree.unlock()
175
    return basis_tree_entries
176
177
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
178
def _populate_different_tree(tree, basis, delta):
179
    """Put all entries into tree, but at a unique location."""
180
    added_ids = set()
181
    added_paths = set()
6855.4.1 by Jelmer Vernooij
Yet more bees.
182
    tree.add(['unique-dir'], [b'unique-dir-id'], ['directory'])
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
183
    for path, ie in basis.iter_entries_by_dir():
184
        if ie.file_id in added_ids:
185
            continue
186
        # We want a unique path for each of these, we use the file-id
187
        tree.add(['unique-dir/' + ie.file_id], [ie.file_id], [ie.kind])
188
        added_ids.add(ie.file_id)
189
    for old_path, new_path, file_id, ie in delta:
190
        if file_id in added_ids:
191
            continue
192
        tree.add(['unique-dir/' + file_id], [file_id], [ie.kind])
193
194
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
195
def apply_inventory_WT_basis(test, basis, delta, invalid_delta=True):
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
196
    """Apply delta to basis and return the result.
197
198
    This sets the parent and then calls update_basis_by_delta.
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
199
    It also puts the basis in the repository under both 'basis' and 'result' to
200
    allow safety checks made by the WT to succeed, and finally ensures that all
201
    items in the delta with a new path are present in the WT before calling
202
    update_basis_by_delta.
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
203
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
204
    :param basis: An inventory to be used as the basis.
205
    :param delta: The inventory delta to apply:
206
    :return: An inventory resulting from the application.
207
    """
6746.2.1 by Jelmer Vernooij
Rename matchingbzrdir to matchingcontroldir.
208
    control = test.make_controldir('tree', format=test.format._matchingcontroldir)
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
209
    control.create_repository()
210
    control.create_branch()
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
211
    tree = test.format.initialize(control)
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
212
    tree.lock_write()
213
    try:
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
214
        target_entries = _create_repo_revisions(tree.branch.repository, basis,
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
215
                                                delta, invalid_delta)
4505.5.5 by Robert Collins
Parents used in a delta must be directories.
216
        # Set the basis state as the trees current state
217
        tree._write_inventory(basis)
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
218
        # This reads basis from the repo and puts it into the tree's local
219
        # cache, if it has one.
6973.10.6 by Jelmer Vernooij
Fix tests.
220
        tree.set_parent_ids([b'basis'])
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
221
    finally:
222
        tree.unlock()
223
    # Fresh lock, reads disk again.
6973.10.6 by Jelmer Vernooij
Fix tests.
224
    with tree.lock_write():
6973.11.6 by Jelmer Vernooij
Fix more http tests.
225
        tree.update_basis_by_delta(b'result', delta)
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
226
        if not invalid_delta:
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
227
            tree._validate()
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
228
    # reload tree - ensure we get what was written.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
229
    tree = tree.controldir.open_workingtree()
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
230
    basis_tree = tree.basis_tree()
231
    basis_tree.lock_read()
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
232
    test.addCleanup(basis_tree.unlock)
6405.2.10 by Jelmer Vernooij
Fix more tests.
233
    basis_inv = basis_tree.root_inventory
5847.4.12 by John Arbash Meinel
(broken) Expand the test coverage for cases we care about.
234
    if target_entries:
235
        basis_entries = list(basis_inv.iter_entries_by_dir())
236
        test.assertEqual(target_entries, basis_entries)
237
    return basis_inv
238
239
240
def apply_inventory_Repository_add_inventory_by_delta(self, basis, delta,
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
241
                                                      invalid_delta=True):
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
242
    """Apply delta to basis and return the result.
243
    
244
    This inserts basis as a whole inventory and then uses
245
    add_inventory_by_delta to add delta.
246
247
    :param basis: An inventory to be used as the basis.
248
    :param delta: The inventory delta to apply:
249
    :return: An inventory resulting from the application.
250
    """
251
    format = self.format()
6746.2.1 by Jelmer Vernooij
Rename matchingbzrdir to matchingcontroldir.
252
    control = self.make_controldir('tree', format=format._matchingcontroldir)
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
253
    repo = format.initialize(control)
6973.11.9 by Jelmer Vernooij
Fix tests.
254
    with repo.lock_write():
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
255
        repo.start_write_group()
256
        try:
6973.5.2 by Jelmer Vernooij
Add more bees.
257
            rev = revision.Revision(b'basis', timestamp=0, timezone=None,
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
258
                message="", committer="foo@example.com")
6973.5.2 by Jelmer Vernooij
Add more bees.
259
            basis.revision_id = b'basis'
4634.35.19 by Andrew Bennetts
Fix test_inv.
260
            create_texts_for_inv(repo, basis)
6973.5.2 by Jelmer Vernooij
Add more bees.
261
            repo.add_revision(b'basis', rev, basis)
4634.35.19 by Andrew Bennetts
Fix test_inv.
262
            repo.commit_write_group()
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
263
        except:
264
            repo.abort_write_group()
265
            raise
6973.11.9 by Jelmer Vernooij
Fix tests.
266
    with repo.lock_write():
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
267
        repo.start_write_group()
268
        try:
6973.5.2 by Jelmer Vernooij
Add more bees.
269
            inv_sha1 = repo.add_inventory_by_delta(b'basis', delta,
270
                b'result', [b'basis'])
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
271
        except:
272
            repo.abort_write_group()
273
            raise
274
        else:
275
            repo.commit_write_group()
276
    # Fresh lock, reads disk again.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
277
    repo = repo.controldir.open_repository()
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
278
    repo.lock_read()
279
    self.addCleanup(repo.unlock)
6973.5.2 by Jelmer Vernooij
Add more bees.
280
    return repo.get_inventory(b'result')
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
281
282
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
283
class TestInventoryUpdates(TestCase):
284
285
    def test_creation_from_root_id(self):
286
        # iff a root id is passed to the constructor, a root directory is made
6684.1.4 by Martin
Start making inventory tests pass on Python 3
287
        inv = inventory.Inventory(root_id=b'tree-root')
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
288
        self.assertNotEqual(None, inv.root)
6684.1.4 by Martin
Start making inventory tests pass on Python 3
289
        self.assertEqual(b'tree-root', inv.root.file_id)
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
290
291
    def test_add_path_of_root(self):
292
        # if no root id is given at creation time, there is no root directory
293
        inv = inventory.Inventory(root_id=None)
294
        self.assertIs(None, inv.root)
295
        # add a root entry by adding its path
6684.1.4 by Martin
Start making inventory tests pass on Python 3
296
        ie = inv.add_path(u"", "directory", b"my-root")
297
        ie.revision = b'test-rev'
298
        self.assertEqual(b"my-root", ie.file_id)
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
299
        self.assertIs(ie, inv.root)
300
301
    def test_add_path(self):
6684.1.4 by Martin
Start making inventory tests pass on Python 3
302
        inv = inventory.Inventory(root_id=b'tree_root')
303
        ie = inv.add_path(u'hello', 'file', b'hello-id')
304
        self.assertEqual(b'hello-id', ie.file_id)
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
305
        self.assertEqual('file', ie.kind)
306
307
    def test_copy(self):
308
        """Make sure copy() works and creates a deep copy."""
6684.1.4 by Martin
Start making inventory tests pass on Python 3
309
        inv = inventory.Inventory(root_id=b'some-tree-root')
310
        ie = inv.add_path(u'hello', 'file', b'hello-id')
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
311
        inv2 = inv.copy()
6684.1.4 by Martin
Start making inventory tests pass on Python 3
312
        inv.root.file_id = b'some-new-root'
313
        ie.name = u'file2'
314
        self.assertEqual(b'some-tree-root', inv2.root.file_id)
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
315
        self.assertEqual(u'hello', inv2.get_entry(b'hello-id').name)
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
316
317
    def test_copy_empty(self):
318
        """Make sure an empty inventory can be copied."""
319
        inv = inventory.Inventory(root_id=None)
320
        inv2 = inv.copy()
321
        self.assertIs(None, inv2.root)
322
323
    def test_copy_copies_root_revision(self):
324
        """Make sure the revision of the root gets copied."""
6684.1.4 by Martin
Start making inventory tests pass on Python 3
325
        inv = inventory.Inventory(root_id=b'someroot')
326
        inv.root.revision = b'therev'
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
327
        inv2 = inv.copy()
6684.1.4 by Martin
Start making inventory tests pass on Python 3
328
        self.assertEqual(b'someroot', inv2.root.file_id)
329
        self.assertEqual(b'therev', inv2.root.revision)
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
330
331
    def test_create_tree_reference(self):
6684.1.4 by Martin
Start making inventory tests pass on Python 3
332
        inv = inventory.Inventory(b'tree-root-123')
333
        inv.add(TreeReference(
334
            b'nested-id', 'nested', parent_id=b'tree-root-123',
335
            revision=b'rev', reference_revision=b'rev2'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
336
337
    def test_error_encoding(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
338
        inv = inventory.Inventory(b'tree-root')
339
        inv.add(InventoryFile(b'a-id', u'\u1234', b'tree-root'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
340
        e = self.assertRaises(errors.InconsistentDelta, inv.add,
6855.4.1 by Jelmer Vernooij
Yet more bees.
341
            InventoryFile(b'b-id', u'\u1234', b'tree-root'))
6973.11.13 by Jelmer Vernooij
Review comments.
342
        self.assertContainsRe(str(e), '\\u1234')
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
343
344
    def test_add_recursive(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
345
        parent = InventoryDirectory(b'src-id', 'src', b'tree-root')
346
        child = InventoryFile(b'hello-id', 'hello.c', b'src-id')
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
347
        parent.children[child.file_id] = child
6855.4.1 by Jelmer Vernooij
Yet more bees.
348
        inv = inventory.Inventory(b'tree-root')
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
349
        inv.add(parent)
6855.4.1 by Jelmer Vernooij
Yet more bees.
350
        self.assertEqual('src/hello.c', inv.id2path(b'hello-id'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
351
352
353
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
354
class TestDeltaApplication(TestCaseWithTransport):
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
355
356
    scenarios = delta_application_scenarios()
7002.1.1 by Martin
Avoid import error from test_inv without dulwich
357
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
358
    def get_empty_inventory(self, reference_inv=None):
359
        """Get an empty inventory.
360
361
        Note that tests should not depend on the revision of the root for
362
        setting up test conditions, as it has to be flexible to accomodate non
363
        rich root repositories.
364
365
        :param reference_inv: If not None, get the revision for the root from
366
            this inventory. This is useful for dealing with older repositories
367
            that routinely discarded the root entry data. If None, the root's
368
            revision is set to 'basis'.
369
        """
370
        inv = inventory.Inventory()
371
        if reference_inv is not None:
372
            inv.root.revision = reference_inv.root.revision
373
        else:
6973.11.6 by Jelmer Vernooij
Fix more http tests.
374
            inv.root.revision = b'basis'
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
375
        return inv
376
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
377
    def make_file_ie(self, file_id=b'file-id', name='name', parent_id=None):
5847.4.10 by John Arbash Meinel
A few more bug fixes.
378
        ie_file = inventory.InventoryFile(file_id, name, parent_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
379
        ie_file.revision = b'result'
5847.4.10 by John Arbash Meinel
A few more bug fixes.
380
        ie_file.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
381
        ie_file.text_sha1 = b''
5847.4.10 by John Arbash Meinel
A few more bug fixes.
382
        return ie_file
383
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
384
    def test_empty_delta(self):
385
        inv = self.get_empty_inventory()
386
        delta = []
387
        inv = self.apply_delta(self, inv, delta)
388
        inv2 = self.get_empty_inventory(inv)
389
        self.assertEqual([], inv2._make_delta(inv))
390
4526.9.22 by Robert Collins
Check fileids in inventory deltas are not None and are strings.
391
    def test_None_file_id(self):
392
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
393
        dir1 = inventory.InventoryDirectory(b'dirid', 'dir1', inv.root.file_id)
6973.11.2 by Jelmer Vernooij
Fix some more tests.
394
        dir1.file_id = None
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
395
        dir1.revision = b'result'
4526.9.22 by Robert Collins
Check fileids in inventory deltas are not None and are strings.
396
        delta = [(None, u'dir1', None, dir1)]
397
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
398
            inv, delta)
399
400
    def test_unicode_file_id(self):
401
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
402
        dir1 = inventory.InventoryDirectory(b'dirid', 'dir1', inv.root.file_id)
6973.11.2 by Jelmer Vernooij
Fix some more tests.
403
        dir1.file_id = u'dirid'
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
404
        dir1.revision = b'result'
4526.9.22 by Robert Collins
Check fileids in inventory deltas are not None and are strings.
405
        delta = [(None, u'dir1', dir1.file_id, dir1)]
406
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
407
            inv, delta)
408
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
409
    def test_repeated_file_id(self):
410
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
411
        file1 = inventory.InventoryFile(b'id', 'path1', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
412
        file1.revision = b'result'
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
413
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
414
        file1.text_sha1 = b""
5847.4.10 by John Arbash Meinel
A few more bug fixes.
415
        file2 = file1.copy()
416
        file2.name = 'path2'
6973.11.6 by Jelmer Vernooij
Fix more http tests.
417
        delta = [(None, u'path1', b'id', file1), (None, u'path2', b'id', file2)]
4505.5.3 by Robert Collins
Test infrastructure for testing all inventory delta applications and fix CHK inventories to reject repeated file ids in deltas.
418
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
419
            inv, delta)
963 by Martin Pool
- add the start of a test for inventory file-id matching
420
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
421
    def test_repeated_new_path(self):
422
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
423
        file1 = inventory.InventoryFile(b'id1', 'path', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
424
        file1.revision = b'result'
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
425
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
426
        file1.text_sha1 = b""
5847.4.10 by John Arbash Meinel
A few more bug fixes.
427
        file2 = file1.copy()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
428
        file2.file_id = b'id2'
429
        delta = [(None, u'path', b'id1', file1), (None, u'path', b'id2', file2)]
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
430
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
431
            inv, delta)
432
433
    def test_repeated_old_path(self):
434
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
435
        file1 = inventory.InventoryFile(b'id1', 'path', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
436
        file1.revision = b'result'
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
437
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
438
        file1.text_sha1 = b""
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
439
        # We can't *create* a source inventory with the same path, but
440
        # a badly generated partial delta might claim the same source twice.
441
        # This would be buggy in two ways: the path is repeated in the delta,
442
        # And the path for one of the file ids doesn't match the source
443
        # location. Alternatively, we could have a repeated fileid, but that
444
        # is separately checked for.
6973.11.6 by Jelmer Vernooij
Fix more http tests.
445
        file2 = inventory.InventoryFile(b'id2', 'path2', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
446
        file2.revision = b'result'
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
447
        file2.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
448
        file2.text_sha1 = b""
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
449
        inv.add(file1)
450
        inv.add(file2)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
451
        delta = [(u'path', None, b'id1', None), (u'path', None, b'id2', None)]
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
452
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
453
            inv, delta)
454
455
    def test_mismatched_id_entry_id(self):
456
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
457
        file1 = inventory.InventoryFile(b'id1', 'path', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
458
        file1.revision = b'result'
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
459
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
460
        file1.text_sha1 = b""
461
        delta = [(None, u'path', b'id', file1)]
4505.5.4 by Robert Collins
Repeated path/id corruption detected.
462
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
463
            inv, delta)
464
4526.9.4 by Robert Collins
Look for trivial issues with new_path and entry being out of sync in deltas.
465
    def test_mismatched_new_path_entry_None(self):
466
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
467
        delta = [(None, u'path', b'id', None)]
4526.9.4 by Robert Collins
Look for trivial issues with new_path and entry being out of sync in deltas.
468
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
469
            inv, delta)
470
471
    def test_mismatched_new_path_None_entry(self):
472
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
473
        file1 = inventory.InventoryFile(b'id1', 'path', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
474
        file1.revision = b'result'
4526.9.4 by Robert Collins
Look for trivial issues with new_path and entry being out of sync in deltas.
475
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
476
        file1.text_sha1 = b""
477
        delta = [(u"path", None, b'id1', file1)]
4526.9.4 by Robert Collins
Look for trivial issues with new_path and entry being out of sync in deltas.
478
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
479
            inv, delta)
480
4505.5.5 by Robert Collins
Parents used in a delta must be directories.
481
    def test_parent_is_not_directory(self):
482
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
483
        file1 = inventory.InventoryFile(b'id1', 'path', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
484
        file1.revision = b'result'
4505.5.5 by Robert Collins
Parents used in a delta must be directories.
485
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
486
        file1.text_sha1 = b""
487
        file2 = inventory.InventoryFile(b'id2', 'path2', b'id1')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
488
        file2.revision = b'result'
4505.5.5 by Robert Collins
Parents used in a delta must be directories.
489
        file2.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
490
        file2.text_sha1 = b""
4505.5.5 by Robert Collins
Parents used in a delta must be directories.
491
        inv.add(file1)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
492
        delta = [(None, u'path/path2', b'id2', file2)]
4505.5.5 by Robert Collins
Parents used in a delta must be directories.
493
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
494
            inv, delta)
495
4505.5.6 by Robert Collins
Check for missing parents in deltas.
496
    def test_parent_is_missing(self):
497
        inv = self.get_empty_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
498
        file2 = inventory.InventoryFile(b'id2', 'path2', 'missingparent')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
499
        file2.revision = b'result'
4505.5.6 by Robert Collins
Check for missing parents in deltas.
500
        file2.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
501
        file2.text_sha1 = b""
502
        delta = [(None, u'path/path2', b'id2', file2)]
4505.5.6 by Robert Collins
Check for missing parents in deltas.
503
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
504
            inv, delta)
505
4526.9.2 by Robert Collins
Handle deltas with new paths not matching the actual path.
506
    def test_new_parent_path_has_wrong_id(self):
507
        inv = self.get_empty_inventory()
6973.11.7 by Jelmer Vernooij
Fix more tests.
508
        parent1 = inventory.InventoryDirectory(b'p-1', 'dir', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
509
        parent1.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
510
        parent2 = inventory.InventoryDirectory(b'p-2', 'dir2', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
511
        parent2.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
512
        file1 = inventory.InventoryFile(b'id', 'path', b'p-2')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
513
        file1.revision = b'result'
4526.9.2 by Robert Collins
Handle deltas with new paths not matching the actual path.
514
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
515
        file1.text_sha1 = b""
4526.9.2 by Robert Collins
Handle deltas with new paths not matching the actual path.
516
        inv.add(parent1)
517
        inv.add(parent2)
518
        # This delta claims that file1 is at dir/path, but actually its at
519
        # dir2/path if you follow the inventory parent structure.
6973.11.6 by Jelmer Vernooij
Fix more http tests.
520
        delta = [(None, u'dir/path', b'id', file1)]
4526.9.2 by Robert Collins
Handle deltas with new paths not matching the actual path.
521
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
522
            inv, delta)
523
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
524
    def test_old_parent_path_is_wrong(self):
525
        inv = self.get_empty_inventory()
6973.11.7 by Jelmer Vernooij
Fix more tests.
526
        parent1 = inventory.InventoryDirectory(b'p-1', 'dir', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
527
        parent1.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
528
        parent2 = inventory.InventoryDirectory(b'p-2', 'dir2', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
529
        parent2.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
530
        file1 = inventory.InventoryFile(b'id', 'path', b'p-2')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
531
        file1.revision = b'result'
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
532
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
533
        file1.text_sha1 = b""
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
534
        inv.add(parent1)
535
        inv.add(parent2)
536
        inv.add(file1)
537
        # This delta claims that file1 was at dir/path, but actually it was at
538
        # dir2/path if you follow the inventory parent structure.
6973.11.6 by Jelmer Vernooij
Fix more http tests.
539
        delta = [(u'dir/path', None, b'id', None)]
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
540
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
541
            inv, delta)
542
543
    def test_old_parent_path_is_for_other_id(self):
544
        inv = self.get_empty_inventory()
6973.11.7 by Jelmer Vernooij
Fix more tests.
545
        parent1 = inventory.InventoryDirectory(b'p-1', 'dir', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
546
        parent1.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
547
        parent2 = inventory.InventoryDirectory(b'p-2', 'dir2', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
548
        parent2.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
549
        file1 = inventory.InventoryFile(b'id', 'path', b'p-2')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
550
        file1.revision = b'result'
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
551
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
552
        file1.text_sha1 = b""
6973.11.7 by Jelmer Vernooij
Fix more tests.
553
        file2 = inventory.InventoryFile(b'id2', 'path', b'p-1')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
554
        file2.revision = b'result'
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
555
        file2.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
556
        file2.text_sha1 = b""
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
557
        inv.add(parent1)
558
        inv.add(parent2)
559
        inv.add(file1)
560
        inv.add(file2)
561
        # This delta claims that file1 was at dir/path, but actually it was at
562
        # dir2/path if you follow the inventory parent structure. At dir/path
563
        # is another entry we should not delete.
6973.11.6 by Jelmer Vernooij
Fix more http tests.
564
        delta = [(u'dir/path', None, b'id', None)]
4526.9.3 by Robert Collins
Handle mismatches between inventory delta paths and actual paths found by traversing parent pointers.
565
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
566
            inv, delta)
567
4526.9.5 by Robert Collins
Require that added ids in inventory deltas be new.
568
    def test_add_existing_id_new_path(self):
569
        inv = self.get_empty_inventory()
6973.11.7 by Jelmer Vernooij
Fix more tests.
570
        parent1 = inventory.InventoryDirectory(b'p-1', 'dir1', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
571
        parent1.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
572
        parent2 = inventory.InventoryDirectory(b'p-1', 'dir2', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
573
        parent2.revision = b'result'
4526.9.5 by Robert Collins
Require that added ids in inventory deltas be new.
574
        inv.add(parent1)
6973.11.7 by Jelmer Vernooij
Fix more tests.
575
        delta = [(None, u'dir2', b'p-1', parent2)]
4526.9.5 by Robert Collins
Require that added ids in inventory deltas be new.
576
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
577
            inv, delta)
578
4526.9.8 by Robert Collins
Check that the paths deltas put entries into are not in use already.
579
    def test_add_new_id_existing_path(self):
580
        inv = self.get_empty_inventory()
6973.11.7 by Jelmer Vernooij
Fix more tests.
581
        parent1 = inventory.InventoryDirectory(b'p-1', 'dir1', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
582
        parent1.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
583
        parent2 = inventory.InventoryDirectory(b'p-2', 'dir1', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
584
        parent2.revision = b'result'
4526.9.8 by Robert Collins
Check that the paths deltas put entries into are not in use already.
585
        inv.add(parent1)
6973.11.7 by Jelmer Vernooij
Fix more tests.
586
        delta = [(None, u'dir1', b'p-2', parent2)]
4526.9.8 by Robert Collins
Check that the paths deltas put entries into are not in use already.
587
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
588
            inv, delta)
589
4526.9.9 by Robert Collins
Add interface tests for dangling children in inventory deltas.
590
    def test_remove_dir_leaving_dangling_child(self):
591
        inv = self.get_empty_inventory()
6973.11.7 by Jelmer Vernooij
Fix more tests.
592
        dir1 = inventory.InventoryDirectory(b'p-1', 'dir1', inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
593
        dir1.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
594
        dir2 = inventory.InventoryDirectory(b'p-2', 'child1', b'p-1')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
595
        dir2.revision = b'result'
6973.11.7 by Jelmer Vernooij
Fix more tests.
596
        dir3 = inventory.InventoryDirectory(b'p-3', 'child2', b'p-1')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
597
        dir3.revision = b'result'
4526.9.9 by Robert Collins
Add interface tests for dangling children in inventory deltas.
598
        inv.add(dir1)
599
        inv.add(dir2)
600
        inv.add(dir3)
6973.11.7 by Jelmer Vernooij
Fix more tests.
601
        delta = [(u'dir1', None, b'p-1', None),
602
            (u'dir1/child2', None, b'p-3', None)]
4526.9.9 by Robert Collins
Add interface tests for dangling children in inventory deltas.
603
        self.assertRaises(errors.InconsistentDelta, self.apply_delta, self,
604
            inv, delta)
605
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
606
    def test_add_file(self):
607
        inv = self.get_empty_inventory()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
608
        file1 = inventory.InventoryFile(b'file-id', 'path', inv.root.file_id)
609
        file1.revision = b'result'
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
610
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
611
        file1.text_sha1 = b''
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
612
        delta = [(None, u'path', b'file-id', file1)]
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
613
        res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
6855.4.10 by Jelmer Vernooij
merge trunk
614
        self.assertEqual(b'file-id', res_inv.get_entry(b'file-id').file_id)
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
615
616
    def test_remove_file(self):
617
        inv = self.get_empty_inventory()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
618
        file1 = inventory.InventoryFile(b'file-id', 'path', inv.root.file_id)
619
        file1.revision = b'result'
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
620
        file1.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
621
        file1.text_sha1 = b''
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
622
        inv.add(file1)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
623
        delta = [(u'path', None, b'file-id', None)]
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
624
        res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
6856 by Jelmer Vernooij
Merge lp:~jelmer/brz/is-versioned.
625
        self.assertEqual(None, res_inv.path2id('path'))
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
626
        self.assertRaises(errors.NoSuchId, res_inv.id2path, b'file-id')
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
627
628
    def test_rename_file(self):
629
        inv = self.get_empty_inventory()
5847.4.10 by John Arbash Meinel
A few more bug fixes.
630
        file1 = self.make_file_ie(name='path', parent_id=inv.root.file_id)
5876.1.1 by John Arbash Meinel
Fix bug #781168, and allow WT.update_basis_by_delta
631
        inv.add(file1)
5847.4.10 by John Arbash Meinel
A few more bug fixes.
632
        file2 = self.make_file_ie(name='path2', parent_id=inv.root.file_id)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
633
        delta = [(u'path', 'path2', b'file-id', file2)]
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
634
        res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
6856 by Jelmer Vernooij
Merge lp:~jelmer/brz/is-versioned.
635
        self.assertEqual(None, res_inv.path2id('path'))
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
636
        self.assertEqual(b'file-id', res_inv.path2id('path2'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
637
5847.4.10 by John Arbash Meinel
A few more bug fixes.
638
    def test_replaced_at_new_path(self):
639
        inv = self.get_empty_inventory()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
640
        file1 = self.make_file_ie(file_id=b'id1', parent_id=inv.root.file_id)
5847.4.10 by John Arbash Meinel
A few more bug fixes.
641
        inv.add(file1)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
642
        file2 = self.make_file_ie(file_id=b'id2', parent_id=inv.root.file_id)
643
        delta = [(u'name', None, b'id1', None),
644
                 (None, u'name', b'id2', file2)]
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
645
        res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
646
        self.assertEqual(b'id2', res_inv.path2id('name'))
5847.4.10 by John Arbash Meinel
A few more bug fixes.
647
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
648
    def test_rename_dir(self):
649
        inv = self.get_empty_inventory()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
650
        dir1 = inventory.InventoryDirectory(b'dir-id', 'dir1', inv.root.file_id)
651
        dir1.revision = b'basis'
652
        file1 = self.make_file_ie(parent_id=b'dir-id')
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
653
        inv.add(dir1)
654
        inv.add(file1)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
655
        dir2 = inventory.InventoryDirectory(b'dir-id', 'dir2', inv.root.file_id)
656
        dir2.revision = b'result'
657
        delta = [('dir1', 'dir2', b'dir-id', dir2)]
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
658
        res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
659
        # The file should be accessible under the new path
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
660
        self.assertEqual(b'file-id', res_inv.path2id('dir2/name'))
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
661
662
    def test_renamed_dir_with_renamed_child(self):
663
        inv = self.get_empty_inventory()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
664
        dir1 = inventory.InventoryDirectory(b'dir-id', 'dir1', inv.root.file_id)
665
        dir1.revision = b'basis'
666
        file1 = self.make_file_ie(b'file-id-1', 'name1', parent_id=b'dir-id')
667
        file2 = self.make_file_ie(b'file-id-2', 'name2', parent_id=b'dir-id')
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
668
        inv.add(dir1)
669
        inv.add(file1)
670
        inv.add(file2)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
671
        dir2 = inventory.InventoryDirectory(b'dir-id', 'dir2', inv.root.file_id)
672
        dir2.revision = b'result'
673
        file2b = self.make_file_ie(b'file-id-2', 'name2', inv.root.file_id)
674
        delta = [('dir1', 'dir2', b'dir-id', dir2),
675
                 ('dir1/name2', 'name2', b'file-id-2', file2b)]
5847.4.35 by John Arbash Meinel
Rename 'expect_fail' to 'invalid_delta' as suggested by Jelmer
676
        res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
677
        # The file should be accessible under the new path
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
678
        self.assertEqual(b'file-id-1', res_inv.path2id('dir2/name1'))
6856 by Jelmer Vernooij
Merge lp:~jelmer/brz/is-versioned.
679
        self.assertEqual(None, res_inv.path2id('dir2/name2'))
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
680
        self.assertEqual(b'file-id-2', res_inv.path2id('name2'))
5847.4.13 by John Arbash Meinel
Mostly more test cases, some small progress in getting
681
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
682
    def test_is_root(self):
683
        """Ensure our root-checking code is accurate."""
6973.11.6 by Jelmer Vernooij
Fix more http tests.
684
        inv = inventory.Inventory(b'TREE_ROOT')
685
        self.assertTrue(inv.is_root(b'TREE_ROOT'))
686
        self.assertFalse(inv.is_root(b'booga'))
687
        inv.root.file_id = b'booga'
688
        self.assertFalse(inv.is_root(b'TREE_ROOT'))
689
        self.assertTrue(inv.is_root(b'booga'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
690
        # works properly even if no root is set
691
        inv.root = None
6973.11.6 by Jelmer Vernooij
Fix more http tests.
692
        self.assertFalse(inv.is_root(b'TREE_ROOT'))
693
        self.assertFalse(inv.is_root(b'booga'))
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
694
5410.1.2 by Daniel Knittl-Frank
Add a test case for `entries()` on empty inventory
695
    def test_entries_for_empty_inventory(self):
696
        """Test that entries() will not fail for an empty inventory"""
5410.1.3 by Daniel Knittl-Frank
fix whitespace (only use spaces)
697
        inv = Inventory(root_id=None)
698
        self.assertEqual([], inv.entries())
5410.1.2 by Daniel Knittl-Frank
Add a test case for `entries()` on empty inventory
699
4634.51.7 by John Arbash Meinel
Finish adding CHKInventory as a permutation in per_inventory.
700
1407 by Robert Collins
define some expected behaviour for inventory_entry.snapshot
701
class TestInventoryEntry(TestCase):
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
702
6478.3.3 by Jelmer Vernooij
Support backslash filenames in Inventory.
703
    def test_file_invalid_entry_name(self):
704
        self.assertRaises(errors.InvalidEntryName, inventory.InventoryFile,
6973.11.6 by Jelmer Vernooij
Fix more http tests.
705
            b'123', 'a/hello.c', ROOT_ID)
6478.3.3 by Jelmer Vernooij
Support backslash filenames in Inventory.
706
707
    def test_file_backslash(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
708
        file = inventory.InventoryFile(b'123', 'h\\ello.c', ROOT_ID)
6478.3.3 by Jelmer Vernooij
Support backslash filenames in Inventory.
709
        self.assertEquals(file.name, 'h\\ello.c')
710
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
711
    def test_file_kind_character(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
712
        file = inventory.InventoryFile(b'123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
713
        self.assertEqual(file.kind_character(), '')
714
715
    def test_dir_kind_character(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
716
        dir = inventory.InventoryDirectory(b'123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
717
        self.assertEqual(dir.kind_character(), '/')
718
719
    def test_link_kind_character(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
720
        dir = inventory.InventoryLink(b'123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
721
        self.assertEqual(dir.kind_character(), '')
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
722
723
    def test_dir_detect_changes(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
724
        left = inventory.InventoryDirectory(b'123', 'hello.c', ROOT_ID)
725
        right = inventory.InventoryDirectory(b'123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
726
        self.assertEqual((False, False), left.detect_changes(right))
727
        self.assertEqual((False, False), right.detect_changes(left))
728
729
    def test_file_detect_changes(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
730
        left = inventory.InventoryFile(b'123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
731
        left.text_sha1 = 123
6973.11.6 by Jelmer Vernooij
Fix more http tests.
732
        right = inventory.InventoryFile(b'123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
733
        right.text_sha1 = 123
734
        self.assertEqual((False, False), left.detect_changes(right))
735
        self.assertEqual((False, False), right.detect_changes(left))
736
        left.executable = True
737
        self.assertEqual((False, True), left.detect_changes(right))
738
        self.assertEqual((False, True), right.detect_changes(left))
739
        right.text_sha1 = 321
740
        self.assertEqual((True, True), left.detect_changes(right))
741
        self.assertEqual((True, True), right.detect_changes(left))
742
743
    def test_symlink_detect_changes(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
744
        left = inventory.InventoryLink(b'123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
745
        left.symlink_target='foo'
6973.11.6 by Jelmer Vernooij
Fix more http tests.
746
        right = inventory.InventoryLink(b'123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
747
        right.symlink_target='foo'
748
        self.assertEqual((False, False), left.detect_changes(right))
749
        self.assertEqual((False, False), right.detect_changes(left))
750
        left.symlink_target = 'different'
751
        self.assertEqual((True, False), left.detect_changes(right))
752
        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
753
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
754
    def test_file_has_text(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
755
        file = inventory.InventoryFile(b'123', 'hello.c', ROOT_ID)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
756
        self.assertTrue(file.has_text())
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
757
758
    def test_directory_has_text(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
759
        dir = inventory.InventoryDirectory(b'123', 'hello.c', ROOT_ID)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
760
        self.assertFalse(dir.has_text())
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
761
762
    def test_link_has_text(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
763
        link = inventory.InventoryLink(b'123', 'hello.c', ROOT_ID)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
764
        self.assertFalse(link.has_text())
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
765
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)
766
    def test_make_entry(self):
767
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
768
            inventory.InventoryFile)
769
        self.assertIsInstance(inventory.make_entry("symlink", "name", ROOT_ID),
770
            inventory.InventoryLink)
771
        self.assertIsInstance(inventory.make_entry("directory", "name", ROOT_ID),
772
            inventory.InventoryDirectory)
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
773
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
774
    def test_make_entry_non_normalized(self):
775
        orig_normalized_filename = osutils.normalized_filename
776
777
        try:
778
            osutils.normalized_filename = osutils._accessible_normalized_filename
779
            entry = inventory.make_entry("file", u'a\u030a', ROOT_ID)
780
            self.assertEqual(u'\xe5', entry.name)
781
            self.assertIsInstance(entry, inventory.InventoryFile)
782
783
            osutils.normalized_filename = osutils._inaccessible_normalized_filename
784
            self.assertRaises(errors.InvalidNormalization,
785
                    inventory.make_entry, 'file', u'a\u030a', ROOT_ID)
786
        finally:
787
            osutils.normalized_filename = orig_normalized_filename
788
789
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
790
class TestDescribeChanges(TestCase):
791
792
    def test_describe_change(self):
793
        # we need to test the following change combinations:
794
        # rename
795
        # reparent
796
        # modify
797
        # gone
798
        # added
799
        # renamed/reparented and modified
800
        # change kind (perhaps can't be done yet?)
801
        # also, merged in combination with all of these?
6973.11.6 by Jelmer Vernooij
Fix more http tests.
802
        old_a = InventoryFile(b'a-id', 'a_file', ROOT_ID)
803
        old_a.text_sha1 = b'123132'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
804
        old_a.text_size = 0
6973.11.6 by Jelmer Vernooij
Fix more http tests.
805
        new_a = InventoryFile(b'a-id', 'a_file', ROOT_ID)
806
        new_a.text_sha1 = b'123132'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
807
        new_a.text_size = 0
808
809
        self.assertChangeDescription('unchanged', old_a, new_a)
810
811
        new_a.text_size = 10
6973.11.6 by Jelmer Vernooij
Fix more http tests.
812
        new_a.text_sha1 = b'abcabc'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
813
        self.assertChangeDescription('modified', old_a, new_a)
814
815
        self.assertChangeDescription('added', None, new_a)
816
        self.assertChangeDescription('removed', old_a, None)
817
        # perhaps a bit questionable but seems like the most reasonable thing...
818
        self.assertChangeDescription('unchanged', None, None)
819
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
820
        # 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
821
        # modification:
822
        new_a.name = 'newfilename'
823
        self.assertChangeDescription('modified and renamed', old_a, new_a)
824
825
        # reparenting is 'renaming'
826
        new_a.name = old_a.name
6973.11.6 by Jelmer Vernooij
Fix more http tests.
827
        new_a.parent_id = b'somedir-id'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
828
        self.assertChangeDescription('modified and renamed', old_a, new_a)
829
830
        # reset the content values so its not modified
831
        new_a.text_size = old_a.text_size
832
        new_a.text_sha1 = old_a.text_sha1
833
        new_a.name = old_a.name
834
835
        new_a.name = 'newfilename'
836
        self.assertChangeDescription('renamed', old_a, new_a)
837
838
        # reparenting is 'renaming'
839
        new_a.name = old_a.name
6973.11.6 by Jelmer Vernooij
Fix more http tests.
840
        new_a.parent_id = b'somedir-id'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
841
        self.assertChangeDescription('renamed', old_a, new_a)
842
843
    def assertChangeDescription(self, expected_change, old_ie, new_ie):
844
        change = InventoryEntry.describe_change(old_ie, new_ie)
845
        self.assertEqual(expected_change, change)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
846
847
4634.51.1 by John Arbash Meinel
Switch away from creating a whole repository just to get a VF.
848
class TestCHKInventory(tests.TestCaseWithMemoryTransport):
3735.2.99 by John Arbash Meinel
Merge bzr.dev 4034. Whitespace cleanup
849
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
850
    def get_chk_bytes(self):
4634.51.1 by John Arbash Meinel
Switch away from creating a whole repository just to get a VF.
851
        factory = groupcompress.make_pack_factory(True, True, 1)
852
        trans = self.get_transport('')
853
        return factory(trans)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
854
855
    def read_bytes(self, chk_bytes, key):
856
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
7018.3.10 by Jelmer Vernooij
Consistent return values in PreviewTree.list_files.
857
        return next(stream).get_bytes_as("fulltext")
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
858
859
    def test_deserialise_gives_CHKInventory(self):
860
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
861
        inv.revision_id = b"revid"
862
        inv.root.revision = b"rootrev"
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
863
        chk_bytes = self.get_chk_bytes()
864
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
865
        bytes = b''.join(chk_inv.to_lines())
866
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
867
        self.assertEqual(b"revid", new_inv.revision_id)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
868
        self.assertEqual("directory", new_inv.root.kind)
869
        self.assertEqual(inv.root.file_id, new_inv.root.file_id)
870
        self.assertEqual(inv.root.parent_id, new_inv.root.parent_id)
871
        self.assertEqual(inv.root.name, new_inv.root.name)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
872
        self.assertEqual(b"rootrev", new_inv.root.revision)
873
        self.assertEqual(b'plain', new_inv._search_key_name)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
874
875
    def test_deserialise_wrong_revid(self):
876
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
877
        inv.revision_id = b"revid"
878
        inv.root.revision = b"rootrev"
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
879
        chk_bytes = self.get_chk_bytes()
880
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
881
        bytes = b''.join(chk_inv.to_lines())
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
882
        self.assertRaises(ValueError, CHKInventory.deserialise, chk_bytes,
6973.11.6 by Jelmer Vernooij
Fix more http tests.
883
            bytes, (b"revid2",))
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
884
885
    def test_captures_rev_root_byid(self):
886
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
887
        inv.revision_id = b"foo"
888
        inv.root.revision = b"bar"
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
889
        chk_bytes = self.get_chk_bytes()
890
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
891
        lines = chk_inv.to_lines()
892
        self.assertEqual([
6973.11.6 by Jelmer Vernooij
Fix more http tests.
893
            b'chkinventory:\n',
894
            b'revision_id: foo\n',
895
            b'root_id: TREE_ROOT\n',
896
            b'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
897
            b'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
898
            ], lines)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
899
        chk_inv = CHKInventory.deserialise(chk_bytes, b''.join(lines), (b'foo',))
900
        self.assertEqual(b'plain', chk_inv._search_key_name)
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
901
902
    def test_captures_parent_id_basename_index(self):
903
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
904
        inv.revision_id = b"foo"
905
        inv.root.revision = b"bar"
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
906
        chk_bytes = self.get_chk_bytes()
3735.2.132 by John Arbash Meinel
Remove references to parent_id_basename_index, now that we know we want it.
907
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
908
        lines = chk_inv.to_lines()
909
        self.assertEqual([
6973.11.6 by Jelmer Vernooij
Fix more http tests.
910
            b'chkinventory:\n',
911
            b'revision_id: foo\n',
912
            b'root_id: TREE_ROOT\n',
913
            b'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
914
            b'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
915
            ], lines)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
916
        chk_inv = CHKInventory.deserialise(chk_bytes, b''.join(lines), (b'foo',))
917
        self.assertEqual(b'plain', chk_inv._search_key_name)
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
918
919
    def test_captures_search_key_name(self):
920
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
921
        inv.revision_id = b"foo"
922
        inv.root.revision = b"bar"
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
923
        chk_bytes = self.get_chk_bytes()
924
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv,
6973.11.7 by Jelmer Vernooij
Fix more tests.
925
                                              search_key_name=b'hash-16-way')
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
926
        lines = chk_inv.to_lines()
927
        self.assertEqual([
6973.11.6 by Jelmer Vernooij
Fix more http tests.
928
            b'chkinventory:\n',
929
            b'search_key_name: hash-16-way\n',
930
            b'root_id: TREE_ROOT\n',
931
            b'parent_id_basename_to_file_id: sha1:eb23f0ad4b07f48e88c76d4c94292be57fb2785f\n',
932
            b'revision_id: foo\n',
933
            b'id_to_entry: sha1:debfe920f1f10e7929260f0534ac9a24d7aabbb4\n',
3735.16.7 by John Arbash Meinel
Start parameterizing CHKInventory and CHKSerializer so that we can
934
            ], lines)
6973.11.7 by Jelmer Vernooij
Fix more tests.
935
        chk_inv = CHKInventory.deserialise(chk_bytes, b''.join(lines), (b'foo',))
6973.11.6 by Jelmer Vernooij
Fix more http tests.
936
        self.assertEqual(b'hash-16-way', chk_inv._search_key_name)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
937
938
    def test_directory_children_on_demand(self):
939
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
940
        inv.revision_id = b"revid"
941
        inv.root.revision = b"rootrev"
942
        inv.add(InventoryFile(b"fileid", "file", inv.root.file_id))
943
        inv.get_entry(b"fileid").revision = b"filerev"
944
        inv.get_entry(b"fileid").executable = True
945
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
946
        inv.get_entry(b"fileid").text_size = 1
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
947
        chk_bytes = self.get_chk_bytes()
948
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
949
        bytes = b''.join(chk_inv.to_lines())
950
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
951
        root_entry = new_inv.get_entry(inv.root.file_id)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
952
        self.assertEqual(None, root_entry._children)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
953
        self.assertEqual({'file'}, set(root_entry.children))
6973.11.6 by Jelmer Vernooij
Fix more http tests.
954
        file_direct = new_inv.get_entry(b"fileid")
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
955
        file_found = root_entry.children['file']
956
        self.assertEqual(file_direct.kind, file_found.kind)
957
        self.assertEqual(file_direct.file_id, file_found.file_id)
958
        self.assertEqual(file_direct.parent_id, file_found.parent_id)
959
        self.assertEqual(file_direct.name, file_found.name)
960
        self.assertEqual(file_direct.revision, file_found.revision)
961
        self.assertEqual(file_direct.text_sha1, file_found.text_sha1)
962
        self.assertEqual(file_direct.text_size, file_found.text_size)
963
        self.assertEqual(file_direct.executable, file_found.executable)
964
3735.2.27 by Robert Collins
Use 4K pages for development3 repositories.
965
    def test_from_inventory_maximum_size(self):
966
        # from_inventory supports the maximum_size parameter.
967
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
968
        inv.revision_id = b"revid"
969
        inv.root.revision = b"rootrev"
3735.2.27 by Robert Collins
Use 4K pages for development3 repositories.
970
        chk_bytes = self.get_chk_bytes()
971
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv, 120)
4413.5.10 by John Arbash Meinel
Clean upt the test_inv tests that assumed _root_node was real and not just a key.
972
        chk_inv.id_to_entry._ensure_root()
3735.2.27 by Robert Collins
Use 4K pages for development3 repositories.
973
        self.assertEqual(120, chk_inv.id_to_entry._root_node.maximum_size)
4413.5.10 by John Arbash Meinel
Clean upt the test_inv tests that assumed _root_node was real and not just a key.
974
        self.assertEqual(1, chk_inv.id_to_entry._root_node._key_width)
975
        p_id_basename = chk_inv.parent_id_basename_to_file_id
976
        p_id_basename._ensure_root()
977
        self.assertEqual(120, p_id_basename._root_node.maximum_size)
978
        self.assertEqual(2, p_id_basename._root_node._key_width)
3735.2.27 by Robert Collins
Use 4K pages for development3 repositories.
979
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
980
    def test_iter_all_ids(self):
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
981
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
982
        inv.revision_id = b"revid"
983
        inv.root.revision = b"rootrev"
984
        inv.add(InventoryFile(b"fileid", "file", inv.root.file_id))
6973.11.7 by Jelmer Vernooij
Fix more tests.
985
        inv.get_entry(b"fileid").revision = b"filerev"
6973.11.6 by Jelmer Vernooij
Fix more http tests.
986
        inv.get_entry(b"fileid").executable = True
987
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
988
        inv.get_entry(b"fileid").text_size = 1
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
989
        chk_bytes = self.get_chk_bytes()
990
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
991
        bytes = b''.join(chk_inv.to_lines())
6973.11.7 by Jelmer Vernooij
Fix more tests.
992
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
993
        fileids = sorted(new_inv.iter_all_ids())
6973.11.6 by Jelmer Vernooij
Fix more http tests.
994
        self.assertEqual([inv.root.file_id, b"fileid"], fileids)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
995
996
    def test__len__(self):
997
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
998
        inv.revision_id = b"revid"
999
        inv.root.revision = b"rootrev"
1000
        inv.add(InventoryFile(b"fileid", "file", inv.root.file_id))
6973.11.7 by Jelmer Vernooij
Fix more tests.
1001
        inv.get_entry(b"fileid").revision = b"filerev"
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1002
        inv.get_entry(b"fileid").executable = True
1003
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
1004
        inv.get_entry(b"fileid").text_size = 1
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1005
        chk_bytes = self.get_chk_bytes()
1006
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
1007
        self.assertEqual(2, len(chk_inv))
1008
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
1009
    def test_get_entry(self):
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1010
        inv = Inventory()
6684.1.4 by Martin
Start making inventory tests pass on Python 3
1011
        inv.revision_id = b"revid"
1012
        inv.root.revision = b"rootrev"
1013
        inv.add(InventoryFile(b"fileid", u"file", inv.root.file_id))
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
1014
        inv.get_entry(b"fileid").revision = b"filerev"
1015
        inv.get_entry(b"fileid").executable = True
1016
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
1017
        inv.get_entry(b"fileid").text_size = 1
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1018
        chk_bytes = self.get_chk_bytes()
1019
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6684.1.4 by Martin
Start making inventory tests pass on Python 3
1020
        data = b''.join(chk_inv.to_lines())
1021
        new_inv = CHKInventory.deserialise(chk_bytes, data, (b"revid",))
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
1022
        root_entry = new_inv.get_entry(inv.root.file_id)
1023
        file_entry = new_inv.get_entry(b"fileid")
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1024
        self.assertEqual("directory", root_entry.kind)
1025
        self.assertEqual(inv.root.file_id, root_entry.file_id)
1026
        self.assertEqual(inv.root.parent_id, root_entry.parent_id)
1027
        self.assertEqual(inv.root.name, root_entry.name)
6684.1.4 by Martin
Start making inventory tests pass on Python 3
1028
        self.assertEqual(b"rootrev", root_entry.revision)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1029
        self.assertEqual("file", file_entry.kind)
6684.1.4 by Martin
Start making inventory tests pass on Python 3
1030
        self.assertEqual(b"fileid", file_entry.file_id)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1031
        self.assertEqual(inv.root.file_id, file_entry.parent_id)
6684.1.4 by Martin
Start making inventory tests pass on Python 3
1032
        self.assertEqual(u"file", file_entry.name)
1033
        self.assertEqual(b"filerev", file_entry.revision)
1034
        self.assertEqual(b"ffff", file_entry.text_sha1)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1035
        self.assertEqual(1, file_entry.text_size)
1036
        self.assertEqual(True, file_entry.executable)
6915.4.4 by Jelmer Vernooij
Fix test.
1037
        self.assertRaises(errors.NoSuchId, new_inv.get_entry, 'missing')
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1038
1039
    def test_has_id_true(self):
1040
        inv = Inventory()
6855.4.1 by Jelmer Vernooij
Yet more bees.
1041
        inv.revision_id = b"revid"
1042
        inv.root.revision = b"rootrev"
1043
        inv.add(InventoryFile(b"fileid", "file", inv.root.file_id))
6855.4.10 by Jelmer Vernooij
merge trunk
1044
        inv.get_entry(b"fileid").revision = b"filerev"
1045
        inv.get_entry(b"fileid").executable = True
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1046
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
6855.4.10 by Jelmer Vernooij
merge trunk
1047
        inv.get_entry(b"fileid").text_size = 1
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1048
        chk_bytes = self.get_chk_bytes()
1049
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1050
        self.assertTrue(chk_inv.has_id(b'fileid'))
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1051
        self.assertTrue(chk_inv.has_id(inv.root.file_id))
1052
1053
    def test_has_id_not(self):
1054
        inv = Inventory()
6855.4.1 by Jelmer Vernooij
Yet more bees.
1055
        inv.revision_id = b"revid"
1056
        inv.root.revision = b"rootrev"
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
1057
        chk_bytes = self.get_chk_bytes()
1058
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1059
        self.assertFalse(chk_inv.has_id(b'fileid'))
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1060
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1061
    def test_id2path(self):
1062
        inv = Inventory()
6855.4.1 by Jelmer Vernooij
Yet more bees.
1063
        inv.revision_id = b"revid"
1064
        inv.root.revision = b"rootrev"
1065
        direntry = InventoryDirectory(b"dirid", "dir", inv.root.file_id)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1066
        fileentry = InventoryFile(b"fileid", "file", b"dirid")
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1067
        inv.add(direntry)
1068
        inv.add(fileentry)
6855.4.10 by Jelmer Vernooij
merge trunk
1069
        inv.get_entry(b"fileid").revision = b"filerev"
1070
        inv.get_entry(b"fileid").executable = True
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1071
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
6855.4.10 by Jelmer Vernooij
merge trunk
1072
        inv.get_entry(b"fileid").text_size = 1
1073
        inv.get_entry(b"dirid").revision = b"filerev"
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1074
        chk_bytes = self.get_chk_bytes()
1075
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1076
        bytes = b''.join(chk_inv.to_lines())
6855.4.1 by Jelmer Vernooij
Yet more bees.
1077
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1078
        self.assertEqual('', new_inv.id2path(inv.root.file_id))
6855.4.1 by Jelmer Vernooij
Yet more bees.
1079
        self.assertEqual('dir', new_inv.id2path(b'dirid'))
1080
        self.assertEqual('dir/file', new_inv.id2path(b'fileid'))
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1081
1082
    def test_path2id(self):
1083
        inv = Inventory()
6855.4.10 by Jelmer Vernooij
merge trunk
1084
        inv.revision_id = b"revid"
1085
        inv.root.revision = b"rootrev"
1086
        direntry = InventoryDirectory(b"dirid", "dir", inv.root.file_id)
1087
        fileentry = InventoryFile(b"fileid", "file", b"dirid")
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1088
        inv.add(direntry)
1089
        inv.add(fileentry)
6855.4.10 by Jelmer Vernooij
merge trunk
1090
        inv.get_entry(b"fileid").revision = b"filerev"
1091
        inv.get_entry(b"fileid").executable = True
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1092
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
6855.4.10 by Jelmer Vernooij
merge trunk
1093
        inv.get_entry(b"fileid").text_size = 1
1094
        inv.get_entry(b"dirid").revision = b"filerev"
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1095
        chk_bytes = self.get_chk_bytes()
1096
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1097
        bytes = b''.join(chk_inv.to_lines())
6855.4.10 by Jelmer Vernooij
merge trunk
1098
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1099
        self.assertEqual(inv.root.file_id, new_inv.path2id(''))
6855.4.10 by Jelmer Vernooij
merge trunk
1100
        self.assertEqual(b'dirid', new_inv.path2id('dir'))
1101
        self.assertEqual(b'fileid', new_inv.path2id('dir/file'))
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
1102
3735.2.57 by Jelmer Vernooij
Make sure CHKInventory._entry_cache gets initialized in create_by_apply_delta.
1103
    def test_create_by_apply_delta_sets_root(self):
1104
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1105
        inv.revision_id = b"revid"
3735.2.57 by Jelmer Vernooij
Make sure CHKInventory._entry_cache gets initialized in create_by_apply_delta.
1106
        chk_bytes = self.get_chk_bytes()
1107
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1108
        inv.add_path("", "directory", b"myrootid", None)
3735.2.57 by Jelmer Vernooij
Make sure CHKInventory._entry_cache gets initialized in create_by_apply_delta.
1109
        inv.revision_id = "expectedid"
1110
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
4526.9.15 by Robert Collins
Fix broken CHK inventory test that was applying an inconsistend delta.
1111
        delta = [("", None, base_inv.root.file_id, None),
6973.11.7 by Jelmer Vernooij
Fix more tests.
1112
            (None, "",  b"myrootid", inv.root)]
1113
        new_inv = base_inv.create_by_apply_delta(delta, b"expectedid")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1114
        self.assertEqual(reference_inv.root, new_inv.root)
3735.2.57 by Jelmer Vernooij
Make sure CHKInventory._entry_cache gets initialized in create_by_apply_delta.
1115
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1116
    def test_create_by_apply_delta_empty_add_child(self):
1117
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1118
        inv.revision_id = b"revid"
1119
        inv.root.revision = b"rootrev"
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1120
        chk_bytes = self.get_chk_bytes()
1121
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1122
        a_entry = InventoryFile(b"A-id", "A", inv.root.file_id)
1123
        a_entry.revision = b"filerev"
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1124
        a_entry.executable = True
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1125
        a_entry.text_sha1 = b"ffff"
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1126
        a_entry.text_size = 1
1127
        inv.add(a_entry)
6973.11.9 by Jelmer Vernooij
Fix tests.
1128
        inv.revision_id = b"expectedid"
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1129
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.9 by Jelmer Vernooij
Fix tests.
1130
        delta = [(None, "A",  b"A-id", a_entry)]
6973.11.7 by Jelmer Vernooij
Fix more tests.
1131
        new_inv = base_inv.create_by_apply_delta(delta, b"expectedid")
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1132
        # new_inv should be the same as reference_inv.
1133
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
1134
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
4413.5.10 by John Arbash Meinel
Clean upt the test_inv tests that assumed _root_node was real and not just a key.
1135
        reference_inv.id_to_entry._ensure_root()
1136
        new_inv.id_to_entry._ensure_root()
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
1137
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
1138
            new_inv.id_to_entry._root_node._key)
3735.2.33 by Robert Collins
Create a smoke-tested CHKInventory.iter_changes(CHKInventory) - incomplete in general but enough to start working with.
1139
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1140
    def test_create_by_apply_delta_empty_add_child_updates_parent_id(self):
1141
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1142
        inv.revision_id = b"revid"
1143
        inv.root.revision = b"rootrev"
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1144
        chk_bytes = self.get_chk_bytes()
3735.2.132 by John Arbash Meinel
Remove references to parent_id_basename_index, now that we know we want it.
1145
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1146
        a_entry = InventoryFile(b"A-id", "A", inv.root.file_id)
1147
        a_entry.revision = b"filerev"
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1148
        a_entry.executable = True
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1149
        a_entry.text_sha1 = b"ffff"
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1150
        a_entry.text_size = 1
1151
        inv.add(a_entry)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1152
        inv.revision_id = b"expectedid"
3735.2.132 by John Arbash Meinel
Remove references to parent_id_basename_index, now that we know we want it.
1153
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1154
        delta = [(None, "A", b"A-id", a_entry)]
1155
        new_inv = base_inv.create_by_apply_delta(delta, b"expectedid")
4413.5.10 by John Arbash Meinel
Clean upt the test_inv tests that assumed _root_node was real and not just a key.
1156
        reference_inv.id_to_entry._ensure_root()
1157
        reference_inv.parent_id_basename_to_file_id._ensure_root()
1158
        new_inv.id_to_entry._ensure_root()
1159
        new_inv.parent_id_basename_to_file_id._ensure_root()
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1160
        # new_inv should be the same as reference_inv.
1161
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
1162
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
1163
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
1164
            new_inv.id_to_entry._root_node._key)
1165
        self.assertEqual(reference_inv.parent_id_basename_to_file_id._root_node._key,
1166
            new_inv.parent_id_basename_to_file_id._root_node._key)
1167
3735.2.33 by Robert Collins
Create a smoke-tested CHKInventory.iter_changes(CHKInventory) - incomplete in general but enough to start working with.
1168
    def test_iter_changes(self):
1169
        # Low level bootstrapping smoke test; comprehensive generic tests via
1170
        # InterTree are coming.
1171
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1172
        inv.revision_id = b"revid"
1173
        inv.root.revision = b"rootrev"
1174
        inv.add(InventoryFile(b"fileid", "file", inv.root.file_id))
6973.11.7 by Jelmer Vernooij
Fix more tests.
1175
        inv.get_entry(b"fileid").revision = b"filerev"
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1176
        inv.get_entry(b"fileid").executable = True
1177
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
1178
        inv.get_entry(b"fileid").text_size = 1
3735.2.33 by Robert Collins
Create a smoke-tested CHKInventory.iter_changes(CHKInventory) - incomplete in general but enough to start working with.
1179
        inv2 = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1180
        inv2.revision_id = b"revid2"
1181
        inv2.root.revision = b"rootrev"
1182
        inv2.add(InventoryFile(b"fileid", "file", inv.root.file_id))
6973.11.7 by Jelmer Vernooij
Fix more tests.
1183
        inv2.get_entry(b"fileid").revision = b"filerev2"
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1184
        inv2.get_entry(b"fileid").executable = False
1185
        inv2.get_entry(b"fileid").text_sha1 = b"bbbb"
1186
        inv2.get_entry(b"fileid").text_size = 2
3735.2.33 by Robert Collins
Create a smoke-tested CHKInventory.iter_changes(CHKInventory) - incomplete in general but enough to start working with.
1187
        # get fresh objects.
1188
        chk_bytes = self.get_chk_bytes()
1189
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1190
        bytes = b''.join(chk_inv.to_lines())
6973.11.7 by Jelmer Vernooij
Fix more tests.
1191
        inv_1 = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
3735.2.33 by Robert Collins
Create a smoke-tested CHKInventory.iter_changes(CHKInventory) - incomplete in general but enough to start working with.
1192
        chk_inv2 = CHKInventory.from_inventory(chk_bytes, inv2)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1193
        bytes = b''.join(chk_inv2.to_lines())
6973.11.7 by Jelmer Vernooij
Fix more tests.
1194
        inv_2 = CHKInventory.deserialise(chk_bytes, bytes, (b"revid2",))
1195
        self.assertEqual([(b'fileid', (u'file', u'file'), True, (True, True),
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1196
            (b'TREE_ROOT', b'TREE_ROOT'), (u'file', u'file'), ('file', 'file'),
3735.2.33 by Robert Collins
Create a smoke-tested CHKInventory.iter_changes(CHKInventory) - incomplete in general but enough to start working with.
1197
            (False, True))],
1198
            list(inv_1.iter_changes(inv_2)))
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
1199
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1200
    def test_parent_id_basename_to_file_id_index_enabled(self):
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
1201
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1202
        inv.revision_id = b"revid"
1203
        inv.root.revision = b"rootrev"
1204
        inv.add(InventoryFile(b"fileid", "file", inv.root.file_id))
6973.11.7 by Jelmer Vernooij
Fix more tests.
1205
        inv.get_entry(b"fileid").revision = b"filerev"
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1206
        inv.get_entry(b"fileid").executable = True
1207
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
1208
        inv.get_entry(b"fileid").text_size = 1
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
1209
        # get fresh objects.
1210
        chk_bytes = self.get_chk_bytes()
3735.2.132 by John Arbash Meinel
Remove references to parent_id_basename_index, now that we know we want it.
1211
        tmp_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1212
        bytes = b''.join(tmp_inv.to_lines())
6973.11.7 by Jelmer Vernooij
Fix more tests.
1213
        chk_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1214
        self.assertIsInstance(chk_inv.parent_id_basename_to_file_id, chk_map.CHKMap)
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
1215
        self.assertEqual(
6973.11.7 by Jelmer Vernooij
Fix more tests.
1216
            {(b'', b''): b'TREE_ROOT', (b'TREE_ROOT', b'file'): b'fileid'},
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
1217
            dict(chk_inv.parent_id_basename_to_file_id.iteritems()))
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1218
1219
    def test_file_entry_to_bytes(self):
1220
        inv = CHKInventory(None)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1221
        ie = inventory.InventoryFile(b'file-id', 'filename', b'parent-id')
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1222
        ie.executable = True
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1223
        ie.revision = b'file-rev-id'
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1224
        ie.text_sha1 = b'abcdefgh'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1225
        ie.text_size = 100
1226
        bytes = inv._entry_to_bytes(ie)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1227
        self.assertEqual(b'file: file-id\nparent-id\nfilename\n'
1228
                         b'file-rev-id\nabcdefgh\n100\nY', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1229
        ie2 = inv._bytes_to_entry(bytes)
1230
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1231
        self.assertIsInstance(ie2.name, text_type)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1232
        self.assertEqual((b'filename', b'file-id', b'file-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1233
                         inv._bytes_to_utf8name_key(bytes))
1234
1235
    def test_file2_entry_to_bytes(self):
1236
        inv = CHKInventory(None)
1237
        # \u30a9 == 'omega'
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1238
        ie = inventory.InventoryFile(b'file-id', u'\u03a9name', b'parent-id')
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1239
        ie.executable = False
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1240
        ie.revision = b'file-rev-id'
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1241
        ie.text_sha1 = b'123456'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1242
        ie.text_size = 25
1243
        bytes = inv._entry_to_bytes(ie)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1244
        self.assertEqual(b'file: file-id\nparent-id\n\xce\xa9name\n'
1245
                         b'file-rev-id\n123456\n25\nN', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1246
        ie2 = inv._bytes_to_entry(bytes)
1247
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1248
        self.assertIsInstance(ie2.name, text_type)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1249
        self.assertEqual((b'\xce\xa9name', b'file-id', b'file-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1250
                         inv._bytes_to_utf8name_key(bytes))
1251
1252
    def test_dir_entry_to_bytes(self):
1253
        inv = CHKInventory(None)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1254
        ie = inventory.InventoryDirectory(b'dir-id', 'dirname', b'parent-id')
1255
        ie.revision = b'dir-rev-id'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1256
        bytes = inv._entry_to_bytes(ie)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1257
        self.assertEqual(b'dir: dir-id\nparent-id\ndirname\ndir-rev-id', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1258
        ie2 = inv._bytes_to_entry(bytes)
1259
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1260
        self.assertIsInstance(ie2.name, text_type)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1261
        self.assertEqual((b'dirname', b'dir-id', b'dir-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1262
                         inv._bytes_to_utf8name_key(bytes))
1263
1264
    def test_dir2_entry_to_bytes(self):
1265
        inv = CHKInventory(None)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1266
        ie = inventory.InventoryDirectory(b'dir-id', u'dir\u03a9name',
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1267
                                          None)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1268
        ie.revision = b'dir-rev-id'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1269
        bytes = inv._entry_to_bytes(ie)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1270
        self.assertEqual(b'dir: dir-id\n\ndir\xce\xa9name\n'
1271
                         b'dir-rev-id', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1272
        ie2 = inv._bytes_to_entry(bytes)
1273
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1274
        self.assertIsInstance(ie2.name, text_type)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1275
        self.assertIs(ie2.parent_id, None)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1276
        self.assertEqual((b'dir\xce\xa9name', b'dir-id', b'dir-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1277
                         inv._bytes_to_utf8name_key(bytes))
1278
1279
    def test_symlink_entry_to_bytes(self):
1280
        inv = CHKInventory(None)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1281
        ie = inventory.InventoryLink(b'link-id', 'linkname', b'parent-id')
1282
        ie.revision = b'link-rev-id'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1283
        ie.symlink_target = u'target/path'
1284
        bytes = inv._entry_to_bytes(ie)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1285
        self.assertEqual(b'symlink: link-id\nparent-id\nlinkname\n'
1286
                         b'link-rev-id\ntarget/path', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1287
        ie2 = inv._bytes_to_entry(bytes)
1288
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1289
        self.assertIsInstance(ie2.name, text_type)
1290
        self.assertIsInstance(ie2.symlink_target, text_type)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1291
        self.assertEqual((b'linkname', b'link-id', b'link-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1292
                         inv._bytes_to_utf8name_key(bytes))
1293
1294
    def test_symlink2_entry_to_bytes(self):
1295
        inv = CHKInventory(None)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1296
        ie = inventory.InventoryLink(b'link-id', u'link\u03a9name', b'parent-id')
1297
        ie.revision = b'link-rev-id'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1298
        ie.symlink_target = u'target/\u03a9path'
1299
        bytes = inv._entry_to_bytes(ie)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1300
        self.assertEqual(b'symlink: link-id\nparent-id\nlink\xce\xa9name\n'
1301
                         b'link-rev-id\ntarget/\xce\xa9path', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1302
        ie2 = inv._bytes_to_entry(bytes)
1303
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1304
        self.assertIsInstance(ie2.name, text_type)
1305
        self.assertIsInstance(ie2.symlink_target, text_type)
6973.11.7 by Jelmer Vernooij
Fix more tests.
1306
        self.assertEqual((b'link\xce\xa9name', b'link-id', b'link-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1307
                         inv._bytes_to_utf8name_key(bytes))
1308
1309
    def test_tree_reference_entry_to_bytes(self):
1310
        inv = CHKInventory(None)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1311
        ie = inventory.TreeReference(b'tree-root-id', u'tree\u03a9name',
1312
                                     b'parent-id')
1313
        ie.revision = b'tree-rev-id'
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
1314
        ie.reference_revision = b'ref-rev-id'
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1315
        bytes = inv._entry_to_bytes(ie)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1316
        self.assertEqual(b'tree: tree-root-id\nparent-id\ntree\xce\xa9name\n'
1317
                         b'tree-rev-id\nref-rev-id', bytes)
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1318
        ie2 = inv._bytes_to_entry(bytes)
1319
        self.assertEqual(ie, ie2)
6973.6.2 by Jelmer Vernooij
Fix more tests.
1320
        self.assertIsInstance(ie2.name, text_type)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1321
        self.assertEqual((b'tree\xce\xa9name', b'tree-root-id', b'tree-rev-id'),
3735.36.12 by John Arbash Meinel
Add some direct tests for CHKInventory._entry_to_bytes
1322
                         inv._bytes_to_utf8name_key(bytes))
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1323
5726.2.4 by John Arbash Meinel
_preload should also handle when some entries have already been expanded.
1324
    def make_basic_utf8_inventory(self):
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1325
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1326
        inv.revision_id = b"revid"
1327
        inv.root.revision = b"rootrev"
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1328
        root_id = inv.root.file_id
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1329
        inv.add(InventoryFile(b"fileid", u'f\xefle', root_id))
1330
        inv.get_entry(b"fileid").revision = b"filerev"
1331
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
1332
        inv.get_entry(b"fileid").text_size = 0
1333
        inv.add(InventoryDirectory(b"dirid", u'dir-\N{EURO SIGN}', root_id))
1334
        inv.add(InventoryFile(b"childid", u'ch\xefld', b"dirid"))
1335
        inv.get_entry(b"childid").revision = b"filerev"
1336
        inv.get_entry(b"childid").text_sha1 = b"ffff"
1337
        inv.get_entry(b"childid").text_size = 0
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1338
        chk_bytes = self.get_chk_bytes()
1339
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1340
        bytes = b''.join(chk_inv.to_lines())
6973.11.7 by Jelmer Vernooij
Fix more tests.
1341
        return CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
5726.2.4 by John Arbash Meinel
_preload should also handle when some entries have already been expanded.
1342
1343
    def test__preload_handles_utf8(self):
1344
        new_inv = self.make_basic_utf8_inventory()
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1345
        self.assertEqual({}, new_inv._fileid_to_entry_cache)
1346
        self.assertFalse(new_inv._fully_cached)
1347
        new_inv._preload_cache()
1348
        self.assertEqual(
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1349
            sorted([new_inv.root_id, b"fileid", b"dirid", b"childid"]),
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1350
            sorted(new_inv._fileid_to_entry_cache.keys()))
5726.2.4 by John Arbash Meinel
_preload should also handle when some entries have already been expanded.
1351
        ie_root = new_inv._fileid_to_entry_cache[new_inv.root_id]
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1352
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1353
                         sorted(ie_root._children.keys()))
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1354
        ie_dir = new_inv._fileid_to_entry_cache[b'dirid']
5726.2.3 by John Arbash Meinel
Properly decode basename. In the map it is always stored as UTF-8, but
1355
        self.assertEqual([u'ch\xefld'], sorted(ie_dir._children.keys()))
1356
5726.2.1 by John Arbash Meinel
Fix bug #737234. Preload all entries for iter_entries_by_dir().
1357
    def test__preload_populates_cache(self):
1358
        inv = Inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1359
        inv.revision_id = b"revid"
1360
        inv.root.revision = b"rootrev"
5726.2.1 by John Arbash Meinel
Fix bug #737234. Preload all entries for iter_entries_by_dir().
1361
        root_id = inv.root.file_id
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1362
        inv.add(InventoryFile(b"fileid", "file", root_id))
1363
        inv.get_entry(b"fileid").revision = b"filerev"
1364
        inv.get_entry(b"fileid").executable = True
1365
        inv.get_entry(b"fileid").text_sha1 = b"ffff"
1366
        inv.get_entry(b"fileid").text_size = 1
1367
        inv.add(InventoryDirectory(b"dirid", "dir", root_id))
1368
        inv.add(InventoryFile(b"childid", "child", b"dirid"))
1369
        inv.get_entry(b"childid").revision = b"filerev"
1370
        inv.get_entry(b"childid").executable = False
1371
        inv.get_entry(b"childid").text_sha1 = b"dddd"
1372
        inv.get_entry(b"childid").text_size = 1
5726.2.1 by John Arbash Meinel
Fix bug #737234. Preload all entries for iter_entries_by_dir().
1373
        chk_bytes = self.get_chk_bytes()
1374
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1375
        bytes = b''.join(chk_inv.to_lines())
1376
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
5726.2.1 by John Arbash Meinel
Fix bug #737234. Preload all entries for iter_entries_by_dir().
1377
        self.assertEqual({}, new_inv._fileid_to_entry_cache)
1378
        self.assertFalse(new_inv._fully_cached)
1379
        new_inv._preload_cache()
1380
        self.assertEqual(
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1381
            sorted([root_id, b"fileid", b"dirid", b"childid"]),
5726.2.1 by John Arbash Meinel
Fix bug #737234. Preload all entries for iter_entries_by_dir().
1382
            sorted(new_inv._fileid_to_entry_cache.keys()))
1383
        self.assertTrue(new_inv._fully_cached)
1384
        ie_root = new_inv._fileid_to_entry_cache[root_id]
1385
        self.assertEqual(['dir', 'file'], sorted(ie_root._children.keys()))
1386
        ie_dir = new_inv._fileid_to_entry_cache['dirid']
1387
        self.assertEqual(['child'], sorted(ie_dir._children.keys()))
5609.27.1 by John Arbash Meinel
Backport the fix for bug #737234 to the 2.3 series.
1388
5726.2.4 by John Arbash Meinel
_preload should also handle when some entries have already been expanded.
1389
    def test__preload_handles_partially_evaluated_inventory(self):
1390
        new_inv = self.make_basic_utf8_inventory()
6915.4.2 by Jelmer Vernooij
Remove __getitem__ and __iter__ from Inventory.
1391
        ie = new_inv.get_entry(new_inv.root_id)
5726.2.4 by John Arbash Meinel
_preload should also handle when some entries have already been expanded.
1392
        self.assertIs(None, ie._children)
1393
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1394
                         sorted(ie.children.keys()))
1395
        # Accessing .children loads _children
1396
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1397
                         sorted(ie._children.keys()))
1398
        new_inv._preload_cache()
1399
        # No change
1400
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1401
                         sorted(ie._children.keys()))
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1402
        ie_dir = new_inv.get_entry(b"dirid")
5726.2.4 by John Arbash Meinel
_preload should also handle when some entries have already been expanded.
1403
        self.assertEqual([u'ch\xefld'],
1404
                         sorted(ie_dir._children.keys()))
1405
6024.2.1 by Bastian Bowe
Test to prove error in CHKInventory.filter method
1406
    def test_filter_change_in_renamed_subfolder(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1407
        inv = Inventory(b'tree-root')
1408
        src_ie = inv.add_path('src', 'directory', b'src-id')
1409
        inv.add_path('src/sub/', 'directory', b'sub-id')
1410
        a_ie = inv.add_path('src/sub/a', 'file', b'a-id')
1411
        a_ie.text_sha1 = osutils.sha_string(b'content\n')
1412
        a_ie.text_size = len(b'content\n')
6024.2.1 by Bastian Bowe
Test to prove error in CHKInventory.filter method
1413
        chk_bytes = self.get_chk_bytes()
1414
        inv = CHKInventory.from_inventory(chk_bytes, inv)
1415
        inv = inv.create_by_apply_delta([
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1416
            ("src/sub/a", "src/sub/a", b"a-id", a_ie),
1417
            ("src", "src2", b"src-id", src_ie),
1418
            ], b'new-rev-2')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1419
        new_inv = inv.filter([b'a-id', b'src-id'])
6024.2.1 by Bastian Bowe
Test to prove error in CHKInventory.filter method
1420
        self.assertEqual([
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1421
            ('', b'tree-root'),
1422
            ('src', b'src-id'),
1423
            ('src/sub', b'sub-id'),
1424
            ('src/sub/a', b'a-id'),
6024.2.1 by Bastian Bowe
Test to prove error in CHKInventory.filter method
1425
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1426
1427
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1428
1429
    def get_chk_bytes(self):
1430
        factory = groupcompress.make_pack_factory(True, True, 1)
1431
        trans = self.get_transport('')
1432
        return factory(trans)
1433
1434
    def make_dir(self, inv, name, parent_id):
6855.4.1 by Jelmer Vernooij
Yet more bees.
1435
        inv.add(inv.make_entry('directory', name, parent_id, name.encode('utf-8') + b'-id'))
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1436
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1437
    def make_file(self, inv, name, parent_id, content=b'content\n'):
6973.11.7 by Jelmer Vernooij
Fix more tests.
1438
        ie = inv.make_entry('file', name, parent_id, name.encode('utf-8') + b'-id')
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1439
        ie.text_sha1 = osutils.sha_string(content)
1440
        ie.text_size = len(content)
1441
        inv.add(ie)
1442
1443
    def make_simple_inventory(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1444
        inv = Inventory(b'TREE_ROOT')
1445
        inv.revision_id = b"revid"
1446
        inv.root.revision = b"rootrev"
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1447
        # /                 TREE_ROOT
1448
        # dir1/             dir1-id
1449
        #   sub-file1       sub-file1-id
1450
        #   sub-file2       sub-file2-id
1451
        #   sub-dir1/       sub-dir1-id
1452
        #     subsub-file1  subsub-file1-id
1453
        # dir2/             dir2-id
1454
        #   sub2-file1      sub2-file1-id
1455
        # top               top-id
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1456
        self.make_dir(inv, 'dir1', b'TREE_ROOT')
1457
        self.make_dir(inv, 'dir2', b'TREE_ROOT')
1458
        self.make_dir(inv, 'sub-dir1', b'dir1-id')
1459
        self.make_file(inv, 'top', b'TREE_ROOT')
1460
        self.make_file(inv, 'sub-file1', b'dir1-id')
1461
        self.make_file(inv, 'sub-file2', b'dir1-id')
1462
        self.make_file(inv, 'subsub-file1', b'sub-dir1-id')
1463
        self.make_file(inv, 'sub2-file1', b'dir2-id')
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1464
        chk_bytes = self.get_chk_bytes()
4634.51.8 by John Arbash Meinel
Catch a corner case that we were missing.
1465
        #  use a small maximum_size to force internal paging structures
1466
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv,
1467
                        maximum_size=100,
6963.1.1 by Jelmer Vernooij
Fix a bunch of tests on python3.
1468
                        search_key_name=b'hash-255-way')
1469
        bytes = b''.join(chk_inv.to_lines())
1470
        return CHKInventory.deserialise(chk_bytes, bytes, (b"revid",))
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1471
1472
    def assert_Getitems(self, expected_fileids, inv, file_ids):
1473
        self.assertEqual(sorted(expected_fileids),
1474
                         sorted([ie.file_id for ie in inv._getitems(file_ids)]))
1475
4634.51.5 by John Arbash Meinel
Change the api a bit.
1476
    def assertExpand(self, all_ids, inv, file_ids):
1477
        (val_all_ids,
1478
         val_children) = inv._expand_fileids_to_parents_and_children(file_ids)
1479
        self.assertEqual(set(all_ids), val_all_ids)
1480
        entries = inv._getitems(val_all_ids)
1481
        expected_children = {}
1482
        for entry in entries:
1483
            s = expected_children.setdefault(entry.parent_id, [])
1484
            s.append(entry.file_id)
1485
        val_children = dict((k, sorted(v)) for k, v
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
1486
                            in val_children.items())
4634.51.5 by John Arbash Meinel
Change the api a bit.
1487
        expected_children = dict((k, sorted(v)) for k, v
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
1488
                            in expected_children.items())
4634.51.5 by John Arbash Meinel
Change the api a bit.
1489
        self.assertEqual(expected_children, val_children)
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1490
1491
    def test_make_simple_inventory(self):
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1492
        inv = self.make_simple_inventory()
1493
        layout = []
1494
        for path, entry in inv.iter_entries_by_dir():
1495
            layout.append((path, entry.file_id))
1496
        self.assertEqual([
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1497
            ('', b'TREE_ROOT'),
1498
            ('dir1', b'dir1-id'),
1499
            ('dir2', b'dir2-id'),
1500
            ('top', b'top-id'),
1501
            ('dir1/sub-dir1', b'sub-dir1-id'),
1502
            ('dir1/sub-file1', b'sub-file1-id'),
1503
            ('dir1/sub-file2', b'sub-file2-id'),
1504
            ('dir1/sub-dir1/subsub-file1', b'subsub-file1-id'),
1505
            ('dir2/sub2-file1', b'sub2-file1-id'),
4634.51.2 by John Arbash Meinel
Start laying the groundwork for testing the expansion code
1506
            ], layout)
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1507
1508
    def test__getitems(self):
1509
        inv = self.make_simple_inventory()
1510
        # Reading from disk
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1511
        self.assert_Getitems([b'dir1-id'], inv, [b'dir1-id'])
1512
        self.assertTrue(b'dir1-id' in inv._fileid_to_entry_cache)
1513
        self.assertFalse(b'sub-file2-id' in inv._fileid_to_entry_cache)
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1514
        # From cache
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1515
        self.assert_Getitems([b'dir1-id'], inv, [b'dir1-id'])
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1516
        # Mixed
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1517
        self.assert_Getitems([b'dir1-id', b'sub-file2-id'], inv,
1518
                             [b'dir1-id', b'sub-file2-id'])
1519
        self.assertTrue(b'dir1-id' in inv._fileid_to_entry_cache)
1520
        self.assertTrue(b'sub-file2-id' in inv._fileid_to_entry_cache)
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1521
1522
    def test_single_file(self):
1523
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1524
        self.assertExpand([b'TREE_ROOT', b'top-id'], inv, ['top-id'])
4634.51.3 by John Arbash Meinel
We have iteration to parents working, need to find children now.
1525
1526
    def test_get_all_parents(self):
1527
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1528
        self.assertExpand([b'TREE_ROOT', b'dir1-id', b'sub-dir1-id',
1529
                           b'subsub-file1-id',
1530
                          ], inv, [b'subsub-file1-id'])
4634.51.4 by John Arbash Meinel
Implement an expansion function that works directly on the chk maps.
1531
1532
    def test_get_children(self):
1533
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1534
        self.assertExpand([b'TREE_ROOT', b'dir1-id', b'sub-dir1-id',
1535
                           b'sub-file1-id', b'sub-file2-id', b'subsub-file1-id',
1536
                          ], inv, [b'dir1-id'])
4634.51.8 by John Arbash Meinel
Catch a corner case that we were missing.
1537
1538
    def test_from_root(self):
1539
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1540
        self.assertExpand([b'TREE_ROOT', b'dir1-id', b'dir2-id', b'sub-dir1-id',
1541
                           b'sub-file1-id', b'sub-file2-id', b'sub2-file1-id',
1542
                           b'subsub-file1-id', b'top-id'], inv, [b'TREE_ROOT'])
4634.51.8 by John Arbash Meinel
Catch a corner case that we were missing.
1543
1544
    def test_top_level_file(self):
1545
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1546
        self.assertExpand([b'TREE_ROOT', b'top-id'], inv, ['top-id'])
4634.51.8 by John Arbash Meinel
Catch a corner case that we were missing.
1547
1548
    def test_subsub_file(self):
1549
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1550
        self.assertExpand([b'TREE_ROOT', b'dir1-id', b'sub-dir1-id',
1551
                           b'subsub-file1-id'], inv, [b'subsub-file1-id'])
4634.51.8 by John Arbash Meinel
Catch a corner case that we were missing.
1552
1553
    def test_sub_and_root(self):
1554
        inv = self.make_simple_inventory()
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1555
        self.assertExpand([b'TREE_ROOT', b'dir1-id', b'sub-dir1-id', b'top-id',
1556
                           b'subsub-file1-id'], inv, ['top-id', b'subsub-file1-id'])
5802.1.2 by Jelmer Vernooij
Add test for mutable_inventory_from_tree.
1557
1558
1559
class TestMutableInventoryFromTree(TestCaseWithTransport):
1560
1561
    def test_empty(self):
1562
        repository = self.make_repository('.')
1563
        tree = repository.revision_tree(revision.NULL_REVISION)
1564
        inv = mutable_inventory_from_tree(tree)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1565
        self.assertEqual(revision.NULL_REVISION, inv.revision_id)
1566
        self.assertEqual(0, len(inv))
5802.1.2 by Jelmer Vernooij
Add test for mutable_inventory_from_tree.
1567
1568
    def test_some_files(self):
1569
        wt = self.make_branch_and_tree('.')
1570
        self.build_tree(['a'])
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1571
        wt.add(['a'], [b'thefileid'])
5802.1.2 by Jelmer Vernooij
Add test for mutable_inventory_from_tree.
1572
        revid = wt.commit("commit")
1573
        tree = wt.branch.repository.revision_tree(revid)
1574
        inv = mutable_inventory_from_tree(tree)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1575
        self.assertEqual(revid, inv.revision_id)
1576
        self.assertEqual(2, len(inv))
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1577
        self.assertEqual("a", inv.get_entry(b'thefileid').name)
5802.1.2 by Jelmer Vernooij
Add test for mutable_inventory_from_tree.
1578
        # The inventory should be mutable and independent of
1579
        # the original tree
6973.11.6 by Jelmer Vernooij
Fix more http tests.
1580
        self.assertFalse(tree.root_inventory.get_entry(b'thefileid').executable)
1581
        inv.get_entry(b'thefileid').executable = True
1582
        self.assertFalse(tree.root_inventory.get_entry(b'thefileid').executable)