2234
2237
self.transport_readonly_server = HttpServer
2237
def filter_suite_by_re(suite, pattern, exclude_pattern=None,
2238
random_order=False):
2240
def condition_id_re(pattern):
2241
"""Create a condition filter which performs a re check on a test's id.
2243
:param pattern: A regular expression string.
2244
:return: A callable that returns True if the re matches.
2246
filter_re = re.compile(pattern)
2247
def condition(test):
2249
return filter_re.search(test_id)
2253
def condition_isinstance(klass_or_klass_list):
2254
"""Create a condition filter which returns isinstance(param, klass).
2256
:return: A callable which when called with one parameter obj return the
2257
result of isinstance(obj, klass_or_klass_list).
2260
return isinstance(obj, klass_or_klass_list)
2264
def exclude_tests_by_condition(suite, condition):
2265
"""Create a test suite which excludes some tests from suite.
2267
:param suite: The suite to get tests from.
2268
:param condition: A callable whose result evaluates True when called with a
2269
test case which should be excluded from the result.
2270
:return: A suite which contains the tests found in suite that fail
2274
for test in iter_suite_tests(suite):
2275
if not condition(test):
2277
return TestUtil.TestSuite(result)
2280
def filter_suite_by_condition(suite, condition):
2281
"""Create a test suite by filtering another one.
2283
:param suite: The source suite.
2284
:param condition: A callable whose result evaluates True when called with a
2285
test case which should be included in the result.
2286
:return: A suite which contains the tests found in suite that pass
2290
for test in iter_suite_tests(suite):
2293
return TestUtil.TestSuite(result)
2296
def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
2297
random_order=DEPRECATED_PARAMETER):
2239
2298
"""Create a test suite by filtering another one.
2241
2300
:param suite: the source suite
2242
2301
:param pattern: pattern that names must match
2243
:param exclude_pattern: pattern that names must not match, if any
2244
:param random_order: if True, tests in the new suite will be put in
2302
:param exclude_pattern: A pattern that names must not match. This parameter
2303
is deprecated as of bzrlib 1.0. Please use the separate function
2304
exclude_tests_by_re instead.
2305
:param random_order: If True, tests in the new suite will be put in
2306
random order. This parameter is deprecated as of bzrlib 1.0. Please
2307
use the separate function randomize_suite instead.
2246
2308
:returns: the newly created suite
2248
return sort_suite_by_re(suite, pattern, exclude_pattern,
2249
random_order, False)
2310
if deprecated_passed(exclude_pattern):
2311
symbol_versioning.warn(
2312
one_zero % "passing exclude_pattern to filter_suite_by_re",
2313
DeprecationWarning, stacklevel=2)
2314
if exclude_pattern is not None:
2315
suite = exclude_tests_by_re(suite, exclude_pattern)
2316
condition = condition_id_re(pattern)
2317
result_suite = filter_suite_by_condition(suite, condition)
2318
if deprecated_passed(random_order):
2319
symbol_versioning.warn(
2320
one_zero % "passing random_order to filter_suite_by_re",
2321
DeprecationWarning, stacklevel=2)
2323
result_suite = randomize_suite(result_suite)
2327
def exclude_tests_by_re(suite, pattern):
2328
"""Create a test suite which excludes some tests from suite.
2330
:param suite: The suite to get tests from.
2331
:param pattern: A regular expression string. Test ids that match this
2332
pattern will be excluded from the result.
2333
:return: A TestSuite that contains all the tests from suite without the
2334
tests that matched pattern. The order of tests is the same as it was in
2337
return exclude_tests_by_condition(suite, condition_id_re(pattern))
2340
def preserve_input(something):
2341
"""A helper for performing test suite transformation chains.
2343
:param something: Anything you want to preserve.
2349
def randomize_suite(suite):
2350
"""Return a new TestSuite with suite's tests in random order.
2352
The tests in the input suite are flattened into a single suite in order to
2353
accomplish this. Any nested TestSuites are removed to provide global
2356
tests = list(iter_suite_tests(suite))
2357
random.shuffle(tests)
2358
return TestUtil.TestSuite(tests)
2361
@deprecated_function(one_zero)
2252
2362
def sort_suite_by_re(suite, pattern, exclude_pattern=None,
2253
2363
random_order=False, append_rest=True):
2254
"""Create a test suite by sorting another one.
2364
"""DEPRECATED: Create a test suite by sorting another one.
2366
This method has been decomposed into separate helper methods that should be
2368
- filter_suite_by_re
2369
- exclude_tests_by_re
2256
2373
:param suite: the source suite
2257
2374
:param pattern: pattern that names must match in order to go
2258
2375
first in the new suite
2259
2376
:param exclude_pattern: pattern that names must not match, if any
2260
2377
:param random_order: if True, tests in the new suite will be put in
2378
random order (with all tests matching pattern
2262
2380
:param append_rest: if False, pattern is a strict filter and not
2263
2381
just an ordering directive
2264
2382
:returns: the newly created suite
2384
if exclude_pattern is not None:
2385
suite = exclude_tests_by_re(suite, exclude_pattern)
2387
order_changer = randomize_suite
2389
order_changer = preserve_input
2391
suites = map(order_changer, split_suite_by_re(suite, pattern))
2392
return TestUtil.TestSuite(suites)
2394
return order_changer(filter_suite_by_re(suite, pattern))
2397
def split_suite_by_re(suite, pattern):
2398
"""Split a test suite into two by a regular expression.
2400
:param suite: The suite to split.
2401
:param pattern: A regular expression string. Test ids that match this
2402
pattern will be in the first test suite returned, and the others in the
2403
second test suite returned.
2404
:return: A tuple of two test suites, where the first contains tests from
2405
suite matching pattern, and the second contains the remainder from
2406
suite. The order within each output suite is the same as it was in
2268
2411
filter_re = re.compile(pattern)
2269
if exclude_pattern is not None:
2270
exclude_re = re.compile(exclude_pattern)
2271
2412
for test in iter_suite_tests(suite):
2272
2413
test_id = test.id()
2273
if exclude_pattern is None or not exclude_re.search(test_id):
2274
if filter_re.search(test_id):
2279
random.shuffle(first)
2280
random.shuffle(second)
2281
return TestUtil.TestSuite(first + second)
2414
if filter_re.search(test_id):
2415
matched.append(test)
2417
did_not_match.append(test)
2418
return TestUtil.TestSuite(matched), TestUtil.TestSuite(did_not_match)
2284
2421
def run_suite(suite, name='test', verbose=False, pattern=".*",