109
107
default_transport = LocalURLServer
112
def packages_to_test():
113
"""Return a list of packages to test.
115
The packages are not globally imported so that import failures are
116
triggered when running selftest, not when importing the command.
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
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,
150
110
class ExtendedTestResult(unittest._TextTestResult):
151
111
"""Accepts, reports and accumulates the results of running tests.
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)
760
726
class TestCase(unittest.TestCase):
761
727
"""Base class for bzr unit tests.
778
744
accidentally overlooked.
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')
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)
775
def _check_leaked_threads(self):
776
active = threading.activeCount()
777
leaked_threads = active - TestCase._active_threads
778
TestCase._active_threads = active
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)
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.
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
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)),
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]))
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)
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.
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
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)
2346
result_suite = randomize_suite(result_suite)
2347
2312
return result_suite
2393
2358
return TestUtil.TestSuite(tests)
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.
2401
This method has been decomposed into separate helper methods that should be
2403
- filter_suite_by_re
2404
- exclude_tests_by_re
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
2415
:param append_rest: if False, pattern is a strict filter and not
2416
just an ordering directive
2417
:returns: the newly created suite
2419
if exclude_pattern is not None:
2420
suite = exclude_tests_by_re(suite, exclude_pattern)
2422
order_changer = randomize_suite
2424
order_changer = preserve_input
2426
suites = map(order_changer, split_suite_by_re(suite, pattern))
2427
return TestUtil.TestSuite(suites)
2429
return order_changer(filter_suite_by_re(suite, pattern))
2432
2361
def split_suite_by_re(suite, pattern):
2433
2362
"""Split a test suite into two by a regular expression.
2511
2440
return result.wasSuccessful()
2443
# Controlled by "bzr selftest -E=..." option
2444
selftest_debug_flags = set()
2514
2447
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
2515
2448
transport=None,
2516
2449
test_suite_factory=None,
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)
2539
2477
if load_list is None:
2540
2478
keep_only = None
2669
2608
suite on a global basis, but it is not encouraged.
2671
2610
testmod_names = [
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',
2803
test_transport_implementations = [
2804
'bzrlib.tests.test_transport_implementations',
2805
'bzrlib.tests.test_read_bundle',
2807
2759
loader = TestUtil.TestLoader()
2809
2761
if keep_only is None:
2816
2768
# modules building their suite with loadTestsFromModuleNames
2817
2769
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
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)
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)
2831
2771
modules_to_doctest = [
2833
2773
'bzrlib.errors',