~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: John Arbash Meinel
  • Date: 2009-08-17 15:38:52 UTC
  • mfrom: (4615.1.1 2.0b1-merge-sort)
  • mto: This revision was merged to the branch mainline in revision 4629.
  • Revision ID: john@arbash-meinel.com-20090817153852-9127xf3aabmcoegt
Bring in the changes to merge_sort.

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
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
21
 
22
22
from bzrlib import (
23
 
    bzrdir,
24
23
    dirstate,
25
24
    errors,
26
25
    inventory,
27
26
    memorytree,
28
27
    osutils,
29
28
    revision as _mod_revision,
30
 
    revisiontree,
31
29
    tests,
32
 
    workingtree_4,
33
30
    )
34
 
from bzrlib.transport import memory
35
31
from bzrlib.tests import test_osutils
36
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
37
32
 
38
33
 
39
34
# TODO:
49
44
# set_path_id  setting id when state is in memory modified
50
45
 
51
46
 
52
 
load_tests = load_tests_apply_scenarios
 
47
def load_tests(basic_tests, module, loader):
 
48
    suite = loader.suiteClass()
 
49
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
 
50
        basic_tests, tests.condition_isinstance(TestCaseWithDirState))
 
51
    tests.multiply_tests(dir_reader_tests,
 
52
                         test_osutils.dir_reader_scenarios(), suite)
 
53
    suite.addTest(remaining_tests)
 
54
    return suite
53
55
 
54
56
 
55
57
class TestCaseWithDirState(tests.TestCaseWithTransport):
56
58
    """Helper functions for creating DirState objects with various content."""
57
59
 
58
 
    scenarios = test_osutils.dir_reader_scenarios()
59
 
 
60
60
    # Set by load_tests
61
61
    _dir_reader_class = None
62
62
    _native_to_unicode = None # Not used yet
64
64
    def setUp(self):
65
65
        tests.TestCaseWithTransport.setUp(self)
66
66
 
67
 
        self.overrideAttr(osutils,
68
 
                          '_selected_dir_reader', self._dir_reader_class())
 
67
        # Save platform specific info and reset it
 
68
        cur_dir_reader = osutils._selected_dir_reader
 
69
 
 
70
        def restore():
 
71
            osutils._selected_dir_reader = cur_dir_reader
 
72
        self.addCleanup(restore)
 
73
 
 
74
        osutils._selected_dir_reader = self._dir_reader_class()
69
75
 
70
76
    def create_empty_dirstate(self):
71
77
        """Return a locked but empty dirstate"""
413
419
            (('', '', tree.get_root_id()), # common details
414
420
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
415
421
              ('d', '', 0, False, rev_id), # first parent details
416
 
              ('d', '', 0, False, rev_id), # second parent details
 
422
              ('d', '', 0, False, rev_id2), # second parent details
417
423
             ])])
418
424
        state = dirstate.DirState.from_tree(tree, 'dirstate')
419
425
        self.check_state_with_reopen(expected_result, state)
494
500
            (('', '', tree.get_root_id()), # common details
495
501
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
496
502
              ('d', '', 0, False, rev_id), # first parent details
497
 
              ('d', '', 0, False, rev_id), # second parent details
 
503
              ('d', '', 0, False, rev_id2), # second parent details
498
504
             ]),
499
505
            (('', 'a file', 'a-file-id'), # common
500
506
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
532
538
 
533
539
class TestDirStateOnFile(TestCaseWithDirState):
534
540
 
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
541
    def test_construct_with_path(self):
549
542
        tree = self.make_branch_and_tree('tree')
550
543
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
579
572
            state.unlock()
580
573
 
581
574
    def test_can_save_in_read_lock(self):
582
 
        state = self.create_updated_dirstate()
 
575
        self.build_tree(['a-file'])
 
576
        state = dirstate.DirState.initialize('dirstate')
 
577
        try:
 
578
            # No stat and no sha1 sum.
 
579
            state.add('a-file', 'a-file-id', 'file', None, '')
 
580
            state.save()
 
581
        finally:
 
582
            state.unlock()
 
583
 
 
584
        # Now open in readonly mode
 
585
        state = dirstate.DirState.on_file('dirstate')
 
586
        state.lock_read()
583
587
        try:
584
588
            entry = state._get_entry(0, path_utf8='a-file')
585
589
            # The current size should be 0 (default)
586
590
            self.assertEqual(0, entry[1][0][2])
587
591
            # We should have a real entry.
588
592
            self.assertNotEqual((None, None), entry)
589
 
            # Set the cutoff-time into the future, so things look cacheable
 
593
            # Make sure everything is old enough
590
594
            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
595
 
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
596
 
                             sha1sum)
 
595
            state._cutoff_time += 10
 
596
            # Change the file length
 
597
            self.build_tree_contents([('a-file', 'shorter')])
 
598
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
 
599
                os.lstat('a-file'))
 
600
            # new file, no cached sha:
 
601
            self.assertEqual(None, sha1sum)
597
602
 
598
603
            # The dirblock has been updated
599
 
            self.assertEqual(st.st_size, entry[1][0][2])
600
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
604
            self.assertEqual(7, entry[1][0][2])
 
605
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
601
606
                             state._dirblock_state)
602
607
 
603
608
            del entry
612
617
        state.lock_read()
613
618
        try:
614
619
            entry = state._get_entry(0, path_utf8='a-file')
615
 
            self.assertEqual(st.st_size, entry[1][0][2])
 
620
            self.assertEqual(7, entry[1][0][2])
616
621
        finally:
617
622
            state.unlock()
618
623
 
619
624
    def test_save_fails_quietly_if_locked(self):
620
625
        """If dirstate is locked, save will fail without complaining."""
621
 
        state = self.create_updated_dirstate()
 
626
        self.build_tree(['a-file'])
 
627
        state = dirstate.DirState.initialize('dirstate')
 
628
        try:
 
629
            # No stat and no sha1 sum.
 
630
            state.add('a-file', 'a-file-id', 'file', None, '')
 
631
            state.save()
 
632
        finally:
 
633
            state.unlock()
 
634
 
 
635
        state = dirstate.DirState.on_file('dirstate')
 
636
        state.lock_read()
622
637
        try:
623
638
            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)
631
 
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
632
 
                             sha1sum)
633
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
639
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
 
640
                os.lstat('a-file'))
 
641
            # No sha - too new
 
642
            self.assertEqual(None, sha1sum)
 
643
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
634
644
                             state._dirblock_state)
635
645
 
636
646
            # Now, before we try to save, grab another dirstate, and take out a
726
736
 
727
737
class TestDirStateManipulations(TestCaseWithDirState):
728
738
 
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
739
    def test_set_state_from_inventory_no_content_no_parents(self):
754
740
        # 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('')
 
741
        tree1 = self.make_branch_and_memory_tree('tree1')
 
742
        tree1.lock_write()
 
743
        try:
 
744
            tree1.add('')
 
745
            revid1 = tree1.commit('foo').encode('utf8')
 
746
            root_id = tree1.get_root_id()
 
747
            inv = tree1.inventory
 
748
        finally:
 
749
            tree1.unlock()
758
750
        expected_result = [], [
759
751
            (('', '', root_id), [
760
752
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
772
764
            # This will unlock it
773
765
            self.check_state_with_reopen(expected_result, state)
774
766
 
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
767
    def test_set_state_from_inventory_preserves_hashcache(self):
820
768
        # https://bugs.launchpad.net/bzr/+bug/146176
821
769
        # set_state_from_inventory should preserve the stat and hash value for
925
873
        state = dirstate.DirState.initialize('dirstate')
926
874
        try:
927
875
            # 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]
 
876
            self.assertEqual(
 
877
                [(('', '', 'TREE_ROOT'), [('d', '', 0, False,
 
878
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
 
879
                list(state._iter_entries()))
 
880
            state.set_path_id('', 'foobarbaz')
 
881
            expected_rows = [
 
882
                (('', '', 'foobarbaz'), [('d', '', 0, False,
 
883
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
939
884
            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
885
            # should work across save too
946
886
            state.save()
947
887
        finally:
965
905
        state._validate()
966
906
        try:
967
907
            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')
 
908
            state.set_path_id('', 'foobarbaz')
977
909
            state._validate()
978
910
            # 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]
 
911
            expected_rows = [
 
912
                (('', '', 'TREE_ROOT'),
 
913
                    [('a', '', 0, False, ''),
 
914
                     ('d', '', 0, False, 'parent-revid'),
 
915
                     ]),
 
916
                (('', '', 'foobarbaz'),
 
917
                    [('d', '', 0, False, ''),
 
918
                     ('a', '', 0, False, ''),
 
919
                     ]),
 
920
                ]
986
921
            state._validate()
987
922
            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
923
            # should work across save too
999
924
            state.save()
1000
925
        finally:
1075
1000
                [(('', '', root_id), [
1076
1001
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1077
1002
                  ('d', '', 0, False, revid1),
1078
 
                  ('d', '', 0, False, revid1)
 
1003
                  ('d', '', 0, False, revid2)
1079
1004
                  ])],
1080
1005
                list(state._iter_entries()))
1081
1006
        finally:
1109
1034
            (('', '', root_id), [
1110
1035
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1111
1036
             ('d', '', 0, False, revid1.encode('utf8')),
1112
 
             ('d', '', 0, False, revid1.encode('utf8'))
 
1037
             ('d', '', 0, False, revid2.encode('utf8'))
1113
1038
             ]),
1114
1039
            (('', 'a file', 'file-id'), [
1115
1040
             ('a', '', 0, False, ''),
1344
1269
            tree1.unlock()
1345
1270
 
1346
1271
 
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
 
 
1403
1272
class TestGetLines(TestCaseWithDirState):
1404
1273
 
1405
1274
    def test_get_line_with_2_rows(self):
1798
1667
class InstrumentedDirState(dirstate.DirState):
1799
1668
    """An DirState with instrumented sha1 functionality."""
1800
1669
 
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)
 
1670
    def __init__(self, path, sha1_provider):
 
1671
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
1804
1672
        self._time_offset = 0
1805
1673
        self._log = []
1806
1674
        # member is dynamically set in DirState.__init__ to turn on trace
2435
2303
        self.assertTrue(len(statvalue) >= 10)
2436
2304
        self.assertEqual(len(text), statvalue.st_size)
2437
2305
        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')])