332
331
self.assertEqual(tests[1].transport_readonly_server, server2)
334
class TestTreeProviderAdapter(TestCase):
335
"""Test the setup of tree_implementation tests."""
337
def test_adapted_tests(self):
338
# the tree implementation adapter is meant to setup one instance for
339
# each working tree format, and one additional instance that will
340
# use the default wt format, but create a revision tree for the tests.
341
# this means that the wt ones should have the workingtree_to_test_tree
342
# attribute set to 'return_parameter' and the revision one set to
343
# revision_tree_from_workingtree.
345
from bzrlib.tests.tree_implementations import (
346
TreeTestProviderAdapter,
348
revision_tree_from_workingtree
350
from bzrlib.workingtree import WorkingTreeFormat
351
input_test = TestTreeProviderAdapter(
352
"test_adapted_tests")
355
formats = [("c", "C"), ("d", "D")]
356
adapter = TreeTestProviderAdapter(server1, server2, formats)
357
suite = adapter.adapt(input_test)
358
tests = list(iter(suite))
359
self.assertEqual(3, len(tests))
360
default_format = WorkingTreeFormat.get_default_format()
361
self.assertEqual(tests[0].workingtree_format, formats[0][0])
362
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
363
self.assertEqual(tests[0].transport_server, server1)
364
self.assertEqual(tests[0].transport_readonly_server, server2)
365
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
366
self.assertEqual(tests[1].workingtree_format, formats[1][0])
367
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
368
self.assertEqual(tests[1].transport_server, server1)
369
self.assertEqual(tests[1].transport_readonly_server, server2)
370
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
371
self.assertEqual(tests[2].workingtree_format, default_format)
372
self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
373
self.assertEqual(tests[2].transport_server, server1)
374
self.assertEqual(tests[2].transport_readonly_server, server2)
375
self.assertEqual(tests[2].workingtree_to_test_tree,
376
revision_tree_from_workingtree)
379
class TestInterTreeProviderAdapter(TestCase):
380
"""A group of tests that test the InterTreeTestAdapter."""
382
def test_adapted_tests(self):
383
# check that constructor parameters are passed through to the adapted
385
# for InterTree tests we want the machinery to bring up two trees in
386
# each instance: the base one, and the one we are interacting with.
387
# because each optimiser can be direction specific, we need to test
388
# each optimiser in its chosen direction.
389
# unlike the TestProviderAdapter we dont want to automatically add a
390
# parameterised one for WorkingTree - the optimisers will tell us what
392
from bzrlib.tests.tree_implementations import (
394
revision_tree_from_workingtree
396
from bzrlib.tests.intertree_implementations import (
397
InterTreeTestProviderAdapter,
399
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
400
input_test = TestInterTreeProviderAdapter(
401
"test_adapted_tests")
404
format1 = WorkingTreeFormat2()
405
format2 = WorkingTreeFormat3()
406
formats = [(str, format1, format2, False, True),
407
(int, format2, format1, False, True)]
408
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
409
suite = adapter.adapt(input_test)
410
tests = list(iter(suite))
411
self.assertEqual(2, len(tests))
412
self.assertEqual(tests[0].intertree_class, formats[0][0])
413
self.assertEqual(tests[0].workingtree_format, formats[0][1])
414
self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
415
self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
416
self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
417
self.assertEqual(tests[0].transport_server, server1)
418
self.assertEqual(tests[0].transport_readonly_server, server2)
419
self.assertEqual(tests[1].intertree_class, formats[1][0])
420
self.assertEqual(tests[1].workingtree_format, formats[1][1])
421
self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
422
self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
423
self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
424
self.assertEqual(tests[1].transport_server, server1)
425
self.assertEqual(tests[1].transport_readonly_server, server2)
428
class TestTestCaseInTempDir(TestCaseInTempDir):
430
def test_home_is_not_working(self):
431
self.assertNotEqual(self.test_dir, self.test_home_dir)
432
cwd = osutils.getcwd()
433
self.assertEqual(self.test_dir, cwd)
434
self.assertEqual(self.test_home_dir, os.environ['HOME'])
335
437
class TestTestCaseWithTransport(TestCaseWithTransport):
336
438
"""Tests for the convenience functions TestCaseWithTransport introduces."""
384
502
self.assertEqual(url, t.clone('..').base)
505
class MockProgress(_BaseProgressBar):
506
"""Progress-bar standin that records calls.
508
Useful for testing pb using code.
512
_BaseProgressBar.__init__(self)
516
self.calls.append(('tick',))
518
def update(self, msg=None, current=None, total=None):
519
self.calls.append(('update', msg, current, total))
522
self.calls.append(('clear',))
524
def note(self, msg, *args):
525
self.calls.append(('note', msg, args))
528
class TestTestResult(TestCase):
530
def test_progress_bar_style_quiet(self):
531
# test using a progress bar.
532
dummy_test = TestTestResult('test_progress_bar_style_quiet')
533
dummy_error = (Exception, None, [])
534
mypb = MockProgress()
535
mypb.update('Running tests', 0, 4)
536
last_calls = mypb.calls[:]
538
result = bzrlib.tests._MyResult(self._log_file,
542
self.assertEqual(last_calls, mypb.calls)
545
"""Shorten a string based on the terminal width"""
546
return result._ellipsise_unimportant_words(s,
547
osutils.terminal_width())
550
result.startTest(dummy_test)
551
# starting a test prints the test name
552
last_calls += [('update', '...tyle_quiet', 0, None)]
553
self.assertEqual(last_calls, mypb.calls)
554
result.addError(dummy_test, dummy_error)
555
last_calls += [('update', 'ERROR ', 1, None),
556
('note', shorten(dummy_test.id() + ': ERROR'), ())
558
self.assertEqual(last_calls, mypb.calls)
561
result.startTest(dummy_test)
562
last_calls += [('update', '...tyle_quiet', 1, None)]
563
self.assertEqual(last_calls, mypb.calls)
564
last_calls += [('update', 'FAIL ', 2, None),
565
('note', shorten(dummy_test.id() + ': FAIL'), ())
567
result.addFailure(dummy_test, dummy_error)
568
self.assertEqual(last_calls, mypb.calls)
571
result.startTest(dummy_test)
572
last_calls += [('update', '...tyle_quiet', 2, None)]
573
self.assertEqual(last_calls, mypb.calls)
574
result.addSuccess(dummy_test)
575
last_calls += [('update', 'OK ', 3, None)]
576
self.assertEqual(last_calls, mypb.calls)
579
result.startTest(dummy_test)
580
last_calls += [('update', '...tyle_quiet', 3, None)]
581
self.assertEqual(last_calls, mypb.calls)
582
result.addSkipped(dummy_test, dummy_error)
583
last_calls += [('update', 'SKIP ', 4, None)]
584
self.assertEqual(last_calls, mypb.calls)
586
def test_elapsed_time_with_benchmarking(self):
587
result = bzrlib.tests._MyResult(self._log_file,
591
result._recordTestStartTime()
593
result.extractBenchmarkTime(self)
594
timed_string = result._testTimeString()
595
# without explicit benchmarking, we should get a simple time.
596
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
597
# if a benchmark time is given, we want a x of y style result.
598
self.time(time.sleep, 0.001)
599
result.extractBenchmarkTime(self)
600
timed_string = result._testTimeString()
601
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms/ [ 1-9][0-9]ms$")
602
# extracting the time from a non-bzrlib testcase sets to None
603
result._recordTestStartTime()
604
result.extractBenchmarkTime(
605
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
606
timed_string = result._testTimeString()
607
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
608
# cheat. Yes, wash thy mouth out with soap.
609
self._benchtime = None
611
def test_assigned_benchmark_file_stores_date(self):
613
result = bzrlib.tests._MyResult(self._log_file,
618
output_string = output.getvalue()
619
# if you are wondering about the regexp please read the comment in
620
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
621
# XXX: what comment? -- Andrew Bennetts
622
self.assertContainsRe(output_string, "--date [0-9.]+")
624
def test_benchhistory_records_test_times(self):
625
result_stream = StringIO()
626
result = bzrlib.tests._MyResult(
630
bench_history=result_stream
633
# we want profile a call and check that its test duration is recorded
634
# make a new test instance that when run will generate a benchmark
635
example_test_case = TestTestResult("_time_hello_world_encoding")
636
# execute the test, which should succeed and record times
637
example_test_case.run(result)
638
lines = result_stream.getvalue().splitlines()
639
self.assertEqual(2, len(lines))
640
self.assertContainsRe(lines[1],
641
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
642
"._time_hello_world_encoding")
644
def _time_hello_world_encoding(self):
645
"""Profile two sleep calls
647
This is used to exercise the test framework.
649
self.time(unicode, 'hello', errors='replace')
650
self.time(unicode, 'world', errors='replace')
652
def test_lsprofiling(self):
653
"""Verbose test result prints lsprof statistics from test cases."""
657
raise TestSkipped("lsprof not installed.")
658
result_stream = StringIO()
659
result = bzrlib.tests._MyResult(
660
unittest._WritelnDecorator(result_stream),
664
# we want profile a call of some sort and check it is output by
665
# addSuccess. We dont care about addError or addFailure as they
666
# are not that interesting for performance tuning.
667
# make a new test instance that when run will generate a profile
668
example_test_case = TestTestResult("_time_hello_world_encoding")
669
example_test_case._gather_lsprof_in_benchmarks = True
670
# execute the test, which should succeed and record profiles
671
example_test_case.run(result)
672
# lsprofile_something()
673
# if this worked we want
674
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
675
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
676
# (the lsprof header)
677
# ... an arbitrary number of lines
678
# and the function call which is time.sleep.
679
# 1 0 ??? ??? ???(sleep)
680
# and then repeated but with 'world', rather than 'hello'.
681
# this should appear in the output stream of our test result.
682
output = result_stream.getvalue()
683
self.assertContainsRe(output,
684
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
685
self.assertContainsRe(output,
686
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
687
self.assertContainsRe(output,
688
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
689
self.assertContainsRe(output,
690
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
693
class TestRunner(TestCase):
695
def dummy_test(self):
698
def run_test_runner(self, testrunner, test):
699
"""Run suite in testrunner, saving global state and restoring it.
701
This current saves and restores:
702
TestCaseInTempDir.TEST_ROOT
704
There should be no tests in this file that use bzrlib.tests.TextTestRunner
705
without using this convenience method, because of our use of global state.
707
old_root = TestCaseInTempDir.TEST_ROOT
709
TestCaseInTempDir.TEST_ROOT = None
710
return testrunner.run(test)
712
TestCaseInTempDir.TEST_ROOT = old_root
714
def test_accepts_and_uses_pb_parameter(self):
715
test = TestRunner('dummy_test')
716
mypb = MockProgress()
717
self.assertEqual([], mypb.calls)
718
runner = TextTestRunner(stream=self._log_file, pb=mypb)
719
result = self.run_test_runner(runner, test)
720
self.assertEqual(1, result.testsRun)
721
self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
722
self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
723
self.assertEqual(('update', 'OK ', 1, None), mypb.calls[2])
724
self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
725
self.assertEqual(('clear',), mypb.calls[4])
726
self.assertEqual(5, len(mypb.calls))
728
def test_skipped_test(self):
729
# run a test that is skipped, and check the suite as a whole still
731
# skipping_test must be hidden in here so it's not run as a real test
733
raise TestSkipped('test intentionally skipped')
734
runner = TextTestRunner(stream=self._log_file, keep_output=True)
735
test = unittest.FunctionTestCase(skipping_test)
736
result = self.run_test_runner(runner, test)
737
self.assertTrue(result.wasSuccessful())
739
def test_bench_history(self):
740
# tests that the running the benchmark produces a history file
741
# containing a timestamp and the revision id of the bzrlib source which
743
workingtree = _get_bzr_source_tree()
744
test = TestRunner('dummy_test')
746
runner = TextTestRunner(stream=self._log_file, bench_history=output)
747
result = self.run_test_runner(runner, test)
748
output_string = output.getvalue()
749
self.assertContainsRe(output_string, "--date [0-9.]+")
750
if workingtree is not None:
751
revision_id = workingtree.get_parent_ids()[0]
752
self.assertEndsWith(output_string.rstrip(), revision_id)
754
def test_success_log_deleted(self):
755
"""Successful tests have their log deleted"""
757
class LogTester(TestCase):
759
def test_success(self):
760
self.log('this will be removed\n')
762
sio = cStringIO.StringIO()
763
runner = TextTestRunner(stream=sio)
764
test = LogTester('test_success')
765
result = self.run_test_runner(runner, test)
767
log = test._get_log()
768
self.assertEqual("DELETED log file to reduce memory footprint", log)
769
self.assertEqual('', test._log_contents)
770
self.assertIs(None, test._log_file_name)
772
def test_fail_log_kept(self):
773
"""Failed tests have their log kept"""
775
class LogTester(TestCase):
778
self.log('this will be kept\n')
779
self.fail('this test fails')
781
sio = cStringIO.StringIO()
782
runner = TextTestRunner(stream=sio)
783
test = LogTester('test_fail')
784
result = self.run_test_runner(runner, test)
786
text = sio.getvalue()
787
self.assertContainsRe(text, 'this will be kept')
788
self.assertContainsRe(text, 'this test fails')
790
log = test._get_log()
791
self.assertContainsRe(log, 'this will be kept')
792
self.assertEqual(log, test._log_contents)
794
def test_error_log_kept(self):
795
"""Tests with errors have their log kept"""
797
class LogTester(TestCase):
799
def test_error(self):
800
self.log('this will be kept\n')
801
raise ValueError('random exception raised')
803
sio = cStringIO.StringIO()
804
runner = TextTestRunner(stream=sio)
805
test = LogTester('test_error')
806
result = self.run_test_runner(runner, test)
808
text = sio.getvalue()
809
self.assertContainsRe(text, 'this will be kept')
810
self.assertContainsRe(text, 'random exception raised')
812
log = test._get_log()
813
self.assertContainsRe(log, 'this will be kept')
814
self.assertEqual(log, test._log_contents)
817
class TestTestCase(TestCase):
818
"""Tests that test the core bzrlib TestCase."""
820
def inner_test(self):
821
# the inner child test
824
def outer_child(self):
825
# the outer child test
827
self.inner_test = TestTestCase("inner_child")
828
result = bzrlib.tests._MyResult(self._log_file,
831
self.inner_test.run(result)
834
def test_trace_nesting(self):
835
# this tests that each test case nests its trace facility correctly.
836
# we do this by running a test case manually. That test case (A)
837
# should setup a new log, log content to it, setup a child case (B),
838
# which should log independently, then case (A) should log a trailer
840
# we do two nested children so that we can verify the state of the
841
# logs after the outer child finishes is correct, which a bad clean
842
# up routine in tearDown might trigger a fault in our test with only
843
# one child, we should instead see the bad result inside our test with
845
# the outer child test
846
original_trace = bzrlib.trace._trace_file
847
outer_test = TestTestCase("outer_child")
848
result = bzrlib.tests._MyResult(self._log_file,
851
outer_test.run(result)
852
self.assertEqual(original_trace, bzrlib.trace._trace_file)
854
def method_that_times_a_bit_twice(self):
855
# call self.time twice to ensure it aggregates
856
self.time(time.sleep, 0.007)
857
self.time(time.sleep, 0.007)
859
def test_time_creates_benchmark_in_result(self):
860
"""Test that the TestCase.time() method accumulates a benchmark time."""
861
sample_test = TestTestCase("method_that_times_a_bit_twice")
862
output_stream = StringIO()
863
result = bzrlib.tests._MyResult(
864
unittest._WritelnDecorator(output_stream),
867
sample_test.run(result)
868
self.assertContainsRe(
869
output_stream.getvalue(),
870
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
872
def test__gather_lsprof_in_benchmarks(self):
873
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
875
Each self.time() call is individually and separately profiled.
880
raise TestSkipped("lsprof not installed.")
881
# overrides the class member with an instance member so no cleanup
883
self._gather_lsprof_in_benchmarks = True
884
self.time(time.sleep, 0.000)
885
self.time(time.sleep, 0.003)
886
self.assertEqual(2, len(self._benchcalls))
887
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
888
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
889
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
890
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
893
@symbol_versioning.deprecated_function(zero_eleven)
894
def sample_deprecated_function():
895
"""A deprecated function to test applyDeprecated with."""
899
def sample_undeprecated_function(a_param):
900
"""A undeprecated function to test applyDeprecated with."""
903
class ApplyDeprecatedHelper(object):
904
"""A helper class for ApplyDeprecated tests."""
906
@symbol_versioning.deprecated_method(zero_eleven)
907
def sample_deprecated_method(self, param_one):
908
"""A deprecated method for testing with."""
911
def sample_normal_method(self):
912
"""A undeprecated method."""
914
@symbol_versioning.deprecated_method(zero_ten)
915
def sample_nested_deprecation(self):
916
return sample_deprecated_function()
387
919
class TestExtraAssertions(TestCase):
388
920
"""Tests for new test assertions in bzrlib test suite"""
392
924
self.assertIsInstance(u'', basestring)
393
925
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
394
926
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
928
def test_assertEndsWith(self):
929
self.assertEndsWith('foo', 'oo')
930
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
932
def test_applyDeprecated_not_deprecated(self):
933
sample_object = ApplyDeprecatedHelper()
934
# calling an undeprecated callable raises an assertion
935
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
936
sample_object.sample_normal_method)
937
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
938
sample_undeprecated_function, "a param value")
939
# calling a deprecated callable (function or method) with the wrong
940
# expected deprecation fails.
941
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
942
sample_object.sample_deprecated_method, "a param value")
943
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
944
sample_deprecated_function)
945
# calling a deprecated callable (function or method) with the right
946
# expected deprecation returns the functions result.
947
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
948
sample_object.sample_deprecated_method, "a param value"))
949
self.assertEqual(2, self.applyDeprecated(zero_eleven,
950
sample_deprecated_function))
951
# calling a nested deprecation with the wrong deprecation version
952
# fails even if a deeper nested function was deprecated with the
954
self.assertRaises(AssertionError, self.applyDeprecated,
955
zero_eleven, sample_object.sample_nested_deprecation)
956
# calling a nested deprecation with the right deprecation value
957
# returns the calls result.
958
self.assertEqual(2, self.applyDeprecated(zero_ten,
959
sample_object.sample_nested_deprecation))
961
def test_callDeprecated(self):
962
def testfunc(be_deprecated, result=None):
963
if be_deprecated is True:
964
symbol_versioning.warn('i am deprecated', DeprecationWarning,
967
result = self.callDeprecated(['i am deprecated'], testfunc, True)
968
self.assertIs(None, result)
969
result = self.callDeprecated([], testfunc, False, 'result')
970
self.assertEqual('result', result)
971
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
972
self.callDeprecated([], testfunc, be_deprecated=False)
975
class TestConvenienceMakers(TestCaseWithTransport):
976
"""Test for the make_* convenience functions."""
978
def test_make_branch_and_tree_with_format(self):
979
# we should be able to supply a format to make_branch_and_tree
980
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
981
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
982
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
983
bzrlib.bzrdir.BzrDirMetaFormat1)
984
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
985
bzrlib.bzrdir.BzrDirFormat6)
987
def test_make_branch_and_mutable_tree(self):
988
# we should be able to get a new branch and a mutable tree from
989
# TestCaseWithTransport
990
tree = self.make_branch_and_memory_tree('a')
991
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
994
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
996
def test_make_tree_for_sftp_branch(self):
997
"""Transports backed by local directories create local trees."""
999
tree = self.make_branch_and_tree('t1')
1000
base = tree.bzrdir.root_transport.base
1001
self.failIf(base.startswith('sftp'),
1002
'base %r is on sftp but should be local' % base)
1003
self.assertEquals(tree.bzrdir.root_transport,
1004
tree.branch.bzrdir.root_transport)
1005
self.assertEquals(tree.bzrdir.root_transport,
1006
tree.branch.repository.bzrdir.root_transport)
1009
class TestSelftest(TestCase):
1010
"""Tests of bzrlib.tests.selftest."""
1012
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1015
factory_called.append(True)
1019
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1020
test_suite_factory=factory)
1021
self.assertEqual([True], factory_called)