~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Ian Clatworthy
  • Date: 2007-08-13 14:16:53 UTC
  • mto: (2733.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2734.
  • Revision ID: ian.clatworthy@internode.on.net-20070813141653-3cbrp00xowq58zv1
Added mini tutorial

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
18
18
 
 
19
import bisect
19
20
import os
20
 
import tempfile
 
21
import time
21
22
 
22
23
from bzrlib import (
23
 
    bzrdir,
24
24
    dirstate,
25
25
    errors,
26
 
    inventory,
27
 
    memorytree,
28
26
    osutils,
29
 
    revision as _mod_revision,
30
 
    revisiontree,
31
 
    tests,
32
 
    workingtree_4,
33
27
    )
34
 
from bzrlib.transport import memory
35
 
from bzrlib.tests import test_osutils
36
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
28
from bzrlib.memorytree import MemoryTree
 
29
from bzrlib.osutils import has_symlinks
 
30
from bzrlib.tests import (
 
31
        TestCase,
 
32
        TestCaseWithTransport,
 
33
        TestSkipped,
 
34
        )
37
35
 
38
36
 
39
37
# TODO:
49
47
# set_path_id  setting id when state is in memory modified
50
48
 
51
49
 
52
 
load_tests = load_tests_apply_scenarios
53
 
 
54
 
 
55
 
class TestCaseWithDirState(tests.TestCaseWithTransport):
 
50
class TestCaseWithDirState(TestCaseWithTransport):
56
51
    """Helper functions for creating DirState objects with various content."""
57
52
 
58
 
    scenarios = test_osutils.dir_reader_scenarios()
59
 
 
60
 
    # Set by load_tests
61
 
    _dir_reader_class = None
62
 
    _native_to_unicode = None # Not used yet
63
 
 
64
 
    def setUp(self):
65
 
        tests.TestCaseWithTransport.setUp(self)
66
 
 
67
 
        self.overrideAttr(osutils,
68
 
                          '_selected_dir_reader', self._dir_reader_class())
69
 
 
70
53
    def create_empty_dirstate(self):
71
54
        """Return a locked but empty dirstate"""
72
55
        state = dirstate.DirState.initialize('dirstate')
180
163
        """
181
164
        # The state should already be write locked, since we just had to do
182
165
        # some operation to get here.
183
 
        self.assertTrue(state._lock_token is not None)
 
166
        assert state._lock_token is not None
184
167
        try:
185
168
            self.assertEqual(expected_result[0],  state.get_parent_ids())
186
169
            # there should be no ghosts in this tree.
377
360
        # There are no files on disk and no parents
378
361
        tree = self.make_branch_and_tree('tree')
379
362
        expected_result = ([], [
380
 
            (('', '', tree.get_root_id()), # common details
 
363
            (('', '', tree.path2id('')), # common details
381
364
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
382
365
             ])])
383
366
        state = dirstate.DirState.from_tree(tree, 'dirstate')
390
373
        rev_id = tree.commit('first post').encode('utf8')
391
374
        root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
392
375
        expected_result = ([rev_id], [
393
 
            (('', '', tree.get_root_id()), # common details
 
376
            (('', '', tree.path2id('')), # common details
394
377
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
395
378
              ('d', '', 0, False, rev_id), # first parent details
396
379
             ])])
410
393
        rev_id2 = tree2.commit('second post', allow_pointless=True)
411
394
        tree.merge_from_branch(tree2.branch)
412
395
        expected_result = ([rev_id, rev_id2], [
413
 
            (('', '', tree.get_root_id()), # common details
 
396
            (('', '', tree.path2id('')), # common details
414
397
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
415
398
              ('d', '', 0, False, rev_id), # first parent details
416
 
              ('d', '', 0, False, rev_id), # second parent details
 
399
              ('d', '', 0, False, rev_id2), # second parent details
417
400
             ])])
418
401
        state = dirstate.DirState.from_tree(tree, 'dirstate')
419
402
        self.check_state_with_reopen(expected_result, state)
429
412
        tree = self.make_branch_and_tree('tree')
430
413
        self.build_tree(['tree/unknown'])
431
414
        expected_result = ([], [
432
 
            (('', '', tree.get_root_id()), # common details
 
415
            (('', '', tree.path2id('')), # common details
433
416
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
434
417
             ])])
435
418
        state = dirstate.DirState.from_tree(tree, 'dirstate')
438
421
    def get_tree_with_a_file(self):
439
422
        tree = self.make_branch_and_tree('tree')
440
423
        self.build_tree(['tree/a file'])
441
 
        tree.add('a file', 'a-file-id')
 
424
        tree.add('a file', 'a file id')
442
425
        return tree
443
426
 
444
427
    def test_non_empty_no_parents_to_dirstate(self):
446
429
        # There are files on disk and no parents
447
430
        tree = self.get_tree_with_a_file()
448
431
        expected_result = ([], [
449
 
            (('', '', tree.get_root_id()), # common details
 
432
            (('', '', tree.path2id('')), # common details
450
433
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
451
434
             ]),
452
 
            (('', 'a file', 'a-file-id'), # common
 
435
            (('', 'a file', 'a file id'), # common
453
436
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
454
437
             ]),
455
438
            ])
464
447
        # and length:
465
448
        self.build_tree_contents([('tree/a file', 'new content\n')])
466
449
        expected_result = ([rev_id], [
467
 
            (('', '', tree.get_root_id()), # common details
 
450
            (('', '', tree.path2id('')), # common details
468
451
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
469
452
              ('d', '', 0, False, rev_id), # first parent details
470
453
             ]),
471
 
            (('', 'a file', 'a-file-id'), # common
 
454
            (('', 'a file', 'a file id'), # common
472
455
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
473
456
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
474
457
               rev_id), # first parent
491
474
        # and length again, giving us three distinct values:
492
475
        self.build_tree_contents([('tree/a file', 'new content\n')])
493
476
        expected_result = ([rev_id, rev_id2], [
494
 
            (('', '', tree.get_root_id()), # common details
 
477
            (('', '', tree.path2id('')), # common details
495
478
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
496
479
              ('d', '', 0, False, rev_id), # first parent details
497
 
              ('d', '', 0, False, rev_id), # second parent details
 
480
              ('d', '', 0, False, rev_id2), # second parent details
498
481
             ]),
499
 
            (('', 'a file', 'a-file-id'), # common
 
482
            (('', 'a file', 'a file id'), # common
500
483
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
501
484
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
502
485
               rev_id), # first parent
532
515
 
533
516
class TestDirStateOnFile(TestCaseWithDirState):
534
517
 
535
 
    def create_updated_dirstate(self):
536
 
        self.build_tree(['a-file'])
537
 
        tree = self.make_branch_and_tree('.')
538
 
        tree.add(['a-file'], ['a-id'])
539
 
        tree.commit('add a-file')
540
 
        # Save and unlock the state, re-open it in readonly mode
541
 
        state = dirstate.DirState.from_tree(tree, 'dirstate')
542
 
        state.save()
543
 
        state.unlock()
544
 
        state = dirstate.DirState.on_file('dirstate')
545
 
        state.lock_read()
546
 
        return state
547
 
 
548
518
    def test_construct_with_path(self):
549
519
        tree = self.make_branch_and_tree('tree')
550
520
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
556
526
        # get a state object
557
527
        # no parents, default tree content
558
528
        expected_result = ([], [
559
 
            (('', '', tree.get_root_id()), # common details
 
529
            (('', '', tree.path2id('')), # common details
560
530
             # current tree details, but new from_tree skips statting, it
561
531
             # uses set_state_from_inventory, and thus depends on the
562
532
             # inventory state.
579
549
            state.unlock()
580
550
 
581
551
    def test_can_save_in_read_lock(self):
582
 
        state = self.create_updated_dirstate()
 
552
        self.build_tree(['a-file'])
 
553
        state = dirstate.DirState.initialize('dirstate')
 
554
        try:
 
555
            # No stat and no sha1 sum.
 
556
            state.add('a-file', 'a-file-id', 'file', None, '')
 
557
            state.save()
 
558
        finally:
 
559
            state.unlock()
 
560
 
 
561
        # Now open in readonly mode
 
562
        state = dirstate.DirState.on_file('dirstate')
 
563
        state.lock_read()
583
564
        try:
584
565
            entry = state._get_entry(0, path_utf8='a-file')
585
 
            # The current size should be 0 (default)
586
 
            self.assertEqual(0, entry[1][0][2])
 
566
            # The current sha1 sum should be empty
 
567
            self.assertEqual('', entry[1][0][1])
587
568
            # We should have a real entry.
588
569
            self.assertNotEqual((None, None), entry)
589
 
            # Set the cutoff-time into the future, so things look cacheable
 
570
            # Make sure everything is old enough
590
571
            state._sha_cutoff_time()
591
 
            state._cutoff_time += 10.0
592
 
            st = os.lstat('a-file')
593
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
594
 
            # We updated the current sha1sum because the file is cacheable
 
572
            state._cutoff_time += 10
 
573
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
574
            # We should have gotten a real sha1
595
575
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
596
576
                             sha1sum)
597
577
 
598
578
            # The dirblock has been updated
599
 
            self.assertEqual(st.st_size, entry[1][0][2])
600
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
579
            self.assertEqual(sha1sum, entry[1][0][1])
 
580
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
601
581
                             state._dirblock_state)
602
582
 
603
583
            del entry
612
592
        state.lock_read()
613
593
        try:
614
594
            entry = state._get_entry(0, path_utf8='a-file')
615
 
            self.assertEqual(st.st_size, entry[1][0][2])
 
595
            self.assertEqual(sha1sum, entry[1][0][1])
616
596
        finally:
617
597
            state.unlock()
618
598
 
619
599
    def test_save_fails_quietly_if_locked(self):
620
600
        """If dirstate is locked, save will fail without complaining."""
621
 
        state = self.create_updated_dirstate()
 
601
        self.build_tree(['a-file'])
 
602
        state = dirstate.DirState.initialize('dirstate')
 
603
        try:
 
604
            # No stat and no sha1 sum.
 
605
            state.add('a-file', 'a-file-id', 'file', None, '')
 
606
            state.save()
 
607
        finally:
 
608
            state.unlock()
 
609
 
 
610
        state = dirstate.DirState.on_file('dirstate')
 
611
        state.lock_read()
622
612
        try:
623
613
            entry = state._get_entry(0, path_utf8='a-file')
624
 
            # No cached sha1 yet.
625
 
            self.assertEqual('', entry[1][0][1])
626
 
            # Set the cutoff-time into the future, so things look cacheable
627
 
            state._sha_cutoff_time()
628
 
            state._cutoff_time += 10.0
629
 
            st = os.lstat('a-file')
630
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
614
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
615
            # We should have gotten a real sha1
631
616
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
632
617
                             sha1sum)
633
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
618
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
634
619
                             state._dirblock_state)
635
620
 
636
621
            # Now, before we try to save, grab another dirstate, and take out a
652
637
                state2.unlock()
653
638
        finally:
654
639
            state.unlock()
655
 
 
 
640
        
656
641
        # The file on disk should not be modified.
657
642
        state = dirstate.DirState.on_file('dirstate')
658
643
        state.lock_read()
662
647
        finally:
663
648
            state.unlock()
664
649
 
665
 
    def test_save_refuses_if_changes_aborted(self):
666
 
        self.build_tree(['a-file', 'a-dir/'])
667
 
        state = dirstate.DirState.initialize('dirstate')
668
 
        try:
669
 
            # No stat and no sha1 sum.
670
 
            state.add('a-file', 'a-file-id', 'file', None, '')
671
 
            state.save()
672
 
        finally:
673
 
            state.unlock()
674
 
 
675
 
        # The dirstate should include TREE_ROOT and 'a-file' and nothing else
676
 
        expected_blocks = [
677
 
            ('', [(('', '', 'TREE_ROOT'),
678
 
                   [('d', '', 0, False, dirstate.DirState.NULLSTAT)])]),
679
 
            ('', [(('', 'a-file', 'a-file-id'),
680
 
                   [('f', '', 0, False, dirstate.DirState.NULLSTAT)])]),
681
 
        ]
682
 
 
683
 
        state = dirstate.DirState.on_file('dirstate')
684
 
        state.lock_write()
685
 
        try:
686
 
            state._read_dirblocks_if_needed()
687
 
            self.assertEqual(expected_blocks, state._dirblocks)
688
 
 
689
 
            # Now modify the state, but mark it as inconsistent
690
 
            state.add('a-dir', 'a-dir-id', 'directory', None, '')
691
 
            state._changes_aborted = True
692
 
            state.save()
693
 
        finally:
694
 
            state.unlock()
695
 
 
696
 
        state = dirstate.DirState.on_file('dirstate')
697
 
        state.lock_read()
698
 
        try:
699
 
            state._read_dirblocks_if_needed()
700
 
            self.assertEqual(expected_blocks, state._dirblocks)
701
 
        finally:
702
 
            state.unlock()
703
 
 
704
650
 
705
651
class TestDirStateInitialize(TestCaseWithDirState):
706
652
 
726
672
 
727
673
class TestDirStateManipulations(TestCaseWithDirState):
728
674
 
729
 
    def make_minimal_tree(self):
730
 
        tree1 = self.make_branch_and_memory_tree('tree1')
731
 
        tree1.lock_write()
732
 
        self.addCleanup(tree1.unlock)
733
 
        tree1.add('')
734
 
        revid1 = tree1.commit('foo')
735
 
        return tree1, revid1
736
 
 
737
 
    def test_update_minimal_updates_id_index(self):
738
 
        state = self.create_dirstate_with_root_and_subdir()
739
 
        self.addCleanup(state.unlock)
740
 
        id_index = state._get_id_index()
741
 
        self.assertEqual(['a-root-value', 'subdir-id'], sorted(id_index))
742
 
        state.add('file-name', 'file-id', 'file', None, '')
743
 
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
744
 
                         sorted(id_index))
745
 
        state.update_minimal(('', 'new-name', 'file-id'), 'f',
746
 
                             path_utf8='new-name')
747
 
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
748
 
                         sorted(id_index))
749
 
        self.assertEqual([('', 'new-name', 'file-id')],
750
 
                         sorted(id_index['file-id']))
751
 
        state._validate()
752
 
 
753
675
    def test_set_state_from_inventory_no_content_no_parents(self):
754
676
        # setting the current inventory is a slow but important api to support.
755
 
        tree1, revid1 = self.make_minimal_tree()
756
 
        inv = tree1.inventory
757
 
        root_id = inv.path2id('')
 
677
        tree1 = self.make_branch_and_memory_tree('tree1')
 
678
        tree1.lock_write()
 
679
        try:
 
680
            tree1.add('')
 
681
            revid1 = tree1.commit('foo').encode('utf8')
 
682
            root_id = tree1.inventory.root.file_id
 
683
            inv = tree1.inventory
 
684
        finally:
 
685
            tree1.unlock()
758
686
        expected_result = [], [
759
687
            (('', '', root_id), [
760
688
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
772
700
            # This will unlock it
773
701
            self.check_state_with_reopen(expected_result, state)
774
702
 
775
 
    def test_set_state_from_scratch_no_parents(self):
776
 
        tree1, revid1 = self.make_minimal_tree()
777
 
        inv = tree1.inventory
778
 
        root_id = inv.path2id('')
779
 
        expected_result = [], [
780
 
            (('', '', root_id), [
781
 
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
782
 
        state = dirstate.DirState.initialize('dirstate')
783
 
        try:
784
 
            state.set_state_from_scratch(inv, [], [])
785
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
786
 
                             state._header_state)
787
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
788
 
                             state._dirblock_state)
789
 
        except:
790
 
            state.unlock()
791
 
            raise
792
 
        else:
793
 
            # This will unlock it
794
 
            self.check_state_with_reopen(expected_result, state)
795
 
 
796
 
    def test_set_state_from_scratch_identical_parent(self):
797
 
        tree1, revid1 = self.make_minimal_tree()
798
 
        inv = tree1.inventory
799
 
        root_id = inv.path2id('')
800
 
        rev_tree1 = tree1.branch.repository.revision_tree(revid1)
801
 
        d_entry = ('d', '', 0, False, dirstate.DirState.NULLSTAT)
802
 
        parent_entry = ('d', '', 0, False, revid1)
803
 
        expected_result = [revid1], [
804
 
            (('', '', root_id), [d_entry, parent_entry])]
805
 
        state = dirstate.DirState.initialize('dirstate')
806
 
        try:
807
 
            state.set_state_from_scratch(inv, [(revid1, rev_tree1)], [])
808
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
809
 
                             state._header_state)
810
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
811
 
                             state._dirblock_state)
812
 
        except:
813
 
            state.unlock()
814
 
            raise
815
 
        else:
816
 
            # This will unlock it
817
 
            self.check_state_with_reopen(expected_result, state)
818
 
 
819
 
    def test_set_state_from_inventory_preserves_hashcache(self):
820
 
        # https://bugs.launchpad.net/bzr/+bug/146176
821
 
        # set_state_from_inventory should preserve the stat and hash value for
822
 
        # workingtree files that are not changed by the inventory.
823
 
 
824
 
        tree = self.make_branch_and_tree('.')
825
 
        # depends on the default format using dirstate...
826
 
        tree.lock_write()
827
 
        try:
828
 
            # make a dirstate with some valid hashcache data
829
 
            # file on disk, but that's not needed for this test
830
 
            foo_contents = 'contents of foo'
831
 
            self.build_tree_contents([('foo', foo_contents)])
832
 
            tree.add('foo', 'foo-id')
833
 
 
834
 
            foo_stat = os.stat('foo')
835
 
            foo_packed = dirstate.pack_stat(foo_stat)
836
 
            foo_sha = osutils.sha_string(foo_contents)
837
 
            foo_size = len(foo_contents)
838
 
 
839
 
            # should not be cached yet, because the file's too fresh
840
 
            self.assertEqual(
841
 
                (('', 'foo', 'foo-id',),
842
 
                 [('f', '', 0, False, dirstate.DirState.NULLSTAT)]),
843
 
                tree._dirstate._get_entry(0, 'foo-id'))
844
 
            # poke in some hashcache information - it wouldn't normally be
845
 
            # stored because it's too fresh
846
 
            tree._dirstate.update_minimal(
847
 
                ('', 'foo', 'foo-id'),
848
 
                'f', False, foo_sha, foo_packed, foo_size, 'foo')
849
 
            # now should be cached
850
 
            self.assertEqual(
851
 
                (('', 'foo', 'foo-id',),
852
 
                 [('f', foo_sha, foo_size, False, foo_packed)]),
853
 
                tree._dirstate._get_entry(0, 'foo-id'))
854
 
 
855
 
            # extract the inventory, and add something to it
856
 
            inv = tree._get_inventory()
857
 
            # should see the file we poked in...
858
 
            self.assertTrue(inv.has_id('foo-id'))
859
 
            self.assertTrue(inv.has_filename('foo'))
860
 
            inv.add_path('bar', 'file', 'bar-id')
861
 
            tree._dirstate._validate()
862
 
            # this used to cause it to lose its hashcache
863
 
            tree._dirstate.set_state_from_inventory(inv)
864
 
            tree._dirstate._validate()
865
 
        finally:
866
 
            tree.unlock()
867
 
 
868
 
        tree.lock_read()
869
 
        try:
870
 
            # now check that the state still has the original hashcache value
871
 
            state = tree._dirstate
872
 
            state._validate()
873
 
            foo_tuple = state._get_entry(0, path_utf8='foo')
874
 
            self.assertEqual(
875
 
                (('', 'foo', 'foo-id',),
876
 
                 [('f', foo_sha, len(foo_contents), False,
877
 
                   dirstate.pack_stat(foo_stat))]),
878
 
                foo_tuple)
879
 
        finally:
880
 
            tree.unlock()
881
 
 
882
703
    def test_set_state_from_inventory_mixed_paths(self):
883
704
        tree1 = self.make_branch_and_tree('tree1')
884
705
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
925
746
        state = dirstate.DirState.initialize('dirstate')
926
747
        try:
927
748
            # check precondition to be sure the state does change appropriately.
928
 
            root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
929
 
            self.assertEqual([root_entry], list(state._iter_entries()))
930
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
931
 
            self.assertEqual(root_entry,
932
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
933
 
            self.assertEqual((None, None),
934
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
935
 
            state.set_path_id('', 'second-root-id')
936
 
            new_root_entry = (('', '', 'second-root-id'),
937
 
                              [('d', '', 0, False, 'x'*32)])
938
 
            expected_rows = [new_root_entry]
 
749
            self.assertEqual(
 
750
                [(('', '', 'TREE_ROOT'), [('d', '', 0, False,
 
751
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
 
752
                list(state._iter_entries()))
 
753
            state.set_path_id('', 'foobarbaz')
 
754
            expected_rows = [
 
755
                (('', '', 'foobarbaz'), [('d', '', 0, False,
 
756
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
939
757
            self.assertEqual(expected_rows, list(state._iter_entries()))
940
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
941
 
            self.assertEqual(new_root_entry, 
942
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
943
 
            self.assertEqual((None, None),
944
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
945
758
            # should work across save too
946
759
            state.save()
947
760
        finally:
965
778
        state._validate()
966
779
        try:
967
780
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
968
 
            root_entry = (('', '', 'TREE_ROOT'),
969
 
                          [('d', '', 0, False, 'x'*32),
970
 
                           ('d', '', 0, False, 'parent-revid')])
971
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
972
 
            self.assertEqual(root_entry,
973
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
974
 
            self.assertEqual((None, None),
975
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
976
 
            state.set_path_id('', 'Asecond-root-id')
 
781
            state.set_path_id('', 'foobarbaz')
977
782
            state._validate()
978
783
            # now see that it is what we expected
979
 
            old_root_entry = (('', '', 'TREE_ROOT'),
980
 
                              [('a', '', 0, False, ''),
981
 
                               ('d', '', 0, False, 'parent-revid')])
982
 
            new_root_entry = (('', '', 'Asecond-root-id'),
983
 
                              [('d', '', 0, False, ''),
984
 
                               ('a', '', 0, False, '')])
985
 
            expected_rows = [new_root_entry, old_root_entry]
 
784
            expected_rows = [
 
785
                (('', '', 'TREE_ROOT'),
 
786
                    [('a', '', 0, False, ''),
 
787
                     ('d', '', 0, False, 'parent-revid'),
 
788
                     ]),
 
789
                (('', '', 'foobarbaz'),
 
790
                    [('d', '', 0, False, ''),
 
791
                     ('a', '', 0, False, ''),
 
792
                     ]),
 
793
                ]
986
794
            state._validate()
987
795
            self.assertEqual(expected_rows, list(state._iter_entries()))
988
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
989
 
            self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
990
 
            self.assertEqual((None, None),
991
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
992
 
            self.assertEqual(old_root_entry,
993
 
                             state._get_entry(1, fileid_utf8='TREE_ROOT'))
994
 
            self.assertEqual(new_root_entry,
995
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
996
 
            self.assertEqual((None, None),
997
 
                             state._get_entry(1, fileid_utf8='Asecond-root-id'))
998
796
            # should work across save too
999
797
            state.save()
1000
798
        finally:
1016
814
        finally:
1017
815
            state.unlock()
1018
816
 
 
817
 
1019
818
    def test_set_parent_trees_no_content(self):
1020
819
        # set_parent_trees is a slow but important api to support.
1021
820
        tree1 = self.make_branch_and_memory_tree('tree1')
1026
825
        finally:
1027
826
            tree1.unlock()
1028
827
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1029
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
828
        tree2 = MemoryTree.create_on_branch(branch2)
1030
829
        tree2.lock_write()
1031
830
        try:
1032
831
            revid2 = tree2.commit('foo')
1033
 
            root_id = tree2.get_root_id()
 
832
            root_id = tree2.inventory.root.file_id
1034
833
        finally:
1035
834
            tree2.unlock()
1036
835
        state = dirstate.DirState.initialize('dirstate')
1064
863
            state.set_parent_trees(
1065
864
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
1066
865
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
1067
 
                 ('ghost-rev', tree2.branch.repository.revision_tree(
1068
 
                                   _mod_revision.NULL_REVISION))),
 
866
                 ('ghost-rev', tree2.branch.repository.revision_tree(None))),
1069
867
                ['ghost-rev'])
1070
868
            self.assertEqual([revid1, revid2, 'ghost-rev'],
1071
869
                             state.get_parent_ids())
1075
873
                [(('', '', root_id), [
1076
874
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1077
875
                  ('d', '', 0, False, revid1),
1078
 
                  ('d', '', 0, False, revid1)
 
876
                  ('d', '', 0, False, revid2)
1079
877
                  ])],
1080
878
                list(state._iter_entries()))
1081
879
        finally:
1096
894
        finally:
1097
895
            tree1.unlock()
1098
896
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1099
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
897
        tree2 = MemoryTree.create_on_branch(branch2)
1100
898
        tree2.lock_write()
1101
899
        try:
1102
900
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1103
901
            revid2 = tree2.commit('foo')
1104
 
            root_id = tree2.get_root_id()
 
902
            root_id = tree2.inventory.root.file_id
1105
903
        finally:
1106
904
            tree2.unlock()
1107
905
        # check the layout in memory
1109
907
            (('', '', root_id), [
1110
908
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1111
909
             ('d', '', 0, False, revid1.encode('utf8')),
1112
 
             ('d', '', 0, False, revid1.encode('utf8'))
 
910
             ('d', '', 0, False, revid2.encode('utf8'))
1113
911
             ]),
1114
912
            (('', 'a file', 'file-id'), [
1115
913
             ('a', '', 0, False, ''),
1147
945
            (('', '', 'TREE_ROOT'), [
1148
946
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1149
947
             ]),
1150
 
            (('', 'a file', 'a-file-id'), [
 
948
            (('', 'a file', 'a file id'), [
1151
949
             ('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
1152
950
             ]),
1153
951
            ]
1154
952
        try:
1155
 
            state.add('a file', 'a-file-id', 'file', stat, '1'*20)
 
953
            state.add('a file', 'a file id', 'file', stat, '1'*20)
1156
954
            # having added it, it should be in the output of iter_entries.
1157
955
            self.assertEqual(expected_entries, list(state._iter_entries()))
1158
956
            # saving and reloading should not affect this.
1161
959
            state.unlock()
1162
960
        state = dirstate.DirState.on_file('dirstate')
1163
961
        state.lock_read()
1164
 
        self.addCleanup(state.unlock)
1165
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
962
        try:
 
963
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
964
        finally:
 
965
            state.unlock()
1166
966
 
1167
967
    def test_add_path_to_unversioned_directory(self):
1168
968
        """Adding a path to an unversioned directory should error.
1173
973
        """
1174
974
        self.build_tree(['unversioned/', 'unversioned/a file'])
1175
975
        state = dirstate.DirState.initialize('dirstate')
1176
 
        self.addCleanup(state.unlock)
1177
 
        self.assertRaises(errors.NotVersionedError, state.add,
1178
 
                          'unversioned/a file', 'a-file-id', 'file', None, None)
 
976
        try:
 
977
            self.assertRaises(errors.NotVersionedError, state.add,
 
978
                'unversioned/a file', 'a file id', 'file', None, None)
 
979
        finally:
 
980
            state.unlock()
1179
981
 
1180
982
    def test_add_directory_to_root_no_parents_all_data(self):
1181
983
        # The most trivial addition of a dir is when there are no parents and
1201
1003
            state.unlock()
1202
1004
        state = dirstate.DirState.on_file('dirstate')
1203
1005
        state.lock_read()
1204
 
        self.addCleanup(state.unlock)
1205
1006
        state._validate()
1206
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1007
        try:
 
1008
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1009
        finally:
 
1010
            state.unlock()
1207
1011
 
1208
 
    def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
 
1012
    def test_add_symlink_to_root_no_parents_all_data(self):
1209
1013
        # The most trivial addition of a symlink when there are no parents and
1210
1014
        # its in the root and all data about the file is supplied
1211
1015
        # bzr doesn't support fake symlinks on windows, yet.
1212
 
        self.requireFeature(tests.SymlinkFeature)
1213
 
        os.symlink(target, link_name)
1214
 
        stat = os.lstat(link_name)
 
1016
        if not has_symlinks():
 
1017
            raise TestSkipped("No symlink support")
 
1018
        os.symlink('target', 'a link')
 
1019
        stat = os.lstat('a link')
1215
1020
        expected_entries = [
1216
1021
            (('', '', 'TREE_ROOT'), [
1217
1022
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1218
1023
             ]),
1219
 
            (('', link_name.encode('UTF-8'), 'a link id'), [
1220
 
             ('l', target.encode('UTF-8'), stat[6],
1221
 
              False, dirstate.pack_stat(stat)), # current tree
 
1024
            (('', 'a link', 'a link id'), [
 
1025
             ('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1222
1026
             ]),
1223
1027
            ]
1224
1028
        state = dirstate.DirState.initialize('dirstate')
1225
1029
        try:
1226
 
            state.add(link_name, 'a link id', 'symlink', stat,
1227
 
                      target.encode('UTF-8'))
 
1030
            state.add('a link', 'a link id', 'symlink', stat, 'target')
1228
1031
            # having added it, it should be in the output of iter_entries.
1229
1032
            self.assertEqual(expected_entries, list(state._iter_entries()))
1230
1033
            # saving and reloading should not affect this.
1233
1036
            state.unlock()
1234
1037
        state = dirstate.DirState.on_file('dirstate')
1235
1038
        state.lock_read()
1236
 
        self.addCleanup(state.unlock)
1237
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
1238
 
 
1239
 
    def test_add_symlink_to_root_no_parents_all_data(self):
1240
 
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1241
 
 
1242
 
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1243
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1244
 
        self._test_add_symlink_to_root_no_parents_all_data(
1245
 
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
 
1039
        try:
 
1040
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1041
        finally:
 
1042
            state.unlock()
1246
1043
 
1247
1044
    def test_add_directory_and_child_no_parents_all_data(self):
1248
1045
        # after adding a directory, we should be able to add children to it.
1256
1053
            (('', 'a dir', 'a dir id'), [
1257
1054
             ('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
1258
1055
             ]),
1259
 
            (('a dir', 'a file', 'a-file-id'), [
 
1056
            (('a dir', 'a file', 'a file id'), [
1260
1057
             ('f', '1'*20, 25, False,
1261
1058
              dirstate.pack_stat(filestat)), # current tree details
1262
1059
             ]),
1264
1061
        state = dirstate.DirState.initialize('dirstate')
1265
1062
        try:
1266
1063
            state.add('a dir', 'a dir id', 'directory', dirstat, None)
1267
 
            state.add('a dir/a file', 'a-file-id', 'file', filestat, '1'*20)
 
1064
            state.add('a dir/a file', 'a file id', 'file', filestat, '1'*20)
1268
1065
            # added it, it should be in the output of iter_entries.
1269
1066
            self.assertEqual(expected_entries, list(state._iter_entries()))
1270
1067
            # saving and reloading should not affect this.
1273
1070
            state.unlock()
1274
1071
        state = dirstate.DirState.on_file('dirstate')
1275
1072
        state.lock_read()
1276
 
        self.addCleanup(state.unlock)
1277
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1073
        try:
 
1074
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1075
        finally:
 
1076
            state.unlock()
1278
1077
 
1279
1078
    def test_add_tree_reference(self):
1280
1079
        # make a dirstate and add a tree reference
1294
1093
            state.unlock()
1295
1094
        # now check we can read it back
1296
1095
        state.lock_read()
1297
 
        self.addCleanup(state.unlock)
1298
1096
        state._validate()
1299
 
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1300
 
        self.assertEqual(entry, entry2)
1301
 
        self.assertEqual(entry, expected_entry)
1302
 
        # and lookup by id should work too
1303
 
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1304
 
        self.assertEqual(entry, expected_entry)
 
1097
        try:
 
1098
            entry2 = state._get_entry(0, 'subdir-id', 'subdir')
 
1099
            self.assertEqual(entry, entry2)
 
1100
            self.assertEqual(entry, expected_entry)
 
1101
            # and lookup by id should work too
 
1102
            entry2 = state._get_entry(0, fileid_utf8='subdir-id')
 
1103
            self.assertEqual(entry, expected_entry)
 
1104
        finally:
 
1105
            state.unlock()
1305
1106
 
1306
1107
    def test_add_forbidden_names(self):
1307
1108
        state = dirstate.DirState.initialize('dirstate')
1311
1112
        self.assertRaises(errors.BzrError,
1312
1113
            state.add, '..', 'ass-id', 'directory', None, None)
1313
1114
 
1314
 
    def test_set_state_with_rename_b_a_bug_395556(self):
1315
 
        # bug 395556 uncovered a bug where the dirstate ends up with a false
1316
 
        # relocation record - in a tree with no parents there should be no
1317
 
        # absent or relocated records. This then leads to further corruption
1318
 
        # when a commit occurs, as the incorrect relocation gathers an
1319
 
        # incorrect absent in tree 1, and future changes go to pot.
1320
 
        tree1 = self.make_branch_and_tree('tree1')
1321
 
        self.build_tree(['tree1/b'])
1322
 
        tree1.lock_write()
1323
 
        try:
1324
 
            tree1.add(['b'], ['b-id'])
1325
 
            root_id = tree1.get_root_id()
1326
 
            inv = tree1.inventory
1327
 
            state = dirstate.DirState.initialize('dirstate')
1328
 
            try:
1329
 
                # Set the initial state with 'b'
1330
 
                state.set_state_from_inventory(inv)
1331
 
                inv.rename('b-id', root_id, 'a')
1332
 
                # Set the new state with 'a', which currently corrupts.
1333
 
                state.set_state_from_inventory(inv)
1334
 
                expected_result1 = [('', '', root_id, 'd'),
1335
 
                                    ('', 'a', 'b-id', 'f'),
1336
 
                                   ]
1337
 
                values = []
1338
 
                for entry in state._iter_entries():
1339
 
                    values.append(entry[0] + entry[1][0][:1])
1340
 
                self.assertEqual(expected_result1, values)
1341
 
            finally:
1342
 
                state.unlock()
1343
 
        finally:
1344
 
            tree1.unlock()
1345
 
 
1346
 
 
1347
 
class TestDirStateHashUpdates(TestCaseWithDirState):
1348
 
 
1349
 
    def do_update_entry(self, state, path):
1350
 
        entry = state._get_entry(0, path_utf8=path)
1351
 
        stat = os.lstat(path)
1352
 
        return dirstate.update_entry(state, entry, os.path.abspath(path), stat)
1353
 
 
1354
 
    def _read_state_content(self, state):
1355
 
        """Read the content of the dirstate file.
1356
 
 
1357
 
        On Windows when one process locks a file, you can't even open() the
1358
 
        file in another process (to read it). So we go directly to
1359
 
        state._state_file. This should always be the exact disk representation,
1360
 
        so it is reasonable to do so.
1361
 
        DirState also always seeks before reading, so it doesn't matter if we
1362
 
        bump the file pointer.
1363
 
        """
1364
 
        state._state_file.seek(0)
1365
 
        return state._state_file.read()
1366
 
 
1367
 
    def test_worth_saving_limit_avoids_writing(self):
1368
 
        tree = self.make_branch_and_tree('.')
1369
 
        self.build_tree(['c', 'd'])
1370
 
        tree.lock_write()
1371
 
        tree.add(['c', 'd'], ['c-id', 'd-id'])
1372
 
        tree.commit('add c and d')
1373
 
        state = InstrumentedDirState.on_file(tree.current_dirstate()._filename,
1374
 
                                             worth_saving_limit=2)
1375
 
        tree.unlock()
1376
 
        state.lock_write()
1377
 
        self.addCleanup(state.unlock)
1378
 
        state._read_dirblocks_if_needed()
1379
 
        state.adjust_time(+20) # Allow things to be cached
1380
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1381
 
                         state._dirblock_state)
1382
 
        content = self._read_state_content(state)
1383
 
        self.do_update_entry(state, 'c')
1384
 
        self.assertEqual(1, len(state._known_hash_changes))
1385
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
1386
 
                         state._dirblock_state)
1387
 
        state.save()
1388
 
        # It should not have set the state to IN_MEMORY_UNMODIFIED because the
1389
 
        # hash values haven't been written out.
1390
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
1391
 
                         state._dirblock_state)
1392
 
        self.assertEqual(content, self._read_state_content(state))
1393
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
1394
 
                         state._dirblock_state)
1395
 
        self.do_update_entry(state, 'd')
1396
 
        self.assertEqual(2, len(state._known_hash_changes))
1397
 
        state.save()
1398
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1399
 
                         state._dirblock_state)
1400
 
        self.assertEqual(0, len(state._known_hash_changes))
1401
 
 
1402
1115
 
1403
1116
class TestGetLines(TestCaseWithDirState):
1404
1117
 
1617
1330
            state.unlock()
1618
1331
 
1619
1332
 
1620
 
class TestIterChildEntries(TestCaseWithDirState):
1621
 
 
1622
 
    def create_dirstate_with_two_trees(self):
1623
 
        """This dirstate contains multiple files and directories.
1624
 
 
1625
 
         /        a-root-value
1626
 
         a/       a-dir
1627
 
         b/       b-dir
1628
 
         c        c-file
1629
 
         d        d-file
1630
 
         a/e/     e-dir
1631
 
         a/f      f-file
1632
 
         b/g      g-file
1633
 
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
1634
 
 
1635
 
        Notice that a/e is an empty directory.
1636
 
 
1637
 
        There is one parent tree, which has the same shape with the following variations:
1638
 
        b/g in the parent is gone.
1639
 
        b/h in the parent has a different id
1640
 
        b/i is new in the parent
1641
 
        c is renamed to b/j in the parent
1642
 
 
1643
 
        :return: The dirstate, still write-locked.
1644
 
        """
1645
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1646
 
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
1647
 
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
1648
 
        root_entry = ('', '', 'a-root-value'), [
1649
 
            ('d', '', 0, False, packed_stat),
1650
 
            ('d', '', 0, False, 'parent-revid'),
1651
 
            ]
1652
 
        a_entry = ('', 'a', 'a-dir'), [
1653
 
            ('d', '', 0, False, packed_stat),
1654
 
            ('d', '', 0, False, 'parent-revid'),
1655
 
            ]
1656
 
        b_entry = ('', 'b', 'b-dir'), [
1657
 
            ('d', '', 0, False, packed_stat),
1658
 
            ('d', '', 0, False, 'parent-revid'),
1659
 
            ]
1660
 
        c_entry = ('', 'c', 'c-file'), [
1661
 
            ('f', null_sha, 10, False, packed_stat),
1662
 
            ('r', 'b/j', 0, False, ''),
1663
 
            ]
1664
 
        d_entry = ('', 'd', 'd-file'), [
1665
 
            ('f', null_sha, 20, False, packed_stat),
1666
 
            ('f', 'd', 20, False, 'parent-revid'),
1667
 
            ]
1668
 
        e_entry = ('a', 'e', 'e-dir'), [
1669
 
            ('d', '', 0, False, packed_stat),
1670
 
            ('d', '', 0, False, 'parent-revid'),
1671
 
            ]
1672
 
        f_entry = ('a', 'f', 'f-file'), [
1673
 
            ('f', null_sha, 30, False, packed_stat),
1674
 
            ('f', 'f', 20, False, 'parent-revid'),
1675
 
            ]
1676
 
        g_entry = ('b', 'g', 'g-file'), [
1677
 
            ('f', null_sha, 30, False, packed_stat),
1678
 
            NULL_PARENT_DETAILS,
1679
 
            ]
1680
 
        h_entry1 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file1'), [
1681
 
            ('f', null_sha, 40, False, packed_stat),
1682
 
            NULL_PARENT_DETAILS,
1683
 
            ]
1684
 
        h_entry2 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file2'), [
1685
 
            NULL_PARENT_DETAILS,
1686
 
            ('f', 'h', 20, False, 'parent-revid'),
1687
 
            ]
1688
 
        i_entry = ('b', 'i', 'i-file'), [
1689
 
            NULL_PARENT_DETAILS,
1690
 
            ('f', 'h', 20, False, 'parent-revid'),
1691
 
            ]
1692
 
        j_entry = ('b', 'j', 'c-file'), [
1693
 
            ('r', 'c', 0, False, ''),
1694
 
            ('f', 'j', 20, False, 'parent-revid'),
1695
 
            ]
1696
 
        dirblocks = []
1697
 
        dirblocks.append(('', [root_entry]))
1698
 
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
1699
 
        dirblocks.append(('a', [e_entry, f_entry]))
1700
 
        dirblocks.append(('b', [g_entry, h_entry1, h_entry2, i_entry, j_entry]))
1701
 
        state = dirstate.DirState.initialize('dirstate')
1702
 
        state._validate()
1703
 
        try:
1704
 
            state._set_data(['parent'], dirblocks)
1705
 
        except:
1706
 
            state.unlock()
1707
 
            raise
1708
 
        return state, dirblocks
1709
 
 
1710
 
    def test_iter_children_b(self):
1711
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1712
 
        self.addCleanup(state.unlock)
1713
 
        expected_result = []
1714
 
        expected_result.append(dirblocks[3][1][2]) # h2
1715
 
        expected_result.append(dirblocks[3][1][3]) # i
1716
 
        expected_result.append(dirblocks[3][1][4]) # j
1717
 
        self.assertEqual(expected_result,
1718
 
            list(state._iter_child_entries(1, 'b')))
1719
 
 
1720
 
    def test_iter_child_root(self):
1721
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1722
 
        self.addCleanup(state.unlock)
1723
 
        expected_result = []
1724
 
        expected_result.append(dirblocks[1][1][0]) # a
1725
 
        expected_result.append(dirblocks[1][1][1]) # b
1726
 
        expected_result.append(dirblocks[1][1][3]) # d
1727
 
        expected_result.append(dirblocks[2][1][0]) # e
1728
 
        expected_result.append(dirblocks[2][1][1]) # f
1729
 
        expected_result.append(dirblocks[3][1][2]) # h2
1730
 
        expected_result.append(dirblocks[3][1][3]) # i
1731
 
        expected_result.append(dirblocks[3][1][4]) # j
1732
 
        self.assertEqual(expected_result,
1733
 
            list(state._iter_child_entries(1, '')))
1734
 
 
1735
 
 
1736
 
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1333
class TestDirstateSortOrder(TestCaseWithTransport):
1737
1334
    """Test that DirState adds entries in the right order."""
1738
1335
 
1739
1336
    def test_add_sorting(self):
1788
1385
 
1789
1386
        # *really* cheesy way to just get an empty tree
1790
1387
        repo = self.make_repository('repo')
1791
 
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1388
        empty_tree = repo.revision_tree(None)
1792
1389
        state.set_parent_trees([('null:', empty_tree)], [])
1793
1390
 
1794
1391
        dirblock_names = [d[0] for d in state._dirblocks]
1798
1395
class InstrumentedDirState(dirstate.DirState):
1799
1396
    """An DirState with instrumented sha1 functionality."""
1800
1397
 
1801
 
    def __init__(self, path, sha1_provider, worth_saving_limit=0):
1802
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider,
1803
 
            worth_saving_limit=worth_saving_limit)
 
1398
    def __init__(self, path):
 
1399
        super(InstrumentedDirState, self).__init__(path)
1804
1400
        self._time_offset = 0
1805
1401
        self._log = []
1806
 
        # member is dynamically set in DirState.__init__ to turn on trace
1807
 
        self._sha1_provider = sha1_provider
1808
 
        self._sha1_file = self._sha1_file_and_log
1809
1402
 
1810
1403
    def _sha_cutoff_time(self):
1811
1404
        timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
1812
1405
        self._cutoff_time = timestamp + self._time_offset
1813
1406
 
1814
 
    def _sha1_file_and_log(self, abspath):
 
1407
    def _sha1_file(self, abspath, entry):
1815
1408
        self._log.append(('sha1', abspath))
1816
 
        return self._sha1_provider.sha1(abspath)
 
1409
        return super(InstrumentedDirState, self)._sha1_file(abspath, entry)
1817
1410
 
1818
1411
    def _read_link(self, abspath, old_link):
1819
1412
        self._log.append(('read_link', abspath, old_link))
1850
1443
        self.st_ino = ino
1851
1444
        self.st_mode = mode
1852
1445
 
1853
 
    @staticmethod
1854
 
    def from_stat(st):
1855
 
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1856
 
            st.st_ino, st.st_mode)
1857
 
 
1858
 
 
1859
 
class TestPackStat(tests.TestCaseWithTransport):
 
1446
 
 
1447
class TestUpdateEntry(TestCaseWithDirState):
 
1448
    """Test the DirState.update_entry functions"""
 
1449
 
 
1450
    def get_state_with_a(self):
 
1451
        """Create a DirState tracking a single object named 'a'"""
 
1452
        state = InstrumentedDirState.initialize('dirstate')
 
1453
        self.addCleanup(state.unlock)
 
1454
        state.add('a', 'a-id', 'file', None, '')
 
1455
        entry = state._get_entry(0, path_utf8='a')
 
1456
        return state, entry
 
1457
 
 
1458
    def test_update_entry(self):
 
1459
        state, entry = self.get_state_with_a()
 
1460
        self.build_tree(['a'])
 
1461
        # Add one where we don't provide the stat or sha already
 
1462
        self.assertEqual(('', 'a', 'a-id'), entry[0])
 
1463
        self.assertEqual([('f', '', 0, False, dirstate.DirState.NULLSTAT)],
 
1464
                         entry[1])
 
1465
        # Flush the buffers to disk
 
1466
        state.save()
 
1467
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1468
                         state._dirblock_state)
 
1469
 
 
1470
        stat_value = os.lstat('a')
 
1471
        packed_stat = dirstate.pack_stat(stat_value)
 
1472
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1473
                                          stat_value=stat_value)
 
1474
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1475
                         link_or_sha1)
 
1476
 
 
1477
        # The dirblock entry should not cache the file's sha1
 
1478
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
 
1479
                         entry[1])
 
1480
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1481
                         state._dirblock_state)
 
1482
        mode = stat_value.st_mode
 
1483
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False)], state._log)
 
1484
 
 
1485
        state.save()
 
1486
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1487
                         state._dirblock_state)
 
1488
 
 
1489
        # If we do it again right away, we don't know if the file has changed
 
1490
        # so we will re-read the file. Roll the clock back so the file is
 
1491
        # guaranteed to look too new.
 
1492
        state.adjust_time(-10)
 
1493
 
 
1494
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1495
                                          stat_value=stat_value)
 
1496
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1497
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1498
                         ], state._log)
 
1499
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1500
                         link_or_sha1)
 
1501
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1502
                         state._dirblock_state)
 
1503
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
 
1504
                         entry[1])
 
1505
        state.save()
 
1506
 
 
1507
        # However, if we move the clock forward so the file is considered
 
1508
        # "stable", it should just cache the value.
 
1509
        state.adjust_time(+20)
 
1510
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1511
                                          stat_value=stat_value)
 
1512
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1513
                         link_or_sha1)
 
1514
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1515
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1516
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1517
                         ], state._log)
 
1518
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1519
                         entry[1])
 
1520
 
 
1521
        # Subsequent calls will just return the cached value
 
1522
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1523
                                          stat_value=stat_value)
 
1524
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1525
                         link_or_sha1)
 
1526
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1527
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1528
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1529
                         ], state._log)
 
1530
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1531
                         entry[1])
 
1532
 
 
1533
    def test_update_entry_symlink(self):
 
1534
        """Update entry should read symlinks."""
 
1535
        if not osutils.has_symlinks():
 
1536
            # PlatformDeficiency / TestSkipped
 
1537
            raise TestSkipped("No symlink support")
 
1538
        state, entry = self.get_state_with_a()
 
1539
        state.save()
 
1540
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1541
                         state._dirblock_state)
 
1542
        os.symlink('target', 'a')
 
1543
 
 
1544
        state.adjust_time(-10) # Make the symlink look new
 
1545
        stat_value = os.lstat('a')
 
1546
        packed_stat = dirstate.pack_stat(stat_value)
 
1547
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1548
                                          stat_value=stat_value)
 
1549
        self.assertEqual('target', link_or_sha1)
 
1550
        self.assertEqual([('read_link', 'a', '')], state._log)
 
1551
        # Dirblock is not updated (the link is too new)
 
1552
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
 
1553
                         entry[1])
 
1554
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1555
                         state._dirblock_state)
 
1556
 
 
1557
        # Because the stat_value looks new, we should re-read the target
 
1558
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1559
                                          stat_value=stat_value)
 
1560
        self.assertEqual('target', link_or_sha1)
 
1561
        self.assertEqual([('read_link', 'a', ''),
 
1562
                          ('read_link', 'a', ''),
 
1563
                         ], state._log)
 
1564
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
 
1565
                         entry[1])
 
1566
        state.adjust_time(+20) # Skip into the future, all files look old
 
1567
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1568
                                          stat_value=stat_value)
 
1569
        self.assertEqual('target', link_or_sha1)
 
1570
        # We need to re-read the link because only now can we cache it
 
1571
        self.assertEqual([('read_link', 'a', ''),
 
1572
                          ('read_link', 'a', ''),
 
1573
                          ('read_link', 'a', ''),
 
1574
                         ], state._log)
 
1575
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
 
1576
                         entry[1])
 
1577
 
 
1578
        # Another call won't re-read the link
 
1579
        self.assertEqual([('read_link', 'a', ''),
 
1580
                          ('read_link', 'a', ''),
 
1581
                          ('read_link', 'a', ''),
 
1582
                         ], state._log)
 
1583
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1584
                                          stat_value=stat_value)
 
1585
        self.assertEqual('target', link_or_sha1)
 
1586
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
 
1587
                         entry[1])
 
1588
 
 
1589
    def do_update_entry(self, state, entry, abspath):
 
1590
        stat_value = os.lstat(abspath)
 
1591
        return state.update_entry(entry, abspath, stat_value)
 
1592
 
 
1593
    def test_update_entry_dir(self):
 
1594
        state, entry = self.get_state_with_a()
 
1595
        self.build_tree(['a/'])
 
1596
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1597
 
 
1598
    def test_update_entry_dir_unchanged(self):
 
1599
        state, entry = self.get_state_with_a()
 
1600
        self.build_tree(['a/'])
 
1601
        state.adjust_time(+20)
 
1602
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1603
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1604
                         state._dirblock_state)
 
1605
        state.save()
 
1606
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1607
                         state._dirblock_state)
 
1608
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1609
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1610
                         state._dirblock_state)
 
1611
 
 
1612
    def test_update_entry_file_unchanged(self):
 
1613
        state, entry = self.get_state_with_a()
 
1614
        self.build_tree(['a'])
 
1615
        sha1sum = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1616
        state.adjust_time(+20)
 
1617
        self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
 
1618
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1619
                         state._dirblock_state)
 
1620
        state.save()
 
1621
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1622
                         state._dirblock_state)
 
1623
        self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
 
1624
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1625
                         state._dirblock_state)
 
1626
 
 
1627
    def create_and_test_file(self, state, entry):
 
1628
        """Create a file at 'a' and verify the state finds it.
 
1629
 
 
1630
        The state should already be versioning *something* at 'a'. This makes
 
1631
        sure that state.update_entry recognizes it as a file.
 
1632
        """
 
1633
        self.build_tree(['a'])
 
1634
        stat_value = os.lstat('a')
 
1635
        packed_stat = dirstate.pack_stat(stat_value)
 
1636
 
 
1637
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
 
1638
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1639
                         link_or_sha1)
 
1640
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1641
                         entry[1])
 
1642
        return packed_stat
 
1643
 
 
1644
    def create_and_test_dir(self, state, entry):
 
1645
        """Create a directory at 'a' and verify the state finds it.
 
1646
 
 
1647
        The state should already be versioning *something* at 'a'. This makes
 
1648
        sure that state.update_entry recognizes it as a directory.
 
1649
        """
 
1650
        self.build_tree(['a/'])
 
1651
        stat_value = os.lstat('a')
 
1652
        packed_stat = dirstate.pack_stat(stat_value)
 
1653
 
 
1654
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
 
1655
        self.assertIs(None, link_or_sha1)
 
1656
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
 
1657
 
 
1658
        return packed_stat
 
1659
 
 
1660
    def create_and_test_symlink(self, state, entry):
 
1661
        """Create a symlink at 'a' and verify the state finds it.
 
1662
 
 
1663
        The state should already be versioning *something* at 'a'. This makes
 
1664
        sure that state.update_entry recognizes it as a symlink.
 
1665
 
 
1666
        This should not be called if this platform does not have symlink
 
1667
        support.
 
1668
        """
 
1669
        # caller should care about skipping test on platforms without symlinks
 
1670
        os.symlink('path/to/foo', 'a')
 
1671
 
 
1672
        stat_value = os.lstat('a')
 
1673
        packed_stat = dirstate.pack_stat(stat_value)
 
1674
 
 
1675
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
 
1676
        self.assertEqual('path/to/foo', link_or_sha1)
 
1677
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
 
1678
                         entry[1])
 
1679
        return packed_stat
 
1680
 
 
1681
    def test_update_file_to_dir(self):
 
1682
        """If a file changes to a directory we return None for the sha.
 
1683
        We also update the inventory record.
 
1684
        """
 
1685
        state, entry = self.get_state_with_a()
 
1686
        # The file sha1 won't be cached unless the file is old
 
1687
        state.adjust_time(+10)
 
1688
        self.create_and_test_file(state, entry)
 
1689
        os.remove('a')
 
1690
        self.create_and_test_dir(state, entry)
 
1691
 
 
1692
    def test_update_file_to_symlink(self):
 
1693
        """File becomes a symlink"""
 
1694
        if not osutils.has_symlinks():
 
1695
            # PlatformDeficiency / TestSkipped
 
1696
            raise TestSkipped("No symlink support")
 
1697
        state, entry = self.get_state_with_a()
 
1698
        # The file sha1 won't be cached unless the file is old
 
1699
        state.adjust_time(+10)
 
1700
        self.create_and_test_file(state, entry)
 
1701
        os.remove('a')
 
1702
        self.create_and_test_symlink(state, entry)
 
1703
 
 
1704
    def test_update_dir_to_file(self):
 
1705
        """Directory becoming a file updates the entry."""
 
1706
        state, entry = self.get_state_with_a()
 
1707
        # The file sha1 won't be cached unless the file is old
 
1708
        state.adjust_time(+10)
 
1709
        self.create_and_test_dir(state, entry)
 
1710
        os.rmdir('a')
 
1711
        self.create_and_test_file(state, entry)
 
1712
 
 
1713
    def test_update_dir_to_symlink(self):
 
1714
        """Directory becomes a symlink"""
 
1715
        if not osutils.has_symlinks():
 
1716
            # PlatformDeficiency / TestSkipped
 
1717
            raise TestSkipped("No symlink support")
 
1718
        state, entry = self.get_state_with_a()
 
1719
        # The symlink target won't be cached if it isn't old
 
1720
        state.adjust_time(+10)
 
1721
        self.create_and_test_dir(state, entry)
 
1722
        os.rmdir('a')
 
1723
        self.create_and_test_symlink(state, entry)
 
1724
 
 
1725
    def test_update_symlink_to_file(self):
 
1726
        """Symlink becomes a file"""
 
1727
        if not has_symlinks():
 
1728
            raise TestSkipped("No symlink support")
 
1729
        state, entry = self.get_state_with_a()
 
1730
        # The symlink and file info won't be cached unless old
 
1731
        state.adjust_time(+10)
 
1732
        self.create_and_test_symlink(state, entry)
 
1733
        os.remove('a')
 
1734
        self.create_and_test_file(state, entry)
 
1735
 
 
1736
    def test_update_symlink_to_dir(self):
 
1737
        """Symlink becomes a directory"""
 
1738
        if not has_symlinks():
 
1739
            raise TestSkipped("No symlink support")
 
1740
        state, entry = self.get_state_with_a()
 
1741
        # The symlink target won't be cached if it isn't old
 
1742
        state.adjust_time(+10)
 
1743
        self.create_and_test_symlink(state, entry)
 
1744
        os.remove('a')
 
1745
        self.create_and_test_dir(state, entry)
 
1746
 
 
1747
    def test__is_executable_win32(self):
 
1748
        state, entry = self.get_state_with_a()
 
1749
        self.build_tree(['a'])
 
1750
 
 
1751
        # Make sure we are using the win32 implementation of _is_executable
 
1752
        state._is_executable = state._is_executable_win32
 
1753
 
 
1754
        # The file on disk is not executable, but we are marking it as though
 
1755
        # it is. With _is_executable_win32 we ignore what is on disk.
 
1756
        entry[1][0] = ('f', '', 0, True, dirstate.DirState.NULLSTAT)
 
1757
 
 
1758
        stat_value = os.lstat('a')
 
1759
        packed_stat = dirstate.pack_stat(stat_value)
 
1760
 
 
1761
        state.adjust_time(-10) # Make sure everything is new
 
1762
        state.update_entry(entry, abspath='a', stat_value=stat_value)
 
1763
 
 
1764
        # The row is updated, but the executable bit stays set.
 
1765
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
 
1766
                         entry[1])
 
1767
 
 
1768
        # Make the disk object look old enough to cache
 
1769
        state.adjust_time(+20)
 
1770
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1771
        state.update_entry(entry, abspath='a', stat_value=stat_value)
 
1772
        self.assertEqual([('f', digest, 14, True, packed_stat)], entry[1])
 
1773
 
 
1774
 
 
1775
class TestPackStat(TestCaseWithTransport):
1860
1776
 
1861
1777
    def assertPackStat(self, expected, stat_value):
1862
1778
        """Check the packed and serialized form of a stat value."""
1927
1843
        # the end it would still be fairly arbitrary, and we don't want the
1928
1844
        # extra overhead if we can avoid it. So sort everything to make sure
1929
1845
        # equality is true
1930
 
        self.assertEqual(len(map_keys), len(paths))
 
1846
        assert len(map_keys) == len(paths)
1931
1847
        expected = {}
1932
1848
        for path, keys in zip(paths, map_keys):
1933
1849
            if keys is None:
1952
1868
        :param paths: A list of directories
1953
1869
        """
1954
1870
        result = state._bisect_dirblocks(paths)
1955
 
        self.assertEqual(len(map_keys), len(paths))
 
1871
        assert len(map_keys) == len(paths)
 
1872
 
1956
1873
        expected = {}
1957
1874
        for path, keys in zip(paths, map_keys):
1958
1875
            if keys is None:
2203
2120
        self.assertContainsRe(str(e),
2204
2121
            'file a-id is absent in row')
2205
2122
 
2206
 
 
2207
 
class TestDirstateTreeReference(TestCaseWithDirState):
2208
 
 
2209
 
    def test_reference_revision_is_none(self):
2210
 
        tree = self.make_branch_and_tree('tree', format='dirstate-with-subtree')
2211
 
        subtree = self.make_branch_and_tree('tree/subtree',
2212
 
                            format='dirstate-with-subtree')
2213
 
        subtree.set_root_id('subtree')
2214
 
        tree.add_reference(subtree)
2215
 
        tree.add('subtree')
2216
 
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2217
 
        key = ('', 'subtree', 'subtree')
2218
 
        expected = ('', [(key,
2219
 
            [('t', '', 0, False, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])])
2220
 
 
2221
 
        try:
2222
 
            self.assertEqual(expected, state._find_block(key))
2223
 
        finally:
2224
 
            state.unlock()
2225
 
 
2226
 
 
2227
 
class TestDiscardMergeParents(TestCaseWithDirState):
2228
 
 
2229
 
    def test_discard_no_parents(self):
2230
 
        # This should be a no-op
2231
 
        state = self.create_empty_dirstate()
2232
 
        self.addCleanup(state.unlock)
2233
 
        state._discard_merge_parents()
2234
 
        state._validate()
2235
 
 
2236
 
    def test_discard_one_parent(self):
2237
 
        # No-op
2238
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2239
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2240
 
            ('d', '', 0, False, packed_stat),
2241
 
            ('d', '', 0, False, packed_stat),
2242
 
            ]
2243
 
        dirblocks = []
2244
 
        dirblocks.append(('', [root_entry_direntry]))
2245
 
        dirblocks.append(('', []))
2246
 
 
2247
 
        state = self.create_empty_dirstate()
2248
 
        self.addCleanup(state.unlock)
2249
 
        state._set_data(['parent-id'], dirblocks[:])
2250
 
        state._validate()
2251
 
 
2252
 
        state._discard_merge_parents()
2253
 
        state._validate()
2254
 
        self.assertEqual(dirblocks, state._dirblocks)
2255
 
 
2256
 
    def test_discard_simple(self):
2257
 
        # No-op
2258
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2259
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2260
 
            ('d', '', 0, False, packed_stat),
2261
 
            ('d', '', 0, False, packed_stat),
2262
 
            ('d', '', 0, False, packed_stat),
2263
 
            ]
2264
 
        expected_root_entry_direntry = ('', '', 'a-root-value'), [
2265
 
            ('d', '', 0, False, packed_stat),
2266
 
            ('d', '', 0, False, packed_stat),
2267
 
            ]
2268
 
        dirblocks = []
2269
 
        dirblocks.append(('', [root_entry_direntry]))
2270
 
        dirblocks.append(('', []))
2271
 
 
2272
 
        state = self.create_empty_dirstate()
2273
 
        self.addCleanup(state.unlock)
2274
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2275
 
        state._validate()
2276
 
 
2277
 
        # This should strip of the extra column
2278
 
        state._discard_merge_parents()
2279
 
        state._validate()
2280
 
        expected_dirblocks = [('', [expected_root_entry_direntry]), ('', [])]
2281
 
        self.assertEqual(expected_dirblocks, state._dirblocks)
2282
 
 
2283
 
    def test_discard_absent(self):
2284
 
        """If entries are only in a merge, discard should remove the entries"""
2285
 
        null_stat = dirstate.DirState.NULLSTAT
2286
 
        present_dir = ('d', '', 0, False, null_stat)
2287
 
        present_file = ('f', '', 0, False, null_stat)
2288
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2289
 
        root_key = ('', '', 'a-root-value')
2290
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2291
 
        file_in_merged_key = ('', 'file-in-merged', 'b-file-id')
2292
 
        dirblocks = [('', [(root_key, [present_dir, present_dir, present_dir])]),
2293
 
                     ('', [(file_in_merged_key,
2294
 
                            [absent, absent, present_file]),
2295
 
                           (file_in_root_key,
2296
 
                            [present_file, present_file, present_file]),
2297
 
                          ]),
2298
 
                    ]
2299
 
 
2300
 
        state = self.create_empty_dirstate()
2301
 
        self.addCleanup(state.unlock)
2302
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2303
 
        state._validate()
2304
 
 
2305
 
        exp_dirblocks = [('', [(root_key, [present_dir, present_dir])]),
2306
 
                         ('', [(file_in_root_key,
2307
 
                                [present_file, present_file]),
2308
 
                              ]),
2309
 
                        ]
2310
 
        state._discard_merge_parents()
2311
 
        state._validate()
2312
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2313
 
 
2314
 
    def test_discard_renamed(self):
2315
 
        null_stat = dirstate.DirState.NULLSTAT
2316
 
        present_dir = ('d', '', 0, False, null_stat)
2317
 
        present_file = ('f', '', 0, False, null_stat)
2318
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2319
 
        root_key = ('', '', 'a-root-value')
2320
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2321
 
        # Renamed relative to parent
2322
 
        file_rename_s_key = ('', 'file-s', 'b-file-id')
2323
 
        file_rename_t_key = ('', 'file-t', 'b-file-id')
2324
 
        # And one that is renamed between the parents, but absent in this
2325
 
        key_in_1 = ('', 'file-in-1', 'c-file-id')
2326
 
        key_in_2 = ('', 'file-in-2', 'c-file-id')
2327
 
 
2328
 
        dirblocks = [
2329
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2330
 
            ('', [(key_in_1,
2331
 
                   [absent, present_file, ('r', 'file-in-2', 'c-file-id')]),
2332
 
                  (key_in_2,
2333
 
                   [absent, ('r', 'file-in-1', 'c-file-id'), present_file]),
2334
 
                  (file_in_root_key,
2335
 
                   [present_file, present_file, present_file]),
2336
 
                  (file_rename_s_key,
2337
 
                   [('r', 'file-t', 'b-file-id'), absent, present_file]),
2338
 
                  (file_rename_t_key,
2339
 
                   [present_file, absent, ('r', 'file-s', 'b-file-id')]),
2340
 
                 ]),
2341
 
        ]
2342
 
        exp_dirblocks = [
2343
 
            ('', [(root_key, [present_dir, present_dir])]),
2344
 
            ('', [(key_in_1, [absent, present_file]),
2345
 
                  (file_in_root_key, [present_file, present_file]),
2346
 
                  (file_rename_t_key, [present_file, absent]),
2347
 
                 ]),
2348
 
        ]
2349
 
        state = self.create_empty_dirstate()
2350
 
        self.addCleanup(state.unlock)
2351
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2352
 
        state._validate()
2353
 
 
2354
 
        state._discard_merge_parents()
2355
 
        state._validate()
2356
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2357
 
 
2358
 
    def test_discard_all_subdir(self):
2359
 
        null_stat = dirstate.DirState.NULLSTAT
2360
 
        present_dir = ('d', '', 0, False, null_stat)
2361
 
        present_file = ('f', '', 0, False, null_stat)
2362
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2363
 
        root_key = ('', '', 'a-root-value')
2364
 
        subdir_key = ('', 'sub', 'dir-id')
2365
 
        child1_key = ('sub', 'child1', 'child1-id')
2366
 
        child2_key = ('sub', 'child2', 'child2-id')
2367
 
        child3_key = ('sub', 'child3', 'child3-id')
2368
 
 
2369
 
        dirblocks = [
2370
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2371
 
            ('', [(subdir_key, [present_dir, present_dir, present_dir])]),
2372
 
            ('sub', [(child1_key, [absent, absent, present_file]),
2373
 
                     (child2_key, [absent, absent, present_file]),
2374
 
                     (child3_key, [absent, absent, present_file]),
2375
 
                    ]),
2376
 
        ]
2377
 
        exp_dirblocks = [
2378
 
            ('', [(root_key, [present_dir, present_dir])]),
2379
 
            ('', [(subdir_key, [present_dir, present_dir])]),
2380
 
            ('sub', []),
2381
 
        ]
2382
 
        state = self.create_empty_dirstate()
2383
 
        self.addCleanup(state.unlock)
2384
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2385
 
        state._validate()
2386
 
 
2387
 
        state._discard_merge_parents()
2388
 
        state._validate()
2389
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2390
 
 
2391
 
 
2392
 
class Test_InvEntryToDetails(tests.TestCase):
2393
 
 
2394
 
    def assertDetails(self, expected, inv_entry):
2395
 
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2396
 
        self.assertEqual(expected, details)
2397
 
        # details should always allow join() and always be a plain str when
2398
 
        # finished
2399
 
        (minikind, fingerprint, size, executable, tree_data) = details
2400
 
        self.assertIsInstance(minikind, str)
2401
 
        self.assertIsInstance(fingerprint, str)
2402
 
        self.assertIsInstance(tree_data, str)
2403
 
 
2404
 
    def test_unicode_symlink(self):
2405
 
        inv_entry = inventory.InventoryLink('link-file-id',
2406
 
                                            u'nam\N{Euro Sign}e',
2407
 
                                            'link-parent-id')
2408
 
        inv_entry.revision = 'link-revision-id'
2409
 
        target = u'link-targ\N{Euro Sign}t'
2410
 
        inv_entry.symlink_target = target
2411
 
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2412
 
                            'link-revision-id'), inv_entry)
2413
 
 
2414
 
 
2415
 
class TestSHA1Provider(tests.TestCaseInTempDir):
2416
 
 
2417
 
    def test_sha1provider_is_an_interface(self):
2418
 
        p = dirstate.SHA1Provider()
2419
 
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2420
 
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2421
 
 
2422
 
    def test_defaultsha1provider_sha1(self):
2423
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2424
 
        self.build_tree_contents([('foo', text)])
2425
 
        expected_sha = osutils.sha_string(text)
2426
 
        p = dirstate.DefaultSHA1Provider()
2427
 
        self.assertEqual(expected_sha, p.sha1('foo'))
2428
 
 
2429
 
    def test_defaultsha1provider_stat_and_sha1(self):
2430
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2431
 
        self.build_tree_contents([('foo', text)])
2432
 
        expected_sha = osutils.sha_string(text)
2433
 
        p = dirstate.DefaultSHA1Provider()
2434
 
        statvalue, sha1 = p.stat_and_sha1('foo')
2435
 
        self.assertTrue(len(statvalue) >= 10)
2436
 
        self.assertEqual(len(text), statvalue.st_size)
2437
 
        self.assertEqual(expected_sha, sha1)
2438
 
 
2439
 
 
2440
 
class _Repo(object):
2441
 
    """A minimal api to get InventoryRevisionTree to work."""
2442
 
 
2443
 
    def __init__(self):
2444
 
        default_format = bzrdir.format_registry.make_bzrdir('default')
2445
 
        self._format = default_format.repository_format
2446
 
 
2447
 
    def lock_read(self):
2448
 
        pass
2449
 
 
2450
 
    def unlock(self):
2451
 
        pass
2452
 
 
2453
 
 
2454
 
class TestUpdateBasisByDelta(tests.TestCase):
2455
 
 
2456
 
    def path_to_ie(self, path, file_id, rev_id, dir_ids):
2457
 
        if path.endswith('/'):
2458
 
            is_dir = True
2459
 
            path = path[:-1]
2460
 
        else:
2461
 
            is_dir = False
2462
 
        dirname, basename = osutils.split(path)
2463
 
        try:
2464
 
            dir_id = dir_ids[dirname]
2465
 
        except KeyError:
2466
 
            dir_id = osutils.basename(dirname) + '-id'
2467
 
        if is_dir:
2468
 
            ie = inventory.InventoryDirectory(file_id, basename, dir_id)
2469
 
            dir_ids[path] = file_id
2470
 
        else:
2471
 
            ie = inventory.InventoryFile(file_id, basename, dir_id)
2472
 
            ie.text_size = 0
2473
 
            ie.text_sha1 = ''
2474
 
        ie.revision = rev_id
2475
 
        return ie
2476
 
 
2477
 
    def create_tree_from_shape(self, rev_id, shape):
2478
 
        dir_ids = {'': 'root-id'}
2479
 
        inv = inventory.Inventory('root-id', rev_id)
2480
 
        for path, file_id in shape:
2481
 
            if path == '':
2482
 
                # Replace the root entry
2483
 
                del inv._byid[inv.root.file_id]
2484
 
                inv.root.file_id = file_id
2485
 
                inv._byid[file_id] = inv.root
2486
 
                dir_ids[''] = file_id
2487
 
                continue
2488
 
            inv.add(self.path_to_ie(path, file_id, rev_id, dir_ids))
2489
 
        return revisiontree.InventoryRevisionTree(_Repo(), inv, rev_id)
2490
 
 
2491
 
    def create_empty_dirstate(self):
2492
 
        fd, path = tempfile.mkstemp(prefix='bzr-dirstate')
2493
 
        self.addCleanup(os.remove, path)
2494
 
        os.close(fd)
2495
 
        state = dirstate.DirState.initialize(path)
2496
 
        self.addCleanup(state.unlock)
2497
 
        return state
2498
 
 
2499
 
    def create_inv_delta(self, delta, rev_id):
2500
 
        """Translate a 'delta shape' into an actual InventoryDelta"""
2501
 
        dir_ids = {'': 'root-id'}
2502
 
        inv_delta = []
2503
 
        for old_path, new_path, file_id in delta:
2504
 
            if old_path is not None and old_path.endswith('/'):
2505
 
                # Don't have to actually do anything for this, because only
2506
 
                # new_path creates InventoryEntries
2507
 
                old_path = old_path[:-1]
2508
 
            if new_path is None: # Delete
2509
 
                inv_delta.append((old_path, None, file_id, None))
2510
 
                continue
2511
 
            ie = self.path_to_ie(new_path, file_id, rev_id, dir_ids)
2512
 
            inv_delta.append((old_path, new_path, file_id, ie))
2513
 
        return inv_delta
2514
 
 
2515
 
    def assertUpdate(self, active, basis, target):
2516
 
        """Assert that update_basis_by_delta works how we want.
2517
 
 
2518
 
        Set up a DirState object with active_shape for tree 0, basis_shape for
2519
 
        tree 1. Then apply the delta from basis_shape to target_shape,
2520
 
        and assert that the DirState is still valid, and that its stored
2521
 
        content matches the target_shape.
2522
 
        """
2523
 
        active_tree = self.create_tree_from_shape('active', active)
2524
 
        basis_tree = self.create_tree_from_shape('basis', basis)
2525
 
        target_tree = self.create_tree_from_shape('target', target)
2526
 
        state = self.create_empty_dirstate()
2527
 
        state.set_state_from_scratch(active_tree.inventory,
2528
 
            [('basis', basis_tree)], [])
2529
 
        delta = target_tree.inventory._make_delta(basis_tree.inventory)
2530
 
        state.update_basis_by_delta(delta, 'target')
2531
 
        state._validate()
2532
 
        dirstate_tree = workingtree_4.DirStateRevisionTree(state,
2533
 
            'target', _Repo())
2534
 
        # The target now that delta has been applied should match the
2535
 
        # RevisionTree
2536
 
        self.assertEqual([], list(dirstate_tree.iter_changes(target_tree)))
2537
 
        # And the dirblock state should be identical to the state if we created
2538
 
        # it from scratch.
2539
 
        state2 = self.create_empty_dirstate()
2540
 
        state2.set_state_from_scratch(active_tree.inventory,
2541
 
            [('target', target_tree)], [])
2542
 
        self.assertEqual(state2._dirblocks, state._dirblocks)
2543
 
        return state
2544
 
 
2545
 
    def assertBadDelta(self, active, basis, delta):
2546
 
        """Test that we raise InconsistentDelta when appropriate.
2547
 
 
2548
 
        :param active: The active tree shape
2549
 
        :param basis: The basis tree shape
2550
 
        :param delta: A description of the delta to apply. Similar to the form
2551
 
            for regular inventory deltas, but omitting the InventoryEntry.
2552
 
            So adding a file is: (None, 'path', 'file-id')
2553
 
            Adding a directory is: (None, 'path/', 'dir-id')
2554
 
            Renaming a dir is: ('old/', 'new/', 'dir-id')
2555
 
            etc.
2556
 
        """
2557
 
        active_tree = self.create_tree_from_shape('active', active)
2558
 
        basis_tree = self.create_tree_from_shape('basis', basis)
2559
 
        inv_delta = self.create_inv_delta(delta, 'target')
2560
 
        state = self.create_empty_dirstate()
2561
 
        state.set_state_from_scratch(active_tree.inventory,
2562
 
            [('basis', basis_tree)], [])
2563
 
        self.assertRaises(errors.InconsistentDelta,
2564
 
            state.update_basis_by_delta, inv_delta, 'target')
2565
 
        ## try:
2566
 
        ##     state.update_basis_by_delta(inv_delta, 'target')
2567
 
        ## except errors.InconsistentDelta, e:
2568
 
        ##     import pdb; pdb.set_trace()
2569
 
        ## else:
2570
 
        ##     import pdb; pdb.set_trace()
2571
 
        self.assertTrue(state._changes_aborted)
2572
 
 
2573
 
    def test_remove_file_matching_active_state(self):
2574
 
        state = self.assertUpdate(
2575
 
            active=[],
2576
 
            basis =[('file', 'file-id')],
2577
 
            target=[],
2578
 
            )
2579
 
 
2580
 
    def test_remove_file_present_in_active_state(self):
2581
 
        state = self.assertUpdate(
2582
 
            active=[('file', 'file-id')],
2583
 
            basis =[('file', 'file-id')],
2584
 
            target=[],
2585
 
            )
2586
 
 
2587
 
    def test_remove_file_present_elsewhere_in_active_state(self):
2588
 
        state = self.assertUpdate(
2589
 
            active=[('other-file', 'file-id')],
2590
 
            basis =[('file', 'file-id')],
2591
 
            target=[],
2592
 
            )
2593
 
 
2594
 
    def test_remove_file_active_state_has_diff_file(self):
2595
 
        state = self.assertUpdate(
2596
 
            active=[('file', 'file-id-2')],
2597
 
            basis =[('file', 'file-id')],
2598
 
            target=[],
2599
 
            )
2600
 
 
2601
 
    def test_remove_file_active_state_has_diff_file_and_file_elsewhere(self):
2602
 
        state = self.assertUpdate(
2603
 
            active=[('file', 'file-id-2'),
2604
 
                    ('other-file', 'file-id')],
2605
 
            basis =[('file', 'file-id')],
2606
 
            target=[],
2607
 
            )
2608
 
 
2609
 
    def test_add_file_matching_active_state(self):
2610
 
        state = self.assertUpdate(
2611
 
            active=[('file', 'file-id')],
2612
 
            basis =[],
2613
 
            target=[('file', 'file-id')],
2614
 
            )
2615
 
 
2616
 
    def test_add_file_missing_in_active_state(self):
2617
 
        state = self.assertUpdate(
2618
 
            active=[],
2619
 
            basis =[],
2620
 
            target=[('file', 'file-id')],
2621
 
            )
2622
 
 
2623
 
    def test_add_file_elsewhere_in_active_state(self):
2624
 
        state = self.assertUpdate(
2625
 
            active=[('other-file', 'file-id')],
2626
 
            basis =[],
2627
 
            target=[('file', 'file-id')],
2628
 
            )
2629
 
 
2630
 
    def test_add_file_active_state_has_diff_file_and_file_elsewhere(self):
2631
 
        state = self.assertUpdate(
2632
 
            active=[('other-file', 'file-id'),
2633
 
                    ('file', 'file-id-2')],
2634
 
            basis =[],
2635
 
            target=[('file', 'file-id')],
2636
 
            )
2637
 
 
2638
 
    def test_rename_file_matching_active_state(self):
2639
 
        state = self.assertUpdate(
2640
 
            active=[('other-file', 'file-id')],
2641
 
            basis =[('file', 'file-id')],
2642
 
            target=[('other-file', 'file-id')],
2643
 
            )
2644
 
 
2645
 
    def test_rename_file_missing_in_active_state(self):
2646
 
        state = self.assertUpdate(
2647
 
            active=[],
2648
 
            basis =[('file', 'file-id')],
2649
 
            target=[('other-file', 'file-id')],
2650
 
            )
2651
 
 
2652
 
    def test_rename_file_present_elsewhere_in_active_state(self):
2653
 
        state = self.assertUpdate(
2654
 
            active=[('third', 'file-id')],
2655
 
            basis =[('file', 'file-id')],
2656
 
            target=[('other-file', 'file-id')],
2657
 
            )
2658
 
 
2659
 
    def test_rename_file_active_state_has_diff_source_file(self):
2660
 
        state = self.assertUpdate(
2661
 
            active=[('file', 'file-id-2')],
2662
 
            basis =[('file', 'file-id')],
2663
 
            target=[('other-file', 'file-id')],
2664
 
            )
2665
 
 
2666
 
    def test_rename_file_active_state_has_diff_target_file(self):
2667
 
        state = self.assertUpdate(
2668
 
            active=[('other-file', 'file-id-2')],
2669
 
            basis =[('file', 'file-id')],
2670
 
            target=[('other-file', 'file-id')],
2671
 
            )
2672
 
 
2673
 
    def test_rename_file_active_has_swapped_files(self):
2674
 
        state = self.assertUpdate(
2675
 
            active=[('file', 'file-id'),
2676
 
                    ('other-file', 'file-id-2')],
2677
 
            basis= [('file', 'file-id'),
2678
 
                    ('other-file', 'file-id-2')],
2679
 
            target=[('file', 'file-id-2'),
2680
 
                    ('other-file', 'file-id')])
2681
 
 
2682
 
    def test_rename_file_basis_has_swapped_files(self):
2683
 
        state = self.assertUpdate(
2684
 
            active=[('file', 'file-id'),
2685
 
                    ('other-file', 'file-id-2')],
2686
 
            basis= [('file', 'file-id-2'),
2687
 
                    ('other-file', 'file-id')],
2688
 
            target=[('file', 'file-id'),
2689
 
                    ('other-file', 'file-id-2')])
2690
 
 
2691
 
    def test_rename_directory_with_contents(self):
2692
 
        state = self.assertUpdate( # active matches basis
2693
 
            active=[('dir1/', 'dir-id'),
2694
 
                    ('dir1/file', 'file-id')],
2695
 
            basis= [('dir1/', 'dir-id'),
2696
 
                    ('dir1/file', 'file-id')],
2697
 
            target=[('dir2/', 'dir-id'),
2698
 
                    ('dir2/file', 'file-id')])
2699
 
        state = self.assertUpdate( # active matches target
2700
 
            active=[('dir2/', 'dir-id'),
2701
 
                    ('dir2/file', 'file-id')],
2702
 
            basis= [('dir1/', 'dir-id'),
2703
 
                    ('dir1/file', 'file-id')],
2704
 
            target=[('dir2/', 'dir-id'),
2705
 
                    ('dir2/file', 'file-id')])
2706
 
        state = self.assertUpdate( # active empty
2707
 
            active=[],
2708
 
            basis= [('dir1/', 'dir-id'),
2709
 
                    ('dir1/file', 'file-id')],
2710
 
            target=[('dir2/', 'dir-id'),
2711
 
                    ('dir2/file', 'file-id')])
2712
 
        state = self.assertUpdate( # active present at other location
2713
 
            active=[('dir3/', 'dir-id'),
2714
 
                    ('dir3/file', 'file-id')],
2715
 
            basis= [('dir1/', 'dir-id'),
2716
 
                    ('dir1/file', 'file-id')],
2717
 
            target=[('dir2/', 'dir-id'),
2718
 
                    ('dir2/file', 'file-id')])
2719
 
        state = self.assertUpdate( # active has different ids
2720
 
            active=[('dir1/', 'dir1-id'),
2721
 
                    ('dir1/file', 'file1-id'),
2722
 
                    ('dir2/', 'dir2-id'),
2723
 
                    ('dir2/file', 'file2-id')],
2724
 
            basis= [('dir1/', 'dir-id'),
2725
 
                    ('dir1/file', 'file-id')],
2726
 
            target=[('dir2/', 'dir-id'),
2727
 
                    ('dir2/file', 'file-id')])
2728
 
 
2729
 
    def test_invalid_file_not_present(self):
2730
 
        state = self.assertBadDelta(
2731
 
            active=[('file', 'file-id')],
2732
 
            basis= [('file', 'file-id')],
2733
 
            delta=[('other-file', 'file', 'file-id')])
2734
 
 
2735
 
    def test_invalid_new_id_same_path(self):
2736
 
        # The bad entry comes after
2737
 
        state = self.assertBadDelta(
2738
 
            active=[('file', 'file-id')],
2739
 
            basis= [('file', 'file-id')],
2740
 
            delta=[(None, 'file', 'file-id-2')])
2741
 
        # The bad entry comes first
2742
 
        state = self.assertBadDelta(
2743
 
            active=[('file', 'file-id-2')],
2744
 
            basis=[('file', 'file-id-2')],
2745
 
            delta=[(None, 'file', 'file-id')])
2746
 
 
2747
 
    def test_invalid_existing_id(self):
2748
 
        state = self.assertBadDelta(
2749
 
            active=[('file', 'file-id')],
2750
 
            basis= [('file', 'file-id')],
2751
 
            delta=[(None, 'file', 'file-id')])
2752
 
 
2753
 
    def test_invalid_parent_missing(self):
2754
 
        state = self.assertBadDelta(
2755
 
            active=[],
2756
 
            basis= [],
2757
 
            delta=[(None, 'path/path2', 'file-id')])
2758
 
        # Note: we force the active tree to have the directory, by knowing how
2759
 
        #       path_to_ie handles entries with missing parents
2760
 
        state = self.assertBadDelta(
2761
 
            active=[('path/', 'path-id')],
2762
 
            basis= [],
2763
 
            delta=[(None, 'path/path2', 'file-id')])
2764
 
        state = self.assertBadDelta(
2765
 
            active=[('path/', 'path-id'),
2766
 
                    ('path/path2', 'file-id')],
2767
 
            basis= [],
2768
 
            delta=[(None, 'path/path2', 'file-id')])
2769
 
 
2770
 
    def test_renamed_dir_same_path(self):
2771
 
        # We replace the parent directory, with another parent dir. But the C
2772
 
        # file doesn't look like it has been moved.
2773
 
        state = self.assertUpdate(# Same as basis
2774
 
            active=[('dir/', 'A-id'),
2775
 
                    ('dir/B', 'B-id')],
2776
 
            basis= [('dir/', 'A-id'),
2777
 
                    ('dir/B', 'B-id')],
2778
 
            target=[('dir/', 'C-id'),
2779
 
                    ('dir/B', 'B-id')])
2780
 
        state = self.assertUpdate(# Same as target
2781
 
            active=[('dir/', 'C-id'),
2782
 
                    ('dir/B', 'B-id')],
2783
 
            basis= [('dir/', 'A-id'),
2784
 
                    ('dir/B', 'B-id')],
2785
 
            target=[('dir/', 'C-id'),
2786
 
                    ('dir/B', 'B-id')])
2787
 
        state = self.assertUpdate(# empty active
2788
 
            active=[],
2789
 
            basis= [('dir/', 'A-id'),
2790
 
                    ('dir/B', 'B-id')],
2791
 
            target=[('dir/', 'C-id'),
2792
 
                    ('dir/B', 'B-id')])
2793
 
        state = self.assertUpdate(# different active
2794
 
            active=[('dir/', 'D-id'),
2795
 
                    ('dir/B', 'B-id')],
2796
 
            basis= [('dir/', 'A-id'),
2797
 
                    ('dir/B', 'B-id')],
2798
 
            target=[('dir/', 'C-id'),
2799
 
                    ('dir/B', 'B-id')])
2800
 
 
2801
 
    def test_parent_child_swap(self):
2802
 
        state = self.assertUpdate(# Same as basis
2803
 
            active=[('A/', 'A-id'),
2804
 
                    ('A/B/', 'B-id'),
2805
 
                    ('A/B/C', 'C-id')],
2806
 
            basis= [('A/', 'A-id'),
2807
 
                    ('A/B/', 'B-id'),
2808
 
                    ('A/B/C', 'C-id')],
2809
 
            target=[('A/', 'B-id'),
2810
 
                    ('A/B/', 'A-id'),
2811
 
                    ('A/B/C', 'C-id')])
2812
 
        state = self.assertUpdate(# Same as target
2813
 
            active=[('A/', 'B-id'),
2814
 
                    ('A/B/', 'A-id'),
2815
 
                    ('A/B/C', 'C-id')],
2816
 
            basis= [('A/', 'A-id'),
2817
 
                    ('A/B/', 'B-id'),
2818
 
                    ('A/B/C', 'C-id')],
2819
 
            target=[('A/', 'B-id'),
2820
 
                    ('A/B/', 'A-id'),
2821
 
                    ('A/B/C', 'C-id')])
2822
 
        state = self.assertUpdate(# empty active
2823
 
            active=[],
2824
 
            basis= [('A/', 'A-id'),
2825
 
                    ('A/B/', 'B-id'),
2826
 
                    ('A/B/C', 'C-id')],
2827
 
            target=[('A/', 'B-id'),
2828
 
                    ('A/B/', 'A-id'),
2829
 
                    ('A/B/C', 'C-id')])
2830
 
        state = self.assertUpdate(# different active
2831
 
            active=[('D/', 'A-id'),
2832
 
                    ('D/E/', 'B-id'),
2833
 
                    ('F', 'C-id')],
2834
 
            basis= [('A/', 'A-id'),
2835
 
                    ('A/B/', 'B-id'),
2836
 
                    ('A/B/C', 'C-id')],
2837
 
            target=[('A/', 'B-id'),
2838
 
                    ('A/B/', 'A-id'),
2839
 
                    ('A/B/C', 'C-id')])
2840
 
 
2841
 
    def test_change_root_id(self):
2842
 
        state = self.assertUpdate( # same as basis
2843
 
            active=[('', 'root-id'),
2844
 
                    ('file', 'file-id')],
2845
 
            basis= [('', 'root-id'),
2846
 
                    ('file', 'file-id')],
2847
 
            target=[('', 'target-root-id'),
2848
 
                    ('file', 'file-id')])
2849
 
        state = self.assertUpdate( # same as target
2850
 
            active=[('', 'target-root-id'),
2851
 
                    ('file', 'file-id')],
2852
 
            basis= [('', 'root-id'),
2853
 
                    ('file', 'file-id')],
2854
 
            target=[('', 'target-root-id'),
2855
 
                    ('file', 'root-id')])
2856
 
        state = self.assertUpdate( # all different
2857
 
            active=[('', 'active-root-id'),
2858
 
                    ('file', 'file-id')],
2859
 
            basis= [('', 'root-id'),
2860
 
                    ('file', 'file-id')],
2861
 
            target=[('', 'target-root-id'),
2862
 
                    ('file', 'root-id')])
2863
 
 
2864
 
    def test_change_file_absent_in_active(self):
2865
 
        state = self.assertUpdate(
2866
 
            active=[],
2867
 
            basis= [('file', 'file-id')],
2868
 
            target=[('file', 'file-id')])
2869
 
 
2870
 
    def test_invalid_changed_file(self):
2871
 
        state = self.assertBadDelta( # Not present in basis
2872
 
            active=[('file', 'file-id')],
2873
 
            basis= [],
2874
 
            delta=[('file', 'file', 'file-id')])
2875
 
        state = self.assertBadDelta( # present at another location in basis
2876
 
            active=[('file', 'file-id')],
2877
 
            basis= [('other-file', 'file-id')],
2878
 
            delta=[('file', 'file', 'file-id')])