~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__dirstate_helpers.py

  • Committer: John Arbash Meinel
  • Date: 2010-09-29 20:56:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5452.
  • Revision ID: john@arbash-meinel.com-20100929205618-qlldxw4ykwt5511n
Move the new NEWS entry to the appropriate section.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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
32
32
    )
33
33
 
34
34
try:
35
 
    from bzrlib import _dirstate_helpers_c
36
 
    has_dirstate_helpers_c = True
 
35
    from bzrlib import _dirstate_helpers_pyx
 
36
    has_dirstate_helpers_pyx = True
37
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()
 
38
    has_dirstate_helpers_pyx = False
 
39
 
 
40
 
 
41
compiled_dirstate_helpers_feature = tests.ModuleAvailableFeature(
 
42
                                'bzrlib._dirstate_helpers_pyx')
48
43
 
49
44
 
50
45
def load_tests(basic_tests, module, loader):
56
51
 
57
52
    ue_scenarios = [('dirstate_Python',
58
53
                     {'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)
 
54
    if compiled_dirstate_helpers_feature.available():
 
55
        update_entry = compiled_dirstate_helpers_feature.module.update_entry
 
56
        pyrex_scenario = ('dirstate_Pyrex', {'update_entry': update_entry})
 
57
        ue_scenarios.append(pyrex_scenario)
63
58
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
64
59
        remaining_tests, tests.condition_isinstance(TestUpdateEntry))
65
60
    tests.multiply_tests(process_entry_tests,
69
64
 
70
65
    pe_scenarios = [('dirstate_Python',
71
66
                     {'_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)
 
67
    if compiled_dirstate_helpers_feature.available():
 
68
        process_entry = compiled_dirstate_helpers_feature.module.ProcessEntryC
 
69
        pyrex_scenario = ('dirstate_Pyrex', {'_process_entry': process_entry})
 
70
        pe_scenarios.append(pyrex_scenario)
76
71
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
77
72
        remaining_tests, tests.condition_isinstance(TestProcessEntry))
78
73
    tests.multiply_tests(process_entry_tests,
245
240
 
246
241
 
247
242
class TestBisectPathLeft(tests.TestCase, TestBisectPathMixin):
248
 
    """Run all Bisect Path tests against _bisect_path_left_py."""
 
243
    """Run all Bisect Path tests against _bisect_path_left."""
249
244
 
250
245
    def get_bisect_path(self):
251
 
        from bzrlib._dirstate_helpers_py import _bisect_path_left_py
252
 
        return _bisect_path_left_py
 
246
        from bzrlib._dirstate_helpers_py import _bisect_path_left
 
247
        return _bisect_path_left
253
248
 
254
249
    def get_bisect(self):
255
250
        return bisect.bisect_left, 0
256
251
 
257
252
 
258
253
class TestCompiledBisectPathLeft(TestBisectPathLeft):
259
 
    """Run all Bisect Path tests against _bisect_path_right_c"""
 
254
    """Run all Bisect Path tests against _bisect_path_lect"""
260
255
 
261
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
256
    _test_needs_features = [compiled_dirstate_helpers_feature]
262
257
 
263
258
    def get_bisect_path(self):
264
 
        from bzrlib._dirstate_helpers_c import _bisect_path_left_c
265
 
        return _bisect_path_left_c
 
259
        from bzrlib._dirstate_helpers_pyx import _bisect_path_left
 
260
        return _bisect_path_left
266
261
 
267
262
 
268
263
class TestBisectPathRight(tests.TestCase, TestBisectPathMixin):
269
 
    """Run all Bisect Path tests against _bisect_path_right_py"""
 
264
    """Run all Bisect Path tests against _bisect_path_right"""
270
265
 
271
266
    def get_bisect_path(self):
272
 
        from bzrlib._dirstate_helpers_py import _bisect_path_right_py
273
 
        return _bisect_path_right_py
 
267
        from bzrlib._dirstate_helpers_py import _bisect_path_right
 
268
        return _bisect_path_right
274
269
 
275
270
    def get_bisect(self):
276
271
        return bisect.bisect_right, -1
277
272
 
278
273
 
279
274
class TestCompiledBisectPathRight(TestBisectPathRight):
280
 
    """Run all Bisect Path tests against _bisect_path_right_c"""
 
275
    """Run all Bisect Path tests against _bisect_path_right"""
281
276
 
282
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
277
    _test_needs_features = [compiled_dirstate_helpers_feature]
283
278
 
284
279
    def get_bisect_path(self):
285
 
        from bzrlib._dirstate_helpers_c import _bisect_path_right_c
286
 
        return _bisect_path_right_c
 
280
        from bzrlib._dirstate_helpers_pyx import _bisect_path_right
 
281
        return _bisect_path_right
287
282
 
288
283
 
289
284
class TestBisectDirblock(tests.TestCase):
300
295
 
301
296
    def get_bisect_dirblock(self):
302
297
        """Return an implementation of bisect_dirblock"""
303
 
        from bzrlib._dirstate_helpers_py import bisect_dirblock_py
304
 
        return bisect_dirblock_py
 
298
        from bzrlib._dirstate_helpers_py import bisect_dirblock
 
299
        return bisect_dirblock
305
300
 
306
301
    def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
307
302
        """Assert that bisect_split works like bisect_left on the split paths.
391
386
    compiled version.
392
387
    """
393
388
 
394
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
389
    _test_needs_features = [compiled_dirstate_helpers_feature]
395
390
 
396
391
    def get_bisect_dirblock(self):
397
 
        from bzrlib._dirstate_helpers_c import bisect_dirblock_c
398
 
        return bisect_dirblock_c
 
392
        from bzrlib._dirstate_helpers_pyx import bisect_dirblock
 
393
        return bisect_dirblock
399
394
 
400
395
 
401
396
class TestCmpByDirs(tests.TestCase):
410
405
 
411
406
    def get_cmp_by_dirs(self):
412
407
        """Get a specific implementation of cmp_by_dirs."""
413
 
        from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
414
 
        return cmp_by_dirs_py
 
408
        from bzrlib._dirstate_helpers_py import cmp_by_dirs
 
409
        return cmp_by_dirs
415
410
 
416
411
    def assertCmpByDirs(self, expected, str1, str2):
417
412
        """Compare the two strings, in both directions.
513
508
class TestCompiledCmpByDirs(TestCmpByDirs):
514
509
    """Test the pyrex implementation of cmp_by_dirs"""
515
510
 
516
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
511
    _test_needs_features = [compiled_dirstate_helpers_feature]
517
512
 
518
513
    def get_cmp_by_dirs(self):
519
 
        from bzrlib._dirstate_helpers_c import cmp_by_dirs_c
520
 
        return cmp_by_dirs_c
 
514
        from bzrlib._dirstate_helpers_pyx import cmp_by_dirs
 
515
        return cmp_by_dirs
521
516
 
522
517
 
523
518
class TestCmpPathByDirblock(tests.TestCase):
532
527
 
533
528
    def get_cmp_path_by_dirblock(self):
534
529
        """Get a specific implementation of _cmp_path_by_dirblock."""
535
 
        from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock_py
536
 
        return _cmp_path_by_dirblock_py
 
530
        from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock
 
531
        return _cmp_path_by_dirblock
537
532
 
538
533
    def assertCmpPathByDirblock(self, paths):
539
534
        """Compare all paths and make sure they evaluate to the correct order.
664
659
class TestCompiledCmpPathByDirblock(TestCmpPathByDirblock):
665
660
    """Test the pyrex implementation of _cmp_path_by_dirblock"""
666
661
 
667
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
662
    _test_needs_features = [compiled_dirstate_helpers_feature]
668
663
 
669
664
    def get_cmp_by_dirs(self):
670
 
        from bzrlib._dirstate_helpers_c import _cmp_path_by_dirblock_c
671
 
        return _cmp_path_by_dirblock_c
 
665
        from bzrlib._dirstate_helpers_pyx import _cmp_path_by_dirblock
 
666
        return _cmp_path_by_dirblock
672
667
 
673
668
 
674
669
class TestMemRChr(tests.TestCase):
675
670
    """Test memrchr functionality"""
676
671
 
677
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
672
    _test_needs_features = [compiled_dirstate_helpers_feature]
678
673
 
679
674
    def assertMemRChr(self, expected, s, c):
680
 
        from bzrlib._dirstate_helpers_c import _py_memrchr
 
675
        from bzrlib._dirstate_helpers_pyx import _py_memrchr
681
676
        self.assertEqual(expected, _py_memrchr(s, c))
682
677
 
683
678
    def test_missing(self):
725
720
    """
726
721
 
727
722
    def get_read_dirblocks(self):
728
 
        from bzrlib._dirstate_helpers_py import _read_dirblocks_py
729
 
        return _read_dirblocks_py
 
723
        from bzrlib._dirstate_helpers_py import _read_dirblocks
 
724
        return _read_dirblocks
730
725
 
731
726
    def test_smoketest(self):
732
727
        """Make sure that we can create and read back a simple file."""
742
737
 
743
738
    def test_trailing_garbage(self):
744
739
        tree, state, expected = self.create_basic_dirstate()
745
 
        # We can modify the file as long as it hasn't been read yet.
 
740
        # On Unix, we can write extra data as long as we haven't read yet, but
 
741
        # on Win32, if you've opened the file with FILE_SHARE_READ, trying to
 
742
        # open it in append mode will fail.
 
743
        state.unlock()
746
744
        f = open('dirstate', 'ab')
747
745
        try:
748
746
            # Add bogus trailing garbage
749
747
            f.write('bogus\n')
750
748
        finally:
751
749
            f.close()
 
750
            state.lock_read()
752
751
        e = self.assertRaises(errors.DirstateCorrupt,
753
752
                              state._read_dirblocks_if_needed)
754
753
        # Make sure we mention the bogus characters in the error
758
757
class TestCompiledReadDirblocks(TestReadDirblocks):
759
758
    """Test the pyrex implementation of _read_dirblocks"""
760
759
 
761
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
 
760
    _test_needs_features = [compiled_dirstate_helpers_feature]
762
761
 
763
762
    def get_read_dirblocks(self):
764
 
        from bzrlib._dirstate_helpers_c import _read_dirblocks_c
765
 
        return _read_dirblocks_c
 
763
        from bzrlib._dirstate_helpers_pyx import _read_dirblocks
 
764
        return _read_dirblocks
766
765
 
767
766
 
768
767
class TestUsingCompiledIfAvailable(tests.TestCase):
769
768
    """Check that any compiled functions that are available are the default.
770
769
 
771
770
    It is possible to have typos, etc in the import line, such that
772
 
    _dirstate_helpers_c is actually available, but the compiled functions are
 
771
    _dirstate_helpers_pyx is actually available, but the compiled functions are
773
772
    not being used.
774
773
    """
775
774
 
776
775
    def test_bisect_dirblock(self):
777
 
        if CompiledDirstateHelpersFeature.available():
778
 
            from bzrlib._dirstate_helpers_c import bisect_dirblock_c
779
 
            self.assertIs(bisect_dirblock_c, dirstate.bisect_dirblock)
 
776
        if compiled_dirstate_helpers_feature.available():
 
777
            from bzrlib._dirstate_helpers_pyx import bisect_dirblock
780
778
        else:
781
 
            from bzrlib._dirstate_helpers_py import bisect_dirblock_py
782
 
            self.assertIs(bisect_dirblock_py, dirstate.bisect_dirblock)
 
779
            from bzrlib._dirstate_helpers_py import bisect_dirblock
 
780
        self.assertIs(bisect_dirblock, dirstate.bisect_dirblock)
783
781
 
784
782
    def test__bisect_path_left(self):
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)
 
783
        if compiled_dirstate_helpers_feature.available():
 
784
            from bzrlib._dirstate_helpers_pyx import _bisect_path_left
788
785
        else:
789
 
            from bzrlib._dirstate_helpers_py import _bisect_path_left_py
790
 
            self.assertIs(_bisect_path_left_py, dirstate._bisect_path_left)
 
786
            from bzrlib._dirstate_helpers_py import _bisect_path_left
 
787
        self.assertIs(_bisect_path_left, dirstate._bisect_path_left)
791
788
 
792
789
    def test__bisect_path_right(self):
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)
 
790
        if compiled_dirstate_helpers_feature.available():
 
791
            from bzrlib._dirstate_helpers_pyx import _bisect_path_right
796
792
        else:
797
 
            from bzrlib._dirstate_helpers_py import _bisect_path_right_py
798
 
            self.assertIs(_bisect_path_right_py, dirstate._bisect_path_right)
 
793
            from bzrlib._dirstate_helpers_py import _bisect_path_right
 
794
        self.assertIs(_bisect_path_right, dirstate._bisect_path_right)
799
795
 
800
796
    def test_cmp_by_dirs(self):
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)
 
797
        if compiled_dirstate_helpers_feature.available():
 
798
            from bzrlib._dirstate_helpers_pyx import cmp_by_dirs
804
799
        else:
805
 
            from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
806
 
            self.assertIs(cmp_by_dirs_py, dirstate.cmp_by_dirs)
 
800
            from bzrlib._dirstate_helpers_py import cmp_by_dirs
 
801
        self.assertIs(cmp_by_dirs, dirstate.cmp_by_dirs)
807
802
 
808
803
    def test__read_dirblocks(self):
809
 
        if CompiledDirstateHelpersFeature.available():
810
 
            from bzrlib._dirstate_helpers_c import _read_dirblocks_c
811
 
            self.assertIs(_read_dirblocks_c, dirstate._read_dirblocks)
 
804
        if compiled_dirstate_helpers_feature.available():
 
805
            from bzrlib._dirstate_helpers_pyx import _read_dirblocks
812
806
        else:
813
 
            from bzrlib._dirstate_helpers_py import _read_dirblocks_py
814
 
            self.assertIs(_read_dirblocks_py, dirstate._read_dirblocks)
 
807
            from bzrlib._dirstate_helpers_py import _read_dirblocks
 
808
        self.assertIs(_read_dirblocks, dirstate._read_dirblocks)
815
809
 
816
810
    def test_update_entry(self):
817
 
        if CompiledDirstateHelpersFeature.available():
818
 
            from bzrlib._dirstate_helpers_c import update_entry
819
 
            self.assertIs(update_entry, dirstate.update_entry)
 
811
        if compiled_dirstate_helpers_feature.available():
 
812
            from bzrlib._dirstate_helpers_pyx import update_entry
820
813
        else:
821
 
            from bzrlib.dirstate import py_update_entry
822
 
            self.assertIs(py_update_entry, dirstate.py_update_entry)
 
814
            from bzrlib.dirstate import update_entry
 
815
        self.assertIs(update_entry, dirstate.update_entry)
823
816
 
824
817
    def test_process_entry(self):
825
 
        if CompiledDirstateHelpersFeature.available():
826
 
            from bzrlib._dirstate_helpers_c import ProcessEntryC
 
818
        if compiled_dirstate_helpers_feature.available():
 
819
            from bzrlib._dirstate_helpers_pyx import ProcessEntryC
827
820
            self.assertIs(ProcessEntryC, dirstate._process_entry)
828
821
        else:
829
822
            from bzrlib.dirstate import ProcessEntryPython
838
831
 
839
832
    def setUp(self):
840
833
        super(TestUpdateEntry, self).setUp()
841
 
        orig = dirstate.update_entry
842
 
        def cleanup():
843
 
            dirstate.update_entry = orig
844
 
        self.addCleanup(cleanup)
845
 
        dirstate.update_entry = self.update_entry
 
834
        self.overrideAttr(dirstate, 'update_entry', self.update_entry)
846
835
 
847
836
    def get_state_with_a(self):
848
837
        """Create a DirState tracking a single object named 'a'"""
1285
1274
 
1286
1275
    def setUp(self):
1287
1276
        super(TestProcessEntry, self).setUp()
1288
 
        orig = dirstate._process_entry
1289
 
        def cleanup():
1290
 
            dirstate._process_entry = orig
1291
 
        self.addCleanup(cleanup)
1292
 
        dirstate._process_entry = self._process_entry
 
1277
        self.overrideAttr(dirstate, '_process_entry', self._process_entry)
1293
1278
 
1294
1279
    def assertChangedFileIds(self, expected, tree):
1295
1280
        tree.lock_read()
1300
1285
            tree.unlock()
1301
1286
        self.assertEqual(sorted(expected), sorted(file_ids))
1302
1287
 
 
1288
    def test_exceptions_raised(self):
 
1289
        # This is a direct test of bug #495023, it relies on osutils.is_inside
 
1290
        # getting called in an inner function. Which makes it a bit brittle,
 
1291
        # but at least it does reproduce the bug.
 
1292
        tree = self.make_branch_and_tree('tree')
 
1293
        self.build_tree(['tree/file', 'tree/dir/', 'tree/dir/sub',
 
1294
                         'tree/dir2/', 'tree/dir2/sub2'])
 
1295
        tree.add(['file', 'dir', 'dir/sub', 'dir2', 'dir2/sub2'])
 
1296
        tree.commit('first commit')
 
1297
        tree.lock_read()
 
1298
        self.addCleanup(tree.unlock)
 
1299
        basis_tree = tree.basis_tree()
 
1300
        def is_inside_raises(*args, **kwargs):
 
1301
            raise RuntimeError('stop this')
 
1302
        self.overrideAttr(osutils, 'is_inside', is_inside_raises)
 
1303
        self.assertListRaises(RuntimeError, tree.iter_changes, basis_tree)
 
1304
 
1303
1305
    def test_simple_changes(self):
1304
1306
        tree = self.make_branch_and_tree('tree')
1305
1307
        self.build_tree(['tree/file'])