/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: Vincent Ladeuil
  • Date: 2008-05-08 21:22:06 UTC
  • mfrom: (3417 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3418.
  • Revision ID: v.ladeuil+lp@free.fr-20080508212206-kwlteu651izgs5we
merge bzr.dev to fix conflicts in NEWS

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
81
82
    deprecated_function,
82
83
    deprecated_method,
83
84
    deprecated_passed,
84
 
    zero_ninetyone,
85
 
    zero_ninetytwo,
86
 
    one_zero,
87
85
    )
88
86
import bzrlib.trace
89
87
from bzrlib.transport import get_transport
720
718
        return password
721
719
 
722
720
 
 
721
def _report_leaked_threads():
 
722
    bzrlib.trace.warning('%s is leaking threads among %d leaking tests',
 
723
                         TestCase._first_thread_leaker_id,
 
724
                         TestCase._leaking_threads_tests)
 
725
 
 
726
 
723
727
class TestCase(unittest.TestCase):
724
728
    """Base class for bzr unit tests.
725
729
    
741
745
    accidentally overlooked.
742
746
    """
743
747
 
 
748
    _active_threads = None
 
749
    _leaking_threads_tests = 0
 
750
    _first_thread_leaker_id = None
744
751
    _log_file_name = None
745
752
    _log_contents = ''
746
753
    _keep_log_file = False
747
754
    # record lsprof data when performing benchmark calls.
748
755
    _gather_lsprof_in_benchmarks = False
749
 
    attrs_to_keep = ('_testMethodName', '_testMethodDoc',
 
756
    attrs_to_keep = ('id', '_testMethodName', '_testMethodDoc',
750
757
                     '_log_contents', '_log_file_name', '_benchtime',
751
758
                     '_TestCase__testMethodName')
752
759
 
763
770
        self._benchtime = None
764
771
        self._clear_hooks()
765
772
        self._clear_debug_flags()
 
773
        TestCase._active_threads = threading.activeCount()
 
774
        self.addCleanup(self._check_leaked_threads)
 
775
 
 
776
    def _check_leaked_threads(self):
 
777
        active = threading.activeCount()
 
778
        leaked_threads = active - TestCase._active_threads
 
779
        TestCase._active_threads = active
 
780
        if leaked_threads:
 
781
            TestCase._leaking_threads_tests += 1
 
782
            if TestCase._first_thread_leaker_id is None:
 
783
                TestCase._first_thread_leaker_id = self.id()
 
784
                # we're not specifically told when all tests are finished.
 
785
                # This will do. We use a function to avoid keeping a reference
 
786
                # to a TestCase object.
 
787
                atexit.register(_report_leaked_threads)
766
788
 
767
789
    def _clear_debug_flags(self):
768
790
        """Prevent externally set debug flags affecting tests.
770
792
        Tests that want to use debug flags can just set them in the
771
793
        debug_flags set during setup/teardown.
772
794
        """
773
 
        if 'selftest_debug' not in debug.debug_flags:
 
795
        if 'allow_debug' not in selftest_debug_flags:
774
796
            self._preserved_debug_flags = set(debug.debug_flags)
775
797
            debug.debug_flags.clear()
776
798
            self.addCleanup(self._restore_debug_flags)
1060
1082
        To test that a deprecated method raises an error, do something like
1061
1083
        this::
1062
1084
 
1063
 
        self.assertRaises(errors.ReservedId,
1064
 
            self.applyDeprecated, zero_ninetyone,
1065
 
                br.append_revision, 'current:')
 
1085
            self.assertRaises(errors.ReservedId,
 
1086
                self.applyDeprecated,
 
1087
                deprecated_in((1, 5, 0)),
 
1088
                br.append_revision,
 
1089
                'current:')
1066
1090
 
1067
1091
        :param deprecation_format: The deprecation format that the callable
1068
1092
            should have been deprecated with. This is the same type as the
1501
1525
            elif isinstance(args[0], basestring):
1502
1526
                args = list(shlex.split(args[0]))
1503
1527
        else:
1504
 
            symbol_versioning.warn(zero_ninetyone %
1505
 
                                   "passing varargs to run_bzr_subprocess",
1506
 
                                   DeprecationWarning, stacklevel=3)
 
1528
            raise ValueError("passing varargs to run_bzr_subprocess")
1507
1529
        process = self.start_bzr_subprocess(args, env_changes=env_changes,
1508
1530
                                            working_dir=working_dir,
1509
1531
                                            allow_plugins=allow_plugins)
2279
2301
    return TestUtil.TestSuite(result)
2280
2302
 
2281
2303
 
2282
 
def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
2283
 
                       random_order=DEPRECATED_PARAMETER):
 
2304
def filter_suite_by_re(suite, pattern):
2284
2305
    """Create a test suite by filtering another one.
2285
2306
    
2286
2307
    :param suite:           the source suite
2287
2308
    :param pattern:         pattern that names must match
2288
 
    :param exclude_pattern: A pattern that names must not match. This parameter
2289
 
        is deprecated as of bzrlib 1.0. Please use the separate function
2290
 
        exclude_tests_by_re instead.
2291
 
    :param random_order:    If True, tests in the new suite will be put in
2292
 
        random order. This parameter is deprecated as of bzrlib 1.0. Please
2293
 
        use the separate function randomize_suite instead.
2294
2309
    :returns: the newly created suite
2295
2310
    """ 
2296
 
    if deprecated_passed(exclude_pattern):
2297
 
        symbol_versioning.warn(
2298
 
            one_zero % "passing exclude_pattern to filter_suite_by_re",
2299
 
                DeprecationWarning, stacklevel=2)
2300
 
        if exclude_pattern is not None:
2301
 
            suite = exclude_tests_by_re(suite, exclude_pattern)
2302
2311
    condition = condition_id_re(pattern)
2303
2312
    result_suite = filter_suite_by_condition(suite, condition)
2304
 
    if deprecated_passed(random_order):
2305
 
        symbol_versioning.warn(
2306
 
            one_zero % "passing random_order to filter_suite_by_re",
2307
 
                DeprecationWarning, stacklevel=2)
2308
 
        if random_order:
2309
 
            result_suite = randomize_suite(result_suite)
2310
2313
    return result_suite
2311
2314
 
2312
2315
 
2356
2359
    return TestUtil.TestSuite(tests)
2357
2360
 
2358
2361
 
2359
 
@deprecated_function(one_zero)
2360
 
def sort_suite_by_re(suite, pattern, exclude_pattern=None,
2361
 
                     random_order=False, append_rest=True):
2362
 
    """DEPRECATED: Create a test suite by sorting another one.
2363
 
 
2364
 
    This method has been decomposed into separate helper methods that should be
2365
 
    called directly:
2366
 
     - filter_suite_by_re
2367
 
     - exclude_tests_by_re
2368
 
     - randomize_suite
2369
 
     - split_suite_by_re
2370
 
    
2371
 
    :param suite:           the source suite
2372
 
    :param pattern:         pattern that names must match in order to go
2373
 
                            first in the new suite
2374
 
    :param exclude_pattern: pattern that names must not match, if any
2375
 
    :param random_order:    if True, tests in the new suite will be put in
2376
 
                            random order (with all tests matching pattern
2377
 
                            first).
2378
 
    :param append_rest:     if False, pattern is a strict filter and not
2379
 
                            just an ordering directive
2380
 
    :returns: the newly created suite
2381
 
    """ 
2382
 
    if exclude_pattern is not None:
2383
 
        suite = exclude_tests_by_re(suite, exclude_pattern)
2384
 
    if random_order:
2385
 
        order_changer = randomize_suite
2386
 
    else:
2387
 
        order_changer = preserve_input
2388
 
    if append_rest:
2389
 
        suites = map(order_changer, split_suite_by_re(suite, pattern))
2390
 
        return TestUtil.TestSuite(suites)
2391
 
    else:
2392
 
        return order_changer(filter_suite_by_re(suite, pattern))
2393
 
 
2394
 
 
2395
2362
def split_suite_by_re(suite, pattern):
2396
2363
    """Split a test suite into two by a regular expression.
2397
2364
    
2474
2441
    return result.wasSuccessful()
2475
2442
 
2476
2443
 
 
2444
# Controlled by "bzr selftest -E=..." option
 
2445
selftest_debug_flags = set()
 
2446
 
 
2447
 
2477
2448
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
2478
2449
             transport=None,
2479
2450
             test_suite_factory=None,
2485
2456
             exclude_pattern=None,
2486
2457
             strict=False,
2487
2458
             load_list=None,
 
2459
             debug_flags=None,
2488
2460
             ):
2489
2461
    """Run the whole test suite under the enhanced runner"""
2490
2462
    # XXX: Very ugly way to do this...
2498
2470
        transport = default_transport
2499
2471
    old_transport = default_transport
2500
2472
    default_transport = transport
 
2473
    global selftest_debug_flags
 
2474
    old_debug_flags = selftest_debug_flags
 
2475
    if debug_flags is not None:
 
2476
        selftest_debug_flags = set(debug_flags)
2501
2477
    try:
2502
2478
        if load_list is None:
2503
2479
            keep_only = None
2519
2495
                     strict=strict)
2520
2496
    finally:
2521
2497
        default_transport = old_transport
 
2498
        selftest_debug_flags = old_debug_flags
2522
2499
 
2523
2500
 
2524
2501
def load_test_id_list(file_name):
2802
2779
        'bzrlib.merge3',
2803
2780
        'bzrlib.option',
2804
2781
        'bzrlib.store',
 
2782
        'bzrlib.symbol_versioning',
2805
2783
        'bzrlib.tests',
2806
2784
        'bzrlib.timestamp',
2807
2785
        'bzrlib.version_info_formats.format_custom',