555
568
tree = self.make_branch_and_memory_tree('dir')
556
569
# Guard against regression into MemoryTransport leaking
557
570
# files to disk instead of keeping them in memory.
558
self.failIf(osutils.lexists('dir'))
571
self.assertFalse(osutils.lexists('dir'))
559
572
self.assertIsInstance(tree, memorytree.MemoryTree)
561
574
def test_make_branch_and_memory_tree_with_format(self):
562
575
"""make_branch_and_memory_tree should accept a format option."""
563
576
format = bzrdir.BzrDirMetaFormat1()
564
format.repository_format = weaverepo.RepositoryFormat7()
577
format.repository_format = repository.format_registry.get_default()
565
578
tree = self.make_branch_and_memory_tree('dir', format=format)
566
579
# Guard against regression into MemoryTransport leaking
567
580
# files to disk instead of keeping them in memory.
568
self.failIf(osutils.lexists('dir'))
581
self.assertFalse(osutils.lexists('dir'))
569
582
self.assertIsInstance(tree, memorytree.MemoryTree)
570
583
self.assertEqual(format.repository_format.__class__,
571
584
tree.branch.repository._format.__class__)
575
588
self.assertIsInstance(builder, branchbuilder.BranchBuilder)
576
589
# Guard against regression into MemoryTransport leaking
577
590
# files to disk instead of keeping them in memory.
578
self.failIf(osutils.lexists('dir'))
591
self.assertFalse(osutils.lexists('dir'))
580
593
def test_make_branch_builder_with_format(self):
581
594
# Use a repo layout that doesn't conform to a 'named' layout, to ensure
582
595
# that the format objects are used.
583
596
format = bzrdir.BzrDirMetaFormat1()
584
repo_format = weaverepo.RepositoryFormat7()
597
repo_format = repository.format_registry.get_default()
585
598
format.repository_format = repo_format
586
599
builder = self.make_branch_builder('dir', format=format)
587
600
the_branch = builder.get_branch()
588
601
# Guard against regression into MemoryTransport leaking
589
602
# files to disk instead of keeping them in memory.
590
self.failIf(osutils.lexists('dir'))
603
self.assertFalse(osutils.lexists('dir'))
591
604
self.assertEqual(format.repository_format.__class__,
592
605
the_branch.repository._format.__class__)
593
606
self.assertEqual(repo_format.get_format_string(),
755
768
self.check_timing(ShortDelayTestCase('test_short_delay'),
758
def _patch_get_bzr_source_tree(self):
759
# Reading from the actual source tree breaks isolation, but we don't
760
# want to assume that thats *all* that would happen.
761
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
763
def test_assigned_benchmark_file_stores_date(self):
764
self._patch_get_bzr_source_tree()
766
result = bzrlib.tests.TextTestResult(self._log_file,
771
output_string = output.getvalue()
772
# if you are wondering about the regexp please read the comment in
773
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
774
# XXX: what comment? -- Andrew Bennetts
775
self.assertContainsRe(output_string, "--date [0-9.]+")
777
def test_benchhistory_records_test_times(self):
778
self._patch_get_bzr_source_tree()
779
result_stream = StringIO()
780
result = bzrlib.tests.TextTestResult(
784
bench_history=result_stream
787
# we want profile a call and check that its test duration is recorded
788
# make a new test instance that when run will generate a benchmark
789
example_test_case = TestTestResult("_time_hello_world_encoding")
790
# execute the test, which should succeed and record times
791
example_test_case.run(result)
792
lines = result_stream.getvalue().splitlines()
793
self.assertEqual(2, len(lines))
794
self.assertContainsRe(lines[1],
795
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
796
"._time_hello_world_encoding")
798
771
def _time_hello_world_encoding(self):
799
772
"""Profile two sleep calls
1222
def _patch_get_bzr_source_tree(self):
1223
# Reading from the actual source tree breaks isolation, but we don't
1224
# want to assume that thats *all* that would happen.
1225
self._get_source_tree_calls = []
1227
self._get_source_tree_calls.append("called")
1229
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', new_get)
1231
def test_bench_history(self):
1232
# tests that the running the benchmark passes bench_history into
1233
# the test result object. We can tell that happens if
1234
# _get_bzr_source_tree is called.
1235
self._patch_get_bzr_source_tree()
1236
test = TestRunner('dummy_test')
1238
runner = tests.TextTestRunner(stream=self._log_file,
1239
bench_history=output)
1240
result = self.run_test_runner(runner, test)
1241
output_string = output.getvalue()
1242
self.assertContainsRe(output_string, "--date [0-9.]+")
1243
self.assertLength(1, self._get_source_tree_calls)
1245
1195
def test_verbose_test_count(self):
1246
1196
"""A verbose test run reports the right test count at the start"""
1247
1197
suite = TestUtil.TestSuite([
3394
3356
self.verbosity)
3395
3357
tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3396
3358
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)