867
test = self.get_passing_test()
868
result.startTest(test)
869
prefix = len(result_stream.getvalue())
870
# the err parameter has the shape:
871
# (class, exception object, traceback)
872
# KnownFailures dont get their tracebacks shown though, so we
874
err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
875
result.report_known_failure(test, err)
876
output = result_stream.getvalue()[prefix:]
877
lines = output.splitlines()
878
self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
879
if sys.version_info > (2, 7):
880
self.expectFailure("_ExpectedFailure on 2.7 loses the message",
881
self.assertNotEqual, lines[1], ' ')
882
self.assertEqual(lines[1], ' foo')
883
self.assertEqual(2, len(lines))
860
_get_test("test_xfail").run(result)
861
self.assertContainsRe(result_stream.getvalue(),
862
"\n\\S+\\.test_xfail\\s+XFAIL\\s+\\d+ms\n"
863
"\\s*(?:Text attachment: )?reason"
885
868
def get_passing_test(self):
886
869
"""Return a test object that can't be run usefully."""
1501
1477
transport_server = memory.MemoryServer()
1502
1478
transport_server.start_server()
1503
1479
self.addCleanup(transport_server.stop_server)
1504
t = transport.get_transport(transport_server.get_url())
1505
bzrdir.BzrDir.create(t.base)
1480
t = transport.get_transport_from_url(transport_server.get_url())
1481
controldir.ControlDir.create(t.base)
1506
1482
self.assertRaises(errors.BzrError,
1507
bzrdir.BzrDir.open_from_transport, t)
1483
controldir.ControlDir.open_from_transport, t)
1508
1484
# But if we declare this as safe, we can open the bzrdir.
1509
1485
self.permit_url(t.base)
1510
1486
self._bzr_selftest_roots.append(t.base)
1511
bzrdir.BzrDir.open_from_transport(t)
1487
controldir.ControlDir.open_from_transport(t)
1513
1489
def test_requireFeature_available(self):
1514
1490
"""self.requireFeature(available) is a no-op."""
1515
class Available(tests.Feature):
1491
class Available(features.Feature):
1516
1492
def _probe(self):return True
1517
1493
feature = Available()
1518
1494
self.requireFeature(feature)
1520
1496
def test_requireFeature_unavailable(self):
1521
1497
"""self.requireFeature(unavailable) raises UnavailableFeature."""
1522
class Unavailable(tests.Feature):
1498
class Unavailable(features.Feature):
1523
1499
def _probe(self):return False
1524
1500
feature = Unavailable()
1525
1501
self.assertRaises(tests.UnavailableFeature,
1742
1730
result = self._run_test('test_fail')
1743
1731
self.assertEqual(1, len(result.failures))
1744
1732
result_content = result.failures[0][1]
1745
self.assertContainsRe(result_content, 'Text attachment: log')
1733
self.assertContainsRe(result_content,
1734
'(?m)^(?:Text attachment: )?log(?:$|: )')
1746
1735
self.assertContainsRe(result_content, 'this was a failing test')
1748
1737
def test_error_has_log(self):
1749
1738
result = self._run_test('test_error')
1750
1739
self.assertEqual(1, len(result.errors))
1751
1740
result_content = result.errors[0][1]
1752
self.assertContainsRe(result_content, 'Text attachment: log')
1741
self.assertContainsRe(result_content,
1742
'(?m)^(?:Text attachment: )?log(?:$|: )')
1753
1743
self.assertContainsRe(result_content, 'this test errored')
1755
1745
def test_skip_has_no_log(self):
2607
2599
self.assertEqual('bzr: interrupted\n', result[1])
2610
class TestFeature(tests.TestCase):
2612
def test_caching(self):
2613
"""Feature._probe is called by the feature at most once."""
2614
class InstrumentedFeature(tests.Feature):
2616
super(InstrumentedFeature, self).__init__()
2619
self.calls.append('_probe')
2621
feature = InstrumentedFeature()
2623
self.assertEqual(['_probe'], feature.calls)
2625
self.assertEqual(['_probe'], feature.calls)
2627
def test_named_str(self):
2628
"""Feature.__str__ should thunk to feature_name()."""
2629
class NamedFeature(tests.Feature):
2630
def feature_name(self):
2632
feature = NamedFeature()
2633
self.assertEqual('symlinks', str(feature))
2635
def test_default_str(self):
2636
"""Feature.__str__ should default to __class__.__name__."""
2637
class NamedFeature(tests.Feature):
2639
feature = NamedFeature()
2640
self.assertEqual('NamedFeature', str(feature))
2643
class TestUnavailableFeature(tests.TestCase):
2645
def test_access_feature(self):
2646
feature = tests.Feature()
2647
exception = tests.UnavailableFeature(feature)
2648
self.assertIs(feature, exception.args[0])
2651
simple_thunk_feature = tests._CompatabilityThunkFeature(
2652
deprecated_in((2, 1, 0)),
2653
'bzrlib.tests.test_selftest',
2654
'simple_thunk_feature','UnicodeFilename',
2655
replacement_module='bzrlib.tests'
2658
class Test_CompatibilityFeature(tests.TestCase):
2660
def test_does_thunk(self):
2661
res = self.callDeprecated(
2662
['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2663
' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2664
simple_thunk_feature.available)
2665
self.assertEqual(tests.UnicodeFilename.available(), res)
2668
class TestModuleAvailableFeature(tests.TestCase):
2670
def test_available_module(self):
2671
feature = tests.ModuleAvailableFeature('bzrlib.tests')
2672
self.assertEqual('bzrlib.tests', feature.module_name)
2673
self.assertEqual('bzrlib.tests', str(feature))
2674
self.assertTrue(feature.available())
2675
self.assertIs(tests, feature.module)
2677
def test_unavailable_module(self):
2678
feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2679
self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2680
self.assertFalse(feature.available())
2681
self.assertIs(None, feature.module)
2684
2602
class TestSelftestFiltering(tests.TestCase):
2686
2604
def setUp(self):
3385
3303
self.assertLength(1, calls)
3306
class _Selftest(object):
3307
"""Mixin for tests needing full selftest output"""
3309
def _inject_stream_into_subunit(self, stream):
3310
"""To be overridden by subclasses that run tests out of process"""
3312
def _run_selftest(self, **kwargs):
3314
self._inject_stream_into_subunit(sio)
3315
tests.selftest(stream=sio, stop_on_failure=False, **kwargs)
3316
return sio.getvalue()
3319
class _ForkedSelftest(_Selftest):
3320
"""Mixin for tests needing full selftest output with forked children"""
3322
_test_needs_features = [features.subunit]
3324
def _inject_stream_into_subunit(self, stream):
3325
"""Monkey-patch subunit so the extra output goes to stream not stdout
3327
Some APIs need rewriting so this kind of bogus hackery can be replaced
3328
by passing the stream param from run_tests down into ProtocolTestCase.
3330
from subunit import ProtocolTestCase
3331
_original_init = ProtocolTestCase.__init__
3332
def _init_with_passthrough(self, *args, **kwargs):
3333
_original_init(self, *args, **kwargs)
3334
self._passthrough = stream
3335
self.overrideAttr(ProtocolTestCase, "__init__", _init_with_passthrough)
3337
def _run_selftest(self, **kwargs):
3338
# GZ 2011-05-26: Add a PosixSystem feature so this check can go away
3339
if getattr(os, "fork", None) is None:
3340
raise tests.TestNotApplicable("Platform doesn't support forking")
3341
# Make sure the fork code is actually invoked by claiming two cores
3342
self.overrideAttr(osutils, "local_concurrency", lambda: 2)
3343
kwargs.setdefault("suite_decorators", []).append(tests.fork_decorator)
3344
return super(_ForkedSelftest, self)._run_selftest(**kwargs)
3347
class TestParallelFork(_ForkedSelftest, tests.TestCase):
3348
"""Check operation of --parallel=fork selftest option"""
3350
def test_error_in_child_during_fork(self):
3351
"""Error in a forked child during test setup should get reported"""
3352
class Test(tests.TestCase):
3353
def testMethod(self):
3355
# We don't care what, just break something that a child will run
3356
self.overrideAttr(tests, "workaround_zealous_crypto_random", None)
3357
out = self._run_selftest(test_suite_factory=Test)
3358
# Lines from the tracebacks of the two child processes may be mixed
3359
# together due to the way subunit parses and forwards the streams,
3360
# so permit extra lines between each part of the error output.
3361
self.assertContainsRe(out,
3364
".+ in fork_for_tests\n"
3366
"\s*workaround_zealous_crypto_random\(\)\n"
3371
class TestUncollectedWarnings(_Selftest, tests.TestCase):
3372
"""Check a test case still alive after being run emits a warning"""
3374
class Test(tests.TestCase):
3375
def test_pass(self):
3377
def test_self_ref(self):
3378
self.also_self = self.test_self_ref
3379
def test_skip(self):
3380
self.skip("Don't need")
3382
def _get_suite(self):
3383
return TestUtil.TestSuite([
3384
self.Test("test_pass"),
3385
self.Test("test_self_ref"),
3386
self.Test("test_skip"),
3389
def _run_selftest_with_suite(self, **kwargs):
3390
old_flags = tests.selftest_debug_flags
3391
tests.selftest_debug_flags = old_flags.union(["uncollected_cases"])
3392
gc_on = gc.isenabled()
3396
output = self._run_selftest(test_suite_factory=self._get_suite,
3401
tests.selftest_debug_flags = old_flags
3402
self.assertNotContainsRe(output, "Uncollected test case.*test_pass")
3403
self.assertContainsRe(output, "Uncollected test case.*test_self_ref")
3406
def test_testsuite(self):
3407
self._run_selftest_with_suite()
3409
def test_pattern(self):
3410
out = self._run_selftest_with_suite(pattern="test_(?:pass|self_ref)$")
3411
self.assertNotContainsRe(out, "test_skip")
3413
def test_exclude_pattern(self):
3414
out = self._run_selftest_with_suite(exclude_pattern="test_skip$")
3415
self.assertNotContainsRe(out, "test_skip")
3417
def test_random_seed(self):
3418
self._run_selftest_with_suite(random_seed="now")
3420
def test_matching_tests_first(self):
3421
self._run_selftest_with_suite(matching_tests_first=True,
3422
pattern="test_self_ref$")
3424
def test_starting_with_and_exclude(self):
3425
out = self._run_selftest_with_suite(starting_with=["bt."],
3426
exclude_pattern="test_skip$")
3427
self.assertNotContainsRe(out, "test_skip")
3429
def test_additonal_decorator(self):
3430
out = self._run_selftest_with_suite(
3431
suite_decorators=[tests.TestDecorator])
3434
class TestUncollectedWarningsSubunit(TestUncollectedWarnings):
3435
"""Check warnings from tests staying alive are emitted with subunit"""
3437
_test_needs_features = [features.subunit]
3439
def _run_selftest_with_suite(self, **kwargs):
3440
return TestUncollectedWarnings._run_selftest_with_suite(self,
3441
runner_class=tests.SubUnitBzrRunner, **kwargs)
3444
class TestUncollectedWarningsForked(_ForkedSelftest, TestUncollectedWarnings):
3445
"""Check warnings from tests staying alive are emitted when forking"""
3388
3448
class TestEnvironHandling(tests.TestCase):
3390
3450
def test_overrideEnv_None_called_twice_doesnt_leak(self):