568
564
tree = self.make_branch_and_memory_tree('dir')
569
565
# Guard against regression into MemoryTransport leaking
570
566
# files to disk instead of keeping them in memory.
571
self.assertFalse(osutils.lexists('dir'))
567
self.failIf(osutils.lexists('dir'))
572
568
self.assertIsInstance(tree, memorytree.MemoryTree)
574
570
def test_make_branch_and_memory_tree_with_format(self):
575
571
"""make_branch_and_memory_tree should accept a format option."""
576
572
format = bzrdir.BzrDirMetaFormat1()
577
format.repository_format = repository.format_registry.get_default()
573
format.repository_format = weaverepo.RepositoryFormat7()
578
574
tree = self.make_branch_and_memory_tree('dir', format=format)
579
575
# Guard against regression into MemoryTransport leaking
580
576
# files to disk instead of keeping them in memory.
581
self.assertFalse(osutils.lexists('dir'))
577
self.failIf(osutils.lexists('dir'))
582
578
self.assertIsInstance(tree, memorytree.MemoryTree)
583
579
self.assertEqual(format.repository_format.__class__,
584
580
tree.branch.repository._format.__class__)
588
584
self.assertIsInstance(builder, branchbuilder.BranchBuilder)
589
585
# Guard against regression into MemoryTransport leaking
590
586
# files to disk instead of keeping them in memory.
591
self.assertFalse(osutils.lexists('dir'))
587
self.failIf(osutils.lexists('dir'))
593
589
def test_make_branch_builder_with_format(self):
594
590
# Use a repo layout that doesn't conform to a 'named' layout, to ensure
595
591
# that the format objects are used.
596
592
format = bzrdir.BzrDirMetaFormat1()
597
repo_format = repository.format_registry.get_default()
593
repo_format = weaverepo.RepositoryFormat7()
598
594
format.repository_format = repo_format
599
595
builder = self.make_branch_builder('dir', format=format)
600
596
the_branch = builder.get_branch()
601
597
# Guard against regression into MemoryTransport leaking
602
598
# files to disk instead of keeping them in memory.
603
self.assertFalse(osutils.lexists('dir'))
599
self.failIf(osutils.lexists('dir'))
604
600
self.assertEqual(format.repository_format.__class__,
605
601
the_branch.repository._format.__class__)
606
602
self.assertEqual(repo_format.get_format_string(),
768
764
self.check_timing(ShortDelayTestCase('test_short_delay'),
767
def _patch_get_bzr_source_tree(self):
768
# Reading from the actual source tree breaks isolation, but we don't
769
# want to assume that thats *all* that would happen.
770
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
772
def test_assigned_benchmark_file_stores_date(self):
773
self._patch_get_bzr_source_tree()
775
result = bzrlib.tests.TextTestResult(self._log_file,
780
output_string = output.getvalue()
781
# if you are wondering about the regexp please read the comment in
782
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
783
# XXX: what comment? -- Andrew Bennetts
784
self.assertContainsRe(output_string, "--date [0-9.]+")
786
def test_benchhistory_records_test_times(self):
787
self._patch_get_bzr_source_tree()
788
result_stream = StringIO()
789
result = bzrlib.tests.TextTestResult(
793
bench_history=result_stream
796
# we want profile a call and check that its test duration is recorded
797
# make a new test instance that when run will generate a benchmark
798
example_test_case = TestTestResult("_time_hello_world_encoding")
799
# execute the test, which should succeed and record times
800
example_test_case.run(result)
801
lines = result_stream.getvalue().splitlines()
802
self.assertEqual(2, len(lines))
803
self.assertContainsRe(lines[1],
804
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
805
"._time_hello_world_encoding")
771
807
def _time_hello_world_encoding(self):
772
808
"""Profile two sleep calls
1231
def _patch_get_bzr_source_tree(self):
1232
# Reading from the actual source tree breaks isolation, but we don't
1233
# want to assume that thats *all* that would happen.
1234
self._get_source_tree_calls = []
1236
self._get_source_tree_calls.append("called")
1238
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', new_get)
1240
def test_bench_history(self):
1241
# tests that the running the benchmark passes bench_history into
1242
# the test result object. We can tell that happens if
1243
# _get_bzr_source_tree is called.
1244
self._patch_get_bzr_source_tree()
1245
test = TestRunner('dummy_test')
1247
runner = tests.TextTestRunner(stream=self._log_file,
1248
bench_history=output)
1249
result = self.run_test_runner(runner, test)
1250
output_string = output.getvalue()
1251
self.assertContainsRe(output_string, "--date [0-9.]+")
1252
self.assertLength(1, self._get_source_tree_calls)
1195
1254
def test_verbose_test_count(self):
1196
1255
"""A verbose test run reports the right test count at the start"""
1197
1256
suite = TestUtil.TestSuite([
3270
3316
self.assertContainsString(result.stream.getvalue(), "leaking threads")
3273
class TestPostMortemDebugging(tests.TestCase):
3274
"""Check post mortem debugging works when tests fail or error"""
3276
class TracebackRecordingResult(tests.ExtendedTestResult):
3278
tests.ExtendedTestResult.__init__(self, StringIO(), 0, 1)
3279
self.postcode = None
3280
def _post_mortem(self, tb=None):
3281
"""Record the code object at the end of the current traceback"""
3282
tb = tb or sys.exc_info()[2]
3285
while next is not None:
3288
self.postcode = tb.tb_frame.f_code
3289
def report_error(self, test, err):
3291
def report_failure(self, test, err):
3294
def test_location_unittest_error(self):
3295
"""Needs right post mortem traceback with erroring unittest case"""
3296
class Test(unittest.TestCase):
3299
result = self.TracebackRecordingResult()
3301
self.assertEqual(result.postcode, Test.runTest.func_code)
3303
def test_location_unittest_failure(self):
3304
"""Needs right post mortem traceback with failing unittest case"""
3305
class Test(unittest.TestCase):
3307
raise self.failureException
3308
result = self.TracebackRecordingResult()
3310
self.assertEqual(result.postcode, Test.runTest.func_code)
3312
def test_location_bt_error(self):
3313
"""Needs right post mortem traceback with erroring bzrlib.tests case"""
3314
class Test(tests.TestCase):
3315
def test_error(self):
3317
result = self.TracebackRecordingResult()
3318
Test("test_error").run(result)
3319
self.assertEqual(result.postcode, Test.test_error.func_code)
3321
def test_location_bt_failure(self):
3322
"""Needs right post mortem traceback with failing bzrlib.tests case"""
3323
class Test(tests.TestCase):
3324
def test_failure(self):
3325
raise self.failureException
3326
result = self.TracebackRecordingResult()
3327
Test("test_failure").run(result)
3328
self.assertEqual(result.postcode, Test.test_failure.func_code)
3330
def test_env_var_triggers_post_mortem(self):
3331
"""Check pdb.post_mortem is called iff BZR_TEST_PDB is set"""
3333
result = tests.ExtendedTestResult(StringIO(), 0, 1)
3334
post_mortem_calls = []
3335
self.overrideAttr(pdb, "post_mortem", post_mortem_calls.append)
3336
self.overrideEnv('BZR_TEST_PDB', None)
3337
result._post_mortem(1)
3338
self.overrideEnv('BZR_TEST_PDB', 'on')
3339
result._post_mortem(2)
3340
self.assertEqual([2], post_mortem_calls)
3343
3319
class TestRunSuite(tests.TestCase):
3345
3321
def test_runner_class(self):
3356
3332
self.verbosity)
3357
3333
tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3358
3334
self.assertLength(1, calls)
3361
class TestEnvironHandling(tests.TestCase):
3363
def test_overrideEnv_None_called_twice_doesnt_leak(self):
3364
self.assertFalse('MYVAR' in os.environ)
3365
self.overrideEnv('MYVAR', '42')
3366
# We use an embedded test to make sure we fix the _captureVar bug
3367
class Test(tests.TestCase):
3369
# The first call save the 42 value
3370
self.overrideEnv('MYVAR', None)
3371
self.assertEquals(None, os.environ.get('MYVAR'))
3372
# Make sure we can call it twice
3373
self.overrideEnv('MYVAR', None)
3374
self.assertEquals(None, os.environ.get('MYVAR'))
3376
result = tests.TextTestResult(output, 0, 1)
3377
Test('test_me').run(result)
3378
if not result.wasStrictlySuccessful():
3379
self.fail(output.getvalue())
3380
# We get our value back
3381
self.assertEquals('42', os.environ.get('MYVAR'))
3384
class TestIsolatedEnv(tests.TestCase):
3385
"""Test isolating tests from os.environ.
3387
Since we use tests that are already isolated from os.environ a bit of care
3388
should be taken when designing the tests to avoid bootstrap side-effects.
3389
The tests start an already clean os.environ which allow doing valid
3390
assertions about which variables are present or not and design tests around
3394
class ScratchMonkey(tests.TestCase):
3399
def test_basics(self):
3400
# Make sure we know the definition of BZR_HOME: not part of os.environ
3401
# for tests.TestCase.
3402
self.assertTrue('BZR_HOME' in tests.isolated_environ)
3403
self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3404
# Being part of isolated_environ, BZR_HOME should not appear here
3405
self.assertFalse('BZR_HOME' in os.environ)
3406
# Make sure we know the definition of LINES: part of os.environ for
3408
self.assertTrue('LINES' in tests.isolated_environ)
3409
self.assertEquals('25', tests.isolated_environ['LINES'])
3410
self.assertEquals('25', os.environ['LINES'])
3412
def test_injecting_unknown_variable(self):
3413
# BZR_HOME is known to be absent from os.environ
3414
test = self.ScratchMonkey('test_me')
3415
tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3416
self.assertEquals('foo', os.environ['BZR_HOME'])
3417
tests.restore_os_environ(test)
3418
self.assertFalse('BZR_HOME' in os.environ)
3420
def test_injecting_known_variable(self):
3421
test = self.ScratchMonkey('test_me')
3422
# LINES is known to be present in os.environ
3423
tests.override_os_environ(test, {'LINES': '42'})
3424
self.assertEquals('42', os.environ['LINES'])
3425
tests.restore_os_environ(test)
3426
self.assertEquals('25', os.environ['LINES'])
3428
def test_deleting_variable(self):
3429
test = self.ScratchMonkey('test_me')
3430
# LINES is known to be present in os.environ
3431
tests.override_os_environ(test, {'LINES': None})
3432
self.assertTrue('LINES' not in os.environ)
3433
tests.restore_os_environ(test)
3434
self.assertEquals('25', os.environ['LINES'])
3437
class TestDocTestSuiteIsolation(tests.TestCase):
3438
"""Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3440
Since tests.TestCase alreay provides an isolation from os.environ, we use
3441
the clean environment as a base for testing. To precisely capture the
3442
isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3445
We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3446
not `os.environ` so each test overrides it to suit its needs.
3450
def get_doctest_suite_for_string(self, klass, string):
3451
class Finder(doctest.DocTestFinder):
3453
def find(*args, **kwargs):
3454
test = doctest.DocTestParser().get_doctest(
3455
string, {}, 'foo', 'foo.py', 0)
3458
suite = klass(test_finder=Finder())
3461
def run_doctest_suite_for_string(self, klass, string):
3462
suite = self.get_doctest_suite_for_string(klass, string)
3464
result = tests.TextTestResult(output, 0, 1)
3466
return result, output
3468
def assertDocTestStringSucceds(self, klass, string):
3469
result, output = self.run_doctest_suite_for_string(klass, string)
3470
if not result.wasStrictlySuccessful():
3471
self.fail(output.getvalue())
3473
def assertDocTestStringFails(self, klass, string):
3474
result, output = self.run_doctest_suite_for_string(klass, string)
3475
if result.wasStrictlySuccessful():
3476
self.fail(output.getvalue())
3478
def test_injected_variable(self):
3479
self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3482
>>> os.environ['LINES']
3485
# doctest.DocTestSuite fails as it sees '25'
3486
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3487
# tests.DocTestSuite sees '42'
3488
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3490
def test_deleted_variable(self):
3491
self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3494
>>> os.environ.get('LINES')
3496
# doctest.DocTestSuite fails as it sees '25'
3497
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3498
# tests.DocTestSuite sees None
3499
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)