/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

  • Committer: Andrew Bennetts
  • Date: 2008-04-10 07:58:01 UTC
  • mfrom: (3352 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3357.
  • Revision ID: andrew.bennetts@canonical.com-20080410075801-n24st9wfiizkvszp
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.timestamp,
115
 
        bzrlib.errors,
116
 
        bzrlib.export,
117
 
        bzrlib.inventory,
118
 
        bzrlib.iterablefile,
119
 
        bzrlib.lockdir,
120
 
        bzrlib.merge3,
121
 
        bzrlib.option,
122
 
        bzrlib.store,
123
 
        bzrlib.version_info_formats.format_custom,
124
 
        # quoted to avoid module-loading circularity
125
 
        'bzrlib.tests',
126
 
        ]
127
 
 
128
112
 
129
113
def packages_to_test():
130
114
    """Return a list of packages to test.
314
298
        self.report_success(test)
315
299
        self._cleanupLogFile(test)
316
300
        unittest.TestResult.addSuccess(self, test)
 
301
        test._log_contents = ''
317
302
 
318
303
    def _testConcluded(self, test):
319
304
        """Common code when a test has finished.
356
341
            # seems best to treat this as success from point-of-view of unittest
357
342
            # -- it actually does nothing so it barely matters :)
358
343
            unittest.TestResult.addSuccess(self, test)
 
344
            test._log_contents = ''
359
345
 
360
346
    def printErrorList(self, flavour, errors):
361
347
        for test, err in errors:
416
402
        self.pb.update('[test 0/%d] starting...' % (self.num_tests))
417
403
 
418
404
    def _progress_prefix_text(self):
419
 
        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
420
414
        if self.num_tests is not None:
421
415
            a +='/%d' % self.num_tests
422
 
        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
423
422
        if self.error_count:
424
 
            a += ', %d errors' % self.error_count
 
423
            a += ', %d err' % self.error_count
425
424
        if self.failure_count:
426
 
            a += ', %d failed' % self.failure_count
427
 
        if self.known_failure_count:
428
 
            a += ', %d known failures' % self.known_failure_count
429
 
        if self.skip_count:
430
 
            a += ', %d skipped' % self.skip_count
 
425
            a += ', %d fail' % self.failure_count
431
426
        if self.unsupported:
432
 
            a += ', %d missing features' % len(self.unsupported)
 
427
            a += ', %d missing' % len(self.unsupported)
433
428
        a += ']'
434
429
        return a
435
430
 
789
784
    _keep_log_file = False
790
785
    # record lsprof data when performing benchmark calls.
791
786
    _gather_lsprof_in_benchmarks = False
 
787
    attrs_to_keep = ('_testMethodName', '_testMethodDoc',
 
788
                     '_log_contents', '_log_file_name', '_benchtime',
 
789
                     '_TestCase__testMethodName')
792
790
 
793
791
    def __init__(self, methodName='testMethod'):
794
792
        super(TestCase, self).__init__(methodName)
810
808
        Tests that want to use debug flags can just set them in the
811
809
        debug_flags set during setup/teardown.
812
810
        """
813
 
        self._preserved_debug_flags = set(debug.debug_flags)
814
 
        debug.debug_flags.clear()
815
 
        self.addCleanup(self._restore_debug_flags)
 
811
        if 'selftest_debug' not in debug.debug_flags:
 
812
            self._preserved_debug_flags = set(debug.debug_flags)
 
813
            debug.debug_flags.clear()
 
814
            self.addCleanup(self._restore_debug_flags)
816
815
 
817
816
    def _clear_hooks(self):
818
817
        # prevent hooks affecting tests
820
819
        import bzrlib.smart.server
821
820
        self._preserved_hooks = {
822
821
            bzrlib.branch.Branch: bzrlib.branch.Branch.hooks,
 
822
            bzrlib.mutabletree.MutableTree: bzrlib.mutabletree.MutableTree.hooks,
823
823
            bzrlib.smart.server.SmartTCPServer: bzrlib.smart.server.SmartTCPServer.hooks,
824
824
            }
825
825
        self.addCleanup(self._restoreHooks)
1282
1282
                    result.addSuccess(self)
1283
1283
                result.stopTest(self)
1284
1284
                return
1285
 
        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
1286
1295
 
1287
1296
    def tearDown(self):
1288
1297
        self._runCleanups()
2268
2277
def condition_id_in_list(id_list):
2269
2278
    """Create a condition filter which verify that test's id in a list.
2270
2279
    
2271
 
    :param name: A TestIdList object.
 
2280
    :param id_list: A TestIdList object.
2272
2281
    :return: A callable that returns True if the test's id appears in the list.
2273
2282
    """
2274
2283
    def condition(test):
2275
 
        return id_list.test_in(test.id())
 
2284
        return id_list.includes(test.id())
2276
2285
    return condition
2277
2286
 
2278
2287
 
2573
2582
    return test_list
2574
2583
 
2575
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
 
2576
2620
class TestIdList(object):
2577
2621
    """Test id list to filter a test suite.
2578
2622
 
2609
2653
                modules[mod_name] = True
2610
2654
        self.modules = modules
2611
2655
 
2612
 
    def is_module_name_used(self, module_name):
 
2656
    def refers_to(self, module_name):
2613
2657
        """Is there tests for the module or one of its sub modules."""
2614
2658
        return self.modules.has_key(module_name)
2615
2659
 
2616
 
    def test_in(self, test_id):
 
2660
    def includes(self, test_id):
2617
2661
        return self.tests.has_key(test_id)
2618
2662
 
2619
2663
 
2651
2695
                   'bzrlib.tests.test_deprecated_graph',
2652
2696
                   'bzrlib.tests.test_diff',
2653
2697
                   'bzrlib.tests.test_dirstate',
 
2698
                   'bzrlib.tests.test_directory_service',
2654
2699
                   'bzrlib.tests.test_email_message',
2655
2700
                   'bzrlib.tests.test_errors',
2656
2701
                   'bzrlib.tests.test_escaped_store',
2691
2736
                   'bzrlib.tests.test_missing',
2692
2737
                   'bzrlib.tests.test_msgeditor',
2693
2738
                   'bzrlib.tests.test_multiparent',
 
2739
                   'bzrlib.tests.test_mutabletree',
2694
2740
                   'bzrlib.tests.test_nonascii',
2695
2741
                   'bzrlib.tests.test_options',
2696
2742
                   'bzrlib.tests.test_osutils',
2708
2754
                   'bzrlib.tests.test_repository',
2709
2755
                   'bzrlib.tests.test_revert',
2710
2756
                   'bzrlib.tests.test_revision',
2711
 
                   'bzrlib.tests.test_revisionnamespaces',
 
2757
                   'bzrlib.tests.test_revisionspec',
2712
2758
                   'bzrlib.tests.test_revisiontree',
2713
2759
                   'bzrlib.tests.test_rio',
2714
2760
                   'bzrlib.tests.test_sampler',
2741
2787
                   'bzrlib.tests.test_tsort',
2742
2788
                   'bzrlib.tests.test_tuned_gzip',
2743
2789
                   'bzrlib.tests.test_ui',
 
2790
                   'bzrlib.tests.test_uncommit',
2744
2791
                   'bzrlib.tests.test_upgrade',
2745
2792
                   'bzrlib.tests.test_urlutils',
2746
2793
                   'bzrlib.tests.test_versionedfile',
2758
2805
        'bzrlib.tests.test_transport_implementations',
2759
2806
        'bzrlib.tests.test_read_bundle',
2760
2807
        ]
2761
 
    suite = TestUtil.TestSuite()
2762
2808
    loader = TestUtil.TestLoader()
2763
2809
 
2764
 
    if keep_only is not None:
 
2810
    if keep_only is None:
 
2811
        loader = TestUtil.TestLoader()
 
2812
    else:
2765
2813
        id_filter = TestIdList(keep_only)
 
2814
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
 
2815
    suite = loader.suiteClass()
2766
2816
 
2767
2817
    # modules building their suite with loadTestsFromModuleNames
2768
 
    if keep_only is None:
2769
 
        suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2770
 
    else:
2771
 
        for mod in [m for m in testmod_names
2772
 
                    if id_filter.is_module_name_used(m)]:
2773
 
            mod_suite = loader.loadTestsFromModuleNames([mod])
2774
 
            mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2775
 
            suite.addTest(mod_suite)
 
2818
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2776
2819
 
2777
2820
    # modules adapted for transport implementations
2778
2821
    from bzrlib.tests.test_transport_implementations import TransportTestProviderAdapter
2779
2822
    adapter = TransportTestProviderAdapter()
2780
 
    if keep_only is None:
2781
 
        adapt_modules(test_transport_implementations, adapter, loader, suite)
2782
 
    else:
2783
 
        for mod in [m for m in test_transport_implementations
2784
 
                    if id_filter.is_module_name_used(m)]:
2785
 
            mod_suite = TestUtil.TestSuite()
2786
 
            adapt_modules([mod], adapter, loader, mod_suite)
2787
 
            mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2788
 
            suite.addTest(mod_suite)
 
2823
    adapt_modules(test_transport_implementations, adapter, loader, suite)
2789
2824
 
2790
2825
    # modules defining their own test_suite()
2791
2826
    for package in [p for p in packages_to_test()
2792
2827
                    if (keep_only is None
2793
 
                        or id_filter.is_module_name_used(p.__name__))]:
 
2828
                        or id_filter.refers_to(p.__name__))]:
2794
2829
        pack_suite = package.test_suite()
2795
 
        if keep_only is not None:
2796
 
            pack_suite = filter_suite_by_id_list(pack_suite, id_filter)
2797
2830
        suite.addTest(pack_suite)
2798
2831
 
2799
 
    # XXX: MODULES_TO_TEST should be obsoleted ?
2800
 
    for mod in [m for m in MODULES_TO_TEST
2801
 
                if keep_only is None or id_filter.is_module_name_used(m)]:
2802
 
        mod_suite = loader.loadTestsFromModule(mod)
2803
 
        if keep_only is not None:
2804
 
            mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2805
 
        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
        ]
2806
2846
 
2807
 
    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
2808
2851
        try:
2809
2852
            doc_suite = doctest.DocTestSuite(mod)
2810
2853
        except ValueError, e:
2811
2854
            print '**failed to get doctest for: %s\n%s' % (mod, e)
2812
2855
            raise
2813
 
        if keep_only is not None:
2814
 
            # DocTests may use ids which doesn't contain the module name
2815
 
            doc_suite = filter_suite_by_id_list(doc_suite, id_filter)
2816
2856
        suite.addTest(doc_suite)
2817
2857
 
2818
2858
    default_encoding = sys.getdefaultencoding()
2819
2859
    for name, plugin in bzrlib.plugin.plugins().items():
2820
2860
        if keep_only is not None:
2821
 
            if not id_filter.is_module_name_used(plugin.module.__name__):
 
2861
            if not id_filter.refers_to(plugin.module.__name__):
2822
2862
                continue
2823
2863
        plugin_suite = plugin.test_suite()
2824
2864
        # We used to catch ImportError here and turn it into just a warning,
2825
2865
        # but really if you don't have --no-plugins this should be a failure.
2826
2866
        # mbp 20080213 - see http://bugs.launchpad.net/bugs/189771
 
2867
        if plugin_suite is None:
 
2868
            plugin_suite = plugin.load_plugin_tests(loader)
2827
2869
        if plugin_suite is not None:
2828
 
            if keep_only is not None:
2829
 
                plugin_suite = filter_suite_by_id_list(plugin_suite,
2830
 
                                                       id_filter)
2831
2870
            suite.addTest(plugin_suite)
2832
2871
        if default_encoding != sys.getdefaultencoding():
2833
2872
            bzrlib.trace.warning(
2835
2874
                sys.getdefaultencoding())
2836
2875
            reload(sys)
2837
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
 
2838
2889
    return suite
2839
2890
 
2840
2891
 
2841
 
def multiply_tests_from_modules(module_name_list, scenario_iter):
 
2892
def multiply_tests_from_modules(module_name_list, scenario_iter, loader=None):
2842
2893
    """Adapt all tests in some given modules to given scenarios.
2843
2894
 
2844
2895
    This is the recommended public interface for test parameterization.
2850
2901
        modules.
2851
2902
    :param scenario_iter: Iterable of pairs of (scenario_name, 
2852
2903
        scenario_param_dict).
 
2904
    :param loader: If provided, will be used instead of a new 
 
2905
        bzrlib.tests.TestLoader() instance.
2853
2906
 
2854
2907
    This returns a new TestSuite containing the cross product of
2855
2908
    all the tests in all the modules, each repeated for each scenario.
2871
2924
    >>> tests[1].param
2872
2925
    2
2873
2926
    """
2874
 
    loader = TestLoader()
2875
 
    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
 
2876
2934
    adapter = TestScenarioApplier()
2877
2935
    adapter.scenarios = list(scenario_iter)
2878
2936
    adapt_modules(module_name_list, adapter, loader, suite)