/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

Update a clean branch with the dirstate improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from cStringIO import StringIO
19
19
import os
20
20
 
21
 
from bzrlib import ignores
 
21
from bzrlib import dirstate, ignores
22
22
import bzrlib
23
23
from bzrlib.branch import Branch
24
24
from bzrlib import bzrdir, conflicts, errors, workingtree
226
226
        self.assertEqual([], tree.get_parent_ids())
227
227
 
228
228
 
 
229
class TestWorkingTreeFormat4(TestCaseWithTransport):
 
230
    """Tests specific to WorkingTreeFormat4."""
 
231
 
 
232
    def test_disk_layout(self):
 
233
        control = bzrdir.BzrDir.create(self.get_url())
 
234
        control.create_repository()
 
235
        control.create_branch()
 
236
        tree = workingtree.WorkingTreeFormat4().initialize(control)
 
237
        # we want:
 
238
        # format 'Bazaar-NG Working Tree format 4'
 
239
        # inventory = blank inventory
 
240
        # pending-merges = ''
 
241
        # stat-cache = ??
 
242
        # no inventory.basis yet
 
243
        t = control.get_workingtree_transport(None)
 
244
        self.assertEqualDiff('Bazaar-NG Working Tree format 4',
 
245
                             t.get('format').read())
 
246
        self.assertEqualDiff('<inventory format="5">\n'
 
247
                             '</inventory>\n',
 
248
                             t.get('inventory').read())
 
249
        self.assertEqualDiff('### bzr hashcache v5\n',
 
250
                             t.get('stat-cache').read())
 
251
        self.assertFalse(t.has('inventory.basis'))
 
252
        # no last-revision file means 'None' or 'NULLREVISION'
 
253
        self.assertFalse(t.has('last-revision'))
 
254
        # TODO RBC 20060210 do a commit, check the inventory.basis is created 
 
255
        # correctly and last-revision file becomes present.
 
256
        # manually make a dirstate toc check the format is as desired.
 
257
        state = dirstate.DirState.on_file(t.local_abspath('dirstate'))
 
258
        self.assertEqual([], state.get_parent_ids())
 
259
 
 
260
    def test_uses_lockdir(self):
 
261
        """WorkingTreeFormat4 uses its own LockDir:
 
262
            
 
263
            - lock is a directory
 
264
            - when the WorkingTree is locked, LockDir can see that
 
265
        """
 
266
        # this test could be factored into a subclass of tests common to both
 
267
        # format 3 and 4, but for now its not much of an issue as there is only one in common.
 
268
        t = self.get_transport()
 
269
        tree = self.make_workingtree()
 
270
        self.assertIsDirectory('.bzr', t)
 
271
        self.assertIsDirectory('.bzr/checkout', t)
 
272
        self.assertIsDirectory('.bzr/checkout/lock', t)
 
273
        our_lock = LockDir(t, '.bzr/checkout/lock')
 
274
        self.assertEquals(our_lock.peek(), None)
 
275
        tree.lock_write()
 
276
        self.assertTrue(our_lock.peek())
 
277
        tree.unlock()
 
278
        self.assertEquals(our_lock.peek(), None)
 
279
 
 
280
    def make_workingtree(self):
 
281
        url = self.get_url()
 
282
        dir = bzrdir.BzrDirMetaFormat1().initialize(url)
 
283
        repo = dir.create_repository()
 
284
        branch = dir.create_branch()
 
285
        try:
 
286
            return workingtree.WorkingTreeFormat4().initialize(dir)
 
287
        except errors.NotLocalUrl:
 
288
            raise TestSkipped('Not a local URL')
 
289
 
 
290
    # TODO: test that dirstate also stores & retrieves the parent list of 
 
291
    # workingtree-parent revisions, including when they have multiple parents.
 
292
    # (in other words, the case when we're constructing a merge of 
 
293
    # revisions which are themselves merges.)
 
294
 
 
295
    # The simplest case is that the the workingtree's primary 
 
296
    # parent tree can be retrieved.  This is required for all WorkingTrees, 
 
297
    # and covered by the generic tests.
 
298
 
 
299
    def test_dirstate_stores_all_parent_inventories(self):
 
300
        tree = self.make_workingtree()
 
301
 
 
302
        # We're going to build in tree a working tree 
 
303
        # with three parent trees, with some files in common.  
 
304
    
 
305
        # We really don't want to do commit or merge in the new dirstate-based
 
306
        # tree, because that might not work yet.  So instead we build
 
307
        # revisions elsewhere and pull them across, doing by hand part of the
 
308
        # work that merge would do.
 
309
 
 
310
        subtree = self.make_branch_and_tree('subdir')
 
311
        self.build_tree(['subdir/file-a',])
 
312
        subtree.add(['file-a'], ['id-a'])
 
313
        rev1 = subtree.commit('commit in subdir')
 
314
        rev1_tree = subtree.basis_tree()
 
315
 
 
316
        subtree2 = subtree.bzrdir.sprout('subdir2').open_workingtree()
 
317
        self.build_tree(['subdir2/file-b'])
 
318
        subtree2.add(['file-b'], ['id-b'])
 
319
        rev2 = subtree2.commit('commit in subdir2')
 
320
        rev2_tree = subtree2.basis_tree()
 
321
 
 
322
        subtree.merge_from_branch(subtree2.branch)
 
323
        rev3 = subtree.commit('merge from subdir2')
 
324
        rev3_tree = subtree.basis_tree()
 
325
 
 
326
        repo = tree.branch.repository
 
327
        repo.fetch(subtree.branch.repository, rev3)
 
328
        # will also pull the others...
 
329
 
 
330
        # tree doesn't contain a text merge yet but we'll just
 
331
        # set the parents as if a merge had taken place. 
 
332
        # this should cause the tree data to be folded into the 
 
333
        # dirstate.
 
334
        ## import pdb;pdb.set_trace()
 
335
        tree.set_parent_trees([
 
336
            (rev1, rev1_tree),
 
337
            (rev2, rev2_tree),
 
338
            (rev3, rev3_tree), ])
 
339
 
 
340
        # now we should be able to get them back out
 
341
        self.assertTreesEqual(tree.revision_tree(rev1), rev1_tree)
 
342
        self.assertTreesEqual(tree.revision_tree(rev2), rev2_tree)
 
343
        self.assertTreesEqual(tree.revision_tree(rev3), rev3_tree)
 
344
 
 
345
    def test_dirstate_doesnt_read_parents_from_repo_when_setting(self):
 
346
        """Setting parent trees on a dirstate working tree takes
 
347
        the trees it's given and doesn't need to read them from the 
 
348
        repository.
 
349
        """
 
350
        tree = self.make_workingtree()
 
351
 
 
352
        subtree = self.make_branch_and_tree('subdir')
 
353
        rev1 = subtree.commit('commit in subdir')
 
354
        rev1_tree = subtree.basis_tree()
 
355
 
 
356
        tree.branch.pull(subtree.branch)
 
357
 
 
358
        # break the repository's legs to make sure it only uses the trees
 
359
        # it's given; any calls to forbidden methods will raise an 
 
360
        # AssertionError
 
361
        repo = tree.branch.repository
 
362
        repo.get_revision = self.fail
 
363
        repo.get_inventory = self.fail
 
364
        repo.get_inventory_xml = self.fail
 
365
        # try to set the parent trees.
 
366
        tree.set_parent_trees([(rev1, rev1_tree)])
 
367
 
 
368
    def test_dirstate_doesnt_read_from_repo_when_returning_cache_tree(self):
 
369
        """Getting parent trees from a dirstate tree does not read from the 
 
370
        repos inventory store. This is an important part of the dirstate
 
371
        performance optimisation work.
 
372
        """
 
373
        tree = self.make_workingtree()
 
374
 
 
375
        subtree = self.make_branch_and_tree('subdir')
 
376
        rev1 = subtree.commit('commit in subdir')
 
377
        rev1_tree = subtree.basis_tree()
 
378
        rev2 = subtree.commit('second commit in subdir', allow_pointless=True)
 
379
        rev2_tree = subtree.basis_tree()
 
380
 
 
381
        tree.branch.pull(subtree.branch)
 
382
 
 
383
        # break the repository's legs to make sure it only uses the trees
 
384
        # it's given; any calls to forbidden methods will raise an 
 
385
        # AssertionError
 
386
        repo = tree.branch.repository
 
387
        repo.get_revision = self.fail
 
388
        repo.get_inventory = self.fail
 
389
        repo.get_inventory_xml = self.fail
 
390
        # set the parent trees.
 
391
        tree.set_parent_trees([(rev1, rev1_tree), (rev2, rev2_tree)])
 
392
        # read the first tree
 
393
        result_rev1_tree = tree.revision_tree(rev1)
 
394
        # read the second
 
395
        result_rev2_tree = tree.revision_tree(rev2)
 
396
        # compare - there should be no differences between the handed and 
 
397
        # returned trees
 
398
        self.assertTreesEqual(rev1_tree, result_rev1_tree)
 
399
        self.assertTreesEqual(rev2_tree, result_rev2_tree)
 
400
 
 
401
    def test_dirstate_doesnt_cache_non_parent_trees(self):
 
402
        """Getting parent trees from a dirstate tree does not read from the 
 
403
        repos inventory store. This is an important part of the dirstate
 
404
        performance optimisation work.
 
405
        """
 
406
        tree = self.make_workingtree()
 
407
 
 
408
        # make a tree that we can try for, which is able to be returned but
 
409
        # must not be
 
410
        subtree = self.make_branch_and_tree('subdir')
 
411
        rev1 = subtree.commit('commit in subdir')
 
412
        tree.branch.pull(subtree.branch)
 
413
        # check it fails
 
414
        self.assertRaises(errors.NoSuchRevision, tree.revision_tree, rev1)
 
415
 
 
416
    def test_no_dirstate_outside_lock(self):
 
417
        # temporary test until the code is mature enough to test from outside.
 
418
        """Getting a dirstate object fails if there is no lock."""
 
419
        def lock_and_call_current_dirstate(tree, lock_method):
 
420
            getattr(tree, lock_method)()
 
421
            tree.current_dirstate()
 
422
            tree.unlock()
 
423
        tree = self.make_workingtree()
 
424
        self.assertRaises(errors.NotWriteLocked, tree.current_dirstate)
 
425
        lock_and_call_current_dirstate(tree, 'lock_read')
 
426
        self.assertRaises(errors.NotWriteLocked, tree.current_dirstate)
 
427
        lock_and_call_current_dirstate(tree, 'lock_write')
 
428
        self.assertRaises(errors.NotWriteLocked, tree.current_dirstate)
 
429
        lock_and_call_current_dirstate(tree, 'lock_tree_write')
 
430
        self.assertRaises(errors.NotWriteLocked, tree.current_dirstate)
 
431
 
 
432
    def test_new_dirstate_on_new_lock(self):
 
433
        # until we have detection for when a dirstate can be reused, we
 
434
        # want to reparse dirstate on every new lock.
 
435
        known_dirstates = set()
 
436
        def lock_and_compare_all_current_dirstate(tree, lock_method):
 
437
            getattr(tree, lock_method)()
 
438
            state = tree.current_dirstate()
 
439
            self.assertFalse(state in known_dirstates)
 
440
            known_dirstates.add(state)
 
441
            tree.unlock()
 
442
        tree = self.make_workingtree()
 
443
        # lock twice with each type to prevent silly per-lock-type bugs.
 
444
        # each lock and compare looks for a unique state object.
 
445
        lock_and_compare_all_current_dirstate(tree, 'lock_read')
 
446
        lock_and_compare_all_current_dirstate(tree, 'lock_read')
 
447
        lock_and_compare_all_current_dirstate(tree, 'lock_tree_write')
 
448
        lock_and_compare_all_current_dirstate(tree, 'lock_tree_write')
 
449
        lock_and_compare_all_current_dirstate(tree, 'lock_write')
 
450
        lock_and_compare_all_current_dirstate(tree, 'lock_write')
 
451
 
 
452
 
229
453
class TestFormat2WorkingTree(TestCaseWithTransport):
230
454
    """Tests that are specific to format 2 trees."""
231
455