/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 up bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
from subprocess import Popen, PIPE
43
43
import sys
44
44
import tempfile
 
45
import threading
45
46
import time
46
47
import unittest
47
48
import warnings
80
81
    deprecated_function,
81
82
    deprecated_method,
82
83
    deprecated_passed,
83
 
    zero_ninetyone,
84
 
    zero_ninetytwo,
85
 
    one_zero,
86
84
    )
87
85
import bzrlib.trace
88
86
from bzrlib.transport import get_transport
109
107
default_transport = LocalURLServer
110
108
 
111
109
 
112
 
def packages_to_test():
113
 
    """Return a list of packages to test.
114
 
 
115
 
    The packages are not globally imported so that import failures are
116
 
    triggered when running selftest, not when importing the command.
117
 
    """
118
 
    import bzrlib.doc
119
 
    import bzrlib.tests.blackbox
120
 
    import bzrlib.tests.branch_implementations
121
 
    import bzrlib.tests.bzrdir_implementations
122
 
    import bzrlib.tests.commands
123
 
    import bzrlib.tests.interrepository_implementations
124
 
    import bzrlib.tests.interversionedfile_implementations
125
 
    import bzrlib.tests.intertree_implementations
126
 
    import bzrlib.tests.inventory_implementations
127
 
    import bzrlib.tests.per_lock
128
 
    import bzrlib.tests.repository_implementations
129
 
    import bzrlib.tests.revisionstore_implementations
130
 
    import bzrlib.tests.tree_implementations
131
 
    import bzrlib.tests.workingtree_implementations
132
 
    return [
133
 
            bzrlib.doc,
134
 
            bzrlib.tests.blackbox,
135
 
            bzrlib.tests.branch_implementations,
136
 
            bzrlib.tests.bzrdir_implementations,
137
 
            bzrlib.tests.commands,
138
 
            bzrlib.tests.interrepository_implementations,
139
 
            bzrlib.tests.interversionedfile_implementations,
140
 
            bzrlib.tests.intertree_implementations,
141
 
            bzrlib.tests.inventory_implementations,
142
 
            bzrlib.tests.per_lock,
143
 
            bzrlib.tests.repository_implementations,
144
 
            bzrlib.tests.revisionstore_implementations,
145
 
            bzrlib.tests.tree_implementations,
146
 
            bzrlib.tests.workingtree_implementations,
147
 
            ]
148
 
 
149
 
 
150
110
class ExtendedTestResult(unittest._TextTestResult):
151
111
    """Accepts, reports and accumulates the results of running tests.
152
112
 
757
717
        return password
758
718
 
759
719
 
 
720
def _report_leaked_threads():
 
721
    bzrlib.trace.warning('%s is leaking threads among %d leaking tests',
 
722
                         TestCase._first_thread_leaker_id,
 
723
                         TestCase._leaking_threads_tests)
 
724
 
 
725
 
760
726
class TestCase(unittest.TestCase):
761
727
    """Base class for bzr unit tests.
762
728
    
778
744
    accidentally overlooked.
779
745
    """
780
746
 
 
747
    _active_threads = None
 
748
    _leaking_threads_tests = 0
 
749
    _first_thread_leaker_id = None
781
750
    _log_file_name = None
782
751
    _log_contents = ''
783
752
    _keep_log_file = False
784
753
    # record lsprof data when performing benchmark calls.
785
754
    _gather_lsprof_in_benchmarks = False
786
 
    attrs_to_keep = ('_testMethodName', '_testMethodDoc',
 
755
    attrs_to_keep = ('id', '_testMethodName', '_testMethodDoc',
787
756
                     '_log_contents', '_log_file_name', '_benchtime',
788
757
                     '_TestCase__testMethodName')
789
758
 
800
769
        self._benchtime = None
801
770
        self._clear_hooks()
802
771
        self._clear_debug_flags()
 
772
        TestCase._active_threads = threading.activeCount()
 
773
        self.addCleanup(self._check_leaked_threads)
 
774
 
 
775
    def _check_leaked_threads(self):
 
776
        active = threading.activeCount()
 
777
        leaked_threads = active - TestCase._active_threads
 
778
        TestCase._active_threads = active
 
779
        if leaked_threads:
 
780
            TestCase._leaking_threads_tests += 1
 
781
            if TestCase._first_thread_leaker_id is None:
 
782
                TestCase._first_thread_leaker_id = self.id()
 
783
                # we're not specifically told when all tests are finished.
 
784
                # This will do. We use a function to avoid keeping a reference
 
785
                # to a TestCase object.
 
786
                atexit.register(_report_leaked_threads)
803
787
 
804
788
    def _clear_debug_flags(self):
805
789
        """Prevent externally set debug flags affecting tests.
807
791
        Tests that want to use debug flags can just set them in the
808
792
        debug_flags set during setup/teardown.
809
793
        """
810
 
        if 'selftest_debug' not in debug.debug_flags:
 
794
        if 'allow_debug' not in selftest_debug_flags:
811
795
            self._preserved_debug_flags = set(debug.debug_flags)
812
796
            debug.debug_flags.clear()
813
797
            self.addCleanup(self._restore_debug_flags)
1097
1081
        To test that a deprecated method raises an error, do something like
1098
1082
        this::
1099
1083
 
1100
 
        self.assertRaises(errors.ReservedId,
1101
 
            self.applyDeprecated, zero_ninetyone,
1102
 
                br.append_revision, 'current:')
 
1084
            self.assertRaises(errors.ReservedId,
 
1085
                self.applyDeprecated,
 
1086
                deprecated_in((1, 5, 0)),
 
1087
                br.append_revision,
 
1088
                'current:')
1103
1089
 
1104
1090
        :param deprecation_format: The deprecation format that the callable
1105
1091
            should have been deprecated with. This is the same type as the
1538
1524
            elif isinstance(args[0], basestring):
1539
1525
                args = list(shlex.split(args[0]))
1540
1526
        else:
1541
 
            symbol_versioning.warn(zero_ninetyone %
1542
 
                                   "passing varargs to run_bzr_subprocess",
1543
 
                                   DeprecationWarning, stacklevel=3)
 
1527
            raise ValueError("passing varargs to run_bzr_subprocess")
1544
1528
        process = self.start_bzr_subprocess(args, env_changes=env_changes,
1545
1529
                                            working_dir=working_dir,
1546
1530
                                            allow_plugins=allow_plugins)
2316
2300
    return TestUtil.TestSuite(result)
2317
2301
 
2318
2302
 
2319
 
def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
2320
 
                       random_order=DEPRECATED_PARAMETER):
 
2303
def filter_suite_by_re(suite, pattern):
2321
2304
    """Create a test suite by filtering another one.
2322
2305
    
2323
2306
    :param suite:           the source suite
2324
2307
    :param pattern:         pattern that names must match
2325
 
    :param exclude_pattern: A pattern that names must not match. This parameter
2326
 
        is deprecated as of bzrlib 1.0. Please use the separate function
2327
 
        exclude_tests_by_re instead.
2328
 
    :param random_order:    If True, tests in the new suite will be put in
2329
 
        random order. This parameter is deprecated as of bzrlib 1.0. Please
2330
 
        use the separate function randomize_suite instead.
2331
2308
    :returns: the newly created suite
2332
2309
    """ 
2333
 
    if deprecated_passed(exclude_pattern):
2334
 
        symbol_versioning.warn(
2335
 
            one_zero % "passing exclude_pattern to filter_suite_by_re",
2336
 
                DeprecationWarning, stacklevel=2)
2337
 
        if exclude_pattern is not None:
2338
 
            suite = exclude_tests_by_re(suite, exclude_pattern)
2339
2310
    condition = condition_id_re(pattern)
2340
2311
    result_suite = filter_suite_by_condition(suite, condition)
2341
 
    if deprecated_passed(random_order):
2342
 
        symbol_versioning.warn(
2343
 
            one_zero % "passing random_order to filter_suite_by_re",
2344
 
                DeprecationWarning, stacklevel=2)
2345
 
        if random_order:
2346
 
            result_suite = randomize_suite(result_suite)
2347
2312
    return result_suite
2348
2313
 
2349
2314
 
2393
2358
    return TestUtil.TestSuite(tests)
2394
2359
 
2395
2360
 
2396
 
@deprecated_function(one_zero)
2397
 
def sort_suite_by_re(suite, pattern, exclude_pattern=None,
2398
 
                     random_order=False, append_rest=True):
2399
 
    """DEPRECATED: Create a test suite by sorting another one.
2400
 
 
2401
 
    This method has been decomposed into separate helper methods that should be
2402
 
    called directly:
2403
 
     - filter_suite_by_re
2404
 
     - exclude_tests_by_re
2405
 
     - randomize_suite
2406
 
     - split_suite_by_re
2407
 
    
2408
 
    :param suite:           the source suite
2409
 
    :param pattern:         pattern that names must match in order to go
2410
 
                            first in the new suite
2411
 
    :param exclude_pattern: pattern that names must not match, if any
2412
 
    :param random_order:    if True, tests in the new suite will be put in
2413
 
                            random order (with all tests matching pattern
2414
 
                            first).
2415
 
    :param append_rest:     if False, pattern is a strict filter and not
2416
 
                            just an ordering directive
2417
 
    :returns: the newly created suite
2418
 
    """ 
2419
 
    if exclude_pattern is not None:
2420
 
        suite = exclude_tests_by_re(suite, exclude_pattern)
2421
 
    if random_order:
2422
 
        order_changer = randomize_suite
2423
 
    else:
2424
 
        order_changer = preserve_input
2425
 
    if append_rest:
2426
 
        suites = map(order_changer, split_suite_by_re(suite, pattern))
2427
 
        return TestUtil.TestSuite(suites)
2428
 
    else:
2429
 
        return order_changer(filter_suite_by_re(suite, pattern))
2430
 
 
2431
 
 
2432
2361
def split_suite_by_re(suite, pattern):
2433
2362
    """Split a test suite into two by a regular expression.
2434
2363
    
2511
2440
    return result.wasSuccessful()
2512
2441
 
2513
2442
 
 
2443
# Controlled by "bzr selftest -E=..." option
 
2444
selftest_debug_flags = set()
 
2445
 
 
2446
 
2514
2447
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
2515
2448
             transport=None,
2516
2449
             test_suite_factory=None,
2522
2455
             exclude_pattern=None,
2523
2456
             strict=False,
2524
2457
             load_list=None,
 
2458
             debug_flags=None,
2525
2459
             ):
2526
2460
    """Run the whole test suite under the enhanced runner"""
2527
2461
    # XXX: Very ugly way to do this...
2535
2469
        transport = default_transport
2536
2470
    old_transport = default_transport
2537
2471
    default_transport = transport
 
2472
    global selftest_debug_flags
 
2473
    old_debug_flags = selftest_debug_flags
 
2474
    if debug_flags is not None:
 
2475
        selftest_debug_flags = set(debug_flags)
2538
2476
    try:
2539
2477
        if load_list is None:
2540
2478
            keep_only = None
2556
2494
                     strict=strict)
2557
2495
    finally:
2558
2496
        default_transport = old_transport
 
2497
        selftest_debug_flags = old_debug_flags
2559
2498
 
2560
2499
 
2561
2500
def load_test_id_list(file_name):
2669
2608
    suite on a global basis, but it is not encouraged.
2670
2609
    """
2671
2610
    testmod_names = [
 
2611
                   'bzrlib.doc',
2672
2612
                   'bzrlib.util.tests.test_bencode',
 
2613
                   'bzrlib.tests.blackbox',
 
2614
                   'bzrlib.tests.branch_implementations',
 
2615
                   'bzrlib.tests.bzrdir_implementations',
 
2616
                   'bzrlib.tests.commands',
 
2617
                   'bzrlib.tests.inventory_implementations',
 
2618
                   'bzrlib.tests.interrepository_implementations',
 
2619
                   'bzrlib.tests.intertree_implementations',
 
2620
                   'bzrlib.tests.interversionedfile_implementations',
 
2621
                   'bzrlib.tests.per_lock',
 
2622
                   'bzrlib.tests.repository_implementations',
 
2623
                   'bzrlib.tests.revisionstore_implementations',
2673
2624
                   'bzrlib.tests.test__dirstate_helpers',
2674
2625
                   'bzrlib.tests.test_ancestry',
2675
2626
                   'bzrlib.tests.test_annotate',
2746
2697
                   'bzrlib.tests.test_permissions',
2747
2698
                   'bzrlib.tests.test_plugins',
2748
2699
                   'bzrlib.tests.test_progress',
 
2700
                   'bzrlib.tests.test_read_bundle',
2749
2701
                   'bzrlib.tests.test_reconfigure',
2750
2702
                   'bzrlib.tests.test_reconcile',
2751
2703
                   'bzrlib.tests.test_registry',
2781
2733
                   'bzrlib.tests.test_transactions',
2782
2734
                   'bzrlib.tests.test_transform',
2783
2735
                   'bzrlib.tests.test_transport',
 
2736
                   'bzrlib.tests.test_transport_implementations',
2784
2737
                   'bzrlib.tests.test_tree',
2785
2738
                   'bzrlib.tests.test_treebuilder',
2786
2739
                   'bzrlib.tests.test_tsort',
2799
2752
                   'bzrlib.tests.test_workingtree_4',
2800
2753
                   'bzrlib.tests.test_wsgi',
2801
2754
                   'bzrlib.tests.test_xml',
 
2755
                   'bzrlib.tests.tree_implementations',
 
2756
                   'bzrlib.tests.workingtree_implementations',
2802
2757
                   ]
2803
 
    test_transport_implementations = [
2804
 
        'bzrlib.tests.test_transport_implementations',
2805
 
        'bzrlib.tests.test_read_bundle',
2806
 
        ]
 
2758
 
2807
2759
    loader = TestUtil.TestLoader()
2808
2760
 
2809
2761
    if keep_only is None:
2816
2768
    # modules building their suite with loadTestsFromModuleNames
2817
2769
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2818
2770
 
2819
 
    # modules adapted for transport implementations
2820
 
    from bzrlib.tests.test_transport_implementations import TransportTestProviderAdapter
2821
 
    adapter = TransportTestProviderAdapter()
2822
 
    adapt_modules(test_transport_implementations, adapter, loader, suite)
2823
 
 
2824
 
    # modules defining their own test_suite()
2825
 
    for package in [p for p in packages_to_test()
2826
 
                    if (keep_only is None
2827
 
                        or id_filter.refers_to(p.__name__))]:
2828
 
        pack_suite = package.test_suite()
2829
 
        suite.addTest(pack_suite)
2830
 
 
2831
2771
    modules_to_doctest = [
2832
2772
        'bzrlib',
2833
2773
        'bzrlib.errors',
2838
2778
        'bzrlib.merge3',
2839
2779
        'bzrlib.option',
2840
2780
        'bzrlib.store',
 
2781
        'bzrlib.symbol_versioning',
2841
2782
        'bzrlib.tests',
2842
2783
        'bzrlib.timestamp',
2843
2784
        'bzrlib.version_info_formats.format_custom',