322
338
self.assertEqual(tests[1].transport_readonly_server, server2)
341
class TestTreeProviderAdapter(TestCase):
342
"""Test the setup of tree_implementation tests."""
344
def test_adapted_tests(self):
345
# the tree implementation adapter is meant to setup one instance for
346
# each working tree format, and one additional instance that will
347
# use the default wt format, but create a revision tree for the tests.
348
# this means that the wt ones should have the workingtree_to_test_tree
349
# attribute set to 'return_parameter' and the revision one set to
350
# revision_tree_from_workingtree.
352
from bzrlib.tests.tree_implementations import (
353
TreeTestProviderAdapter,
355
revision_tree_from_workingtree
357
from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
358
input_test = TestTreeProviderAdapter(
359
"test_adapted_tests")
362
formats = [("c", "C"), ("d", "D")]
363
adapter = TreeTestProviderAdapter(server1, server2, formats)
364
suite = adapter.adapt(input_test)
365
tests = list(iter(suite))
366
self.assertEqual(4, len(tests))
367
# this must match the default format setp up in
368
# TreeTestProviderAdapter.adapt
369
default_format = WorkingTreeFormat3
370
self.assertEqual(tests[0].workingtree_format, formats[0][0])
371
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
372
self.assertEqual(tests[0].transport_server, server1)
373
self.assertEqual(tests[0].transport_readonly_server, server2)
374
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
375
self.assertEqual(tests[1].workingtree_format, formats[1][0])
376
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
377
self.assertEqual(tests[1].transport_server, server1)
378
self.assertEqual(tests[1].transport_readonly_server, server2)
379
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
380
self.assertIsInstance(tests[2].workingtree_format, default_format)
381
#self.assertEqual(tests[2].bzrdir_format,
382
# default_format._matchingbzrdir)
383
self.assertEqual(tests[2].transport_server, server1)
384
self.assertEqual(tests[2].transport_readonly_server, server2)
385
self.assertEqual(tests[2].workingtree_to_test_tree,
386
revision_tree_from_workingtree)
389
class TestInterTreeProviderAdapter(TestCase):
390
"""A group of tests that test the InterTreeTestAdapter."""
392
def test_adapted_tests(self):
393
# check that constructor parameters are passed through to the adapted
395
# for InterTree tests we want the machinery to bring up two trees in
396
# each instance: the base one, and the one we are interacting with.
397
# because each optimiser can be direction specific, we need to test
398
# each optimiser in its chosen direction.
399
# unlike the TestProviderAdapter we dont want to automatically add a
400
# parameterised one for WorkingTree - the optimisers will tell us what
402
from bzrlib.tests.tree_implementations import (
404
revision_tree_from_workingtree
406
from bzrlib.tests.intertree_implementations import (
407
InterTreeTestProviderAdapter,
409
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
410
input_test = TestInterTreeProviderAdapter(
411
"test_adapted_tests")
414
format1 = WorkingTreeFormat2()
415
format2 = WorkingTreeFormat3()
416
formats = [(str, format1, format2, "converter1"),
417
(int, format2, format1, "converter2")]
418
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
419
suite = adapter.adapt(input_test)
420
tests = list(iter(suite))
421
self.assertEqual(2, len(tests))
422
self.assertEqual(tests[0].intertree_class, formats[0][0])
423
self.assertEqual(tests[0].workingtree_format, formats[0][1])
424
self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
425
self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
426
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
427
self.assertEqual(tests[0].transport_server, server1)
428
self.assertEqual(tests[0].transport_readonly_server, server2)
429
self.assertEqual(tests[1].intertree_class, formats[1][0])
430
self.assertEqual(tests[1].workingtree_format, formats[1][1])
431
self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
432
self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
433
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
434
self.assertEqual(tests[1].transport_server, server1)
435
self.assertEqual(tests[1].transport_readonly_server, server2)
438
class TestTestCaseInTempDir(TestCaseInTempDir):
440
def test_home_is_not_working(self):
441
self.assertNotEqual(self.test_dir, self.test_home_dir)
442
cwd = osutils.getcwd()
443
self.assertEqual(self.test_dir, cwd)
444
self.assertEqual(self.test_home_dir, os.environ['HOME'])
447
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
449
def test_home_is_non_existant_dir_under_root(self):
450
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
452
This is because TestCaseWithMemoryTransport is for tests that do not
453
need any disk resources: they should be hooked into bzrlib in such a
454
way that no global settings are being changed by the test (only a
455
few tests should need to do that), and having a missing dir as home is
456
an effective way to ensure that this is the case.
458
self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
460
self.assertEqual(self.test_home_dir, os.environ['HOME'])
462
def test_cwd_is_TEST_ROOT(self):
463
self.assertEqual(self.test_dir, self.TEST_ROOT)
464
cwd = osutils.getcwd()
465
self.assertEqual(self.test_dir, cwd)
467
def test_make_branch_and_memory_tree(self):
468
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
470
This is hard to comprehensively robustly test, so we settle for making
471
a branch and checking no directory was created at its relpath.
473
tree = self.make_branch_and_memory_tree('dir')
474
# Guard against regression into MemoryTransport leaking
475
# files to disk instead of keeping them in memory.
476
self.failIf(osutils.lexists('dir'))
477
self.assertIsInstance(tree, memorytree.MemoryTree)
479
def test_make_branch_and_memory_tree_with_format(self):
480
"""make_branch_and_memory_tree should accept a format option."""
481
format = bzrdir.BzrDirMetaFormat1()
482
format.repository_format = weaverepo.RepositoryFormat7()
483
tree = self.make_branch_and_memory_tree('dir', format=format)
484
# Guard against regression into MemoryTransport leaking
485
# files to disk instead of keeping them in memory.
486
self.failIf(osutils.lexists('dir'))
487
self.assertIsInstance(tree, memorytree.MemoryTree)
488
self.assertEqual(format.repository_format.__class__,
489
tree.branch.repository._format.__class__)
325
492
class TestTestCaseWithTransport(TestCaseWithTransport):
326
493
"""Tests for the convenience functions TestCaseWithTransport introduces."""
395
578
self.calls.append(('clear',))
580
def note(self, msg, *args):
581
self.calls.append(('note', msg, args))
398
584
class TestTestResult(TestCase):
400
def test_progress_bar_style_quiet(self):
401
# test using a progress bar.
402
dummy_test = TestTestResult('test_progress_bar_style_quiet')
403
dummy_error = (Exception, None, [])
404
mypb = MockProgress()
405
mypb.update('Running tests', 0, 4)
406
last_calls = mypb.calls[:]
407
result = bzrlib.tests._MyResult(self._log_file,
411
self.assertEqual(last_calls, mypb.calls)
414
result.startTest(dummy_test)
415
# starting a test prints the test name
416
self.assertEqual(last_calls + [('update', '...tyle_quiet', 0, None)], mypb.calls)
417
last_calls = mypb.calls[:]
418
result.addError(dummy_test, dummy_error)
419
self.assertEqual(last_calls + [('update', 'ERROR ', 1, None)], mypb.calls)
420
last_calls = mypb.calls[:]
423
result.startTest(dummy_test)
424
self.assertEqual(last_calls + [('update', '...tyle_quiet', 1, None)], mypb.calls)
425
last_calls = mypb.calls[:]
426
result.addFailure(dummy_test, dummy_error)
427
self.assertEqual(last_calls + [('update', 'FAIL ', 2, None)], mypb.calls)
428
last_calls = mypb.calls[:]
431
result.startTest(dummy_test)
432
self.assertEqual(last_calls + [('update', '...tyle_quiet', 2, None)], mypb.calls)
433
last_calls = mypb.calls[:]
434
result.addSuccess(dummy_test)
435
self.assertEqual(last_calls + [('update', 'OK ', 3, None)], mypb.calls)
436
last_calls = mypb.calls[:]
439
result.startTest(dummy_test)
440
self.assertEqual(last_calls + [('update', '...tyle_quiet', 3, None)], mypb.calls)
441
last_calls = mypb.calls[:]
442
result.addSkipped(dummy_test, dummy_error)
443
self.assertEqual(last_calls + [('update', 'SKIP ', 4, None)], mypb.calls)
444
last_calls = mypb.calls[:]
446
586
def test_elapsed_time_with_benchmarking(self):
447
result = bzrlib.tests._MyResult(self._log_file,
587
result = bzrlib.tests.TextTestResult(self._log_file,
453
593
result.extractBenchmarkTime(self)
454
594
timed_string = result._testTimeString()
455
595
# without explicit benchmarking, we should get a simple time.
456
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
596
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
457
597
# if a benchmark time is given, we want a x of y style result.
458
598
self.time(time.sleep, 0.001)
459
599
result.extractBenchmarkTime(self)
460
600
timed_string = result._testTimeString()
461
self.assertContainsRe(timed_string, "^ [0-9]ms/ [ 1-9][0-9]ms$")
601
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
462
602
# extracting the time from a non-bzrlib testcase sets to None
463
603
result._recordTestStartTime()
464
604
result.extractBenchmarkTime(
465
605
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
466
606
timed_string = result._testTimeString()
467
self.assertContainsRe(timed_string, "^ [0-9]ms$")
607
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
468
608
# cheat. Yes, wash thy mouth out with soap.
469
609
self._benchtime = None
611
def test_assigned_benchmark_file_stores_date(self):
613
result = bzrlib.tests.TextTestResult(self._log_file,
618
output_string = output.getvalue()
620
# if you are wondering about the regexp please read the comment in
621
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
622
# XXX: what comment? -- Andrew Bennetts
623
self.assertContainsRe(output_string, "--date [0-9.]+")
625
def test_benchhistory_records_test_times(self):
626
result_stream = StringIO()
627
result = bzrlib.tests.TextTestResult(
631
bench_history=result_stream
634
# we want profile a call and check that its test duration is recorded
635
# make a new test instance that when run will generate a benchmark
636
example_test_case = TestTestResult("_time_hello_world_encoding")
637
# execute the test, which should succeed and record times
638
example_test_case.run(result)
639
lines = result_stream.getvalue().splitlines()
640
self.assertEqual(2, len(lines))
641
self.assertContainsRe(lines[1],
642
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
643
"._time_hello_world_encoding")
471
645
def _time_hello_world_encoding(self):
472
646
"""Profile two sleep calls
562
723
result = self.run_test_runner(runner, test)
563
724
self.assertTrue(result.wasSuccessful())
726
def test_bench_history(self):
727
# tests that the running the benchmark produces a history file
728
# containing a timestamp and the revision id of the bzrlib source which
730
workingtree = _get_bzr_source_tree()
731
test = TestRunner('dummy_test')
733
runner = TextTestRunner(stream=self._log_file, bench_history=output)
734
result = self.run_test_runner(runner, test)
735
output_string = output.getvalue()
736
self.assertContainsRe(output_string, "--date [0-9.]+")
737
if workingtree is not None:
738
revision_id = workingtree.get_parent_ids()[0]
739
self.assertEndsWith(output_string.rstrip(), revision_id)
741
def test_success_log_deleted(self):
742
"""Successful tests have their log deleted"""
744
class LogTester(TestCase):
746
def test_success(self):
747
self.log('this will be removed\n')
749
sio = cStringIO.StringIO()
750
runner = TextTestRunner(stream=sio)
751
test = LogTester('test_success')
752
result = self.run_test_runner(runner, test)
754
log = test._get_log()
755
self.assertEqual("DELETED log file to reduce memory footprint", log)
756
self.assertEqual('', test._log_contents)
757
self.assertIs(None, test._log_file_name)
759
def test_fail_log_kept(self):
760
"""Failed tests have their log kept"""
762
class LogTester(TestCase):
765
self.log('this will be kept\n')
766
self.fail('this test fails')
768
sio = cStringIO.StringIO()
769
runner = TextTestRunner(stream=sio)
770
test = LogTester('test_fail')
771
result = self.run_test_runner(runner, test)
773
text = sio.getvalue()
774
self.assertContainsRe(text, 'this will be kept')
775
self.assertContainsRe(text, 'this test fails')
777
log = test._get_log()
778
self.assertContainsRe(log, 'this will be kept')
779
self.assertEqual(log, test._log_contents)
781
def test_error_log_kept(self):
782
"""Tests with errors have their log kept"""
784
class LogTester(TestCase):
786
def test_error(self):
787
self.log('this will be kept\n')
788
raise ValueError('random exception raised')
790
sio = cStringIO.StringIO()
791
runner = TextTestRunner(stream=sio)
792
test = LogTester('test_error')
793
result = self.run_test_runner(runner, test)
795
text = sio.getvalue()
796
self.assertContainsRe(text, 'this will be kept')
797
self.assertContainsRe(text, 'random exception raised')
799
log = test._get_log()
800
self.assertContainsRe(log, 'this will be kept')
801
self.assertEqual(log, test._log_contents)
566
804
class TestTestCase(TestCase):
567
805
"""Tests that test the core bzrlib TestCase."""
652
922
self.assertEndsWith('foo', 'oo')
653
923
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
925
def test_applyDeprecated_not_deprecated(self):
926
sample_object = ApplyDeprecatedHelper()
927
# calling an undeprecated callable raises an assertion
928
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
929
sample_object.sample_normal_method)
930
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
931
sample_undeprecated_function, "a param value")
932
# calling a deprecated callable (function or method) with the wrong
933
# expected deprecation fails.
934
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
935
sample_object.sample_deprecated_method, "a param value")
936
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
937
sample_deprecated_function)
938
# calling a deprecated callable (function or method) with the right
939
# expected deprecation returns the functions result.
940
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
941
sample_object.sample_deprecated_method, "a param value"))
942
self.assertEqual(2, self.applyDeprecated(zero_eleven,
943
sample_deprecated_function))
944
# calling a nested deprecation with the wrong deprecation version
945
# fails even if a deeper nested function was deprecated with the
947
self.assertRaises(AssertionError, self.applyDeprecated,
948
zero_eleven, sample_object.sample_nested_deprecation)
949
# calling a nested deprecation with the right deprecation value
950
# returns the calls result.
951
self.assertEqual(2, self.applyDeprecated(zero_ten,
952
sample_object.sample_nested_deprecation))
954
def test_callDeprecated(self):
955
def testfunc(be_deprecated, result=None):
956
if be_deprecated is True:
957
symbol_versioning.warn('i am deprecated', DeprecationWarning,
960
result = self.callDeprecated(['i am deprecated'], testfunc, True)
961
self.assertIs(None, result)
962
result = self.callDeprecated([], testfunc, False, 'result')
963
self.assertEqual('result', result)
964
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
965
self.callDeprecated([], testfunc, be_deprecated=False)
656
968
class TestConvenienceMakers(TestCaseWithTransport):
657
969
"""Test for the make_* convenience functions."""