/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5752.3.8 by John Arbash Meinel
Merge bzr.dev 5764 to resolve release-notes (aka NEWS) conflicts
1
# Copyright (C) 2007-2011 Canonical Ltd
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
17
18
"""Tests for WorkingTreeFormat4"""
19
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
20
import os
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
21
import time
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
22
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
23
from bzrlib import (
24
    bzrdir,
25
    dirstate,
26
    errors,
1551.15.6 by Aaron Bentley
Use ROOT_ID when the repository supports old clients (Bug #107168)
27
    inventory,
2466.4.1 by John Arbash Meinel
Add a (failing) test that exposes how _iter_changes is accidentally walking into unversioned directories.
28
    osutils,
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
29
    workingtree_4,
30
    )
31
from bzrlib.lockdir import LockDir
32
from bzrlib.tests import TestCaseWithTransport, TestSkipped
33
from bzrlib.tree import InterTree
34
35
36
class TestWorkingTreeFormat4(TestCaseWithTransport):
37
    """Tests specific to WorkingTreeFormat4."""
38
39
    def test_disk_layout(self):
40
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
41
        control.create_repository()
42
        control.create_branch()
43
        tree = workingtree_4.WorkingTreeFormat4().initialize(control)
44
        # we want:
45
        # format 'Bazaar Working Tree format 4'
46
        # stat-cache = ??
47
        t = control.get_workingtree_transport(None)
2255.2.230 by Robert Collins
Update tree format signatures to mention introducing bzr version.
48
        self.assertEqualDiff('Bazaar Working Tree Format 4 (bzr 0.15)\n',
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
49
                             t.get('format').read())
50
        self.assertFalse(t.has('inventory.basis'))
51
        # no last-revision file means 'None' or 'NULLREVISION'
52
        self.assertFalse(t.has('last-revision'))
53
        state = dirstate.DirState.on_file(t.local_abspath('dirstate'))
54
        state.lock_read()
55
        try:
56
            self.assertEqual([], state.get_parent_ids())
57
        finally:
58
            state.unlock()
59
60
    def test_uses_lockdir(self):
61
        """WorkingTreeFormat4 uses its own LockDir:
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
62
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
63
            - lock is a directory
64
            - when the WorkingTree is locked, LockDir can see that
65
        """
66
        # this test could be factored into a subclass of tests common to both
4285.2.1 by Vincent Ladeuil
Cleanup test imports and use features to better track skipped tests.
67
        # format 3 and 4, but for now its not much of an issue as there is only
68
        # one in common.
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
69
        t = self.get_transport()
70
        tree = self.make_workingtree()
71
        self.assertIsDirectory('.bzr', t)
72
        self.assertIsDirectory('.bzr/checkout', t)
73
        self.assertIsDirectory('.bzr/checkout/lock', t)
74
        our_lock = LockDir(t, '.bzr/checkout/lock')
75
        self.assertEquals(our_lock.peek(), None)
76
        tree.lock_write()
77
        self.assertTrue(our_lock.peek())
78
        tree.unlock()
79
        self.assertEquals(our_lock.peek(), None)
80
81
    def make_workingtree(self, relpath=''):
82
        url = self.get_url(relpath)
83
        if relpath:
84
            self.build_tree([relpath + '/'])
85
        dir = bzrdir.BzrDirMetaFormat1().initialize(url)
86
        repo = dir.create_repository()
87
        branch = dir.create_branch()
88
        try:
89
            return workingtree_4.WorkingTreeFormat4().initialize(dir)
90
        except errors.NotLocalUrl:
91
            raise TestSkipped('Not a local URL')
92
93
    def test_dirstate_stores_all_parent_inventories(self):
94
        tree = self.make_workingtree()
95
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
96
        # We're going to build in tree a working tree
97
        # with three parent trees, with some files in common.
98
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
99
        # We really don't want to do commit or merge in the new dirstate-based
100
        # tree, because that might not work yet.  So instead we build
101
        # revisions elsewhere and pull them across, doing by hand part of the
102
        # work that merge would do.
103
104
        subtree = self.make_branch_and_tree('subdir')
105
        # writelock the tree so its repository doesn't get readlocked by
106
        # the revision tree locks. This works around the bug where we dont
107
        # permit lock upgrading.
108
        subtree.lock_write()
109
        self.addCleanup(subtree.unlock)
110
        self.build_tree(['subdir/file-a',])
111
        subtree.add(['file-a'], ['id-a'])
112
        rev1 = subtree.commit('commit in subdir')
113
114
        subtree2 = subtree.bzrdir.sprout('subdir2').open_workingtree()
115
        self.build_tree(['subdir2/file-b'])
116
        subtree2.add(['file-b'], ['id-b'])
117
        rev2 = subtree2.commit('commit in subdir2')
118
119
        subtree.flush()
3462.1.7 by John Arbash Meinel
fix a test that assumed WT4.set_parent_trees() wouldn't filter the list.
120
        subtree3 = subtree.bzrdir.sprout('subdir3').open_workingtree()
121
        rev3 = subtree3.commit('merge from subdir2')
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
122
123
        repo = tree.branch.repository
3462.1.7 by John Arbash Meinel
fix a test that assumed WT4.set_parent_trees() wouldn't filter the list.
124
        repo.fetch(subtree.branch.repository, rev1)
125
        repo.fetch(subtree2.branch.repository, rev2)
126
        repo.fetch(subtree3.branch.repository, rev3)
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
127
        # will also pull the others...
128
129
        # create repository based revision trees
3462.1.7 by John Arbash Meinel
fix a test that assumed WT4.set_parent_trees() wouldn't filter the list.
130
        rev1_revtree = repo.revision_tree(rev1)
131
        rev2_revtree = repo.revision_tree(rev2)
132
        rev3_revtree = repo.revision_tree(rev3)
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
133
        # tree doesn't contain a text merge yet but we'll just
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
134
        # set the parents as if a merge had taken place.
135
        # this should cause the tree data to be folded into the
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
136
        # dirstate.
137
        tree.set_parent_trees([
138
            (rev1, rev1_revtree),
139
            (rev2, rev2_revtree),
140
            (rev3, rev3_revtree), ])
141
142
        # create tree-sourced revision trees
143
        rev1_tree = tree.revision_tree(rev1)
144
        rev1_tree.lock_read()
145
        self.addCleanup(rev1_tree.unlock)
146
        rev2_tree = tree.revision_tree(rev2)
147
        rev2_tree.lock_read()
148
        self.addCleanup(rev2_tree.unlock)
149
        rev3_tree = tree.revision_tree(rev3)
150
        rev3_tree.lock_read()
151
        self.addCleanup(rev3_tree.unlock)
152
153
        # now we should be able to get them back out
154
        self.assertTreesEqual(rev1_revtree, rev1_tree)
155
        self.assertTreesEqual(rev2_revtree, rev2_tree)
156
        self.assertTreesEqual(rev3_revtree, rev3_tree)
157
158
    def test_dirstate_doesnt_read_parents_from_repo_when_setting(self):
159
        """Setting parent trees on a dirstate working tree takes
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
160
        the trees it's given and doesn't need to read them from the
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
161
        repository.
162
        """
163
        tree = self.make_workingtree()
164
165
        subtree = self.make_branch_and_tree('subdir')
166
        rev1 = subtree.commit('commit in subdir')
167
        rev1_tree = subtree.basis_tree()
168
        rev1_tree.lock_read()
169
        self.addCleanup(rev1_tree.unlock)
170
171
        tree.branch.pull(subtree.branch)
172
173
        # break the repository's legs to make sure it only uses the trees
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
174
        # it's given; any calls to forbidden methods will raise an
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
175
        # AssertionError
176
        repo = tree.branch.repository
177
        repo.get_revision = self.fail
178
        repo.get_inventory = self.fail
4988.5.1 by Jelmer Vernooij
Rename Repository.get_inventory_xml -> Repository._get_inventory_xml.
179
        repo._get_inventory_xml = self.fail
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
180
        # try to set the parent trees.
181
        tree.set_parent_trees([(rev1, rev1_tree)])
182
183
    def test_dirstate_doesnt_read_from_repo_when_returning_cache_tree(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
184
        """Getting parent trees from a dirstate tree does not read from the
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
185
        repos inventory store. This is an important part of the dirstate
186
        performance optimisation work.
187
        """
188
        tree = self.make_workingtree()
189
190
        subtree = self.make_branch_and_tree('subdir')
191
        # writelock the tree so its repository doesn't get readlocked by
192
        # the revision tree locks. This works around the bug where we dont
193
        # permit lock upgrading.
194
        subtree.lock_write()
195
        self.addCleanup(subtree.unlock)
196
        rev1 = subtree.commit('commit in subdir')
197
        rev1_tree = subtree.basis_tree()
198
        rev1_tree.lock_read()
199
        rev1_tree.inventory
200
        self.addCleanup(rev1_tree.unlock)
201
        rev2 = subtree.commit('second commit in subdir', allow_pointless=True)
202
        rev2_tree = subtree.basis_tree()
203
        rev2_tree.lock_read()
204
        rev2_tree.inventory
205
        self.addCleanup(rev2_tree.unlock)
206
207
        tree.branch.pull(subtree.branch)
208
209
        # break the repository's legs to make sure it only uses the trees
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
210
        # it's given; any calls to forbidden methods will raise an
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
211
        # AssertionError
212
        repo = tree.branch.repository
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
213
        # dont uncomment this: the revision object must be accessed to
214
        # answer 'get_parent_ids' for the revision tree- dirstate does not
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
215
        # cache the parents of a parent tree at this point.
216
        #repo.get_revision = self.fail
217
        repo.get_inventory = self.fail
4988.5.1 by Jelmer Vernooij
Rename Repository.get_inventory_xml -> Repository._get_inventory_xml.
218
        repo._get_inventory_xml = self.fail
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
219
        # set the parent trees.
220
        tree.set_parent_trees([(rev1, rev1_tree), (rev2, rev2_tree)])
221
        # read the first tree
222
        result_rev1_tree = tree.revision_tree(rev1)
223
        # read the second
224
        result_rev2_tree = tree.revision_tree(rev2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
225
        # compare - there should be no differences between the handed and
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
226
        # returned trees
227
        self.assertTreesEqual(rev1_tree, result_rev1_tree)
228
        self.assertTreesEqual(rev2_tree, result_rev2_tree)
229
230
    def test_dirstate_doesnt_cache_non_parent_trees(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
231
        """Getting parent trees from a dirstate tree does not read from the
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
232
        repos inventory store. This is an important part of the dirstate
233
        performance optimisation work.
234
        """
235
        tree = self.make_workingtree()
236
237
        # make a tree that we can try for, which is able to be returned but
238
        # must not be
239
        subtree = self.make_branch_and_tree('subdir')
240
        rev1 = subtree.commit('commit in subdir')
241
        tree.branch.pull(subtree.branch)
242
        # check it fails
243
        self.assertRaises(errors.NoSuchRevision, tree.revision_tree, rev1)
244
245
    def test_no_dirstate_outside_lock(self):
246
        # temporary test until the code is mature enough to test from outside.
247
        """Getting a dirstate object fails if there is no lock."""
248
        def lock_and_call_current_dirstate(tree, lock_method):
249
            getattr(tree, lock_method)()
250
            tree.current_dirstate()
251
            tree.unlock()
252
        tree = self.make_workingtree()
253
        self.assertRaises(errors.ObjectNotLocked, tree.current_dirstate)
254
        lock_and_call_current_dirstate(tree, 'lock_read')
255
        self.assertRaises(errors.ObjectNotLocked, tree.current_dirstate)
256
        lock_and_call_current_dirstate(tree, 'lock_write')
257
        self.assertRaises(errors.ObjectNotLocked, tree.current_dirstate)
258
        lock_and_call_current_dirstate(tree, 'lock_tree_write')
259
        self.assertRaises(errors.ObjectNotLocked, tree.current_dirstate)
260
5847.4.1 by John Arbash Meinel
When WT4.set_parent_trees() is called, sometimes we can use
261
    def test_set_parent_trees_uses_update_basis_by_delta(self):
262
        builder = self.make_branch_builder('source')
263
        builder.start_series()
264
        self.addCleanup(builder.finish_series)
265
        builder.build_snapshot('A', [], [
266
            ('add', ('', 'root-id', 'directory', None)),
267
            ('add', ('a', 'a-id', 'file', 'content\n'))])
268
        builder.build_snapshot('B', ['A'], [
269
            ('modify', ('a-id', 'new content\nfor a\n')),
270
            ('add', ('b', 'b-id', 'file', 'b-content\n'))])
271
        tree = self.make_workingtree('tree')
272
        source_branch = builder.get_branch()
273
        tree.branch.repository.fetch(source_branch.repository, 'B')
274
        tree.pull(source_branch, stop_revision='A')
275
        tree.lock_write()
276
        self.addCleanup(tree.unlock)
277
        state = tree.current_dirstate()
278
        called = []
279
        orig_update = state.update_basis_by_delta
280
        def log_update_basis_by_delta(delta, new_revid):
281
            called.append(new_revid)
282
            return orig_update(delta, new_revid)
283
        state.update_basis_by_delta = log_update_basis_by_delta
284
        basis = tree.basis_tree()
285
        self.assertEqual('a-id', basis.path2id('a'))
286
        self.assertEqual(None, basis.path2id('b'))
287
        def fail_set_parent_trees(trees, ghosts):
288
            raise AssertionError('dirstate.set_parent_trees() was called')
289
        state.set_parent_trees = fail_set_parent_trees
290
        repo = tree.branch.repository
291
        tree.pull(source_branch, stop_revision='B')
292
        self.assertEqual(['B'], called)
293
        basis = tree.basis_tree()
294
        self.assertEqual('a-id', basis.path2id('a'))
295
        self.assertEqual('b-id', basis.path2id('b'))
296
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
297
    def test_new_dirstate_on_new_lock(self):
298
        # until we have detection for when a dirstate can be reused, we
299
        # want to reparse dirstate on every new lock.
300
        known_dirstates = set()
301
        def lock_and_compare_all_current_dirstate(tree, lock_method):
302
            getattr(tree, lock_method)()
303
            state = tree.current_dirstate()
304
            self.assertFalse(state in known_dirstates)
305
            known_dirstates.add(state)
306
            tree.unlock()
307
        tree = self.make_workingtree()
308
        # lock twice with each type to prevent silly per-lock-type bugs.
309
        # each lock and compare looks for a unique state object.
310
        lock_and_compare_all_current_dirstate(tree, 'lock_read')
311
        lock_and_compare_all_current_dirstate(tree, 'lock_read')
312
        lock_and_compare_all_current_dirstate(tree, 'lock_tree_write')
313
        lock_and_compare_all_current_dirstate(tree, 'lock_tree_write')
314
        lock_and_compare_all_current_dirstate(tree, 'lock_write')
315
        lock_and_compare_all_current_dirstate(tree, 'lock_write')
316
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
317
    def test_constructing_invalid_interdirstate_raises(self):
318
        tree = self.make_workingtree()
319
        rev_id = tree.commit('first post')
320
        rev_id2 = tree.commit('second post')
321
        rev_tree = tree.branch.repository.revision_tree(rev_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
322
        # Exception is not a great thing to raise, but this test is
323
        # very short, and code is used to sanity check other tests, so
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
324
        # a full error object is YAGNI.
325
        self.assertRaises(
326
            Exception, workingtree_4.InterDirStateTree, rev_tree, tree)
327
        self.assertRaises(
328
            Exception, workingtree_4.InterDirStateTree, tree, rev_tree)
329
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
330
    def test_revtree_to_revtree_not_interdirstate(self):
331
        # we should not get a dirstate optimiser for two repository sourced
332
        # revtrees. we can't prove a negative, so we dont do exhaustive tests
333
        # of all formats; though that could be written in the future it doesn't
334
        # seem well worth it.
335
        tree = self.make_workingtree()
336
        rev_id = tree.commit('first post')
337
        rev_id2 = tree.commit('second post')
338
        rev_tree = tree.branch.repository.revision_tree(rev_id)
339
        rev_tree2 = tree.branch.repository.revision_tree(rev_id2)
340
        optimiser = InterTree.get(rev_tree, rev_tree2)
341
        self.assertIsInstance(optimiser, InterTree)
342
        self.assertFalse(isinstance(optimiser, workingtree_4.InterDirStateTree))
343
        optimiser = InterTree.get(rev_tree2, rev_tree)
344
        self.assertIsInstance(optimiser, InterTree)
345
        self.assertFalse(isinstance(optimiser, workingtree_4.InterDirStateTree))
346
347
    def test_revtree_not_in_dirstate_to_dirstate_not_interdirstate(self):
348
        # we should not get a dirstate optimiser when the revision id for of
349
        # the source is not in the dirstate of the target.
350
        tree = self.make_workingtree()
351
        rev_id = tree.commit('first post')
352
        rev_id2 = tree.commit('second post')
353
        rev_tree = tree.branch.repository.revision_tree(rev_id)
354
        tree.lock_read()
355
        optimiser = InterTree.get(rev_tree, tree)
356
        self.assertIsInstance(optimiser, InterTree)
357
        self.assertFalse(isinstance(optimiser, workingtree_4.InterDirStateTree))
358
        optimiser = InterTree.get(tree, rev_tree)
359
        self.assertIsInstance(optimiser, InterTree)
360
        self.assertFalse(isinstance(optimiser, workingtree_4.InterDirStateTree))
361
        tree.unlock()
362
363
    def test_empty_basis_to_dirstate_tree(self):
364
        # we should get a InterDirStateTree for doing
365
        # 'changes_from' from the first basis dirstate revision tree to a
366
        # WorkingTree4.
367
        tree = self.make_workingtree()
368
        tree.lock_read()
369
        basis_tree = tree.basis_tree()
370
        basis_tree.lock_read()
371
        optimiser = InterTree.get(basis_tree, tree)
372
        tree.unlock()
373
        basis_tree.unlock()
374
        self.assertIsInstance(optimiser, workingtree_4.InterDirStateTree)
375
376
    def test_nonempty_basis_to_dirstate_tree(self):
377
        # we should get a InterDirStateTree for doing
378
        # 'changes_from' from a non-null basis dirstate revision tree to a
379
        # WorkingTree4.
380
        tree = self.make_workingtree()
381
        tree.commit('first post')
382
        tree.lock_read()
383
        basis_tree = tree.basis_tree()
384
        basis_tree.lock_read()
385
        optimiser = InterTree.get(basis_tree, tree)
386
        tree.unlock()
387
        basis_tree.unlock()
388
        self.assertIsInstance(optimiser, workingtree_4.InterDirStateTree)
389
390
    def test_empty_basis_revtree_to_dirstate_tree(self):
391
        # we should get a InterDirStateTree for doing
392
        # 'changes_from' from an empty repository based rev tree to a
393
        # WorkingTree4.
394
        tree = self.make_workingtree()
395
        tree.lock_read()
396
        basis_tree = tree.branch.repository.revision_tree(tree.last_revision())
397
        basis_tree.lock_read()
398
        optimiser = InterTree.get(basis_tree, tree)
399
        tree.unlock()
400
        basis_tree.unlock()
401
        self.assertIsInstance(optimiser, workingtree_4.InterDirStateTree)
402
403
    def test_nonempty_basis_revtree_to_dirstate_tree(self):
404
        # we should get a InterDirStateTree for doing
405
        # 'changes_from' from a non-null repository based rev tree to a
406
        # WorkingTree4.
407
        tree = self.make_workingtree()
408
        tree.commit('first post')
409
        tree.lock_read()
410
        basis_tree = tree.branch.repository.revision_tree(tree.last_revision())
411
        basis_tree.lock_read()
412
        optimiser = InterTree.get(basis_tree, tree)
413
        tree.unlock()
414
        basis_tree.unlock()
415
        self.assertIsInstance(optimiser, workingtree_4.InterDirStateTree)
416
417
    def test_tree_to_basis_in_other_tree(self):
418
        # we should get a InterDirStateTree when
419
        # the source revid is in the dirstate object of the target and
420
        # the dirstates are different. This is largely covered by testing
421
        # with repository revtrees, so is just for extra confidence.
422
        tree = self.make_workingtree('a')
423
        tree.commit('first post')
424
        tree2 = self.make_workingtree('b')
425
        tree2.pull(tree.branch)
426
        basis_tree = tree.basis_tree()
427
        tree2.lock_read()
428
        basis_tree.lock_read()
429
        optimiser = InterTree.get(basis_tree, tree2)
430
        tree2.unlock()
431
        basis_tree.unlock()
432
        self.assertIsInstance(optimiser, workingtree_4.InterDirStateTree)
433
434
    def test_merged_revtree_to_tree(self):
435
        # we should get a InterDirStateTree when
436
        # the source tree is a merged tree present in the dirstate of target.
437
        tree = self.make_workingtree('a')
438
        tree.commit('first post')
439
        tree.commit('tree 1 commit 2')
440
        tree2 = self.make_workingtree('b')
441
        tree2.pull(tree.branch)
442
        tree2.commit('tree 2 commit 2')
443
        tree.merge_from_branch(tree2.branch)
444
        second_parent_tree = tree.revision_tree(tree.get_parent_ids()[1])
445
        second_parent_tree.lock_read()
446
        tree.lock_read()
447
        optimiser = InterTree.get(second_parent_tree, tree)
448
        tree.unlock()
449
        second_parent_tree.unlock()
450
        self.assertIsInstance(optimiser, workingtree_4.InterDirStateTree)
2255.2.144 by John Arbash Meinel
Simplify update_minimal a bit more, by making id_index a
451
452
    def test_id2path(self):
453
        tree = self.make_workingtree('tree')
2255.2.147 by John Arbash Meinel
Move fast id => path lookups down into DirState
454
        self.build_tree(['tree/a', 'tree/b'])
2255.2.144 by John Arbash Meinel
Simplify update_minimal a bit more, by making id_index a
455
        tree.add(['a'], ['a-id'])
456
        self.assertEqual(u'a', tree.id2path('a-id'))
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
457
        self.assertRaises(errors.NoSuchId, tree.id2path, 'a')
2255.2.144 by John Arbash Meinel
Simplify update_minimal a bit more, by making id_index a
458
        tree.commit('a')
2255.2.147 by John Arbash Meinel
Move fast id => path lookups down into DirState
459
        tree.add(['b'], ['b-id'])
2255.2.144 by John Arbash Meinel
Simplify update_minimal a bit more, by making id_index a
460
2321.1.2 by Robert Collins
Skip new tests that depend on unicode file paths.
461
        try:
2825.6.1 by Robert Collins
* ``WorkingTree.rename_one`` will now raise an error if normalisation of the
462
            new_path = u'b\u03bcrry'
463
            tree.rename_one('a', new_path)
2321.1.2 by Robert Collins
Skip new tests that depend on unicode file paths.
464
        except UnicodeEncodeError:
465
            # support running the test on non-unicode platforms
466
            new_path = 'c'
2825.6.1 by Robert Collins
* ``WorkingTree.rename_one`` will now raise an error if normalisation of the
467
            tree.rename_one('a', new_path)
2321.1.2 by Robert Collins
Skip new tests that depend on unicode file paths.
468
        self.assertEqual(new_path, tree.id2path('a-id'))
2255.2.144 by John Arbash Meinel
Simplify update_minimal a bit more, by making id_index a
469
        tree.commit(u'b\xb5rry')
470
        tree.unversion(['a-id'])
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
471
        self.assertRaises(errors.NoSuchId, tree.id2path, 'a-id')
2255.2.147 by John Arbash Meinel
Move fast id => path lookups down into DirState
472
        self.assertEqual('b', tree.id2path('b-id'))
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
473
        self.assertRaises(errors.NoSuchId, tree.id2path, 'c-id')
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
474
475
    def test_unique_root_id_per_tree(self):
476
        # each time you initialize a new tree, it gets a different root id
2255.2.207 by Robert Collins
Reinstate format change for test_workingtree_4
477
        format_name = 'dirstate-with-subtree'
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
478
        tree1 = self.make_branch_and_tree('tree1',
479
            format=format_name)
480
        tree2 = self.make_branch_and_tree('tree2',
481
            format=format_name)
482
        self.assertNotEqual(tree1.get_root_id(), tree2.get_root_id())
483
        # when you branch, it inherits the same root id
484
        rev1 = tree1.commit('first post')
485
        tree3 = tree1.bzrdir.sprout('tree3').open_workingtree()
486
        self.assertEqual(tree3.get_root_id(), tree1.get_root_id())
487
2255.11.2 by Martin Pool
Add more dirstate root-id-changing tests
488
    def test_set_root_id(self):
489
        # similar to some code that fails in the dirstate-plus-subtree branch
490
        # -- setting the root id while adding a parent seems to scramble the
491
        # dirstate invariants. -- mbp 20070303
492
        def validate():
493
            wt.lock_read()
494
            try:
495
                wt.current_dirstate()._validate()
496
            finally:
497
                wt.unlock()
498
        wt = self.make_workingtree('tree')
499
        wt.set_root_id('TREE-ROOTID')
500
        validate()
501
        wt.commit('somenthing')
502
        validate()
503
        # now switch and commit again
504
        wt.set_root_id('tree-rootid')
505
        validate()
506
        wt.commit('again')
507
        validate()
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
508
1551.15.6 by Aaron Bentley
Use ROOT_ID when the repository supports old clients (Bug #107168)
509
    def test_default_root_id(self):
510
        tree = self.make_branch_and_tree('tag', format='dirstate-tags')
511
        self.assertEqual(inventory.ROOT_ID, tree.get_root_id())
512
        tree = self.make_branch_and_tree('subtree',
513
                                         format='dirstate-with-subtree')
514
        self.assertNotEqual(inventory.ROOT_ID, tree.get_root_id())
515
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
516
    def test_non_subtree_with_nested_trees(self):
517
        # prior to dirstate, st/diff/commit ignored nested trees.
518
        # dirstate, as opposed to dirstate-with-subtree, should
519
        # behave the same way.
520
        tree = self.make_branch_and_tree('.', format='dirstate')
521
        self.assertFalse(tree.supports_tree_reference())
522
        self.build_tree(['dir/'])
523
        # for testing easily.
524
        tree.set_root_id('root')
525
        tree.add(['dir'], ['dir-id'])
526
        subtree = self.make_branch_and_tree('dir')
527
        # the most primitive operation: kind
528
        self.assertEqual('directory', tree.kind('dir-id'))
4570.2.8 by Robert Collins
Adjust WorkingTree4 specific test to deal with iter_changes reporting required directories.
529
        # a diff against the basis should give us a directory and the root (as
530
        # the root is new too).
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
531
        tree.lock_read()
532
        expected = [('dir-id',
533
            (None, u'dir'),
534
            True,
535
            (False, True),
536
            (None, 'root'),
537
            (None, u'dir'),
538
            (None, 'directory'),
4570.2.8 by Robert Collins
Adjust WorkingTree4 specific test to deal with iter_changes reporting required directories.
539
            (None, False)),
540
            ('root', (None, u''), True, (False, True), (None, None),
541
            (None, u''), (None, 'directory'), (None, 0))]
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
542
        self.assertEqual(expected, list(tree.iter_changes(tree.basis_tree(),
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
543
            specific_files=['dir'])))
544
        tree.unlock()
545
        # do a commit, we want to trigger the dirstate fast-path too
546
        tree.commit('first post')
547
        # change the path for the subdir, which will trigger getting all
548
        # its data:
549
        os.rename('dir', 'also-dir')
550
        # now the diff will use the fast path
551
        tree.lock_read()
552
        expected = [('dir-id',
553
            (u'dir', u'dir'),
554
            True,
555
            (True, True),
556
            ('root', 'root'),
557
            ('dir', 'dir'),
558
            ('directory', None),
559
            (False, False))]
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
560
        self.assertEqual(expected, list(tree.iter_changes(tree.basis_tree())))
2255.2.232 by Robert Collins
Make WorkingTree4 report support for references based on the repositories capabilities.
561
        tree.unlock()
562
563
    def test_with_subtree_supports_tree_references(self):
564
        # dirstate-with-subtree should support tree-references.
565
        tree = self.make_branch_and_tree('.', format='dirstate-with-subtree')
566
        self.assertTrue(tree.supports_tree_reference())
567
        # having checked this is on, the tree interface, and intertree
568
        # interface tests, will proceed to test the subtree support of
569
        # workingtree_4.
2466.4.1 by John Arbash Meinel
Add a (failing) test that exposes how _iter_changes is accidentally walking into unversioned directories.
570
571
    def test_iter_changes_ignores_unversioned_dirs(self):
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
572
        """iter_changes should not descend into unversioned directories."""
2466.4.1 by John Arbash Meinel
Add a (failing) test that exposes how _iter_changes is accidentally walking into unversioned directories.
573
        tree = self.make_branch_and_tree('.', format='dirstate')
574
        # We have an unversioned directory at the root, a versioned one with
575
        # other versioned files and an unversioned directory, and another
576
        # versioned dir with nothing but an unversioned directory.
577
        self.build_tree(['unversioned/',
578
                         'unversioned/a',
579
                         'unversioned/b/',
580
                         'versioned/',
581
                         'versioned/unversioned/',
582
                         'versioned/unversioned/a',
583
                         'versioned/unversioned/b/',
584
                         'versioned2/',
585
                         'versioned2/a',
586
                         'versioned2/unversioned/',
587
                         'versioned2/unversioned/a',
588
                         'versioned2/unversioned/b/',
589
                        ])
590
        tree.add(['versioned', 'versioned2', 'versioned2/a'])
591
        tree.commit('one', rev_id='rev-1')
592
        # Trap osutils._walkdirs_utf8 to spy on what dirs have been accessed.
593
        returned = []
594
        def walkdirs_spy(*args, **kwargs):
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
595
            for val in orig(*args, **kwargs):
2466.4.1 by John Arbash Meinel
Add a (failing) test that exposes how _iter_changes is accidentally walking into unversioned directories.
596
                returned.append(val[0][0])
597
                yield val
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
598
        orig = self.overrideAttr(osutils, '_walkdirs_utf8', walkdirs_spy)
2466.4.1 by John Arbash Meinel
Add a (failing) test that exposes how _iter_changes is accidentally walking into unversioned directories.
599
600
        basis = tree.basis_tree()
601
        tree.lock_read()
602
        self.addCleanup(tree.unlock)
603
        basis.lock_read()
604
        self.addCleanup(basis.unlock)
2466.4.2 by John Arbash Meinel
Clean up the (failing) test so that the last thing
605
        changes = [c[1] for c in
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
606
                   tree.iter_changes(basis, want_unversioned=True)]
2466.4.2 by John Arbash Meinel
Clean up the (failing) test so that the last thing
607
        self.assertEqual([(None, 'unversioned'),
608
                          (None, 'versioned/unversioned'),
609
                          (None, 'versioned2/unversioned'),
610
                         ], changes)
611
        self.assertEqual(['', 'versioned', 'versioned2'], returned)
612
        del returned[:] # reset
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
613
        changes = [c[1] for c in tree.iter_changes(basis)]
2466.4.2 by John Arbash Meinel
Clean up the (failing) test so that the last thing
614
        self.assertEqual([], changes)
615
        self.assertEqual(['', 'versioned', 'versioned2'], returned)
3207.2.1 by jameinel
Add a test that _iter_changes raises a clearer error when we encounter an invalid rename.
616
4496.2.1 by Ian Clatworthy
(igc) Improve paths are not versioned reporting (Benoît PIERRE)
617
    def test_iter_changes_unversioned_error(self):
618
        """ Check if a PathsNotVersionedError is correctly raised and the
619
            paths list contains all unversioned entries only.
620
        """
621
        tree = self.make_branch_and_tree('tree')
622
        self.build_tree_contents([('tree/bar', '')])
623
        tree.add(['bar'], ['bar-id'])
624
        tree.lock_read()
625
        self.addCleanup(tree.unlock)
626
        tree_iter_changes = lambda files: [
627
            c for c in tree.iter_changes(tree.basis_tree(), specific_files=files,
628
                                         require_versioned=True)
629
        ]
630
        e = self.assertRaises(errors.PathsNotVersionedError,
631
                              tree_iter_changes, ['bar', 'foo'])
632
        self.assertEqual(e.paths, ['foo'])
633
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
634
    def get_tree_with_cachable_file_foo(self):
635
        tree = self.make_branch_and_tree('.')
5755.1.1 by John Arbash Meinel
Change WT._observed_sha1 to also update st.st_size.
636
        tree.lock_write()
637
        self.addCleanup(tree.unlock)
638
        self.build_tree_contents([('foo', 'a bit of content for foo\n')])
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
639
        tree.add(['foo'], ['foo-id'])
5755.1.1 by John Arbash Meinel
Change WT._observed_sha1 to also update st.st_size.
640
        tree.current_dirstate()._cutoff_time = time.time() + 60
3207.2.1 by jameinel
Add a test that _iter_changes raises a clearer error when we encounter an invalid rename.
641
        return tree
642
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
643
    def test_commit_updates_hash_cache(self):
644
        tree = self.get_tree_with_cachable_file_foo()
645
        revid = tree.commit('a commit')
646
        # tree's dirstate should now have a valid stat entry for foo.
647
        entry = tree._get_entry(path='foo')
648
        expected_sha1 = osutils.sha_file_by_name('foo')
649
        self.assertEqual(expected_sha1, entry[1][0][1])
5755.1.2 by John Arbash Meinel
use soft constants rather than '25'
650
        self.assertEqual(len('a bit of content for foo\n'), entry[1][0][2])
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
651
652
    def test_observed_sha1_cachable(self):
653
        tree = self.get_tree_with_cachable_file_foo()
654
        expected_sha1 = osutils.sha_file_by_name('foo')
3709.3.2 by Robert Collins
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.
655
        statvalue = os.lstat("foo")
5755.1.1 by John Arbash Meinel
Change WT._observed_sha1 to also update st.st_size.
656
        tree._observed_sha1("foo-id", "foo", (expected_sha1, statvalue))
657
        entry = tree._get_entry(path="foo")
658
        entry_state = entry[1][0]
659
        self.assertEqual(expected_sha1, entry_state[1])
5755.1.2 by John Arbash Meinel
use soft constants rather than '25'
660
        self.assertEqual(statvalue.st_size, entry_state[2])
5755.1.1 by John Arbash Meinel
Change WT._observed_sha1 to also update st.st_size.
661
        tree.unlock()
662
        tree.lock_read()
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
663
        tree = tree.bzrdir.open_workingtree()
664
        tree.lock_read()
3207.2.1 by jameinel
Add a test that _iter_changes raises a clearer error when we encounter an invalid rename.
665
        self.addCleanup(tree.unlock)
5755.1.1 by John Arbash Meinel
Change WT._observed_sha1 to also update st.st_size.
666
        entry = tree._get_entry(path="foo")
667
        entry_state = entry[1][0]
668
        self.assertEqual(expected_sha1, entry_state[1])
5755.1.2 by John Arbash Meinel
use soft constants rather than '25'
669
        self.assertEqual(statvalue.st_size, entry_state[2])
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
670
671
    def test_observed_sha1_new_file(self):
672
        tree = self.make_branch_and_tree('.')
673
        self.build_tree(['foo'])
674
        tree.add(['foo'], ['foo-id'])
3207.2.2 by John Arbash Meinel
Fix bug #187169, when an invalid delta is supplied to update_basis_by_delta
675
        tree.lock_read()
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
676
        try:
677
            current_sha1 = tree._get_entry(path="foo")[1][0][1]
678
        finally:
679
            tree.unlock()
680
        tree.lock_write()
681
        try:
682
            tree._observed_sha1("foo-id", "foo",
3709.3.2 by Robert Collins
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.
683
                (osutils.sha_file_by_name('foo'), os.lstat("foo")))
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
684
            # Must not have changed
685
            self.assertEqual(current_sha1,
686
                tree._get_entry(path="foo")[1][0][1])
687
        finally:
688
            tree.unlock()
3709.3.2 by Robert Collins
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.
689
690
    def test_get_file_with_stat_id_only(self):
691
        # Explicit test to ensure we get a lstat value from WT4 trees.
692
        tree = self.make_branch_and_tree('.')
693
        self.build_tree(['foo'])
694
        tree.add(['foo'], ['foo-id'])
695
        tree.lock_read()
696
        self.addCleanup(tree.unlock)
697
        file_obj, statvalue = tree.get_file_with_stat('foo-id')
698
        expected = os.lstat('foo')
4807.2.2 by John Arbash Meinel
Move all the stat comparison and platform checkning code to assertEqualStat.
699
        self.assertEqualStat(expected, statvalue)
3709.3.2 by Robert Collins
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.
700
        self.assertEqual(["contents of foo\n"], file_obj.readlines())
701
702
703
class TestCorruptDirstate(TestCaseWithTransport):
704
    """Tests for how we handle when the dirstate has been corrupted."""
705
706
    def create_wt4(self):
707
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
708
        control.create_repository()
709
        control.create_branch()
710
        tree = workingtree_4.WorkingTreeFormat4().initialize(control)
711
        return tree
712
713
    def test_invalid_rename(self):
714
        tree = self.create_wt4()
715
        # Create a corrupted dirstate
716
        tree.lock_write()
717
        try:
4285.2.1 by Vincent Ladeuil
Cleanup test imports and use features to better track skipped tests.
718
            # We need a parent, or we always compare with NULL
719
            tree.commit('init')
3709.3.2 by Robert Collins
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.
720
            state = tree.current_dirstate()
721
            state._read_dirblocks_if_needed()
722
            # Now add in an invalid entry, a rename with a dangling pointer
723
            state._dirblocks[1][1].append((('', 'foo', 'foo-id'),
724
                                            [('f', '', 0, False, ''),
725
                                             ('r', 'bar', 0 , False, '')]))
726
            self.assertListRaises(errors.CorruptDirstate,
727
                                  tree.iter_changes, tree.basis_tree())
728
        finally:
729
            tree.unlock()
730
731
    def get_simple_dirblocks(self, state):
732
        """Extract the simple information from the DirState.
733
734
        This returns the dirblocks, only with the sha1sum and stat details
735
        filtered out.
736
        """
737
        simple_blocks = []
738
        for block in state._dirblocks:
739
            simple_block = (block[0], [])
740
            for entry in block[1]:
741
                # Include the key for each entry, and for each parent include
742
                # just the minikind, so we know if it was
743
                # present/absent/renamed/etc
744
                simple_block[1].append((entry[0], [i[0] for i in entry[1]]))
745
            simple_blocks.append(simple_block)
746
        return simple_blocks
747
748
    def test_update_basis_with_invalid_delta(self):
749
        """When given an invalid delta, it should abort, and not be saved."""
750
        self.build_tree(['dir/', 'dir/file'])
751
        tree = self.create_wt4()
752
        tree.lock_write()
753
        self.addCleanup(tree.unlock)
754
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
755
        first_revision_id = tree.commit('init')
756
757
        root_id = tree.path2id('')
758
        state = tree.current_dirstate()
759
        state._read_dirblocks_if_needed()
760
        self.assertEqual([
761
            ('', [(('', '', root_id), ['d', 'd'])]),
762
            ('', [(('', 'dir', 'dir-id'), ['d', 'd'])]),
763
            ('dir', [(('dir', 'file', 'file-id'), ['f', 'f'])]),
764
        ],  self.get_simple_dirblocks(state))
765
766
        tree.remove(['dir/file'])
767
        self.assertEqual([
768
            ('', [(('', '', root_id), ['d', 'd'])]),
769
            ('', [(('', 'dir', 'dir-id'), ['d', 'd'])]),
770
            ('dir', [(('dir', 'file', 'file-id'), ['a', 'f'])]),
771
        ],  self.get_simple_dirblocks(state))
772
        # Make sure the removal is written to disk
773
        tree.flush()
774
775
        # self.assertRaises(Exception, tree.update_basis_by_delta,
776
        new_dir = inventory.InventoryDirectory('dir-id', 'new-dir', root_id)
777
        new_dir.revision = 'new-revision-id'
778
        new_file = inventory.InventoryFile('file-id', 'new-file', root_id)
779
        new_file.revision = 'new-revision-id'
780
        self.assertRaises(errors.InconsistentDelta,
781
            tree.update_basis_by_delta, 'new-revision-id',
782
            [('dir', 'new-dir', 'dir-id', new_dir),
783
             ('dir/file', 'new-dir/new-file', 'file-id', new_file),
784
            ])
785
        del state
786
787
        # Now when we re-read the file it should not have been modified
788
        tree.unlock()
789
        tree.lock_read()
790
        self.assertEqual(first_revision_id, tree.last_revision())
791
        state = tree.current_dirstate()
792
        state._read_dirblocks_if_needed()
793
        self.assertEqual([
794
            ('', [(('', '', root_id), ['d', 'd'])]),
795
            ('', [(('', 'dir', 'dir-id'), ['d', 'd'])]),
796
            ('dir', [(('dir', 'file', 'file-id'), ['a', 'f'])]),
797
        ],  self.get_simple_dirblocks(state))
4634.156.1 by Vincent Ladeuil
Don't traceback when unversioning a directory.
798
799
800
class TestInventoryCoherency(TestCaseWithTransport):
801
802
    def test_inventory_is_synced_when_unversioning_a_dir(self):
803
        """Unversioning the root of a subtree unversions the entire subtree."""
804
        tree = self.make_branch_and_tree('.')
805
        self.build_tree(['a/', 'a/b', 'c/'])
806
        tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
807
        # within a lock unversion should take effect
808
        tree.lock_write()
809
        self.addCleanup(tree.unlock)
810
        # Force access to the in memory inventory to trigger bug #494221: try
811
        # maintaining the in-memory inventory
812
        inv = tree.inventory
4634.156.2 by Vincent Ladeuil
Ensure the entries are removed from the inventory
813
        self.assertTrue(inv.has_id('a-id'))
814
        self.assertTrue(inv.has_id('b-id'))
4634.156.1 by Vincent Ladeuil
Don't traceback when unversioning a directory.
815
        tree.unversion(['a-id', 'b-id'])
4634.156.2 by Vincent Ladeuil
Ensure the entries are removed from the inventory
816
        self.assertFalse(inv.has_id('a-id'))
817
        self.assertFalse(inv.has_id('b-id'))