~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__dirstate_helpers.py

  • Committer: Vincent Ladeuil
  • Date: 2009-04-17 12:42:50 UTC
  • mto: (4331.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4332.
  • Revision ID: v.ladeuil+lp@free.fr-20090417124250-vb9zcpl3rgfu0uop
Modernize dirstate helpers tests parametrization.

* bzrlib/tests/test__dirstate_helpers.py:
(load_tests): Add parametrized tests for C/Python dirstate helpers
on top dir readers ones.
(TestUpdateEntry): Use parametrization facilities and load_tests
instead of inheritance.
(TestUpdateEntry.test_sha1provider_sha1_used): Ouch ! Direct use
of dirstate.update_entry, while correct now, wasn't respecting the
parametrization before (set_update_entry wasn't called).
(TestProcessEntry): Use parametrization facilities and load_tests
instead of inheritance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    test_osutils,
32
32
    )
33
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
 
34
49
 
35
50
def load_tests(basic_tests, module, loader):
36
 
    # FIXME: we should also parametrize agsinst C/Python
 
51
    # FIXME: we should also parametrize agsinst Sha1Providers !
37
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
 
38
83
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
39
 
        basic_tests, tests.condition_isinstance(
 
84
        remaining_tests, tests.condition_isinstance(
40
85
            test_dirstate.TestCaseWithDirState))
41
 
    tests.multiply_tests(dir_reader_tests,
42
 
                         test_osutils.dir_reader_scenarios(), suite)
 
86
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios, suite)
43
87
    suite.addTest(remaining_tests)
 
88
 
44
89
    return suite
45
90
 
46
91
 
47
 
class _CompiledDirstateHelpersFeature(tests.Feature):
48
 
    def _probe(self):
49
 
        try:
50
 
            import bzrlib._dirstate_helpers_c
51
 
        except ImportError:
52
 
            return False
53
 
        return True
54
 
 
55
 
    def feature_name(self):
56
 
        return 'bzrlib._dirstate_helpers_c'
57
 
 
58
 
CompiledDirstateHelpersFeature = _CompiledDirstateHelpersFeature()
59
 
 
60
 
 
61
92
class TestBisectPathMixin(object):
62
93
    """Test that _bisect_path_*() returns the expected values.
63
94
 
802
833
class TestUpdateEntry(test_dirstate.TestCaseWithDirState):
803
834
    """Test the DirState.update_entry functions"""
804
835
 
 
836
    # Set by load_tests
 
837
    update_entry = None
 
838
 
 
839
    def setUp(self):
 
840
        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
 
846
 
805
847
    def get_state_with_a(self):
806
848
        """Create a DirState tracking a single object named 'a'"""
807
849
        state = test_dirstate.InstrumentedDirState.initialize('dirstate')
808
850
        self.addCleanup(state.unlock)
809
851
        state.add('a', 'a-id', 'file', None, '')
810
852
        entry = state._get_entry(0, path_utf8='a')
811
 
        self.set_update_entry()
812
853
        return state, entry
813
854
 
814
 
    def set_update_entry(self):
815
 
        self.update_entry = dirstate.py_update_entry
816
 
 
817
855
    def test_observed_sha1_cachable(self):
818
856
        state, entry = self.get_state_with_a()
819
857
        atime = time.time() - 10
1034
1072
                         state._dirblock_state)
1035
1073
 
1036
1074
    def test_update_entry_tree_reference(self):
1037
 
        self.set_update_entry()
1038
1075
        state = test_dirstate.InstrumentedDirState.initialize('dirstate')
1039
1076
        self.addCleanup(state.unlock)
1040
1077
        state.add('r', 'r-id', 'tree-reference', None, '')
1076
1113
 
1077
1114
        return packed_stat
1078
1115
 
 
1116
    # FIXME: Add unicode version
1079
1117
    def create_and_test_symlink(self, state, entry):
1080
1118
        """Create a symlink at 'a' and verify the state finds it.
1081
1119
 
1178
1216
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1179
1217
                         entry[1])
1180
1218
 
1181
 
        # Make the disk object look old enough to cache (but it won't cache the sha
1182
 
        # as it is a new file).
 
1219
        # Make the disk object look old enough to cache (but it won't cache the
 
1220
        # sha as it is a new file).
1183
1221
        state.adjust_time(+20)
1184
1222
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
1185
1223
        self.update_entry(state, entry, abspath='a', stat_value=stat_value)
1206
1244
        entry = state._get_entry(0, path_utf8='a file')
1207
1245
        state._sha_cutoff_time()
1208
1246
        state._cutoff_time += 10
1209
 
        sha1 = dirstate.update_entry(state, entry, 'tree/a file',
1210
 
            os.lstat('tree/a file'))
 
1247
        sha1 = self.update_entry(state, entry, 'tree/a file',
 
1248
                                 os.lstat('tree/a file'))
1211
1249
        self.assertEqual(expected_sha, sha1)
1212
1250
 
1213
1251
    def test_sha1provider_stat_and_sha1_used(self):
1218
1256
        state._sha1_provider = UppercaseSHA1Provider()
1219
1257
        # If we used the standard provider, it would look like nothing has
1220
1258
        # changed
1221
 
        file_ids_changed = [change[0] for change 
1222
 
                in tree.iter_changes(tree.basis_tree())]
 
1259
        file_ids_changed = [change[0] for change
 
1260
                            in tree.iter_changes(tree.basis_tree())]
1223
1261
        self.assertEqual(['a-file-id'], file_ids_changed)
1224
1262
 
1225
1263
 
1240
1278
        return statvalue, sha1
1241
1279
 
1242
1280
 
1243
 
class TestCompiledUpdateEntry(TestUpdateEntry):
1244
 
    """Test the pyrex implementation of _read_dirblocks"""
1245
 
 
1246
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
1247
 
 
1248
 
    def set_update_entry(self):
1249
 
        from bzrlib._dirstate_helpers_c import update_entry
1250
 
        self.update_entry = update_entry
1251
 
 
1252
 
 
1253
 
class TestProcessEntryPython(test_dirstate.TestCaseWithDirState):
 
1281
class TestProcessEntry(test_dirstate.TestCaseWithDirState):
 
1282
 
 
1283
    # Set by load_tests
 
1284
    _process_entry = None
1254
1285
 
1255
1286
    def setUp(self):
1256
 
        super(TestProcessEntryPython, self).setUp()
1257
 
        self.setup_process_entry()
1258
 
 
1259
 
    def setup_process_entry(self):
1260
 
        from bzrlib import dirstate
 
1287
        super(TestProcessEntry, self).setUp()
1261
1288
        orig = dirstate._process_entry
1262
1289
        def cleanup():
1263
1290
            dirstate._process_entry = orig
1264
1291
        self.addCleanup(cleanup)
1265
 
        dirstate._process_entry = dirstate.ProcessEntryPython
 
1292
        dirstate._process_entry = self._process_entry
1266
1293
 
1267
1294
    def assertChangedFileIds(self, expected, tree):
1268
1295
        tree.lock_read()
1292
1319
        state._sha1_provider = UppercaseSHA1Provider()
1293
1320
        self.assertChangedFileIds(['file-id'], tree)
1294
1321
 
1295
 
 
1296
 
class TestProcessEntryC(TestProcessEntryPython):
1297
 
    _test_needs_features = [CompiledDirstateHelpersFeature]
1298
 
 
1299
 
    def setup_process_entry(self):
1300
 
        from bzrlib import _dirstate_helpers_c
1301
 
        orig = dirstate._process_entry
1302
 
        def cleanup():
1303
 
            dirstate._process_entry = orig
1304
 
        self.addCleanup(cleanup)
1305
 
        dirstate._process_entry = _dirstate_helpers_c.ProcessEntryC
1306