38
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
37
39
from bzrlib.tests.TestUtil import _load_module_by_name
38
40
import bzrlib.errors as errors
39
41
from bzrlib import symbol_versioning
42
from bzrlib.symbol_versioning import zero_ten, zero_eleven
40
43
from bzrlib.trace import note
44
from bzrlib.transport.memory import MemoryServer, MemoryTransport
41
45
from bzrlib.version import _get_bzr_source_tree
419
424
self.assertEqual(tests[1].transport_server, server1)
420
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'])
422
437
class TestTestCaseWithTransport(TestCaseWithTransport):
423
438
"""Tests for the convenience functions TestCaseWithTransport introduces."""
463
478
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
481
class TestTestCaseTransports(TestCaseWithTransport):
484
super(TestTestCaseTransports, self).setUp()
485
self.transport_server = MemoryServer
487
def test_make_bzrdir_preserves_transport(self):
488
t = self.get_transport()
489
result_bzrdir = self.make_bzrdir('subdir')
490
self.assertIsInstance(result_bzrdir.transport,
492
# should not be on disk, should only be in memory
493
self.failIfExists('subdir')
466
496
class TestChrootedTest(ChrootedTestCase):
468
498
def test_root_is_root(self):
718
748
output_string = output.getvalue()
719
749
self.assertContainsRe(output_string, "--date [0-9.]+")
720
750
if workingtree is not None:
721
revision_id = workingtree.last_revision()
751
revision_id = workingtree.get_parent_ids()[0]
722
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)
725
817
class TestTestCase(TestCase):
726
818
"""Tests that test the core bzrlib TestCase."""
798
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()
801
919
class TestExtraAssertions(TestCase):
802
920
"""Tests for new test assertions in bzrlib test suite"""
811
929
self.assertEndsWith('foo', 'oo')
812
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))
814
961
def test_callDeprecated(self):
815
962
def testfunc(be_deprecated, result=None):
816
963
if be_deprecated is True:
821
968
self.assertIs(None, result)
822
969
result = self.callDeprecated([], testfunc, False, 'result')
823
970
self.assertEqual('result', result)
824
self.callDeprecated(['i am deprecated'], testfunc,
971
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
826
972
self.callDeprecated([], testfunc, be_deprecated=False)
838
984
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
839
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)
842
1009
class TestSelftest(TestCase):
843
1010
"""Tests of bzrlib.tests.selftest."""