78
89
TestUtil._load_module_by_name,
79
90
'bzrlib.no-name-yet')
81
93
class MetaTestLog(tests.TestCase):
83
95
def test_logging(self):
84
96
"""Test logs are captured when a test fails."""
85
97
self.log('a test message')
86
self._log_file.flush()
87
self.assertContainsRe(self._get_log(keep_log_file=True),
98
details = self.getDetails()
100
self.assertThat(log.content_type, Equals(ContentType(
101
"text", "plain", {"charset": "utf8"})))
102
self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
103
self.assertThat(self.get_log(),
104
DocTestMatches(u"...a test message\n", ELLIPSIS))
91
107
class TestUnicodeFilename(tests.TestCase):
687
709
def sample_function(self):
689
711
test = Sample("a")
690
test.attrs_to_keep = test.attrs_to_keep + ('_benchcalls',)
692
self.assertLength(1, test._benchcalls)
713
case = terminal._events[0][1]
714
self.assertLength(1, case._benchcalls)
693
715
# We must be able to unpack it as the test reporting code wants
694
(_, _, _), stats = test._benchcalls[0]
716
(_, _, _), stats = case._benchcalls[0]
695
717
self.assertTrue(callable(stats.pprint))
824
842
def stopTestRun(self): pass
825
843
def startTests(self): pass
826
844
def report_test_start(self, test): pass
827
def report_known_failure(self, test, err):
828
self._call = test, err
845
def report_known_failure(self, test, err=None, details=None):
846
self._call = test, 'known failure'
829
847
result = InstrumentedTestResult(None, None, None, None)
830
848
class Test(tests.TestCase):
831
849
def test_function(self):
835
853
# it should invoke 'report_known_failure'.
836
854
self.assertEqual(2, len(result._call))
837
self.assertEqual(test, result._call[0])
838
self.assertEqual(tests.KnownFailure, result._call[1][0])
839
self.assertIsInstance(result._call[1][1], tests.KnownFailure)
855
self.assertEqual(test.id(), result._call[0].id())
856
self.assertEqual('known failure', result._call[1])
840
857
# we dont introspec the traceback, if the rest is ok, it would be
841
858
# exceptional for it not to be.
842
859
# it should update the known_failure_count on the object.
914
931
result.report_unsupported(test, feature)
915
932
output = result_stream.getvalue()[prefix:]
916
933
lines = output.splitlines()
917
self.assertEqual(lines, ['NODEP 0ms',
918
" The feature 'Feature' is not available."])
934
# We don't check for the final '0ms' since it may fail on slow hosts
935
self.assertStartsWith(lines[0], 'NODEP')
936
self.assertEqual(lines[1],
937
" The feature 'Feature' is not available.")
920
939
def test_unavailable_exception(self):
921
940
"""An UnavailableFeature being raised should invoke addNotSupported."""
1018
1037
# the final output when real failures occur.
1019
1038
class Test(tests.TestCase):
1020
1039
def known_failure_test(self):
1021
raise tests.KnownFailure('failed')
1040
self.expectFailure('failed', self.assertTrue, False)
1022
1041
test = unittest.TestSuite()
1023
1042
test.addTest(Test("known_failure_test"))
1024
1043
def failing_test():
1025
raise AssertionError('foo')
1026
1045
test.addTest(unittest.FunctionTestCase(failing_test))
1027
1046
stream = StringIO()
1028
1047
runner = tests.TextTestRunner(stream=stream)
1029
1048
result = self.run_test_runner(runner, test)
1030
1049
lines = stream.getvalue().splitlines()
1031
1050
self.assertContainsRe(stream.getvalue(),
1051
'(?sm)^bzr selftest.*$'
1034
1053
'^======================================================================\n'
1035
'^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1054
'^FAIL: failing_test\n'
1036
1055
'^----------------------------------------------------------------------\n'
1037
1056
'Traceback \\(most recent call last\\):\n'
1038
1057
' .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1039
' raise AssertionError\\(\'foo\'\\)\n'
1058
' self.fail\\(\'foo\'\\)\n'
1041
1060
'^----------------------------------------------------------------------\n'
1176
1195
# Reading from the actual source tree breaks isolation, but we don't
1177
1196
# want to assume that thats *all* that would happen.
1178
1197
self._get_source_tree_calls = []
1179
def _get_bzr_source_tree():
1180
1199
self._get_source_tree_calls.append("called")
1182
orig_get_bzr_source_tree = bzrlib.version._get_bzr_source_tree
1183
bzrlib.version._get_bzr_source_tree = _get_bzr_source_tree
1185
bzrlib.version._get_bzr_source_tree = orig_get_bzr_source_tree
1186
self.addCleanup(restore)
1201
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', new_get)
1188
1203
def test_bench_history(self):
1189
1204
# tests that the running the benchmark passes bench_history into
1199
1214
self.assertContainsRe(output_string, "--date [0-9.]+")
1200
1215
self.assertLength(1, self._get_source_tree_calls)
1202
def assertLogDeleted(self, test):
1203
log = test._get_log()
1204
self.assertEqual("DELETED log file to reduce memory footprint", log)
1205
self.assertEqual('', test._log_contents)
1206
self.assertIs(None, test._log_file_name)
1208
def test_success_log_deleted(self):
1209
"""Successful tests have their log deleted"""
1211
class LogTester(tests.TestCase):
1213
def test_success(self):
1214
self.log('this will be removed\n')
1217
runner = tests.TextTestRunner(stream=sio)
1218
test = LogTester('test_success')
1219
result = self.run_test_runner(runner, test)
1221
self.assertLogDeleted(test)
1223
def test_skipped_log_deleted(self):
1224
"""Skipped tests have their log deleted"""
1226
class LogTester(tests.TestCase):
1228
def test_skipped(self):
1229
self.log('this will be removed\n')
1230
raise tests.TestSkipped()
1233
runner = tests.TextTestRunner(stream=sio)
1234
test = LogTester('test_skipped')
1235
result = self.run_test_runner(runner, test)
1237
self.assertLogDeleted(test)
1239
def test_not_aplicable_log_deleted(self):
1240
"""Not applicable tests have their log deleted"""
1242
class LogTester(tests.TestCase):
1244
def test_not_applicable(self):
1245
self.log('this will be removed\n')
1246
raise tests.TestNotApplicable()
1249
runner = tests.TextTestRunner(stream=sio)
1250
test = LogTester('test_not_applicable')
1251
result = self.run_test_runner(runner, test)
1253
self.assertLogDeleted(test)
1255
def test_known_failure_log_deleted(self):
1256
"""Know failure tests have their log deleted"""
1258
class LogTester(tests.TestCase):
1260
def test_known_failure(self):
1261
self.log('this will be removed\n')
1262
raise tests.KnownFailure()
1265
runner = tests.TextTestRunner(stream=sio)
1266
test = LogTester('test_known_failure')
1267
result = self.run_test_runner(runner, test)
1269
self.assertLogDeleted(test)
1271
def test_fail_log_kept(self):
1272
"""Failed tests have their log kept"""
1274
class LogTester(tests.TestCase):
1276
def test_fail(self):
1277
self.log('this will be kept\n')
1278
self.fail('this test fails')
1281
runner = tests.TextTestRunner(stream=sio)
1282
test = LogTester('test_fail')
1283
result = self.run_test_runner(runner, test)
1285
text = sio.getvalue()
1286
self.assertContainsRe(text, 'this will be kept')
1287
self.assertContainsRe(text, 'this test fails')
1289
log = test._get_log()
1290
self.assertContainsRe(log, 'this will be kept')
1291
self.assertEqual(log, test._log_contents)
1293
def test_error_log_kept(self):
1294
"""Tests with errors have their log kept"""
1296
class LogTester(tests.TestCase):
1298
def test_error(self):
1299
self.log('this will be kept\n')
1300
raise ValueError('random exception raised')
1303
runner = tests.TextTestRunner(stream=sio)
1304
test = LogTester('test_error')
1305
result = self.run_test_runner(runner, test)
1307
text = sio.getvalue()
1308
self.assertContainsRe(text, 'this will be kept')
1309
self.assertContainsRe(text, 'random exception raised')
1311
log = test._get_log()
1312
self.assertContainsRe(log, 'this will be kept')
1313
self.assertEqual(log, test._log_contents)
1315
1217
def test_startTestRun(self):
1316
1218
"""run should call result.startTestRun()"""
1410
1312
self.assertEqual(flags, bzrlib.debug.debug_flags)
1412
1314
def change_selftest_debug_flags(self, new_flags):
1413
orig_selftest_flags = tests.selftest_debug_flags
1414
self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
1415
tests.selftest_debug_flags = set(new_flags)
1417
def _restore_selftest_debug_flags(self, flags):
1418
tests.selftest_debug_flags = flags
1315
self.overrideAttr(tests, 'selftest_debug_flags', set(new_flags))
1420
1317
def test_allow_debug_flag(self):
1421
1318
"""The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
1648
1548
self.calls.append(('addNotSupported', test, feature))
1649
1549
result = InstrumentedTestResult()
1650
1550
test.run(result)
1551
case = result.calls[0][1]
1651
1552
self.assertEqual([
1652
('startTest', test),
1653
('addNotSupported', test, the_feature),
1553
('startTest', case),
1554
('addNotSupported', case, the_feature),
1658
1559
def test_start_server_registers_url(self):
1659
transport_server = MemoryServer()
1560
transport_server = memory.MemoryServer()
1660
1561
# A little strict, but unlikely to be changed soon.
1661
1562
self.assertEqual([], self._bzr_selftest_roots)
1662
1563
self.start_server(transport_server)
1718
1619
self.assertRaises(AssertionError,
1719
1620
self.assertListRaises, _TestException, success_generator)
1622
def test_overrideAttr_without_value(self):
1623
self.test_attr = 'original' # Define a test attribute
1624
obj = self # Make 'obj' visible to the embedded test
1625
class Test(tests.TestCase):
1628
tests.TestCase.setUp(self)
1629
self.orig = self.overrideAttr(obj, 'test_attr')
1631
def test_value(self):
1632
self.assertEqual('original', self.orig)
1633
self.assertEqual('original', obj.test_attr)
1634
obj.test_attr = 'modified'
1635
self.assertEqual('modified', obj.test_attr)
1637
test = Test('test_value')
1638
test.run(unittest.TestResult())
1639
self.assertEqual('original', obj.test_attr)
1641
def test_overrideAttr_with_value(self):
1642
self.test_attr = 'original' # Define a test attribute
1643
obj = self # Make 'obj' visible to the embedded test
1644
class Test(tests.TestCase):
1647
tests.TestCase.setUp(self)
1648
self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
1650
def test_value(self):
1651
self.assertEqual('original', self.orig)
1652
self.assertEqual('modified', obj.test_attr)
1654
test = Test('test_value')
1655
test.run(unittest.TestResult())
1656
self.assertEqual('original', obj.test_attr)
1722
1659
# NB: Don't delete this; it's not actually from 0.11!
1723
1660
@deprecated_function(deprecated_in((0, 11, 0)))
2010
1946
self.assertEqual(transport_server, captured_transport[0])
2012
1948
def test_transport_sftp(self):
2014
import bzrlib.transport.sftp
2015
except errors.ParamikoNotPresent:
2016
raise tests.TestSkipped("Paramiko not present")
2017
self.check_transport_set(bzrlib.transport.sftp.SFTPAbsoluteServer)
1949
self.requireFeature(features.paramiko)
1950
self.check_transport_set(stub_sftp.SFTPAbsoluteServer)
2019
1952
def test_transport_memory(self):
2020
self.check_transport_set(bzrlib.transport.memory.MemoryServer)
1953
self.check_transport_set(memory.MemoryServer)
2023
1956
class TestSelftestWithIdList(tests.TestCaseInTempDir, SelfTestHelper):
2424
2357
self.assertEqual('bzr: interrupted\n', result[1])
2427
class TestKnownFailure(tests.TestCase):
2429
def test_known_failure(self):
2430
"""Check that KnownFailure is defined appropriately."""
2431
# a KnownFailure is an assertion error for compatability with unaware
2433
self.assertIsInstance(tests.KnownFailure(""), AssertionError)
2435
def test_expect_failure(self):
2437
self.expectFailure("Doomed to failure", self.assertTrue, False)
2438
except tests.KnownFailure, e:
2439
self.assertEqual('Doomed to failure', e.args[0])
2441
self.expectFailure("Doomed to failure", self.assertTrue, True)
2442
except AssertionError, e:
2443
self.assertEqual('Unexpected success. Should have failed:'
2444
' Doomed to failure', e.args[0])
2446
self.fail('Assertion not raised')
2449
2360
class TestFeature(tests.TestCase):
2451
2362
def test_caching(self):
2487
2398
self.assertIs(feature, exception.args[0])
2401
simple_thunk_feature = tests._CompatabilityThunkFeature(
2402
deprecated_in((2, 1, 0)),
2403
'bzrlib.tests.test_selftest',
2404
'simple_thunk_feature','UnicodeFilename',
2405
replacement_module='bzrlib.tests'
2408
class Test_CompatibilityFeature(tests.TestCase):
2410
def test_does_thunk(self):
2411
res = self.callDeprecated(
2412
['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2413
' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2414
simple_thunk_feature.available)
2415
self.assertEqual(tests.UnicodeFilename.available(), res)
2418
class TestModuleAvailableFeature(tests.TestCase):
2420
def test_available_module(self):
2421
feature = tests.ModuleAvailableFeature('bzrlib.tests')
2422
self.assertEqual('bzrlib.tests', feature.module_name)
2423
self.assertEqual('bzrlib.tests', str(feature))
2424
self.assertTrue(feature.available())
2425
self.assertIs(tests, feature.module)
2427
def test_unavailable_module(self):
2428
feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2429
self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2430
self.assertFalse(feature.available())
2431
self.assertIs(None, feature.module)
2490
2434
class TestSelftestFiltering(tests.TestCase):
2492
2436
def setUp(self):
2671
2615
# the test framework
2672
2616
self.assertEquals('always fails', str(e))
2673
2617
# check that there's no traceback in the test log
2674
self.assertNotContainsRe(self._get_log(keep_log_file=True),
2618
self.assertNotContainsRe(self.get_log(), r'Traceback')
2677
2620
def test_run_bzr_user_error_caught(self):
2678
2621
# Running bzr in blackbox mode, normal/expected/user errors should be
2679
2622
# caught in the regular way and turned into an error message plus exit
2681
transport_server = MemoryServer()
2682
transport_server.setUp()
2683
self.addCleanup(transport_server.tearDown)
2624
transport_server = memory.MemoryServer()
2625
transport_server.start_server()
2626
self.addCleanup(transport_server.stop_server)
2684
2627
url = transport_server.get_url()
2685
2628
self.permit_url(url)
2686
2629
out, err = self.run_bzr(["log", "%s/nonexistantpath" % url], retcode=3)
2842
2785
# test doubles that supply a few sample tests to load, and check they
2845
def _test_suite_testmod_names():
2788
def testmod_names():
2846
2789
calls.append("testmod_names")
2848
2791
'bzrlib.tests.blackbox.test_branch',
2849
2792
'bzrlib.tests.per_transport',
2850
2793
'bzrlib.tests.test_selftest',
2852
original_testmod_names = tests._test_suite_testmod_names
2853
def _test_suite_modules_to_doctest():
2795
self.overrideAttr(tests, '_test_suite_testmod_names', testmod_names)
2854
2797
calls.append("modules_to_doctest")
2855
2798
return ['bzrlib.timestamp']
2856
orig_modules_to_doctest = tests._test_suite_modules_to_doctest
2857
def restore_names():
2858
tests._test_suite_testmod_names = original_testmod_names
2859
tests._test_suite_modules_to_doctest = orig_modules_to_doctest
2860
self.addCleanup(restore_names)
2861
tests._test_suite_testmod_names = _test_suite_testmod_names
2862
tests._test_suite_modules_to_doctest = _test_suite_modules_to_doctest
2799
self.overrideAttr(tests, '_test_suite_modules_to_doctest', doctests)
2863
2800
expected_test_list = [
2864
2801
# testmod_names
2865
2802
'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',