~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__dirstate_helpers.py

  • Committer: Jelmer Vernooij
  • Date: 2009-06-09 00:59:51 UTC
  • mto: (4443.1.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 4444.
  • Revision ID: jelmer@samba.org-20090609005951-apv900cdk35o2ygh
Move squashing of XML-invalid characters to XMLSerializer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007, 2008 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
28
28
    )
29
29
from bzrlib.tests import (
30
30
    test_dirstate,
31
 
    )
32
 
from bzrlib.tests.test_osutils import dir_reader_scenarios
33
 
from bzrlib.tests.scenarios import (
34
 
    load_tests_apply_scenarios,
35
 
    multiply_scenarios,
36
 
    )
37
 
from bzrlib.tests import (
38
 
    features,
39
 
    )
40
 
 
41
 
 
42
 
load_tests = load_tests_apply_scenarios
43
 
 
44
 
 
45
 
compiled_dirstate_helpers_feature = features.ModuleAvailableFeature(
46
 
    'bzrlib._dirstate_helpers_pyx')
47
 
 
48
 
 
49
 
# FIXME: we should also parametrize against SHA1Provider !
50
 
 
51
 
ue_scenarios = [('dirstate_Python',
52
 
    {'update_entry': dirstate.py_update_entry})]
53
 
if compiled_dirstate_helpers_feature.available():
54
 
    update_entry = compiled_dirstate_helpers_feature.module.update_entry
55
 
    ue_scenarios.append(('dirstate_Pyrex', {'update_entry': update_entry}))
56
 
 
57
 
pe_scenarios = [('dirstate_Python',
58
 
    {'_process_entry': dirstate.ProcessEntryPython})]
59
 
if compiled_dirstate_helpers_feature.available():
60
 
    process_entry = compiled_dirstate_helpers_feature.module.ProcessEntryC
61
 
    pe_scenarios.append(('dirstate_Pyrex', {'_process_entry': process_entry}))
 
31
    test_osutils,
 
32
    )
 
33
 
 
34
try:
 
35
    from bzrlib import _dirstate_helpers_c
 
36
    has_dirstate_helpers_c = True
 
37
except ImportError:
 
38
    has_dirstate_helpers_c = False
 
39
 
 
40
 
 
41
class _CompiledDirstateHelpersFeature(tests.Feature):
 
42
    def _probe(self):
 
43
        return has_dirstate_helpers_c
 
44
 
 
45
    def feature_name(self):
 
46
        return 'bzrlib._dirstate_helpers_c'
 
47
CompiledDirstateHelpersFeature = _CompiledDirstateHelpersFeature()
 
48
 
 
49
 
 
50
def load_tests(basic_tests, module, loader):
 
51
    # FIXME: we should also parametrize against SHA1Provider !
 
52
    suite = loader.suiteClass()
 
53
    remaining_tests = basic_tests
 
54
 
 
55
    dir_reader_scenarios = test_osutils.dir_reader_scenarios()
 
56
 
 
57
    ue_scenarios = [('dirstate_Python',
 
58
                     {'update_entry': dirstate.py_update_entry})]
 
59
    if has_dirstate_helpers_c:
 
60
        c_scenario = ('dirstate_C',
 
61
                     {'update_entry': _dirstate_helpers_c.update_entry})
 
62
        ue_scenarios.append(c_scenario)
 
63
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
 
64
        remaining_tests, tests.condition_isinstance(TestUpdateEntry))
 
65
    tests.multiply_tests(process_entry_tests,
 
66
                         tests.multiply_scenarios(dir_reader_scenarios,
 
67
                                                  ue_scenarios),
 
68
                         suite)
 
69
 
 
70
    pe_scenarios = [('dirstate_Python',
 
71
                     {'_process_entry': dirstate.ProcessEntryPython})]
 
72
    if has_dirstate_helpers_c:
 
73
        c_scenario = ('dirstate_C',
 
74
                     {'_process_entry': _dirstate_helpers_c.ProcessEntryC})
 
75
        pe_scenarios.append(c_scenario)
 
76
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
 
77
        remaining_tests, tests.condition_isinstance(TestProcessEntry))
 
78
    tests.multiply_tests(process_entry_tests,
 
79
                         tests.multiply_scenarios(dir_reader_scenarios,
 
80
                                                  pe_scenarios),
 
81
                         suite)
 
82
 
 
83
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
 
84
        remaining_tests, tests.condition_isinstance(
 
85
            test_dirstate.TestCaseWithDirState))
 
86
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios, suite)
 
87
    suite.addTest(remaining_tests)
 
88
 
 
89
    return suite
62
90
 
63
91
 
64
92
class TestBisectPathMixin(object):
217
245
 
218
246
 
219
247
class TestBisectPathLeft(tests.TestCase, TestBisectPathMixin):
220
 
    """Run all Bisect Path tests against _bisect_path_left."""
 
248
    """Run all Bisect Path tests against _bisect_path_left_py."""
221
249
 
222
250
    def get_bisect_path(self):
223
 
        from bzrlib._dirstate_helpers_py import _bisect_path_left
224
 
        return _bisect_path_left
 
251
        from bzrlib._dirstate_helpers_py import _bisect_path_left_py
 
252
        return _bisect_path_left_py
225
253
 
226
254
    def get_bisect(self):
227
255
        return bisect.bisect_left, 0
228
256
 
229
257
 
230
258
class TestCompiledBisectPathLeft(TestBisectPathLeft):
231
 
    """Run all Bisect Path tests against _bisect_path_lect"""
 
259
    """Run all Bisect Path tests against _bisect_path_right_c"""
232
260
 
233
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
261
    _test_needs_features = [CompiledDirstateHelpersFeature]
234
262
 
235
263
    def get_bisect_path(self):
236
 
        from bzrlib._dirstate_helpers_pyx import _bisect_path_left
237
 
        return _bisect_path_left
 
264
        from bzrlib._dirstate_helpers_c import _bisect_path_left_c
 
265
        return _bisect_path_left_c
238
266
 
239
267
 
240
268
class TestBisectPathRight(tests.TestCase, TestBisectPathMixin):
241
 
    """Run all Bisect Path tests against _bisect_path_right"""
 
269
    """Run all Bisect Path tests against _bisect_path_right_py"""
242
270
 
243
271
    def get_bisect_path(self):
244
 
        from bzrlib._dirstate_helpers_py import _bisect_path_right
245
 
        return _bisect_path_right
 
272
        from bzrlib._dirstate_helpers_py import _bisect_path_right_py
 
273
        return _bisect_path_right_py
246
274
 
247
275
    def get_bisect(self):
248
276
        return bisect.bisect_right, -1
249
277
 
250
278
 
251
279
class TestCompiledBisectPathRight(TestBisectPathRight):
252
 
    """Run all Bisect Path tests against _bisect_path_right"""
 
280
    """Run all Bisect Path tests against _bisect_path_right_c"""
253
281
 
254
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
282
    _test_needs_features = [CompiledDirstateHelpersFeature]
255
283
 
256
284
    def get_bisect_path(self):
257
 
        from bzrlib._dirstate_helpers_pyx import _bisect_path_right
258
 
        return _bisect_path_right
 
285
        from bzrlib._dirstate_helpers_c import _bisect_path_right_c
 
286
        return _bisect_path_right_c
259
287
 
260
288
 
261
289
class TestBisectDirblock(tests.TestCase):
272
300
 
273
301
    def get_bisect_dirblock(self):
274
302
        """Return an implementation of bisect_dirblock"""
275
 
        from bzrlib._dirstate_helpers_py import bisect_dirblock
276
 
        return bisect_dirblock
 
303
        from bzrlib._dirstate_helpers_py import bisect_dirblock_py
 
304
        return bisect_dirblock_py
277
305
 
278
306
    def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
279
307
        """Assert that bisect_split works like bisect_left on the split paths.
363
391
    compiled version.
364
392
    """
365
393
 
366
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
394
    _test_needs_features = [CompiledDirstateHelpersFeature]
367
395
 
368
396
    def get_bisect_dirblock(self):
369
 
        from bzrlib._dirstate_helpers_pyx import bisect_dirblock
370
 
        return bisect_dirblock
 
397
        from bzrlib._dirstate_helpers_c import bisect_dirblock_c
 
398
        return bisect_dirblock_c
371
399
 
372
400
 
373
401
class TestCmpByDirs(tests.TestCase):
382
410
 
383
411
    def get_cmp_by_dirs(self):
384
412
        """Get a specific implementation of cmp_by_dirs."""
385
 
        from bzrlib._dirstate_helpers_py import cmp_by_dirs
386
 
        return cmp_by_dirs
 
413
        from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
 
414
        return cmp_by_dirs_py
387
415
 
388
416
    def assertCmpByDirs(self, expected, str1, str2):
389
417
        """Compare the two strings, in both directions.
485
513
class TestCompiledCmpByDirs(TestCmpByDirs):
486
514
    """Test the pyrex implementation of cmp_by_dirs"""
487
515
 
488
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
516
    _test_needs_features = [CompiledDirstateHelpersFeature]
489
517
 
490
518
    def get_cmp_by_dirs(self):
491
 
        from bzrlib._dirstate_helpers_pyx import cmp_by_dirs
492
 
        return cmp_by_dirs
 
519
        from bzrlib._dirstate_helpers_c import cmp_by_dirs_c
 
520
        return cmp_by_dirs_c
493
521
 
494
522
 
495
523
class TestCmpPathByDirblock(tests.TestCase):
504
532
 
505
533
    def get_cmp_path_by_dirblock(self):
506
534
        """Get a specific implementation of _cmp_path_by_dirblock."""
507
 
        from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock
508
 
        return _cmp_path_by_dirblock
 
535
        from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock_py
 
536
        return _cmp_path_by_dirblock_py
509
537
 
510
538
    def assertCmpPathByDirblock(self, paths):
511
539
        """Compare all paths and make sure they evaluate to the correct order.
636
664
class TestCompiledCmpPathByDirblock(TestCmpPathByDirblock):
637
665
    """Test the pyrex implementation of _cmp_path_by_dirblock"""
638
666
 
639
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
667
    _test_needs_features = [CompiledDirstateHelpersFeature]
640
668
 
641
669
    def get_cmp_by_dirs(self):
642
 
        from bzrlib._dirstate_helpers_pyx import _cmp_path_by_dirblock
643
 
        return _cmp_path_by_dirblock
 
670
        from bzrlib._dirstate_helpers_c import _cmp_path_by_dirblock_c
 
671
        return _cmp_path_by_dirblock_c
644
672
 
645
673
 
646
674
class TestMemRChr(tests.TestCase):
647
675
    """Test memrchr functionality"""
648
676
 
649
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
677
    _test_needs_features = [CompiledDirstateHelpersFeature]
650
678
 
651
679
    def assertMemRChr(self, expected, s, c):
652
 
        from bzrlib._dirstate_helpers_pyx import _py_memrchr
 
680
        from bzrlib._dirstate_helpers_c import _py_memrchr
653
681
        self.assertEqual(expected, _py_memrchr(s, c))
654
682
 
655
683
    def test_missing(self):
696
724
    implementation.
697
725
    """
698
726
 
699
 
    # inherits scenarios from test_dirstate
700
 
 
701
727
    def get_read_dirblocks(self):
702
 
        from bzrlib._dirstate_helpers_py import _read_dirblocks
703
 
        return _read_dirblocks
 
728
        from bzrlib._dirstate_helpers_py import _read_dirblocks_py
 
729
        return _read_dirblocks_py
704
730
 
705
731
    def test_smoketest(self):
706
732
        """Make sure that we can create and read back a simple file."""
716
742
 
717
743
    def test_trailing_garbage(self):
718
744
        tree, state, expected = self.create_basic_dirstate()
719
 
        # On Unix, we can write extra data as long as we haven't read yet, but
720
 
        # on Win32, if you've opened the file with FILE_SHARE_READ, trying to
721
 
        # open it in append mode will fail.
722
 
        state.unlock()
 
745
        # We can modify the file as long as it hasn't been read yet.
723
746
        f = open('dirstate', 'ab')
724
747
        try:
725
748
            # Add bogus trailing garbage
726
749
            f.write('bogus\n')
727
750
        finally:
728
751
            f.close()
729
 
            state.lock_read()
730
752
        e = self.assertRaises(errors.DirstateCorrupt,
731
753
                              state._read_dirblocks_if_needed)
732
754
        # Make sure we mention the bogus characters in the error
736
758
class TestCompiledReadDirblocks(TestReadDirblocks):
737
759
    """Test the pyrex implementation of _read_dirblocks"""
738
760
 
739
 
    _test_needs_features = [compiled_dirstate_helpers_feature]
 
761
    _test_needs_features = [CompiledDirstateHelpersFeature]
740
762
 
741
763
    def get_read_dirblocks(self):
742
 
        from bzrlib._dirstate_helpers_pyx import _read_dirblocks
743
 
        return _read_dirblocks
 
764
        from bzrlib._dirstate_helpers_c import _read_dirblocks_c
 
765
        return _read_dirblocks_c
744
766
 
745
767
 
746
768
class TestUsingCompiledIfAvailable(tests.TestCase):
747
769
    """Check that any compiled functions that are available are the default.
748
770
 
749
771
    It is possible to have typos, etc in the import line, such that
750
 
    _dirstate_helpers_pyx is actually available, but the compiled functions are
 
772
    _dirstate_helpers_c is actually available, but the compiled functions are
751
773
    not being used.
752
774
    """
753
775
 
754
776
    def test_bisect_dirblock(self):
755
 
        if compiled_dirstate_helpers_feature.available():
756
 
            from bzrlib._dirstate_helpers_pyx import bisect_dirblock
 
777
        if CompiledDirstateHelpersFeature.available():
 
778
            from bzrlib._dirstate_helpers_c import bisect_dirblock_c
 
779
            self.assertIs(bisect_dirblock_c, dirstate.bisect_dirblock)
757
780
        else:
758
 
            from bzrlib._dirstate_helpers_py import bisect_dirblock
759
 
        self.assertIs(bisect_dirblock, dirstate.bisect_dirblock)
 
781
            from bzrlib._dirstate_helpers_py import bisect_dirblock_py
 
782
            self.assertIs(bisect_dirblock_py, dirstate.bisect_dirblock)
760
783
 
761
784
    def test__bisect_path_left(self):
762
 
        if compiled_dirstate_helpers_feature.available():
763
 
            from bzrlib._dirstate_helpers_pyx import _bisect_path_left
 
785
        if CompiledDirstateHelpersFeature.available():
 
786
            from bzrlib._dirstate_helpers_c import _bisect_path_left_c
 
787
            self.assertIs(_bisect_path_left_c, dirstate._bisect_path_left)
764
788
        else:
765
 
            from bzrlib._dirstate_helpers_py import _bisect_path_left
766
 
        self.assertIs(_bisect_path_left, dirstate._bisect_path_left)
 
789
            from bzrlib._dirstate_helpers_py import _bisect_path_left_py
 
790
            self.assertIs(_bisect_path_left_py, dirstate._bisect_path_left)
767
791
 
768
792
    def test__bisect_path_right(self):
769
 
        if compiled_dirstate_helpers_feature.available():
770
 
            from bzrlib._dirstate_helpers_pyx import _bisect_path_right
 
793
        if CompiledDirstateHelpersFeature.available():
 
794
            from bzrlib._dirstate_helpers_c import _bisect_path_right_c
 
795
            self.assertIs(_bisect_path_right_c, dirstate._bisect_path_right)
771
796
        else:
772
 
            from bzrlib._dirstate_helpers_py import _bisect_path_right
773
 
        self.assertIs(_bisect_path_right, dirstate._bisect_path_right)
 
797
            from bzrlib._dirstate_helpers_py import _bisect_path_right_py
 
798
            self.assertIs(_bisect_path_right_py, dirstate._bisect_path_right)
774
799
 
775
800
    def test_cmp_by_dirs(self):
776
 
        if compiled_dirstate_helpers_feature.available():
777
 
            from bzrlib._dirstate_helpers_pyx import cmp_by_dirs
 
801
        if CompiledDirstateHelpersFeature.available():
 
802
            from bzrlib._dirstate_helpers_c import cmp_by_dirs_c
 
803
            self.assertIs(cmp_by_dirs_c, dirstate.cmp_by_dirs)
778
804
        else:
779
 
            from bzrlib._dirstate_helpers_py import cmp_by_dirs
780
 
        self.assertIs(cmp_by_dirs, dirstate.cmp_by_dirs)
 
805
            from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
 
806
            self.assertIs(cmp_by_dirs_py, dirstate.cmp_by_dirs)
781
807
 
782
808
    def test__read_dirblocks(self):
783
 
        if compiled_dirstate_helpers_feature.available():
784
 
            from bzrlib._dirstate_helpers_pyx import _read_dirblocks
 
809
        if CompiledDirstateHelpersFeature.available():
 
810
            from bzrlib._dirstate_helpers_c import _read_dirblocks_c
 
811
            self.assertIs(_read_dirblocks_c, dirstate._read_dirblocks)
785
812
        else:
786
 
            from bzrlib._dirstate_helpers_py import _read_dirblocks
787
 
        self.assertIs(_read_dirblocks, dirstate._read_dirblocks)
 
813
            from bzrlib._dirstate_helpers_py import _read_dirblocks_py
 
814
            self.assertIs(_read_dirblocks_py, dirstate._read_dirblocks)
788
815
 
789
816
    def test_update_entry(self):
790
 
        if compiled_dirstate_helpers_feature.available():
791
 
            from bzrlib._dirstate_helpers_pyx import update_entry
 
817
        if CompiledDirstateHelpersFeature.available():
 
818
            from bzrlib._dirstate_helpers_c import update_entry
 
819
            self.assertIs(update_entry, dirstate.update_entry)
792
820
        else:
793
 
            from bzrlib.dirstate import update_entry
794
 
        self.assertIs(update_entry, dirstate.update_entry)
 
821
            from bzrlib.dirstate import py_update_entry
 
822
            self.assertIs(py_update_entry, dirstate.py_update_entry)
795
823
 
796
824
    def test_process_entry(self):
797
 
        if compiled_dirstate_helpers_feature.available():
798
 
            from bzrlib._dirstate_helpers_pyx import ProcessEntryC
 
825
        if CompiledDirstateHelpersFeature.available():
 
826
            from bzrlib._dirstate_helpers_c import ProcessEntryC
799
827
            self.assertIs(ProcessEntryC, dirstate._process_entry)
800
828
        else:
801
829
            from bzrlib.dirstate import ProcessEntryPython
805
833
class TestUpdateEntry(test_dirstate.TestCaseWithDirState):
806
834
    """Test the DirState.update_entry functions"""
807
835
 
808
 
    scenarios = multiply_scenarios(
809
 
        dir_reader_scenarios(), ue_scenarios)
810
 
 
811
836
    # Set by load_tests
812
837
    update_entry = None
813
838
 
814
839
    def setUp(self):
815
840
        super(TestUpdateEntry, self).setUp()
816
 
        self.overrideAttr(dirstate, 'update_entry', self.update_entry)
 
841
        orig = dirstate.update_entry
 
842
        def cleanup():
 
843
            dirstate.update_entry = orig
 
844
        self.addCleanup(cleanup)
 
845
        dirstate.update_entry = self.update_entry
817
846
 
818
847
    def get_state_with_a(self):
819
848
        """Create a DirState tracking a single object named 'a'"""
825
854
 
826
855
    def test_observed_sha1_cachable(self):
827
856
        state, entry = self.get_state_with_a()
828
 
        state.save()
829
857
        atime = time.time() - 10
830
858
        self.build_tree(['a'])
831
 
        statvalue = test_dirstate._FakeStat.from_stat(os.lstat('a'))
832
 
        statvalue.st_mtime = statvalue.st_ctime = atime
833
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
834
 
                         state._dirblock_state)
 
859
        statvalue = os.lstat('a')
 
860
        statvalue = test_dirstate._FakeStat(statvalue.st_size, atime, atime,
 
861
            statvalue.st_dev, statvalue.st_ino, statvalue.st_mode)
835
862
        state._observed_sha1(entry, "foo", statvalue)
836
863
        self.assertEqual('foo', entry[1][0][1])
837
864
        packed_stat = dirstate.pack_stat(statvalue)
838
865
        self.assertEqual(packed_stat, entry[1][0][4])
839
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
840
 
                         state._dirblock_state)
841
866
 
842
867
    def test_observed_sha1_not_cachable(self):
843
868
        state, entry = self.get_state_with_a()
844
 
        state.save()
845
869
        oldval = entry[1][0][1]
846
870
        oldstat = entry[1][0][4]
847
871
        self.build_tree(['a'])
848
872
        statvalue = os.lstat('a')
849
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
850
 
                         state._dirblock_state)
851
873
        state._observed_sha1(entry, "foo", statvalue)
852
874
        self.assertEqual(oldval, entry[1][0][1])
853
875
        self.assertEqual(oldstat, entry[1][0][4])
854
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
855
 
                         state._dirblock_state)
856
876
 
857
877
    def test_update_entry(self):
858
878
        state, _ = self.get_state_with_a()
883
903
                                          stat_value=stat_value)
884
904
        self.assertEqual(None, link_or_sha1)
885
905
 
886
 
        # The dirblock entry should not have computed or cached the file's
887
 
        # sha1, but it did update the files' st_size. However, this is not
888
 
        # worth writing a dirstate file for, so we leave the state UNMODIFIED
 
906
        # The dirblock entry should not have cached the file's sha1 (too new)
889
907
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
890
908
                         entry[1][0])
891
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
909
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
892
910
                         state._dirblock_state)
893
911
        mode = stat_value.st_mode
894
912
        self.assertEqual([('is_exec', mode, False)], state._log)
897
915
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
898
916
                         state._dirblock_state)
899
917
 
900
 
        # Roll the clock back so the file is guaranteed to look too new. We
901
 
        # should still not compute the sha1.
 
918
        # If we do it again right away, we don't know if the file has changed
 
919
        # so we will re-read the file. Roll the clock back so the file is
 
920
        # guaranteed to look too new.
902
921
        state.adjust_time(-10)
903
922
        del state._log[:]
904
923
 
906
925
                                          stat_value=stat_value)
907
926
        self.assertEqual([('is_exec', mode, False)], state._log)
908
927
        self.assertEqual(None, link_or_sha1)
909
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
928
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
910
929
                         state._dirblock_state)
911
930
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
912
931
                         entry[1][0])
922
941
        self.assertEqual([('is_exec', mode, False)], state._log)
923
942
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
924
943
                         entry[1][0])
925
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
926
 
                         state._dirblock_state)
927
944
 
928
945
        # If the file is no longer new, and the clock has been moved forward
929
946
        # sufficiently, it will cache the sha.
954
971
 
955
972
    def test_update_entry_symlink(self):
956
973
        """Update entry should read symlinks."""
957
 
        self.requireFeature(features.SymlinkFeature)
 
974
        self.requireFeature(tests.SymlinkFeature)
958
975
        state, entry = self.get_state_with_a()
959
976
        state.save()
960
977
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
971
988
        # Dirblock is not updated (the link is too new)
972
989
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
973
990
                         entry[1])
974
 
        # The file entry turned into a symlink, that is considered
975
 
        # HASH modified worthy.
976
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
991
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
977
992
                         state._dirblock_state)
978
993
 
979
994
        # Because the stat_value looks new, we should re-read the target
980
 
        del state._log[:]
981
995
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
982
996
                                          stat_value=stat_value)
983
997
        self.assertEqual('target', link_or_sha1)
984
 
        self.assertEqual([('read_link', 'a', '')], state._log)
 
998
        self.assertEqual([('read_link', 'a', ''),
 
999
                          ('read_link', 'a', ''),
 
1000
                         ], state._log)
985
1001
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
986
1002
                         entry[1])
987
 
        state.save()
988
1003
        state.adjust_time(+20) # Skip into the future, all files look old
989
 
        del state._log[:]
990
1004
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
991
1005
                                          stat_value=stat_value)
992
 
        # The symlink stayed a symlink. So while it is new enough to cache, we
993
 
        # don't bother setting the flag, because it is not really worth saving
994
 
        # (when we stat the symlink, we'll have paged in the target.)
995
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
996
 
                         state._dirblock_state)
997
1006
        self.assertEqual('target', link_or_sha1)
998
1007
        # We need to re-read the link because only now can we cache it
999
 
        self.assertEqual([('read_link', 'a', '')], state._log)
 
1008
        self.assertEqual([('read_link', 'a', ''),
 
1009
                          ('read_link', 'a', ''),
 
1010
                          ('read_link', 'a', ''),
 
1011
                         ], state._log)
1000
1012
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
1001
1013
                         entry[1])
1002
1014
 
1003
 
        del state._log[:]
1004
1015
        # Another call won't re-read the link
1005
 
        self.assertEqual([], state._log)
 
1016
        self.assertEqual([('read_link', 'a', ''),
 
1017
                          ('read_link', 'a', ''),
 
1018
                          ('read_link', 'a', ''),
 
1019
                         ], state._log)
1006
1020
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
1007
1021
                                          stat_value=stat_value)
1008
1022
        self.assertEqual('target', link_or_sha1)
1023
1037
        self.build_tree(['a/'])
1024
1038
        state.adjust_time(+20)
1025
1039
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1026
 
        # a/ used to be a file, but is now a directory, worth saving
1027
1040
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
1028
1041
                         state._dirblock_state)
1029
1042
        state.save()
1030
1043
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1031
1044
                         state._dirblock_state)
1032
 
        # No changes to a/ means not worth saving.
1033
 
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1034
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1035
 
                         state._dirblock_state)
1036
 
        # Change the last-modified time for the directory
1037
 
        t = time.time() - 100.0
1038
 
        try:
1039
 
            os.utime('a', (t, t))
1040
 
        except OSError:
1041
 
            # It looks like Win32 + FAT doesn't allow to change times on a dir.
1042
 
            raise tests.TestSkipped("can't update mtime of a dir on FAT")
1043
 
        saved_packed_stat = entry[1][0][-1]
1044
 
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1045
 
        # We *do* go ahead and update the information in the dirblocks, but we
1046
 
        # don't bother setting IN_MEMORY_MODIFIED because it is trivial to
1047
 
        # recompute.
1048
 
        self.assertNotEqual(saved_packed_stat, entry[1][0][-1])
 
1045
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1049
1046
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1050
1047
                         state._dirblock_state)
1051
1048
 
1151
1148
 
1152
1149
    def test_update_file_to_symlink(self):
1153
1150
        """File becomes a symlink"""
1154
 
        self.requireFeature(features.SymlinkFeature)
 
1151
        self.requireFeature(tests.SymlinkFeature)
1155
1152
        state, entry = self.get_state_with_a()
1156
1153
        # The file sha1 won't be cached unless the file is old
1157
1154
        state.adjust_time(+10)
1170
1167
 
1171
1168
    def test_update_dir_to_symlink(self):
1172
1169
        """Directory becomes a symlink"""
1173
 
        self.requireFeature(features.SymlinkFeature)
 
1170
        self.requireFeature(tests.SymlinkFeature)
1174
1171
        state, entry = self.get_state_with_a()
1175
1172
        # The symlink target won't be cached if it isn't old
1176
1173
        state.adjust_time(+10)
1180
1177
 
1181
1178
    def test_update_symlink_to_file(self):
1182
1179
        """Symlink becomes a file"""
1183
 
        self.requireFeature(features.SymlinkFeature)
 
1180
        self.requireFeature(tests.SymlinkFeature)
1184
1181
        state, entry = self.get_state_with_a()
1185
1182
        # The symlink and file info won't be cached unless old
1186
1183
        state.adjust_time(+10)
1190
1187
 
1191
1188
    def test_update_symlink_to_dir(self):
1192
1189
        """Symlink becomes a directory"""
1193
 
        self.requireFeature(features.SymlinkFeature)
 
1190
        self.requireFeature(tests.SymlinkFeature)
1194
1191
        state, entry = self.get_state_with_a()
1195
1192
        # The symlink target won't be cached if it isn't old
1196
1193
        state.adjust_time(+10)
1283
1280
 
1284
1281
class TestProcessEntry(test_dirstate.TestCaseWithDirState):
1285
1282
 
1286
 
    scenarios = multiply_scenarios(dir_reader_scenarios(), pe_scenarios)
1287
 
 
1288
1283
    # Set by load_tests
1289
1284
    _process_entry = None
1290
1285
 
1291
1286
    def setUp(self):
1292
1287
        super(TestProcessEntry, self).setUp()
1293
 
        self.overrideAttr(dirstate, '_process_entry', self._process_entry)
 
1288
        orig = dirstate._process_entry
 
1289
        def cleanup():
 
1290
            dirstate._process_entry = orig
 
1291
        self.addCleanup(cleanup)
 
1292
        dirstate._process_entry = self._process_entry
1294
1293
 
1295
1294
    def assertChangedFileIds(self, expected, tree):
1296
1295
        tree.lock_read()
1301
1300
            tree.unlock()
1302
1301
        self.assertEqual(sorted(expected), sorted(file_ids))
1303
1302
 
1304
 
    def test_exceptions_raised(self):
1305
 
        # This is a direct test of bug #495023, it relies on osutils.is_inside
1306
 
        # getting called in an inner function. Which makes it a bit brittle,
1307
 
        # but at least it does reproduce the bug.
1308
 
        tree = self.make_branch_and_tree('tree')
1309
 
        self.build_tree(['tree/file', 'tree/dir/', 'tree/dir/sub',
1310
 
                         'tree/dir2/', 'tree/dir2/sub2'])
1311
 
        tree.add(['file', 'dir', 'dir/sub', 'dir2', 'dir2/sub2'])
1312
 
        tree.commit('first commit')
1313
 
        tree.lock_read()
1314
 
        self.addCleanup(tree.unlock)
1315
 
        basis_tree = tree.basis_tree()
1316
 
        def is_inside_raises(*args, **kwargs):
1317
 
            raise RuntimeError('stop this')
1318
 
        self.overrideAttr(osutils, 'is_inside', is_inside_raises)
1319
 
        self.assertListRaises(RuntimeError, tree.iter_changes, basis_tree)
1320
 
 
1321
1303
    def test_simple_changes(self):
1322
1304
        tree = self.make_branch_and_tree('tree')
1323
1305
        self.build_tree(['tree/file'])