~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Robert Collins
  • Date: 2007-08-06 23:49:18 UTC
  • mto: (2592.3.81 repository)
  • mto: This revision was merged to the branch mainline in revision 2933.
  • Revision ID: robertc@robertcollins.net-20070806234918-xc9w5f86tgjphf9u
Prevent the duplicate additions of names to FileNames collections.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 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
19
import bisect
20
20
import os
 
21
import time
21
22
 
22
23
from bzrlib import (
23
24
    dirstate,
24
25
    errors,
25
 
    inventory,
26
 
    memorytree,
27
26
    osutils,
28
 
    revision as _mod_revision,
29
 
    tests,
30
27
    )
31
 
from bzrlib.tests import test_osutils
 
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
        )
32
35
 
33
36
 
34
37
# TODO:
44
47
# set_path_id  setting id when state is in memory modified
45
48
 
46
49
 
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
55
 
 
56
 
 
57
 
class TestCaseWithDirState(tests.TestCaseWithTransport):
 
50
class TestCaseWithDirState(TestCaseWithTransport):
58
51
    """Helper functions for creating DirState objects with various content."""
59
52
 
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
 
        # 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()
75
 
 
76
53
    def create_empty_dirstate(self):
77
54
        """Return a locked but empty dirstate"""
78
55
        state = dirstate.DirState.initialize('dirstate')
186
163
        """
187
164
        # The state should already be write locked, since we just had to do
188
165
        # some operation to get here.
189
 
        self.assertTrue(state._lock_token is not None)
 
166
        assert state._lock_token is not None
190
167
        try:
191
168
            self.assertEqual(expected_result[0],  state.get_parent_ids())
192
169
            # there should be no ghosts in this tree.
383
360
        # There are no files on disk and no parents
384
361
        tree = self.make_branch_and_tree('tree')
385
362
        expected_result = ([], [
386
 
            (('', '', tree.get_root_id()), # common details
 
363
            (('', '', tree.path2id('')), # common details
387
364
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
388
365
             ])])
389
366
        state = dirstate.DirState.from_tree(tree, 'dirstate')
396
373
        rev_id = tree.commit('first post').encode('utf8')
397
374
        root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
398
375
        expected_result = ([rev_id], [
399
 
            (('', '', tree.get_root_id()), # common details
 
376
            (('', '', tree.path2id('')), # common details
400
377
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
401
378
              ('d', '', 0, False, rev_id), # first parent details
402
379
             ])])
416
393
        rev_id2 = tree2.commit('second post', allow_pointless=True)
417
394
        tree.merge_from_branch(tree2.branch)
418
395
        expected_result = ([rev_id, rev_id2], [
419
 
            (('', '', tree.get_root_id()), # common details
 
396
            (('', '', tree.path2id('')), # common details
420
397
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
421
398
              ('d', '', 0, False, rev_id), # first parent details
422
 
              ('d', '', 0, False, rev_id), # second parent details
 
399
              ('d', '', 0, False, rev_id2), # second parent details
423
400
             ])])
424
401
        state = dirstate.DirState.from_tree(tree, 'dirstate')
425
402
        self.check_state_with_reopen(expected_result, state)
435
412
        tree = self.make_branch_and_tree('tree')
436
413
        self.build_tree(['tree/unknown'])
437
414
        expected_result = ([], [
438
 
            (('', '', tree.get_root_id()), # common details
 
415
            (('', '', tree.path2id('')), # common details
439
416
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
440
417
             ])])
441
418
        state = dirstate.DirState.from_tree(tree, 'dirstate')
444
421
    def get_tree_with_a_file(self):
445
422
        tree = self.make_branch_and_tree('tree')
446
423
        self.build_tree(['tree/a file'])
447
 
        tree.add('a file', 'a-file-id')
 
424
        tree.add('a file', 'a file id')
448
425
        return tree
449
426
 
450
427
    def test_non_empty_no_parents_to_dirstate(self):
452
429
        # There are files on disk and no parents
453
430
        tree = self.get_tree_with_a_file()
454
431
        expected_result = ([], [
455
 
            (('', '', tree.get_root_id()), # common details
 
432
            (('', '', tree.path2id('')), # common details
456
433
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
457
434
             ]),
458
 
            (('', 'a file', 'a-file-id'), # common
 
435
            (('', 'a file', 'a file id'), # common
459
436
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
460
437
             ]),
461
438
            ])
470
447
        # and length:
471
448
        self.build_tree_contents([('tree/a file', 'new content\n')])
472
449
        expected_result = ([rev_id], [
473
 
            (('', '', tree.get_root_id()), # common details
 
450
            (('', '', tree.path2id('')), # common details
474
451
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
475
452
              ('d', '', 0, False, rev_id), # first parent details
476
453
             ]),
477
 
            (('', 'a file', 'a-file-id'), # common
 
454
            (('', 'a file', 'a file id'), # common
478
455
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
479
456
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
480
457
               rev_id), # first parent
497
474
        # and length again, giving us three distinct values:
498
475
        self.build_tree_contents([('tree/a file', 'new content\n')])
499
476
        expected_result = ([rev_id, rev_id2], [
500
 
            (('', '', tree.get_root_id()), # common details
 
477
            (('', '', tree.path2id('')), # common details
501
478
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
502
479
              ('d', '', 0, False, rev_id), # first parent details
503
 
              ('d', '', 0, False, rev_id), # second parent details
 
480
              ('d', '', 0, False, rev_id2), # second parent details
504
481
             ]),
505
 
            (('', 'a file', 'a-file-id'), # common
 
482
            (('', 'a file', 'a file id'), # common
506
483
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
507
484
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
508
485
               rev_id), # first parent
549
526
        # get a state object
550
527
        # no parents, default tree content
551
528
        expected_result = ([], [
552
 
            (('', '', tree.get_root_id()), # common details
 
529
            (('', '', tree.path2id('')), # common details
553
530
             # current tree details, but new from_tree skips statting, it
554
531
             # uses set_state_from_inventory, and thus depends on the
555
532
             # inventory state.
586
563
        state.lock_read()
587
564
        try:
588
565
            entry = state._get_entry(0, path_utf8='a-file')
589
 
            # The current size should be 0 (default)
590
 
            self.assertEqual(0, entry[1][0][2])
 
566
            # The current sha1 sum should be empty
 
567
            self.assertEqual('', entry[1][0][1])
591
568
            # We should have a real entry.
592
569
            self.assertNotEqual((None, None), entry)
593
570
            # Make sure everything is old enough
594
571
            state._sha_cutoff_time()
595
572
            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)
 
573
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
574
            # We should have gotten a real sha1
 
575
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
576
                             sha1sum)
602
577
 
603
578
            # The dirblock has been updated
604
 
            self.assertEqual(7, entry[1][0][2])
 
579
            self.assertEqual(sha1sum, entry[1][0][1])
605
580
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
606
581
                             state._dirblock_state)
607
582
 
617
592
        state.lock_read()
618
593
        try:
619
594
            entry = state._get_entry(0, path_utf8='a-file')
620
 
            self.assertEqual(7, entry[1][0][2])
 
595
            self.assertEqual(sha1sum, entry[1][0][1])
621
596
        finally:
622
597
            state.unlock()
623
598
 
636
611
        state.lock_read()
637
612
        try:
638
613
            entry = state._get_entry(0, path_utf8='a-file')
639
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
640
 
                os.lstat('a-file'))
641
 
            # No sha - too new
642
 
            self.assertEqual(None, sha1sum)
 
614
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
615
            # We should have gotten a real sha1
 
616
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
617
                             sha1sum)
643
618
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
644
619
                             state._dirblock_state)
645
620
 
662
637
                state2.unlock()
663
638
        finally:
664
639
            state.unlock()
665
 
 
 
640
        
666
641
        # The file on disk should not be modified.
667
642
        state = dirstate.DirState.on_file('dirstate')
668
643
        state.lock_read()
672
647
        finally:
673
648
            state.unlock()
674
649
 
675
 
    def test_save_refuses_if_changes_aborted(self):
676
 
        self.build_tree(['a-file', 'a-dir/'])
677
 
        state = dirstate.DirState.initialize('dirstate')
678
 
        try:
679
 
            # No stat and no sha1 sum.
680
 
            state.add('a-file', 'a-file-id', 'file', None, '')
681
 
            state.save()
682
 
        finally:
683
 
            state.unlock()
684
 
 
685
 
        # The dirstate should include TREE_ROOT and 'a-file' and nothing else
686
 
        expected_blocks = [
687
 
            ('', [(('', '', 'TREE_ROOT'),
688
 
                   [('d', '', 0, False, dirstate.DirState.NULLSTAT)])]),
689
 
            ('', [(('', 'a-file', 'a-file-id'),
690
 
                   [('f', '', 0, False, dirstate.DirState.NULLSTAT)])]),
691
 
        ]
692
 
 
693
 
        state = dirstate.DirState.on_file('dirstate')
694
 
        state.lock_write()
695
 
        try:
696
 
            state._read_dirblocks_if_needed()
697
 
            self.assertEqual(expected_blocks, state._dirblocks)
698
 
 
699
 
            # Now modify the state, but mark it as inconsistent
700
 
            state.add('a-dir', 'a-dir-id', 'directory', None, '')
701
 
            state._changes_aborted = True
702
 
            state.save()
703
 
        finally:
704
 
            state.unlock()
705
 
 
706
 
        state = dirstate.DirState.on_file('dirstate')
707
 
        state.lock_read()
708
 
        try:
709
 
            state._read_dirblocks_if_needed()
710
 
            self.assertEqual(expected_blocks, state._dirblocks)
711
 
        finally:
712
 
            state.unlock()
713
 
 
714
650
 
715
651
class TestDirStateInitialize(TestCaseWithDirState):
716
652
 
743
679
        try:
744
680
            tree1.add('')
745
681
            revid1 = tree1.commit('foo').encode('utf8')
746
 
            root_id = tree1.get_root_id()
 
682
            root_id = tree1.inventory.root.file_id
747
683
            inv = tree1.inventory
748
684
        finally:
749
685
            tree1.unlock()
764
700
            # This will unlock it
765
701
            self.check_state_with_reopen(expected_result, state)
766
702
 
767
 
    def test_set_state_from_inventory_preserves_hashcache(self):
768
 
        # https://bugs.launchpad.net/bzr/+bug/146176
769
 
        # set_state_from_inventory should preserve the stat and hash value for
770
 
        # workingtree files that are not changed by the inventory.
771
 
 
772
 
        tree = self.make_branch_and_tree('.')
773
 
        # depends on the default format using dirstate...
774
 
        tree.lock_write()
775
 
        try:
776
 
            # make a dirstate with some valid hashcache data
777
 
            # file on disk, but that's not needed for this test
778
 
            foo_contents = 'contents of foo'
779
 
            self.build_tree_contents([('foo', foo_contents)])
780
 
            tree.add('foo', 'foo-id')
781
 
 
782
 
            foo_stat = os.stat('foo')
783
 
            foo_packed = dirstate.pack_stat(foo_stat)
784
 
            foo_sha = osutils.sha_string(foo_contents)
785
 
            foo_size = len(foo_contents)
786
 
 
787
 
            # should not be cached yet, because the file's too fresh
788
 
            self.assertEqual(
789
 
                (('', 'foo', 'foo-id',),
790
 
                 [('f', '', 0, False, dirstate.DirState.NULLSTAT)]),
791
 
                tree._dirstate._get_entry(0, 'foo-id'))
792
 
            # poke in some hashcache information - it wouldn't normally be
793
 
            # stored because it's too fresh
794
 
            tree._dirstate.update_minimal(
795
 
                ('', 'foo', 'foo-id'),
796
 
                'f', False, foo_sha, foo_packed, foo_size, 'foo')
797
 
            # now should be cached
798
 
            self.assertEqual(
799
 
                (('', 'foo', 'foo-id',),
800
 
                 [('f', foo_sha, foo_size, False, foo_packed)]),
801
 
                tree._dirstate._get_entry(0, 'foo-id'))
802
 
 
803
 
            # extract the inventory, and add something to it
804
 
            inv = tree._get_inventory()
805
 
            # should see the file we poked in...
806
 
            self.assertTrue(inv.has_id('foo-id'))
807
 
            self.assertTrue(inv.has_filename('foo'))
808
 
            inv.add_path('bar', 'file', 'bar-id')
809
 
            tree._dirstate._validate()
810
 
            # this used to cause it to lose its hashcache
811
 
            tree._dirstate.set_state_from_inventory(inv)
812
 
            tree._dirstate._validate()
813
 
        finally:
814
 
            tree.unlock()
815
 
 
816
 
        tree.lock_read()
817
 
        try:
818
 
            # now check that the state still has the original hashcache value
819
 
            state = tree._dirstate
820
 
            state._validate()
821
 
            foo_tuple = state._get_entry(0, path_utf8='foo')
822
 
            self.assertEqual(
823
 
                (('', 'foo', 'foo-id',),
824
 
                 [('f', foo_sha, len(foo_contents), False,
825
 
                   dirstate.pack_stat(foo_stat))]),
826
 
                foo_tuple)
827
 
        finally:
828
 
            tree.unlock()
829
 
 
830
703
    def test_set_state_from_inventory_mixed_paths(self):
831
704
        tree1 = self.make_branch_and_tree('tree1')
832
705
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
873
746
        state = dirstate.DirState.initialize('dirstate')
874
747
        try:
875
748
            # check precondition to be sure the state does change appropriately.
876
 
            root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
877
 
            self.assertEqual([root_entry], list(state._iter_entries()))
878
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
879
 
            self.assertEqual(root_entry,
880
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
881
 
            self.assertEqual((None, None),
882
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
883
 
            state.set_path_id('', 'second-root-id')
884
 
            new_root_entry = (('', '', 'second-root-id'),
885
 
                              [('d', '', 0, False, 'x'*32)])
886
 
            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')])]
887
757
            self.assertEqual(expected_rows, list(state._iter_entries()))
888
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
889
 
            self.assertEqual(new_root_entry, 
890
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
891
 
            self.assertEqual((None, None),
892
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
893
758
            # should work across save too
894
759
            state.save()
895
760
        finally:
913
778
        state._validate()
914
779
        try:
915
780
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
916
 
            root_entry = (('', '', 'TREE_ROOT'),
917
 
                          [('d', '', 0, False, 'x'*32),
918
 
                           ('d', '', 0, False, 'parent-revid')])
919
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
920
 
            self.assertEqual(root_entry,
921
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
922
 
            self.assertEqual((None, None),
923
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
924
 
            state.set_path_id('', 'Asecond-root-id')
 
781
            state.set_path_id('', 'foobarbaz')
925
782
            state._validate()
926
783
            # now see that it is what we expected
927
 
            old_root_entry = (('', '', 'TREE_ROOT'),
928
 
                              [('a', '', 0, False, ''),
929
 
                               ('d', '', 0, False, 'parent-revid')])
930
 
            new_root_entry = (('', '', 'Asecond-root-id'),
931
 
                              [('d', '', 0, False, ''),
932
 
                               ('a', '', 0, False, '')])
933
 
            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
                ]
934
794
            state._validate()
935
795
            self.assertEqual(expected_rows, list(state._iter_entries()))
936
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
937
 
            self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
938
 
            self.assertEqual((None, None),
939
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
940
 
            self.assertEqual(old_root_entry,
941
 
                             state._get_entry(1, fileid_utf8='TREE_ROOT'))
942
 
            self.assertEqual(new_root_entry,
943
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
944
 
            self.assertEqual((None, None),
945
 
                             state._get_entry(1, fileid_utf8='Asecond-root-id'))
946
796
            # should work across save too
947
797
            state.save()
948
798
        finally:
964
814
        finally:
965
815
            state.unlock()
966
816
 
 
817
 
967
818
    def test_set_parent_trees_no_content(self):
968
819
        # set_parent_trees is a slow but important api to support.
969
820
        tree1 = self.make_branch_and_memory_tree('tree1')
974
825
        finally:
975
826
            tree1.unlock()
976
827
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
977
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
828
        tree2 = MemoryTree.create_on_branch(branch2)
978
829
        tree2.lock_write()
979
830
        try:
980
831
            revid2 = tree2.commit('foo')
981
 
            root_id = tree2.get_root_id()
 
832
            root_id = tree2.inventory.root.file_id
982
833
        finally:
983
834
            tree2.unlock()
984
835
        state = dirstate.DirState.initialize('dirstate')
1012
863
            state.set_parent_trees(
1013
864
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
1014
865
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
1015
 
                 ('ghost-rev', tree2.branch.repository.revision_tree(
1016
 
                                   _mod_revision.NULL_REVISION))),
 
866
                 ('ghost-rev', tree2.branch.repository.revision_tree(None))),
1017
867
                ['ghost-rev'])
1018
868
            self.assertEqual([revid1, revid2, 'ghost-rev'],
1019
869
                             state.get_parent_ids())
1023
873
                [(('', '', root_id), [
1024
874
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1025
875
                  ('d', '', 0, False, revid1),
1026
 
                  ('d', '', 0, False, revid1)
 
876
                  ('d', '', 0, False, revid2)
1027
877
                  ])],
1028
878
                list(state._iter_entries()))
1029
879
        finally:
1044
894
        finally:
1045
895
            tree1.unlock()
1046
896
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1047
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
897
        tree2 = MemoryTree.create_on_branch(branch2)
1048
898
        tree2.lock_write()
1049
899
        try:
1050
900
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1051
901
            revid2 = tree2.commit('foo')
1052
 
            root_id = tree2.get_root_id()
 
902
            root_id = tree2.inventory.root.file_id
1053
903
        finally:
1054
904
            tree2.unlock()
1055
905
        # check the layout in memory
1057
907
            (('', '', root_id), [
1058
908
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1059
909
             ('d', '', 0, False, revid1.encode('utf8')),
1060
 
             ('d', '', 0, False, revid1.encode('utf8'))
 
910
             ('d', '', 0, False, revid2.encode('utf8'))
1061
911
             ]),
1062
912
            (('', 'a file', 'file-id'), [
1063
913
             ('a', '', 0, False, ''),
1095
945
            (('', '', 'TREE_ROOT'), [
1096
946
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1097
947
             ]),
1098
 
            (('', 'a file', 'a-file-id'), [
 
948
            (('', 'a file', 'a file id'), [
1099
949
             ('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
1100
950
             ]),
1101
951
            ]
1102
952
        try:
1103
 
            state.add('a file', 'a-file-id', 'file', stat, '1'*20)
 
953
            state.add('a file', 'a file id', 'file', stat, '1'*20)
1104
954
            # having added it, it should be in the output of iter_entries.
1105
955
            self.assertEqual(expected_entries, list(state._iter_entries()))
1106
956
            # saving and reloading should not affect this.
1109
959
            state.unlock()
1110
960
        state = dirstate.DirState.on_file('dirstate')
1111
961
        state.lock_read()
1112
 
        self.addCleanup(state.unlock)
1113
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
962
        try:
 
963
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
964
        finally:
 
965
            state.unlock()
1114
966
 
1115
967
    def test_add_path_to_unversioned_directory(self):
1116
968
        """Adding a path to an unversioned directory should error.
1121
973
        """
1122
974
        self.build_tree(['unversioned/', 'unversioned/a file'])
1123
975
        state = dirstate.DirState.initialize('dirstate')
1124
 
        self.addCleanup(state.unlock)
1125
 
        self.assertRaises(errors.NotVersionedError, state.add,
1126
 
                          '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()
1127
981
 
1128
982
    def test_add_directory_to_root_no_parents_all_data(self):
1129
983
        # The most trivial addition of a dir is when there are no parents and
1149
1003
            state.unlock()
1150
1004
        state = dirstate.DirState.on_file('dirstate')
1151
1005
        state.lock_read()
1152
 
        self.addCleanup(state.unlock)
1153
1006
        state._validate()
1154
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1007
        try:
 
1008
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1009
        finally:
 
1010
            state.unlock()
1155
1011
 
1156
 
    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):
1157
1013
        # The most trivial addition of a symlink when there are no parents and
1158
1014
        # its in the root and all data about the file is supplied
1159
1015
        # bzr doesn't support fake symlinks on windows, yet.
1160
 
        self.requireFeature(tests.SymlinkFeature)
1161
 
        os.symlink(target, link_name)
1162
 
        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')
1163
1020
        expected_entries = [
1164
1021
            (('', '', 'TREE_ROOT'), [
1165
1022
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1166
1023
             ]),
1167
 
            (('', link_name.encode('UTF-8'), 'a link id'), [
1168
 
             ('l', target.encode('UTF-8'), stat[6],
1169
 
              False, dirstate.pack_stat(stat)), # current tree
 
1024
            (('', 'a link', 'a link id'), [
 
1025
             ('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1170
1026
             ]),
1171
1027
            ]
1172
1028
        state = dirstate.DirState.initialize('dirstate')
1173
1029
        try:
1174
 
            state.add(link_name, 'a link id', 'symlink', stat,
1175
 
                      target.encode('UTF-8'))
 
1030
            state.add('a link', 'a link id', 'symlink', stat, 'target')
1176
1031
            # having added it, it should be in the output of iter_entries.
1177
1032
            self.assertEqual(expected_entries, list(state._iter_entries()))
1178
1033
            # saving and reloading should not affect this.
1181
1036
            state.unlock()
1182
1037
        state = dirstate.DirState.on_file('dirstate')
1183
1038
        state.lock_read()
1184
 
        self.addCleanup(state.unlock)
1185
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
1186
 
 
1187
 
    def test_add_symlink_to_root_no_parents_all_data(self):
1188
 
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1189
 
 
1190
 
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1191
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1192
 
        self._test_add_symlink_to_root_no_parents_all_data(
1193
 
            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()
1194
1043
 
1195
1044
    def test_add_directory_and_child_no_parents_all_data(self):
1196
1045
        # after adding a directory, we should be able to add children to it.
1204
1053
            (('', 'a dir', 'a dir id'), [
1205
1054
             ('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
1206
1055
             ]),
1207
 
            (('a dir', 'a file', 'a-file-id'), [
 
1056
            (('a dir', 'a file', 'a file id'), [
1208
1057
             ('f', '1'*20, 25, False,
1209
1058
              dirstate.pack_stat(filestat)), # current tree details
1210
1059
             ]),
1212
1061
        state = dirstate.DirState.initialize('dirstate')
1213
1062
        try:
1214
1063
            state.add('a dir', 'a dir id', 'directory', dirstat, None)
1215
 
            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)
1216
1065
            # added it, it should be in the output of iter_entries.
1217
1066
            self.assertEqual(expected_entries, list(state._iter_entries()))
1218
1067
            # saving and reloading should not affect this.
1221
1070
            state.unlock()
1222
1071
        state = dirstate.DirState.on_file('dirstate')
1223
1072
        state.lock_read()
1224
 
        self.addCleanup(state.unlock)
1225
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1073
        try:
 
1074
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1075
        finally:
 
1076
            state.unlock()
1226
1077
 
1227
1078
    def test_add_tree_reference(self):
1228
1079
        # make a dirstate and add a tree reference
1242
1093
            state.unlock()
1243
1094
        # now check we can read it back
1244
1095
        state.lock_read()
1245
 
        self.addCleanup(state.unlock)
1246
1096
        state._validate()
1247
 
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1248
 
        self.assertEqual(entry, entry2)
1249
 
        self.assertEqual(entry, expected_entry)
1250
 
        # and lookup by id should work too
1251
 
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1252
 
        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()
1253
1106
 
1254
1107
    def test_add_forbidden_names(self):
1255
1108
        state = dirstate.DirState.initialize('dirstate')
1259
1112
        self.assertRaises(errors.BzrError,
1260
1113
            state.add, '..', 'ass-id', 'directory', None, None)
1261
1114
 
1262
 
    def test_set_state_with_rename_b_a_bug_395556(self):
1263
 
        # bug 395556 uncovered a bug where the dirstate ends up with a false
1264
 
        # relocation record - in a tree with no parents there should be no
1265
 
        # absent or relocated records. This then leads to further corruption
1266
 
        # when a commit occurs, as the incorrect relocation gathers an
1267
 
        # incorrect absent in tree 1, and future changes go to pot.
1268
 
        tree1 = self.make_branch_and_tree('tree1')
1269
 
        self.build_tree(['tree1/b'])
1270
 
        tree1.lock_write()
1271
 
        try:
1272
 
            tree1.add(['b'], ['b-id'])
1273
 
            root_id = tree1.get_root_id()
1274
 
            inv = tree1.inventory
1275
 
            state = dirstate.DirState.initialize('dirstate')
1276
 
            try:
1277
 
                # Set the initial state with 'b'
1278
 
                state.set_state_from_inventory(inv)
1279
 
                inv.rename('b-id', root_id, 'a')
1280
 
                # Set the new state with 'a', which currently corrupts.
1281
 
                state.set_state_from_inventory(inv)
1282
 
                expected_result1 = [('', '', root_id, 'd'),
1283
 
                                    ('', 'a', 'b-id', 'f'),
1284
 
                                   ]
1285
 
                values = []
1286
 
                for entry in state._iter_entries():
1287
 
                    values.append(entry[0] + entry[1][0][:1])
1288
 
                self.assertEqual(expected_result1, values)
1289
 
            finally:
1290
 
                state.unlock()
1291
 
        finally:
1292
 
            tree1.unlock()
1293
 
 
1294
1115
 
1295
1116
class TestGetLines(TestCaseWithDirState):
1296
1117
 
1509
1330
            state.unlock()
1510
1331
 
1511
1332
 
1512
 
class TestIterChildEntries(TestCaseWithDirState):
1513
 
 
1514
 
    def create_dirstate_with_two_trees(self):
1515
 
        """This dirstate contains multiple files and directories.
1516
 
 
1517
 
         /        a-root-value
1518
 
         a/       a-dir
1519
 
         b/       b-dir
1520
 
         c        c-file
1521
 
         d        d-file
1522
 
         a/e/     e-dir
1523
 
         a/f      f-file
1524
 
         b/g      g-file
1525
 
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
1526
 
 
1527
 
        Notice that a/e is an empty directory.
1528
 
 
1529
 
        There is one parent tree, which has the same shape with the following variations:
1530
 
        b/g in the parent is gone.
1531
 
        b/h in the parent has a different id
1532
 
        b/i is new in the parent
1533
 
        c is renamed to b/j in the parent
1534
 
 
1535
 
        :return: The dirstate, still write-locked.
1536
 
        """
1537
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1538
 
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
1539
 
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
1540
 
        root_entry = ('', '', 'a-root-value'), [
1541
 
            ('d', '', 0, False, packed_stat),
1542
 
            ('d', '', 0, False, 'parent-revid'),
1543
 
            ]
1544
 
        a_entry = ('', 'a', 'a-dir'), [
1545
 
            ('d', '', 0, False, packed_stat),
1546
 
            ('d', '', 0, False, 'parent-revid'),
1547
 
            ]
1548
 
        b_entry = ('', 'b', 'b-dir'), [
1549
 
            ('d', '', 0, False, packed_stat),
1550
 
            ('d', '', 0, False, 'parent-revid'),
1551
 
            ]
1552
 
        c_entry = ('', 'c', 'c-file'), [
1553
 
            ('f', null_sha, 10, False, packed_stat),
1554
 
            ('r', 'b/j', 0, False, ''),
1555
 
            ]
1556
 
        d_entry = ('', 'd', 'd-file'), [
1557
 
            ('f', null_sha, 20, False, packed_stat),
1558
 
            ('f', 'd', 20, False, 'parent-revid'),
1559
 
            ]
1560
 
        e_entry = ('a', 'e', 'e-dir'), [
1561
 
            ('d', '', 0, False, packed_stat),
1562
 
            ('d', '', 0, False, 'parent-revid'),
1563
 
            ]
1564
 
        f_entry = ('a', 'f', 'f-file'), [
1565
 
            ('f', null_sha, 30, False, packed_stat),
1566
 
            ('f', 'f', 20, False, 'parent-revid'),
1567
 
            ]
1568
 
        g_entry = ('b', 'g', 'g-file'), [
1569
 
            ('f', null_sha, 30, False, packed_stat),
1570
 
            NULL_PARENT_DETAILS,
1571
 
            ]
1572
 
        h_entry1 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file1'), [
1573
 
            ('f', null_sha, 40, False, packed_stat),
1574
 
            NULL_PARENT_DETAILS,
1575
 
            ]
1576
 
        h_entry2 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file2'), [
1577
 
            NULL_PARENT_DETAILS,
1578
 
            ('f', 'h', 20, False, 'parent-revid'),
1579
 
            ]
1580
 
        i_entry = ('b', 'i', 'i-file'), [
1581
 
            NULL_PARENT_DETAILS,
1582
 
            ('f', 'h', 20, False, 'parent-revid'),
1583
 
            ]
1584
 
        j_entry = ('b', 'j', 'c-file'), [
1585
 
            ('r', 'c', 0, False, ''),
1586
 
            ('f', 'j', 20, False, 'parent-revid'),
1587
 
            ]
1588
 
        dirblocks = []
1589
 
        dirblocks.append(('', [root_entry]))
1590
 
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
1591
 
        dirblocks.append(('a', [e_entry, f_entry]))
1592
 
        dirblocks.append(('b', [g_entry, h_entry1, h_entry2, i_entry, j_entry]))
1593
 
        state = dirstate.DirState.initialize('dirstate')
1594
 
        state._validate()
1595
 
        try:
1596
 
            state._set_data(['parent'], dirblocks)
1597
 
        except:
1598
 
            state.unlock()
1599
 
            raise
1600
 
        return state, dirblocks
1601
 
 
1602
 
    def test_iter_children_b(self):
1603
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1604
 
        self.addCleanup(state.unlock)
1605
 
        expected_result = []
1606
 
        expected_result.append(dirblocks[3][1][2]) # h2
1607
 
        expected_result.append(dirblocks[3][1][3]) # i
1608
 
        expected_result.append(dirblocks[3][1][4]) # j
1609
 
        self.assertEqual(expected_result,
1610
 
            list(state._iter_child_entries(1, 'b')))
1611
 
 
1612
 
    def test_iter_child_root(self):
1613
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1614
 
        self.addCleanup(state.unlock)
1615
 
        expected_result = []
1616
 
        expected_result.append(dirblocks[1][1][0]) # a
1617
 
        expected_result.append(dirblocks[1][1][1]) # b
1618
 
        expected_result.append(dirblocks[1][1][3]) # d
1619
 
        expected_result.append(dirblocks[2][1][0]) # e
1620
 
        expected_result.append(dirblocks[2][1][1]) # f
1621
 
        expected_result.append(dirblocks[3][1][2]) # h2
1622
 
        expected_result.append(dirblocks[3][1][3]) # i
1623
 
        expected_result.append(dirblocks[3][1][4]) # j
1624
 
        self.assertEqual(expected_result,
1625
 
            list(state._iter_child_entries(1, '')))
1626
 
 
1627
 
 
1628
 
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1333
class TestDirstateSortOrder(TestCaseWithTransport):
1629
1334
    """Test that DirState adds entries in the right order."""
1630
1335
 
1631
1336
    def test_add_sorting(self):
1680
1385
 
1681
1386
        # *really* cheesy way to just get an empty tree
1682
1387
        repo = self.make_repository('repo')
1683
 
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1388
        empty_tree = repo.revision_tree(None)
1684
1389
        state.set_parent_trees([('null:', empty_tree)], [])
1685
1390
 
1686
1391
        dirblock_names = [d[0] for d in state._dirblocks]
1690
1395
class InstrumentedDirState(dirstate.DirState):
1691
1396
    """An DirState with instrumented sha1 functionality."""
1692
1397
 
1693
 
    def __init__(self, path, sha1_provider):
1694
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
 
1398
    def __init__(self, path):
 
1399
        super(InstrumentedDirState, self).__init__(path)
1695
1400
        self._time_offset = 0
1696
1401
        self._log = []
1697
 
        # member is dynamically set in DirState.__init__ to turn on trace
1698
 
        self._sha1_provider = sha1_provider
1699
 
        self._sha1_file = self._sha1_file_and_log
1700
1402
 
1701
1403
    def _sha_cutoff_time(self):
1702
1404
        timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
1703
1405
        self._cutoff_time = timestamp + self._time_offset
1704
1406
 
1705
 
    def _sha1_file_and_log(self, abspath):
 
1407
    def _sha1_file(self, abspath, entry):
1706
1408
        self._log.append(('sha1', abspath))
1707
 
        return self._sha1_provider.sha1(abspath)
 
1409
        return super(InstrumentedDirState, self)._sha1_file(abspath, entry)
1708
1410
 
1709
1411
    def _read_link(self, abspath, old_link):
1710
1412
        self._log.append(('read_link', abspath, old_link))
1741
1443
        self.st_ino = ino
1742
1444
        self.st_mode = mode
1743
1445
 
1744
 
    @staticmethod
1745
 
    def from_stat(st):
1746
 
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1747
 
            st.st_ino, st.st_mode)
1748
 
 
1749
 
 
1750
 
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):
1751
1776
 
1752
1777
    def assertPackStat(self, expected, stat_value):
1753
1778
        """Check the packed and serialized form of a stat value."""
1818
1843
        # the end it would still be fairly arbitrary, and we don't want the
1819
1844
        # extra overhead if we can avoid it. So sort everything to make sure
1820
1845
        # equality is true
1821
 
        self.assertEqual(len(map_keys), len(paths))
 
1846
        assert len(map_keys) == len(paths)
1822
1847
        expected = {}
1823
1848
        for path, keys in zip(paths, map_keys):
1824
1849
            if keys is None:
1843
1868
        :param paths: A list of directories
1844
1869
        """
1845
1870
        result = state._bisect_dirblocks(paths)
1846
 
        self.assertEqual(len(map_keys), len(paths))
 
1871
        assert len(map_keys) == len(paths)
 
1872
 
1847
1873
        expected = {}
1848
1874
        for path, keys in zip(paths, map_keys):
1849
1875
            if keys is None:
2094
2120
        self.assertContainsRe(str(e),
2095
2121
            'file a-id is absent in row')
2096
2122
 
2097
 
 
2098
 
class TestDirstateTreeReference(TestCaseWithDirState):
2099
 
 
2100
 
    def test_reference_revision_is_none(self):
2101
 
        tree = self.make_branch_and_tree('tree', format='dirstate-with-subtree')
2102
 
        subtree = self.make_branch_and_tree('tree/subtree',
2103
 
                            format='dirstate-with-subtree')
2104
 
        subtree.set_root_id('subtree')
2105
 
        tree.add_reference(subtree)
2106
 
        tree.add('subtree')
2107
 
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2108
 
        key = ('', 'subtree', 'subtree')
2109
 
        expected = ('', [(key,
2110
 
            [('t', '', 0, False, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])])
2111
 
 
2112
 
        try:
2113
 
            self.assertEqual(expected, state._find_block(key))
2114
 
        finally:
2115
 
            state.unlock()
2116
 
 
2117
 
 
2118
 
class TestDiscardMergeParents(TestCaseWithDirState):
2119
 
 
2120
 
    def test_discard_no_parents(self):
2121
 
        # This should be a no-op
2122
 
        state = self.create_empty_dirstate()
2123
 
        self.addCleanup(state.unlock)
2124
 
        state._discard_merge_parents()
2125
 
        state._validate()
2126
 
 
2127
 
    def test_discard_one_parent(self):
2128
 
        # No-op
2129
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2130
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2131
 
            ('d', '', 0, False, packed_stat),
2132
 
            ('d', '', 0, False, packed_stat),
2133
 
            ]
2134
 
        dirblocks = []
2135
 
        dirblocks.append(('', [root_entry_direntry]))
2136
 
        dirblocks.append(('', []))
2137
 
 
2138
 
        state = self.create_empty_dirstate()
2139
 
        self.addCleanup(state.unlock)
2140
 
        state._set_data(['parent-id'], dirblocks[:])
2141
 
        state._validate()
2142
 
 
2143
 
        state._discard_merge_parents()
2144
 
        state._validate()
2145
 
        self.assertEqual(dirblocks, state._dirblocks)
2146
 
 
2147
 
    def test_discard_simple(self):
2148
 
        # No-op
2149
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2150
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2151
 
            ('d', '', 0, False, packed_stat),
2152
 
            ('d', '', 0, False, packed_stat),
2153
 
            ('d', '', 0, False, packed_stat),
2154
 
            ]
2155
 
        expected_root_entry_direntry = ('', '', 'a-root-value'), [
2156
 
            ('d', '', 0, False, packed_stat),
2157
 
            ('d', '', 0, False, packed_stat),
2158
 
            ]
2159
 
        dirblocks = []
2160
 
        dirblocks.append(('', [root_entry_direntry]))
2161
 
        dirblocks.append(('', []))
2162
 
 
2163
 
        state = self.create_empty_dirstate()
2164
 
        self.addCleanup(state.unlock)
2165
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2166
 
        state._validate()
2167
 
 
2168
 
        # This should strip of the extra column
2169
 
        state._discard_merge_parents()
2170
 
        state._validate()
2171
 
        expected_dirblocks = [('', [expected_root_entry_direntry]), ('', [])]
2172
 
        self.assertEqual(expected_dirblocks, state._dirblocks)
2173
 
 
2174
 
    def test_discard_absent(self):
2175
 
        """If entries are only in a merge, discard should remove the entries"""
2176
 
        null_stat = dirstate.DirState.NULLSTAT
2177
 
        present_dir = ('d', '', 0, False, null_stat)
2178
 
        present_file = ('f', '', 0, False, null_stat)
2179
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2180
 
        root_key = ('', '', 'a-root-value')
2181
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2182
 
        file_in_merged_key = ('', 'file-in-merged', 'b-file-id')
2183
 
        dirblocks = [('', [(root_key, [present_dir, present_dir, present_dir])]),
2184
 
                     ('', [(file_in_merged_key,
2185
 
                            [absent, absent, present_file]),
2186
 
                           (file_in_root_key,
2187
 
                            [present_file, present_file, present_file]),
2188
 
                          ]),
2189
 
                    ]
2190
 
 
2191
 
        state = self.create_empty_dirstate()
2192
 
        self.addCleanup(state.unlock)
2193
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2194
 
        state._validate()
2195
 
 
2196
 
        exp_dirblocks = [('', [(root_key, [present_dir, present_dir])]),
2197
 
                         ('', [(file_in_root_key,
2198
 
                                [present_file, present_file]),
2199
 
                              ]),
2200
 
                        ]
2201
 
        state._discard_merge_parents()
2202
 
        state._validate()
2203
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2204
 
 
2205
 
    def test_discard_renamed(self):
2206
 
        null_stat = dirstate.DirState.NULLSTAT
2207
 
        present_dir = ('d', '', 0, False, null_stat)
2208
 
        present_file = ('f', '', 0, False, null_stat)
2209
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2210
 
        root_key = ('', '', 'a-root-value')
2211
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2212
 
        # Renamed relative to parent
2213
 
        file_rename_s_key = ('', 'file-s', 'b-file-id')
2214
 
        file_rename_t_key = ('', 'file-t', 'b-file-id')
2215
 
        # And one that is renamed between the parents, but absent in this
2216
 
        key_in_1 = ('', 'file-in-1', 'c-file-id')
2217
 
        key_in_2 = ('', 'file-in-2', 'c-file-id')
2218
 
 
2219
 
        dirblocks = [
2220
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2221
 
            ('', [(key_in_1,
2222
 
                   [absent, present_file, ('r', 'file-in-2', 'c-file-id')]),
2223
 
                  (key_in_2,
2224
 
                   [absent, ('r', 'file-in-1', 'c-file-id'), present_file]),
2225
 
                  (file_in_root_key,
2226
 
                   [present_file, present_file, present_file]),
2227
 
                  (file_rename_s_key,
2228
 
                   [('r', 'file-t', 'b-file-id'), absent, present_file]),
2229
 
                  (file_rename_t_key,
2230
 
                   [present_file, absent, ('r', 'file-s', 'b-file-id')]),
2231
 
                 ]),
2232
 
        ]
2233
 
        exp_dirblocks = [
2234
 
            ('', [(root_key, [present_dir, present_dir])]),
2235
 
            ('', [(key_in_1, [absent, present_file]),
2236
 
                  (file_in_root_key, [present_file, present_file]),
2237
 
                  (file_rename_t_key, [present_file, absent]),
2238
 
                 ]),
2239
 
        ]
2240
 
        state = self.create_empty_dirstate()
2241
 
        self.addCleanup(state.unlock)
2242
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2243
 
        state._validate()
2244
 
 
2245
 
        state._discard_merge_parents()
2246
 
        state._validate()
2247
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2248
 
 
2249
 
    def test_discard_all_subdir(self):
2250
 
        null_stat = dirstate.DirState.NULLSTAT
2251
 
        present_dir = ('d', '', 0, False, null_stat)
2252
 
        present_file = ('f', '', 0, False, null_stat)
2253
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2254
 
        root_key = ('', '', 'a-root-value')
2255
 
        subdir_key = ('', 'sub', 'dir-id')
2256
 
        child1_key = ('sub', 'child1', 'child1-id')
2257
 
        child2_key = ('sub', 'child2', 'child2-id')
2258
 
        child3_key = ('sub', 'child3', 'child3-id')
2259
 
 
2260
 
        dirblocks = [
2261
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2262
 
            ('', [(subdir_key, [present_dir, present_dir, present_dir])]),
2263
 
            ('sub', [(child1_key, [absent, absent, present_file]),
2264
 
                     (child2_key, [absent, absent, present_file]),
2265
 
                     (child3_key, [absent, absent, present_file]),
2266
 
                    ]),
2267
 
        ]
2268
 
        exp_dirblocks = [
2269
 
            ('', [(root_key, [present_dir, present_dir])]),
2270
 
            ('', [(subdir_key, [present_dir, present_dir])]),
2271
 
            ('sub', []),
2272
 
        ]
2273
 
        state = self.create_empty_dirstate()
2274
 
        self.addCleanup(state.unlock)
2275
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2276
 
        state._validate()
2277
 
 
2278
 
        state._discard_merge_parents()
2279
 
        state._validate()
2280
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2281
 
 
2282
 
 
2283
 
class Test_InvEntryToDetails(tests.TestCase):
2284
 
 
2285
 
    def assertDetails(self, expected, inv_entry):
2286
 
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2287
 
        self.assertEqual(expected, details)
2288
 
        # details should always allow join() and always be a plain str when
2289
 
        # finished
2290
 
        (minikind, fingerprint, size, executable, tree_data) = details
2291
 
        self.assertIsInstance(minikind, str)
2292
 
        self.assertIsInstance(fingerprint, str)
2293
 
        self.assertIsInstance(tree_data, str)
2294
 
 
2295
 
    def test_unicode_symlink(self):
2296
 
        inv_entry = inventory.InventoryLink('link-file-id',
2297
 
                                            u'nam\N{Euro Sign}e',
2298
 
                                            'link-parent-id')
2299
 
        inv_entry.revision = 'link-revision-id'
2300
 
        target = u'link-targ\N{Euro Sign}t'
2301
 
        inv_entry.symlink_target = target
2302
 
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2303
 
                            'link-revision-id'), inv_entry)
2304
 
 
2305
 
 
2306
 
class TestSHA1Provider(tests.TestCaseInTempDir):
2307
 
 
2308
 
    def test_sha1provider_is_an_interface(self):
2309
 
        p = dirstate.SHA1Provider()
2310
 
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2311
 
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2312
 
 
2313
 
    def test_defaultsha1provider_sha1(self):
2314
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2315
 
        self.build_tree_contents([('foo', text)])
2316
 
        expected_sha = osutils.sha_string(text)
2317
 
        p = dirstate.DefaultSHA1Provider()
2318
 
        self.assertEqual(expected_sha, p.sha1('foo'))
2319
 
 
2320
 
    def test_defaultsha1provider_stat_and_sha1(self):
2321
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2322
 
        self.build_tree_contents([('foo', text)])
2323
 
        expected_sha = osutils.sha_string(text)
2324
 
        p = dirstate.DefaultSHA1Provider()
2325
 
        statvalue, sha1 = p.stat_and_sha1('foo')
2326
 
        self.assertTrue(len(statvalue) >= 10)
2327
 
        self.assertEqual(len(text), statvalue.st_size)
2328
 
        self.assertEqual(expected_sha, sha1)