/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
 
110
110
default_transport = LocalURLServer
111
111
 
112
 
MODULES_TO_TEST = []
113
 
MODULES_TO_DOCTEST = [
114
 
        bzrlib,
115
 
        bzrlib.timestamp,
116
 
        bzrlib.errors,
117
 
        bzrlib.export,
118
 
        bzrlib.inventory,
119
 
        bzrlib.iterablefile,
120
 
        bzrlib.lockdir,
121
 
        bzrlib.merge3,
122
 
        bzrlib.option,
123
 
        bzrlib.store,
124
 
        bzrlib.version_info_formats.format_custom,
125
 
        # quoted to avoid module-loading circularity
126
 
        'bzrlib.tests',
127
 
        ]
128
 
 
129
112
 
130
113
def packages_to_test():
131
114
    """Return a list of packages to test.
315
298
        self.report_success(test)
316
299
        self._cleanupLogFile(test)
317
300
        unittest.TestResult.addSuccess(self, test)
 
301
        test._log_contents = ''
318
302
 
319
303
    def _testConcluded(self, test):
320
304
        """Common code when a test has finished.
357
341
            # seems best to treat this as success from point-of-view of unittest
358
342
            # -- it actually does nothing so it barely matters :)
359
343
            unittest.TestResult.addSuccess(self, test)
 
344
            test._log_contents = ''
360
345
 
361
346
    def printErrorList(self, flavour, errors):
362
347
        for test, err in errors:
417
402
        self.pb.update('[test 0/%d] starting...' % (self.num_tests))
418
403
 
419
404
    def _progress_prefix_text(self):
420
 
        a = '[%d' % self.count
 
405
        # the longer this text, the less space we have to show the test
 
406
        # name...
 
407
        a = '[%d' % self.count              # total that have been run
 
408
        # tests skipped as known not to be relevant are not important enough
 
409
        # to show here
 
410
        ## if self.skip_count:
 
411
        ##     a += ', %d skip' % self.skip_count
 
412
        ## if self.known_failure_count:
 
413
        ##     a += '+%dX' % self.known_failure_count
421
414
        if self.num_tests is not None:
422
415
            a +='/%d' % self.num_tests
423
 
        a += ' in %ds' % (time.time() - self._overall_start_time)
 
416
        a += ' in '
 
417
        runtime = time.time() - self._overall_start_time
 
418
        if runtime >= 60:
 
419
            a += '%dm%ds' % (runtime / 60, runtime % 60)
 
420
        else:
 
421
            a += '%ds' % runtime
424
422
        if self.error_count:
425
 
            a += ', %d errors' % self.error_count
 
423
            a += ', %d err' % self.error_count
426
424
        if self.failure_count:
427
 
            a += ', %d failed' % self.failure_count
428
 
        if self.known_failure_count:
429
 
            a += ', %d known failures' % self.known_failure_count
430
 
        if self.skip_count:
431
 
            a += ', %d skipped' % self.skip_count
 
425
            a += ', %d fail' % self.failure_count
432
426
        if self.unsupported:
433
 
            a += ', %d missing features' % len(self.unsupported)
 
427
            a += ', %d missing' % len(self.unsupported)
434
428
        a += ']'
435
429
        return a
436
430
 
790
784
    _keep_log_file = False
791
785
    # record lsprof data when performing benchmark calls.
792
786
    _gather_lsprof_in_benchmarks = False
 
787
    attrs_to_keep = ('_testMethodName', '_testMethodDoc',
 
788
                     '_log_contents', '_log_file_name', '_benchtime',
 
789
                     '_TestCase__testMethodName')
793
790
 
794
791
    def __init__(self, methodName='testMethod'):
795
792
        super(TestCase, self).__init__(methodName)
822
819
        import bzrlib.smart.server
823
820
        self._preserved_hooks = {
824
821
            bzrlib.branch.Branch: bzrlib.branch.Branch.hooks,
 
822
            bzrlib.mutabletree.MutableTree: bzrlib.mutabletree.MutableTree.hooks,
825
823
            bzrlib.smart.server.SmartTCPServer: bzrlib.smart.server.SmartTCPServer.hooks,
826
824
            }
827
825
        self.addCleanup(self._restoreHooks)
1284
1282
                    result.addSuccess(self)
1285
1283
                result.stopTest(self)
1286
1284
                return
1287
 
        return unittest.TestCase.run(self, result)
 
1285
        try:
 
1286
            return unittest.TestCase.run(self, result)
 
1287
        finally:
 
1288
            saved_attrs = {}
 
1289
            absent_attr = object()
 
1290
            for attr_name in self.attrs_to_keep:
 
1291
                attr = getattr(self, attr_name, absent_attr)
 
1292
                if attr is not absent_attr:
 
1293
                    saved_attrs[attr_name] = attr
 
1294
            self.__dict__ = saved_attrs
1288
1295
 
1289
1296
    def tearDown(self):
1290
1297
        self._runCleanups()
2270
2277
def condition_id_in_list(id_list):
2271
2278
    """Create a condition filter which verify that test's id in a list.
2272
2279
    
2273
 
    :param name: A TestIdList object.
 
2280
    :param id_list: A TestIdList object.
2274
2281
    :return: A callable that returns True if the test's id appears in the list.
2275
2282
    """
2276
2283
    def condition(test):
2277
 
        return id_list.test_in(test.id())
 
2284
        return id_list.includes(test.id())
2278
2285
    return condition
2279
2286
 
2280
2287
 
2575
2582
    return test_list
2576
2583
 
2577
2584
 
 
2585
def suite_matches_id_list(test_suite, id_list):
 
2586
    """Warns about tests not appearing or appearing more than once.
 
2587
 
 
2588
    :param test_suite: A TestSuite object.
 
2589
    :param test_id_list: The list of test ids that should be found in 
 
2590
         test_suite.
 
2591
 
 
2592
    :return: (absents, duplicates) absents is a list containing the test found
 
2593
        in id_list but not in test_suite, duplicates is a list containing the
 
2594
        test found multiple times in test_suite.
 
2595
 
 
2596
    When using a prefined test id list, it may occurs that some tests do not
 
2597
    exist anymore or that some tests use the same id. This function warns the
 
2598
    tester about potential problems in his workflow (test lists are volatile)
 
2599
    or in the test suite itself (using the same id for several tests does not
 
2600
    help to localize defects).
 
2601
    """
 
2602
    # Build a dict counting id occurrences
 
2603
    tests = dict()
 
2604
    for test in iter_suite_tests(test_suite):
 
2605
        id = test.id()
 
2606
        tests[id] = tests.get(id, 0) + 1
 
2607
 
 
2608
    not_found = []
 
2609
    duplicates = []
 
2610
    for id in id_list:
 
2611
        occurs = tests.get(id, 0)
 
2612
        if not occurs:
 
2613
            not_found.append(id)
 
2614
        elif occurs > 1:
 
2615
            duplicates.append(id)
 
2616
 
 
2617
    return not_found, duplicates
 
2618
 
 
2619
 
2578
2620
class TestIdList(object):
2579
2621
    """Test id list to filter a test suite.
2580
2622
 
2611
2653
                modules[mod_name] = True
2612
2654
        self.modules = modules
2613
2655
 
2614
 
    def is_module_name_used(self, module_name):
 
2656
    def refers_to(self, module_name):
2615
2657
        """Is there tests for the module or one of its sub modules."""
2616
2658
        return self.modules.has_key(module_name)
2617
2659
 
2618
 
    def test_in(self, test_id):
 
2660
    def includes(self, test_id):
2619
2661
        return self.tests.has_key(test_id)
2620
2662
 
2621
2663
 
2694
2736
                   'bzrlib.tests.test_missing',
2695
2737
                   'bzrlib.tests.test_msgeditor',
2696
2738
                   'bzrlib.tests.test_multiparent',
 
2739
                   'bzrlib.tests.test_mutabletree',
2697
2740
                   'bzrlib.tests.test_nonascii',
2698
2741
                   'bzrlib.tests.test_options',
2699
2742
                   'bzrlib.tests.test_osutils',
2711
2754
                   'bzrlib.tests.test_repository',
2712
2755
                   'bzrlib.tests.test_revert',
2713
2756
                   'bzrlib.tests.test_revision',
2714
 
                   'bzrlib.tests.test_revisionnamespaces',
 
2757
                   'bzrlib.tests.test_revisionspec',
2715
2758
                   'bzrlib.tests.test_revisiontree',
2716
2759
                   'bzrlib.tests.test_rio',
2717
2760
                   'bzrlib.tests.test_sampler',
2762
2805
        'bzrlib.tests.test_transport_implementations',
2763
2806
        'bzrlib.tests.test_read_bundle',
2764
2807
        ]
2765
 
    suite = TestUtil.TestSuite()
2766
2808
    loader = TestUtil.TestLoader()
2767
2809
 
2768
 
    if keep_only is not None:
 
2810
    if keep_only is None:
 
2811
        loader = TestUtil.TestLoader()
 
2812
    else:
2769
2813
        id_filter = TestIdList(keep_only)
 
2814
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
 
2815
    suite = loader.suiteClass()
2770
2816
 
2771
2817
    # modules building their suite with loadTestsFromModuleNames
2772
 
    if keep_only is None:
2773
 
        suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2774
 
    else:
2775
 
        for mod in [m for m in testmod_names
2776
 
                    if id_filter.is_module_name_used(m)]:
2777
 
            mod_suite = loader.loadTestsFromModuleNames([mod])
2778
 
            mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2779
 
            suite.addTest(mod_suite)
 
2818
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2780
2819
 
2781
2820
    # modules adapted for transport implementations
2782
2821
    from bzrlib.tests.test_transport_implementations import TransportTestProviderAdapter
2783
2822
    adapter = TransportTestProviderAdapter()
2784
 
    if keep_only is None:
2785
 
        adapt_modules(test_transport_implementations, adapter, loader, suite)
2786
 
    else:
2787
 
        for mod in [m for m in test_transport_implementations
2788
 
                    if id_filter.is_module_name_used(m)]:
2789
 
            mod_suite = TestUtil.TestSuite()
2790
 
            adapt_modules([mod], adapter, loader, mod_suite)
2791
 
            mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2792
 
            suite.addTest(mod_suite)
 
2823
    adapt_modules(test_transport_implementations, adapter, loader, suite)
2793
2824
 
2794
2825
    # modules defining their own test_suite()
2795
2826
    for package in [p for p in packages_to_test()
2796
2827
                    if (keep_only is None
2797
 
                        or id_filter.is_module_name_used(p.__name__))]:
 
2828
                        or id_filter.refers_to(p.__name__))]:
2798
2829
        pack_suite = package.test_suite()
2799
 
        if keep_only is not None:
2800
 
            pack_suite = filter_suite_by_id_list(pack_suite, id_filter)
2801
2830
        suite.addTest(pack_suite)
2802
2831
 
2803
 
    # XXX: MODULES_TO_TEST should be obsoleted ?
2804
 
    for mod in [m for m in MODULES_TO_TEST
2805
 
                if keep_only is None or id_filter.is_module_name_used(m)]:
2806
 
        mod_suite = loader.loadTestsFromModule(mod)
2807
 
        if keep_only is not None:
2808
 
            mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2809
 
        suite.addTest(mod_suite)
 
2832
    modules_to_doctest = [
 
2833
        'bzrlib',
 
2834
        'bzrlib.errors',
 
2835
        'bzrlib.export',
 
2836
        'bzrlib.inventory',
 
2837
        'bzrlib.iterablefile',
 
2838
        'bzrlib.lockdir',
 
2839
        'bzrlib.merge3',
 
2840
        'bzrlib.option',
 
2841
        'bzrlib.store',
 
2842
        'bzrlib.tests',
 
2843
        'bzrlib.timestamp',
 
2844
        'bzrlib.version_info_formats.format_custom',
 
2845
        ]
2810
2846
 
2811
 
    for mod in MODULES_TO_DOCTEST:
 
2847
    for mod in modules_to_doctest:
 
2848
        if not (keep_only is None or id_filter.refers_to(mod)):
 
2849
            # No tests to keep here, move along
 
2850
            continue
2812
2851
        try:
2813
2852
            doc_suite = doctest.DocTestSuite(mod)
2814
2853
        except ValueError, e:
2815
2854
            print '**failed to get doctest for: %s\n%s' % (mod, e)
2816
2855
            raise
2817
 
        if keep_only is not None:
2818
 
            # DocTests may use ids which doesn't contain the module name
2819
 
            doc_suite = filter_suite_by_id_list(doc_suite, id_filter)
2820
2856
        suite.addTest(doc_suite)
2821
2857
 
2822
2858
    default_encoding = sys.getdefaultencoding()
2823
2859
    for name, plugin in bzrlib.plugin.plugins().items():
2824
2860
        if keep_only is not None:
2825
 
            if not id_filter.is_module_name_used(plugin.module.__name__):
 
2861
            if not id_filter.refers_to(plugin.module.__name__):
2826
2862
                continue
2827
2863
        plugin_suite = plugin.test_suite()
2828
2864
        # We used to catch ImportError here and turn it into just a warning,
2829
2865
        # but really if you don't have --no-plugins this should be a failure.
2830
2866
        # mbp 20080213 - see http://bugs.launchpad.net/bugs/189771
 
2867
        if plugin_suite is None:
 
2868
            plugin_suite = plugin.load_plugin_tests(loader)
2831
2869
        if plugin_suite is not None:
2832
 
            if keep_only is not None:
2833
 
                plugin_suite = filter_suite_by_id_list(plugin_suite,
2834
 
                                                       id_filter)
2835
2870
            suite.addTest(plugin_suite)
2836
2871
        if default_encoding != sys.getdefaultencoding():
2837
2872
            bzrlib.trace.warning(
2839
2874
                sys.getdefaultencoding())
2840
2875
            reload(sys)
2841
2876
            sys.setdefaultencoding(default_encoding)
 
2877
 
 
2878
    if keep_only is not None:
 
2879
        # Now that the referred modules have loaded their tests, keep only the
 
2880
        # requested ones.
 
2881
        suite = filter_suite_by_id_list(suite, id_filter)
 
2882
        # Do some sanity checks on the id_list filtering
 
2883
        not_found, duplicates = suite_matches_id_list(suite, keep_only)
 
2884
        for id in not_found:
 
2885
            bzrlib.trace.warning('"%s" not found in the test suite', id)
 
2886
        for id in duplicates:
 
2887
            bzrlib.trace.warning('"%s" is used as an id by several tests', id)
 
2888
 
2842
2889
    return suite
2843
2890
 
2844
2891
 
2845
 
def multiply_tests_from_modules(module_name_list, scenario_iter):
 
2892
def multiply_tests_from_modules(module_name_list, scenario_iter, loader=None):
2846
2893
    """Adapt all tests in some given modules to given scenarios.
2847
2894
 
2848
2895
    This is the recommended public interface for test parameterization.
2854
2901
        modules.
2855
2902
    :param scenario_iter: Iterable of pairs of (scenario_name, 
2856
2903
        scenario_param_dict).
 
2904
    :param loader: If provided, will be used instead of a new 
 
2905
        bzrlib.tests.TestLoader() instance.
2857
2906
 
2858
2907
    This returns a new TestSuite containing the cross product of
2859
2908
    all the tests in all the modules, each repeated for each scenario.
2875
2924
    >>> tests[1].param
2876
2925
    2
2877
2926
    """
2878
 
    loader = TestLoader()
2879
 
    suite = TestSuite()
 
2927
    # XXX: Isn't load_tests() a better way to provide the same functionality
 
2928
    # without forcing a predefined TestScenarioApplier ? --vila 080215
 
2929
    if loader is None:
 
2930
        loader = TestUtil.TestLoader()
 
2931
 
 
2932
    suite = loader.suiteClass()
 
2933
 
2880
2934
    adapter = TestScenarioApplier()
2881
2935
    adapter.scenarios = list(scenario_iter)
2882
2936
    adapt_modules(module_name_list, adapter, loader, suite)