~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Aaron Bentley
  • Date: 2012-07-19 15:44:55 UTC
  • mto: This revision was merged to the branch mainline in revision 6540.
  • Revision ID: aaron@aaronbentley.com-20120719154455-j7y8fm7o9y95vo38
Eliminate get_stored_uncommitted from API.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
20
19
import os
 
20
import tempfile
21
21
 
22
22
from bzrlib import (
 
23
    controldir,
23
24
    dirstate,
24
25
    errors,
25
26
    inventory,
26
27
    memorytree,
27
28
    osutils,
28
29
    revision as _mod_revision,
 
30
    revisiontree,
29
31
    tests,
30
 
    )
31
 
from bzrlib.tests import test_osutils
 
32
    workingtree_4,
 
33
    )
 
34
from bzrlib.tests import (
 
35
    features,
 
36
    test_osutils,
 
37
    )
 
38
from bzrlib.tests.scenarios import load_tests_apply_scenarios
32
39
 
33
40
 
34
41
# TODO:
44
51
# set_path_id  setting id when state is in memory modified
45
52
 
46
53
 
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
 
54
load_tests = load_tests_apply_scenarios
55
55
 
56
56
 
57
57
class TestCaseWithDirState(tests.TestCaseWithTransport):
58
58
    """Helper functions for creating DirState objects with various content."""
59
59
 
 
60
    scenarios = test_osutils.dir_reader_scenarios()
 
61
 
60
62
    # Set by load_tests
61
63
    _dir_reader_class = None
62
64
    _native_to_unicode = None # Not used yet
532
534
 
533
535
class TestDirStateOnFile(TestCaseWithDirState):
534
536
 
 
537
    def create_updated_dirstate(self):
 
538
        self.build_tree(['a-file'])
 
539
        tree = self.make_branch_and_tree('.')
 
540
        tree.add(['a-file'], ['a-id'])
 
541
        tree.commit('add a-file')
 
542
        # Save and unlock the state, re-open it in readonly mode
 
543
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
544
        state.save()
 
545
        state.unlock()
 
546
        state = dirstate.DirState.on_file('dirstate')
 
547
        state.lock_read()
 
548
        return state
 
549
 
535
550
    def test_construct_with_path(self):
536
551
        tree = self.make_branch_and_tree('tree')
537
552
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
566
581
            state.unlock()
567
582
 
568
583
    def test_can_save_in_read_lock(self):
569
 
        self.build_tree(['a-file'])
570
 
        state = dirstate.DirState.initialize('dirstate')
571
 
        try:
572
 
            # No stat and no sha1 sum.
573
 
            state.add('a-file', 'a-file-id', 'file', None, '')
574
 
            state.save()
575
 
        finally:
576
 
            state.unlock()
577
 
 
578
 
        # Now open in readonly mode
579
 
        state = dirstate.DirState.on_file('dirstate')
580
 
        state.lock_read()
 
584
        state = self.create_updated_dirstate()
581
585
        try:
582
586
            entry = state._get_entry(0, path_utf8='a-file')
583
587
            # The current size should be 0 (default)
584
588
            self.assertEqual(0, entry[1][0][2])
585
589
            # We should have a real entry.
586
590
            self.assertNotEqual((None, None), entry)
587
 
            # Make sure everything is old enough
 
591
            # Set the cutoff-time into the future, so things look cacheable
588
592
            state._sha_cutoff_time()
589
 
            state._cutoff_time += 10
590
 
            # Change the file length
591
 
            self.build_tree_contents([('a-file', 'shorter')])
592
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
593
 
                os.lstat('a-file'))
594
 
            # new file, no cached sha:
595
 
            self.assertEqual(None, sha1sum)
 
593
            state._cutoff_time += 10.0
 
594
            st = os.lstat('a-file')
 
595
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
596
            # We updated the current sha1sum because the file is cacheable
 
597
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
598
                             sha1sum)
596
599
 
597
600
            # The dirblock has been updated
598
 
            self.assertEqual(7, entry[1][0][2])
599
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
601
            self.assertEqual(st.st_size, entry[1][0][2])
 
602
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
600
603
                             state._dirblock_state)
601
604
 
602
605
            del entry
611
614
        state.lock_read()
612
615
        try:
613
616
            entry = state._get_entry(0, path_utf8='a-file')
614
 
            self.assertEqual(7, entry[1][0][2])
 
617
            self.assertEqual(st.st_size, entry[1][0][2])
615
618
        finally:
616
619
            state.unlock()
617
620
 
618
621
    def test_save_fails_quietly_if_locked(self):
619
622
        """If dirstate is locked, save will fail without complaining."""
620
 
        self.build_tree(['a-file'])
621
 
        state = dirstate.DirState.initialize('dirstate')
622
 
        try:
623
 
            # No stat and no sha1 sum.
624
 
            state.add('a-file', 'a-file-id', 'file', None, '')
625
 
            state.save()
626
 
        finally:
627
 
            state.unlock()
628
 
 
629
 
        state = dirstate.DirState.on_file('dirstate')
630
 
        state.lock_read()
 
623
        state = self.create_updated_dirstate()
631
624
        try:
632
625
            entry = state._get_entry(0, path_utf8='a-file')
633
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
634
 
                os.lstat('a-file'))
635
 
            # No sha - too new
636
 
            self.assertEqual(None, sha1sum)
637
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
626
            # No cached sha1 yet.
 
627
            self.assertEqual('', entry[1][0][1])
 
628
            # Set the cutoff-time into the future, so things look cacheable
 
629
            state._sha_cutoff_time()
 
630
            state._cutoff_time += 10.0
 
631
            st = os.lstat('a-file')
 
632
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
633
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
634
                             sha1sum)
 
635
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
638
636
                             state._dirblock_state)
639
637
 
640
638
            # Now, before we try to save, grab another dirstate, and take out a
730
728
 
731
729
class TestDirStateManipulations(TestCaseWithDirState):
732
730
 
 
731
    def make_minimal_tree(self):
 
732
        tree1 = self.make_branch_and_memory_tree('tree1')
 
733
        tree1.lock_write()
 
734
        self.addCleanup(tree1.unlock)
 
735
        tree1.add('')
 
736
        revid1 = tree1.commit('foo')
 
737
        return tree1, revid1
 
738
 
 
739
    def test_update_minimal_updates_id_index(self):
 
740
        state = self.create_dirstate_with_root_and_subdir()
 
741
        self.addCleanup(state.unlock)
 
742
        id_index = state._get_id_index()
 
743
        self.assertEqual(['a-root-value', 'subdir-id'], sorted(id_index))
 
744
        state.add('file-name', 'file-id', 'file', None, '')
 
745
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
 
746
                         sorted(id_index))
 
747
        state.update_minimal(('', 'new-name', 'file-id'), 'f',
 
748
                             path_utf8='new-name')
 
749
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
 
750
                         sorted(id_index))
 
751
        self.assertEqual([('', 'new-name', 'file-id')],
 
752
                         sorted(id_index['file-id']))
 
753
        state._validate()
 
754
 
733
755
    def test_set_state_from_inventory_no_content_no_parents(self):
734
756
        # setting the current inventory is a slow but important api to support.
735
 
        tree1 = self.make_branch_and_memory_tree('tree1')
736
 
        tree1.lock_write()
737
 
        try:
738
 
            tree1.add('')
739
 
            revid1 = tree1.commit('foo').encode('utf8')
740
 
            root_id = tree1.get_root_id()
741
 
            inv = tree1.inventory
742
 
        finally:
743
 
            tree1.unlock()
 
757
        tree1, revid1 = self.make_minimal_tree()
 
758
        inv = tree1.root_inventory
 
759
        root_id = inv.path2id('')
744
760
        expected_result = [], [
745
761
            (('', '', root_id), [
746
762
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
758
774
            # This will unlock it
759
775
            self.check_state_with_reopen(expected_result, state)
760
776
 
 
777
    def test_set_state_from_scratch_no_parents(self):
 
778
        tree1, revid1 = self.make_minimal_tree()
 
779
        inv = tree1.root_inventory
 
780
        root_id = inv.path2id('')
 
781
        expected_result = [], [
 
782
            (('', '', root_id), [
 
783
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
 
784
        state = dirstate.DirState.initialize('dirstate')
 
785
        try:
 
786
            state.set_state_from_scratch(inv, [], [])
 
787
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
788
                             state._header_state)
 
789
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
790
                             state._dirblock_state)
 
791
        except:
 
792
            state.unlock()
 
793
            raise
 
794
        else:
 
795
            # This will unlock it
 
796
            self.check_state_with_reopen(expected_result, state)
 
797
 
 
798
    def test_set_state_from_scratch_identical_parent(self):
 
799
        tree1, revid1 = self.make_minimal_tree()
 
800
        inv = tree1.root_inventory
 
801
        root_id = inv.path2id('')
 
802
        rev_tree1 = tree1.branch.repository.revision_tree(revid1)
 
803
        d_entry = ('d', '', 0, False, dirstate.DirState.NULLSTAT)
 
804
        parent_entry = ('d', '', 0, False, revid1)
 
805
        expected_result = [revid1], [
 
806
            (('', '', root_id), [d_entry, parent_entry])]
 
807
        state = dirstate.DirState.initialize('dirstate')
 
808
        try:
 
809
            state.set_state_from_scratch(inv, [(revid1, rev_tree1)], [])
 
810
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
811
                             state._header_state)
 
812
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
813
                             state._dirblock_state)
 
814
        except:
 
815
            state.unlock()
 
816
            raise
 
817
        else:
 
818
            # This will unlock it
 
819
            self.check_state_with_reopen(expected_result, state)
 
820
 
761
821
    def test_set_state_from_inventory_preserves_hashcache(self):
762
822
        # https://bugs.launchpad.net/bzr/+bug/146176
763
823
        # set_state_from_inventory should preserve the stat and hash value for
795
855
                tree._dirstate._get_entry(0, 'foo-id'))
796
856
 
797
857
            # extract the inventory, and add something to it
798
 
            inv = tree._get_inventory()
 
858
            inv = tree._get_root_inventory()
799
859
            # should see the file we poked in...
800
860
            self.assertTrue(inv.has_id('foo-id'))
801
861
            self.assertTrue(inv.has_filename('foo'))
831
891
                      ['a-id', 'b-id', 'a-b-id', 'foo-id', 'bar-id'])
832
892
            tree1.commit('rev1', rev_id='rev1')
833
893
            root_id = tree1.get_root_id()
834
 
            inv = tree1.inventory
 
894
            inv = tree1.root_inventory
835
895
        finally:
836
896
            tree1.unlock()
837
897
        expected_result1 = [('', '', root_id, 'd'),
1151
1211
        # The most trivial addition of a symlink when there are no parents and
1152
1212
        # its in the root and all data about the file is supplied
1153
1213
        # bzr doesn't support fake symlinks on windows, yet.
1154
 
        self.requireFeature(tests.SymlinkFeature)
 
1214
        self.requireFeature(features.SymlinkFeature)
1155
1215
        os.symlink(target, link_name)
1156
1216
        stat = os.lstat(link_name)
1157
1217
        expected_entries = [
1182
1242
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1183
1243
 
1184
1244
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1185
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1245
        self.requireFeature(features.UnicodeFilenameFeature)
1186
1246
        self._test_add_symlink_to_root_no_parents_all_data(
1187
1247
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
1188
1248
 
1265
1325
        try:
1266
1326
            tree1.add(['b'], ['b-id'])
1267
1327
            root_id = tree1.get_root_id()
1268
 
            inv = tree1.inventory
 
1328
            inv = tree1.root_inventory
1269
1329
            state = dirstate.DirState.initialize('dirstate')
1270
1330
            try:
1271
1331
                # Set the initial state with 'b'
1286
1346
            tree1.unlock()
1287
1347
 
1288
1348
 
 
1349
class TestDirStateHashUpdates(TestCaseWithDirState):
 
1350
 
 
1351
    def do_update_entry(self, state, path):
 
1352
        entry = state._get_entry(0, path_utf8=path)
 
1353
        stat = os.lstat(path)
 
1354
        return dirstate.update_entry(state, entry, os.path.abspath(path), stat)
 
1355
 
 
1356
    def _read_state_content(self, state):
 
1357
        """Read the content of the dirstate file.
 
1358
 
 
1359
        On Windows when one process locks a file, you can't even open() the
 
1360
        file in another process (to read it). So we go directly to
 
1361
        state._state_file. This should always be the exact disk representation,
 
1362
        so it is reasonable to do so.
 
1363
        DirState also always seeks before reading, so it doesn't matter if we
 
1364
        bump the file pointer.
 
1365
        """
 
1366
        state._state_file.seek(0)
 
1367
        return state._state_file.read()
 
1368
 
 
1369
    def test_worth_saving_limit_avoids_writing(self):
 
1370
        tree = self.make_branch_and_tree('.')
 
1371
        self.build_tree(['c', 'd'])
 
1372
        tree.lock_write()
 
1373
        tree.add(['c', 'd'], ['c-id', 'd-id'])
 
1374
        tree.commit('add c and d')
 
1375
        state = InstrumentedDirState.on_file(tree.current_dirstate()._filename,
 
1376
                                             worth_saving_limit=2)
 
1377
        tree.unlock()
 
1378
        state.lock_write()
 
1379
        self.addCleanup(state.unlock)
 
1380
        state._read_dirblocks_if_needed()
 
1381
        state.adjust_time(+20) # Allow things to be cached
 
1382
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1383
                         state._dirblock_state)
 
1384
        content = self._read_state_content(state)
 
1385
        self.do_update_entry(state, 'c')
 
1386
        self.assertEqual(1, len(state._known_hash_changes))
 
1387
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1388
                         state._dirblock_state)
 
1389
        state.save()
 
1390
        # It should not have set the state to IN_MEMORY_UNMODIFIED because the
 
1391
        # hash values haven't been written out.
 
1392
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1393
                         state._dirblock_state)
 
1394
        self.assertEqual(content, self._read_state_content(state))
 
1395
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1396
                         state._dirblock_state)
 
1397
        self.do_update_entry(state, 'd')
 
1398
        self.assertEqual(2, len(state._known_hash_changes))
 
1399
        state.save()
 
1400
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1401
                         state._dirblock_state)
 
1402
        self.assertEqual(0, len(state._known_hash_changes))
 
1403
 
 
1404
 
1289
1405
class TestGetLines(TestCaseWithDirState):
1290
1406
 
1291
1407
    def test_get_line_with_2_rows(self):
1684
1800
class InstrumentedDirState(dirstate.DirState):
1685
1801
    """An DirState with instrumented sha1 functionality."""
1686
1802
 
1687
 
    def __init__(self, path, sha1_provider):
1688
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
 
1803
    def __init__(self, path, sha1_provider, worth_saving_limit=0):
 
1804
        super(InstrumentedDirState, self).__init__(path, sha1_provider,
 
1805
            worth_saving_limit=worth_saving_limit)
1689
1806
        self._time_offset = 0
1690
1807
        self._log = []
1691
1808
        # member is dynamically set in DirState.__init__ to turn on trace
2092
2209
class TestDirstateTreeReference(TestCaseWithDirState):
2093
2210
 
2094
2211
    def test_reference_revision_is_none(self):
2095
 
        tree = self.make_branch_and_tree('tree', format='dirstate-with-subtree')
 
2212
        tree = self.make_branch_and_tree('tree', format='development-subtree')
2096
2213
        subtree = self.make_branch_and_tree('tree/subtree',
2097
 
                            format='dirstate-with-subtree')
 
2214
                            format='development-subtree')
2098
2215
        subtree.set_root_id('subtree')
2099
2216
        tree.add_reference(subtree)
2100
2217
        tree.add('subtree')
2320
2437
        self.assertTrue(len(statvalue) >= 10)
2321
2438
        self.assertEqual(len(text), statvalue.st_size)
2322
2439
        self.assertEqual(expected_sha, sha1)
 
2440
 
 
2441
 
 
2442
class _Repo(object):
 
2443
    """A minimal api to get InventoryRevisionTree to work."""
 
2444
 
 
2445
    def __init__(self):
 
2446
        default_format = controldir.format_registry.make_bzrdir('default')
 
2447
        self._format = default_format.repository_format
 
2448
 
 
2449
    def lock_read(self):
 
2450
        pass
 
2451
 
 
2452
    def unlock(self):
 
2453
        pass
 
2454
 
 
2455
 
 
2456
class TestUpdateBasisByDelta(tests.TestCase):
 
2457
 
 
2458
    def path_to_ie(self, path, file_id, rev_id, dir_ids):
 
2459
        if path.endswith('/'):
 
2460
            is_dir = True
 
2461
            path = path[:-1]
 
2462
        else:
 
2463
            is_dir = False
 
2464
        dirname, basename = osutils.split(path)
 
2465
        try:
 
2466
            dir_id = dir_ids[dirname]
 
2467
        except KeyError:
 
2468
            dir_id = osutils.basename(dirname) + '-id'
 
2469
        if is_dir:
 
2470
            ie = inventory.InventoryDirectory(file_id, basename, dir_id)
 
2471
            dir_ids[path] = file_id
 
2472
        else:
 
2473
            ie = inventory.InventoryFile(file_id, basename, dir_id)
 
2474
            ie.text_size = 0
 
2475
            ie.text_sha1 = ''
 
2476
        ie.revision = rev_id
 
2477
        return ie
 
2478
 
 
2479
    def create_tree_from_shape(self, rev_id, shape):
 
2480
        dir_ids = {'': 'root-id'}
 
2481
        inv = inventory.Inventory('root-id', rev_id)
 
2482
        for path, file_id in shape:
 
2483
            if path == '':
 
2484
                # Replace the root entry
 
2485
                del inv._byid[inv.root.file_id]
 
2486
                inv.root.file_id = file_id
 
2487
                inv._byid[file_id] = inv.root
 
2488
                dir_ids[''] = file_id
 
2489
                continue
 
2490
            inv.add(self.path_to_ie(path, file_id, rev_id, dir_ids))
 
2491
        return revisiontree.InventoryRevisionTree(_Repo(), inv, rev_id)
 
2492
 
 
2493
    def create_empty_dirstate(self):
 
2494
        fd, path = tempfile.mkstemp(prefix='bzr-dirstate')
 
2495
        self.addCleanup(os.remove, path)
 
2496
        os.close(fd)
 
2497
        state = dirstate.DirState.initialize(path)
 
2498
        self.addCleanup(state.unlock)
 
2499
        return state
 
2500
 
 
2501
    def create_inv_delta(self, delta, rev_id):
 
2502
        """Translate a 'delta shape' into an actual InventoryDelta"""
 
2503
        dir_ids = {'': 'root-id'}
 
2504
        inv_delta = []
 
2505
        for old_path, new_path, file_id in delta:
 
2506
            if old_path is not None and old_path.endswith('/'):
 
2507
                # Don't have to actually do anything for this, because only
 
2508
                # new_path creates InventoryEntries
 
2509
                old_path = old_path[:-1]
 
2510
            if new_path is None: # Delete
 
2511
                inv_delta.append((old_path, None, file_id, None))
 
2512
                continue
 
2513
            ie = self.path_to_ie(new_path, file_id, rev_id, dir_ids)
 
2514
            inv_delta.append((old_path, new_path, file_id, ie))
 
2515
        return inv_delta
 
2516
 
 
2517
    def assertUpdate(self, active, basis, target):
 
2518
        """Assert that update_basis_by_delta works how we want.
 
2519
 
 
2520
        Set up a DirState object with active_shape for tree 0, basis_shape for
 
2521
        tree 1. Then apply the delta from basis_shape to target_shape,
 
2522
        and assert that the DirState is still valid, and that its stored
 
2523
        content matches the target_shape.
 
2524
        """
 
2525
        active_tree = self.create_tree_from_shape('active', active)
 
2526
        basis_tree = self.create_tree_from_shape('basis', basis)
 
2527
        target_tree = self.create_tree_from_shape('target', target)
 
2528
        state = self.create_empty_dirstate()
 
2529
        state.set_state_from_scratch(active_tree.root_inventory,
 
2530
            [('basis', basis_tree)], [])
 
2531
        delta = target_tree.root_inventory._make_delta(
 
2532
            basis_tree.root_inventory)
 
2533
        state.update_basis_by_delta(delta, 'target')
 
2534
        state._validate()
 
2535
        dirstate_tree = workingtree_4.DirStateRevisionTree(state,
 
2536
            'target', _Repo())
 
2537
        # The target now that delta has been applied should match the
 
2538
        # RevisionTree
 
2539
        self.assertEqual([], list(dirstate_tree.iter_changes(target_tree)))
 
2540
        # And the dirblock state should be identical to the state if we created
 
2541
        # it from scratch.
 
2542
        state2 = self.create_empty_dirstate()
 
2543
        state2.set_state_from_scratch(active_tree.root_inventory,
 
2544
            [('target', target_tree)], [])
 
2545
        self.assertEqual(state2._dirblocks, state._dirblocks)
 
2546
        return state
 
2547
 
 
2548
    def assertBadDelta(self, active, basis, delta):
 
2549
        """Test that we raise InconsistentDelta when appropriate.
 
2550
 
 
2551
        :param active: The active tree shape
 
2552
        :param basis: The basis tree shape
 
2553
        :param delta: A description of the delta to apply. Similar to the form
 
2554
            for regular inventory deltas, but omitting the InventoryEntry.
 
2555
            So adding a file is: (None, 'path', 'file-id')
 
2556
            Adding a directory is: (None, 'path/', 'dir-id')
 
2557
            Renaming a dir is: ('old/', 'new/', 'dir-id')
 
2558
            etc.
 
2559
        """
 
2560
        active_tree = self.create_tree_from_shape('active', active)
 
2561
        basis_tree = self.create_tree_from_shape('basis', basis)
 
2562
        inv_delta = self.create_inv_delta(delta, 'target')
 
2563
        state = self.create_empty_dirstate()
 
2564
        state.set_state_from_scratch(active_tree.root_inventory,
 
2565
            [('basis', basis_tree)], [])
 
2566
        self.assertRaises(errors.InconsistentDelta,
 
2567
            state.update_basis_by_delta, inv_delta, 'target')
 
2568
        ## try:
 
2569
        ##     state.update_basis_by_delta(inv_delta, 'target')
 
2570
        ## except errors.InconsistentDelta, e:
 
2571
        ##     import pdb; pdb.set_trace()
 
2572
        ## else:
 
2573
        ##     import pdb; pdb.set_trace()
 
2574
        self.assertTrue(state._changes_aborted)
 
2575
 
 
2576
    def test_remove_file_matching_active_state(self):
 
2577
        state = self.assertUpdate(
 
2578
            active=[],
 
2579
            basis =[('file', 'file-id')],
 
2580
            target=[],
 
2581
            )
 
2582
 
 
2583
    def test_remove_file_present_in_active_state(self):
 
2584
        state = self.assertUpdate(
 
2585
            active=[('file', 'file-id')],
 
2586
            basis =[('file', 'file-id')],
 
2587
            target=[],
 
2588
            )
 
2589
 
 
2590
    def test_remove_file_present_elsewhere_in_active_state(self):
 
2591
        state = self.assertUpdate(
 
2592
            active=[('other-file', 'file-id')],
 
2593
            basis =[('file', 'file-id')],
 
2594
            target=[],
 
2595
            )
 
2596
 
 
2597
    def test_remove_file_active_state_has_diff_file(self):
 
2598
        state = self.assertUpdate(
 
2599
            active=[('file', 'file-id-2')],
 
2600
            basis =[('file', 'file-id')],
 
2601
            target=[],
 
2602
            )
 
2603
 
 
2604
    def test_remove_file_active_state_has_diff_file_and_file_elsewhere(self):
 
2605
        state = self.assertUpdate(
 
2606
            active=[('file', 'file-id-2'),
 
2607
                    ('other-file', 'file-id')],
 
2608
            basis =[('file', 'file-id')],
 
2609
            target=[],
 
2610
            )
 
2611
 
 
2612
    def test_add_file_matching_active_state(self):
 
2613
        state = self.assertUpdate(
 
2614
            active=[('file', 'file-id')],
 
2615
            basis =[],
 
2616
            target=[('file', 'file-id')],
 
2617
            )
 
2618
 
 
2619
    def test_add_file_missing_in_active_state(self):
 
2620
        state = self.assertUpdate(
 
2621
            active=[],
 
2622
            basis =[],
 
2623
            target=[('file', 'file-id')],
 
2624
            )
 
2625
 
 
2626
    def test_add_file_elsewhere_in_active_state(self):
 
2627
        state = self.assertUpdate(
 
2628
            active=[('other-file', 'file-id')],
 
2629
            basis =[],
 
2630
            target=[('file', 'file-id')],
 
2631
            )
 
2632
 
 
2633
    def test_add_file_active_state_has_diff_file_and_file_elsewhere(self):
 
2634
        state = self.assertUpdate(
 
2635
            active=[('other-file', 'file-id'),
 
2636
                    ('file', 'file-id-2')],
 
2637
            basis =[],
 
2638
            target=[('file', 'file-id')],
 
2639
            )
 
2640
 
 
2641
    def test_rename_file_matching_active_state(self):
 
2642
        state = self.assertUpdate(
 
2643
            active=[('other-file', 'file-id')],
 
2644
            basis =[('file', 'file-id')],
 
2645
            target=[('other-file', 'file-id')],
 
2646
            )
 
2647
 
 
2648
    def test_rename_file_missing_in_active_state(self):
 
2649
        state = self.assertUpdate(
 
2650
            active=[],
 
2651
            basis =[('file', 'file-id')],
 
2652
            target=[('other-file', 'file-id')],
 
2653
            )
 
2654
 
 
2655
    def test_rename_file_present_elsewhere_in_active_state(self):
 
2656
        state = self.assertUpdate(
 
2657
            active=[('third', 'file-id')],
 
2658
            basis =[('file', 'file-id')],
 
2659
            target=[('other-file', 'file-id')],
 
2660
            )
 
2661
 
 
2662
    def test_rename_file_active_state_has_diff_source_file(self):
 
2663
        state = self.assertUpdate(
 
2664
            active=[('file', 'file-id-2')],
 
2665
            basis =[('file', 'file-id')],
 
2666
            target=[('other-file', 'file-id')],
 
2667
            )
 
2668
 
 
2669
    def test_rename_file_active_state_has_diff_target_file(self):
 
2670
        state = self.assertUpdate(
 
2671
            active=[('other-file', 'file-id-2')],
 
2672
            basis =[('file', 'file-id')],
 
2673
            target=[('other-file', 'file-id')],
 
2674
            )
 
2675
 
 
2676
    def test_rename_file_active_has_swapped_files(self):
 
2677
        state = self.assertUpdate(
 
2678
            active=[('file', 'file-id'),
 
2679
                    ('other-file', 'file-id-2')],
 
2680
            basis= [('file', 'file-id'),
 
2681
                    ('other-file', 'file-id-2')],
 
2682
            target=[('file', 'file-id-2'),
 
2683
                    ('other-file', 'file-id')])
 
2684
 
 
2685
    def test_rename_file_basis_has_swapped_files(self):
 
2686
        state = self.assertUpdate(
 
2687
            active=[('file', 'file-id'),
 
2688
                    ('other-file', 'file-id-2')],
 
2689
            basis= [('file', 'file-id-2'),
 
2690
                    ('other-file', 'file-id')],
 
2691
            target=[('file', 'file-id'),
 
2692
                    ('other-file', 'file-id-2')])
 
2693
 
 
2694
    def test_rename_directory_with_contents(self):
 
2695
        state = self.assertUpdate( # active matches basis
 
2696
            active=[('dir1/', 'dir-id'),
 
2697
                    ('dir1/file', 'file-id')],
 
2698
            basis= [('dir1/', 'dir-id'),
 
2699
                    ('dir1/file', 'file-id')],
 
2700
            target=[('dir2/', 'dir-id'),
 
2701
                    ('dir2/file', 'file-id')])
 
2702
        state = self.assertUpdate( # active matches target
 
2703
            active=[('dir2/', 'dir-id'),
 
2704
                    ('dir2/file', 'file-id')],
 
2705
            basis= [('dir1/', 'dir-id'),
 
2706
                    ('dir1/file', 'file-id')],
 
2707
            target=[('dir2/', 'dir-id'),
 
2708
                    ('dir2/file', 'file-id')])
 
2709
        state = self.assertUpdate( # active empty
 
2710
            active=[],
 
2711
            basis= [('dir1/', 'dir-id'),
 
2712
                    ('dir1/file', 'file-id')],
 
2713
            target=[('dir2/', 'dir-id'),
 
2714
                    ('dir2/file', 'file-id')])
 
2715
        state = self.assertUpdate( # active present at other location
 
2716
            active=[('dir3/', 'dir-id'),
 
2717
                    ('dir3/file', 'file-id')],
 
2718
            basis= [('dir1/', 'dir-id'),
 
2719
                    ('dir1/file', 'file-id')],
 
2720
            target=[('dir2/', 'dir-id'),
 
2721
                    ('dir2/file', 'file-id')])
 
2722
        state = self.assertUpdate( # active has different ids
 
2723
            active=[('dir1/', 'dir1-id'),
 
2724
                    ('dir1/file', 'file1-id'),
 
2725
                    ('dir2/', 'dir2-id'),
 
2726
                    ('dir2/file', 'file2-id')],
 
2727
            basis= [('dir1/', 'dir-id'),
 
2728
                    ('dir1/file', 'file-id')],
 
2729
            target=[('dir2/', 'dir-id'),
 
2730
                    ('dir2/file', 'file-id')])
 
2731
 
 
2732
    def test_invalid_file_not_present(self):
 
2733
        state = self.assertBadDelta(
 
2734
            active=[('file', 'file-id')],
 
2735
            basis= [('file', 'file-id')],
 
2736
            delta=[('other-file', 'file', 'file-id')])
 
2737
 
 
2738
    def test_invalid_new_id_same_path(self):
 
2739
        # The bad entry comes after
 
2740
        state = self.assertBadDelta(
 
2741
            active=[('file', 'file-id')],
 
2742
            basis= [('file', 'file-id')],
 
2743
            delta=[(None, 'file', 'file-id-2')])
 
2744
        # The bad entry comes first
 
2745
        state = self.assertBadDelta(
 
2746
            active=[('file', 'file-id-2')],
 
2747
            basis=[('file', 'file-id-2')],
 
2748
            delta=[(None, 'file', 'file-id')])
 
2749
 
 
2750
    def test_invalid_existing_id(self):
 
2751
        state = self.assertBadDelta(
 
2752
            active=[('file', 'file-id')],
 
2753
            basis= [('file', 'file-id')],
 
2754
            delta=[(None, 'file', 'file-id')])
 
2755
 
 
2756
    def test_invalid_parent_missing(self):
 
2757
        state = self.assertBadDelta(
 
2758
            active=[],
 
2759
            basis= [],
 
2760
            delta=[(None, 'path/path2', 'file-id')])
 
2761
        # Note: we force the active tree to have the directory, by knowing how
 
2762
        #       path_to_ie handles entries with missing parents
 
2763
        state = self.assertBadDelta(
 
2764
            active=[('path/', 'path-id')],
 
2765
            basis= [],
 
2766
            delta=[(None, 'path/path2', 'file-id')])
 
2767
        state = self.assertBadDelta(
 
2768
            active=[('path/', 'path-id'),
 
2769
                    ('path/path2', 'file-id')],
 
2770
            basis= [],
 
2771
            delta=[(None, 'path/path2', 'file-id')])
 
2772
 
 
2773
    def test_renamed_dir_same_path(self):
 
2774
        # We replace the parent directory, with another parent dir. But the C
 
2775
        # file doesn't look like it has been moved.
 
2776
        state = self.assertUpdate(# Same as basis
 
2777
            active=[('dir/', 'A-id'),
 
2778
                    ('dir/B', 'B-id')],
 
2779
            basis= [('dir/', 'A-id'),
 
2780
                    ('dir/B', 'B-id')],
 
2781
            target=[('dir/', 'C-id'),
 
2782
                    ('dir/B', 'B-id')])
 
2783
        state = self.assertUpdate(# Same as target
 
2784
            active=[('dir/', 'C-id'),
 
2785
                    ('dir/B', 'B-id')],
 
2786
            basis= [('dir/', 'A-id'),
 
2787
                    ('dir/B', 'B-id')],
 
2788
            target=[('dir/', 'C-id'),
 
2789
                    ('dir/B', 'B-id')])
 
2790
        state = self.assertUpdate(# empty active
 
2791
            active=[],
 
2792
            basis= [('dir/', 'A-id'),
 
2793
                    ('dir/B', 'B-id')],
 
2794
            target=[('dir/', 'C-id'),
 
2795
                    ('dir/B', 'B-id')])
 
2796
        state = self.assertUpdate(# different active
 
2797
            active=[('dir/', 'D-id'),
 
2798
                    ('dir/B', 'B-id')],
 
2799
            basis= [('dir/', 'A-id'),
 
2800
                    ('dir/B', 'B-id')],
 
2801
            target=[('dir/', 'C-id'),
 
2802
                    ('dir/B', 'B-id')])
 
2803
 
 
2804
    def test_parent_child_swap(self):
 
2805
        state = self.assertUpdate(# Same as basis
 
2806
            active=[('A/', 'A-id'),
 
2807
                    ('A/B/', 'B-id'),
 
2808
                    ('A/B/C', 'C-id')],
 
2809
            basis= [('A/', 'A-id'),
 
2810
                    ('A/B/', 'B-id'),
 
2811
                    ('A/B/C', 'C-id')],
 
2812
            target=[('A/', 'B-id'),
 
2813
                    ('A/B/', 'A-id'),
 
2814
                    ('A/B/C', 'C-id')])
 
2815
        state = self.assertUpdate(# Same as target
 
2816
            active=[('A/', 'B-id'),
 
2817
                    ('A/B/', 'A-id'),
 
2818
                    ('A/B/C', 'C-id')],
 
2819
            basis= [('A/', 'A-id'),
 
2820
                    ('A/B/', 'B-id'),
 
2821
                    ('A/B/C', 'C-id')],
 
2822
            target=[('A/', 'B-id'),
 
2823
                    ('A/B/', 'A-id'),
 
2824
                    ('A/B/C', 'C-id')])
 
2825
        state = self.assertUpdate(# empty active
 
2826
            active=[],
 
2827
            basis= [('A/', 'A-id'),
 
2828
                    ('A/B/', 'B-id'),
 
2829
                    ('A/B/C', 'C-id')],
 
2830
            target=[('A/', 'B-id'),
 
2831
                    ('A/B/', 'A-id'),
 
2832
                    ('A/B/C', 'C-id')])
 
2833
        state = self.assertUpdate(# different active
 
2834
            active=[('D/', 'A-id'),
 
2835
                    ('D/E/', 'B-id'),
 
2836
                    ('F', 'C-id')],
 
2837
            basis= [('A/', 'A-id'),
 
2838
                    ('A/B/', 'B-id'),
 
2839
                    ('A/B/C', 'C-id')],
 
2840
            target=[('A/', 'B-id'),
 
2841
                    ('A/B/', 'A-id'),
 
2842
                    ('A/B/C', 'C-id')])
 
2843
 
 
2844
    def test_change_root_id(self):
 
2845
        state = self.assertUpdate( # same as basis
 
2846
            active=[('', 'root-id'),
 
2847
                    ('file', 'file-id')],
 
2848
            basis= [('', 'root-id'),
 
2849
                    ('file', 'file-id')],
 
2850
            target=[('', 'target-root-id'),
 
2851
                    ('file', 'file-id')])
 
2852
        state = self.assertUpdate( # same as target
 
2853
            active=[('', 'target-root-id'),
 
2854
                    ('file', 'file-id')],
 
2855
            basis= [('', 'root-id'),
 
2856
                    ('file', 'file-id')],
 
2857
            target=[('', 'target-root-id'),
 
2858
                    ('file', 'root-id')])
 
2859
        state = self.assertUpdate( # all different
 
2860
            active=[('', 'active-root-id'),
 
2861
                    ('file', 'file-id')],
 
2862
            basis= [('', 'root-id'),
 
2863
                    ('file', 'file-id')],
 
2864
            target=[('', 'target-root-id'),
 
2865
                    ('file', 'root-id')])
 
2866
 
 
2867
    def test_change_file_absent_in_active(self):
 
2868
        state = self.assertUpdate(
 
2869
            active=[],
 
2870
            basis= [('file', 'file-id')],
 
2871
            target=[('file', 'file-id')])
 
2872
 
 
2873
    def test_invalid_changed_file(self):
 
2874
        state = self.assertBadDelta( # Not present in basis
 
2875
            active=[('file', 'file-id')],
 
2876
            basis= [],
 
2877
            delta=[('file', 'file', 'file-id')])
 
2878
        state = self.assertBadDelta( # present at another location in basis
 
2879
            active=[('file', 'file-id')],
 
2880
            basis= [('other-file', 'file-id')],
 
2881
            delta=[('file', 'file', 'file-id')])