571
555
tree = self.make_branch_and_memory_tree('dir')
572
556
# Guard against regression into MemoryTransport leaking
573
557
# files to disk instead of keeping them in memory.
574
self.assertFalse(osutils.lexists('dir'))
558
self.failIf(osutils.lexists('dir'))
575
559
self.assertIsInstance(tree, memorytree.MemoryTree)
577
561
def test_make_branch_and_memory_tree_with_format(self):
578
562
"""make_branch_and_memory_tree should accept a format option."""
579
563
format = bzrdir.BzrDirMetaFormat1()
580
format.repository_format = repository.format_registry.get_default()
564
format.repository_format = weaverepo.RepositoryFormat7()
581
565
tree = self.make_branch_and_memory_tree('dir', format=format)
582
566
# Guard against regression into MemoryTransport leaking
583
567
# files to disk instead of keeping them in memory.
584
self.assertFalse(osutils.lexists('dir'))
568
self.failIf(osutils.lexists('dir'))
585
569
self.assertIsInstance(tree, memorytree.MemoryTree)
586
570
self.assertEqual(format.repository_format.__class__,
587
571
tree.branch.repository._format.__class__)
591
575
self.assertIsInstance(builder, branchbuilder.BranchBuilder)
592
576
# Guard against regression into MemoryTransport leaking
593
577
# files to disk instead of keeping them in memory.
594
self.assertFalse(osutils.lexists('dir'))
578
self.failIf(osutils.lexists('dir'))
596
580
def test_make_branch_builder_with_format(self):
597
581
# Use a repo layout that doesn't conform to a 'named' layout, to ensure
598
582
# that the format objects are used.
599
583
format = bzrdir.BzrDirMetaFormat1()
600
repo_format = repository.format_registry.get_default()
584
repo_format = weaverepo.RepositoryFormat7()
601
585
format.repository_format = repo_format
602
586
builder = self.make_branch_builder('dir', format=format)
603
587
the_branch = builder.get_branch()
604
588
# Guard against regression into MemoryTransport leaking
605
589
# files to disk instead of keeping them in memory.
606
self.assertFalse(osutils.lexists('dir'))
590
self.failIf(osutils.lexists('dir'))
607
591
self.assertEqual(format.repository_format.__class__,
608
592
the_branch.repository._format.__class__)
609
593
self.assertEqual(repo_format.get_format_string(),
771
755
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")
774
798
def _time_hello_world_encoding(self):
775
799
"""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)
1198
1245
def test_verbose_test_count(self):
1199
1246
"""A verbose test run reports the right test count at the start"""
1200
1247
suite = TestUtil.TestSuite([
3359
3394
self.verbosity)
3360
3395
tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3361
3396
self.assertLength(1, calls)
3364
class TestEnvironHandling(tests.TestCase):
3366
def test_overrideEnv_None_called_twice_doesnt_leak(self):
3367
self.assertFalse('MYVAR' in os.environ)
3368
self.overrideEnv('MYVAR', '42')
3369
# We use an embedded test to make sure we fix the _captureVar bug
3370
class Test(tests.TestCase):
3372
# The first call save the 42 value
3373
self.overrideEnv('MYVAR', None)
3374
self.assertEquals(None, os.environ.get('MYVAR'))
3375
# Make sure we can call it twice
3376
self.overrideEnv('MYVAR', None)
3377
self.assertEquals(None, os.environ.get('MYVAR'))
3379
result = tests.TextTestResult(output, 0, 1)
3380
Test('test_me').run(result)
3381
if not result.wasStrictlySuccessful():
3382
self.fail(output.getvalue())
3383
# We get our value back
3384
self.assertEquals('42', os.environ.get('MYVAR'))
3387
class TestIsolatedEnv(tests.TestCase):
3388
"""Test isolating tests from os.environ.
3390
Since we use tests that are already isolated from os.environ a bit of care
3391
should be taken when designing the tests to avoid bootstrap side-effects.
3392
The tests start an already clean os.environ which allow doing valid
3393
assertions about which variables are present or not and design tests around
3397
class ScratchMonkey(tests.TestCase):
3402
def test_basics(self):
3403
# Make sure we know the definition of BZR_HOME: not part of os.environ
3404
# for tests.TestCase.
3405
self.assertTrue('BZR_HOME' in tests.isolated_environ)
3406
self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3407
# Being part of isolated_environ, BZR_HOME should not appear here
3408
self.assertFalse('BZR_HOME' in os.environ)
3409
# Make sure we know the definition of LINES: part of os.environ for
3411
self.assertTrue('LINES' in tests.isolated_environ)
3412
self.assertEquals('25', tests.isolated_environ['LINES'])
3413
self.assertEquals('25', os.environ['LINES'])
3415
def test_injecting_unknown_variable(self):
3416
# BZR_HOME is known to be absent from os.environ
3417
test = self.ScratchMonkey('test_me')
3418
tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3419
self.assertEquals('foo', os.environ['BZR_HOME'])
3420
tests.restore_os_environ(test)
3421
self.assertFalse('BZR_HOME' in os.environ)
3423
def test_injecting_known_variable(self):
3424
test = self.ScratchMonkey('test_me')
3425
# LINES is known to be present in os.environ
3426
tests.override_os_environ(test, {'LINES': '42'})
3427
self.assertEquals('42', os.environ['LINES'])
3428
tests.restore_os_environ(test)
3429
self.assertEquals('25', os.environ['LINES'])
3431
def test_deleting_variable(self):
3432
test = self.ScratchMonkey('test_me')
3433
# LINES is known to be present in os.environ
3434
tests.override_os_environ(test, {'LINES': None})
3435
self.assertTrue('LINES' not in os.environ)
3436
tests.restore_os_environ(test)
3437
self.assertEquals('25', os.environ['LINES'])
3440
class TestDocTestSuiteIsolation(tests.TestCase):
3441
"""Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3443
Since tests.TestCase alreay provides an isolation from os.environ, we use
3444
the clean environment as a base for testing. To precisely capture the
3445
isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3448
We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3449
not `os.environ` so each test overrides it to suit its needs.
3453
def get_doctest_suite_for_string(self, klass, string):
3454
class Finder(doctest.DocTestFinder):
3456
def find(*args, **kwargs):
3457
test = doctest.DocTestParser().get_doctest(
3458
string, {}, 'foo', 'foo.py', 0)
3461
suite = klass(test_finder=Finder())
3464
def run_doctest_suite_for_string(self, klass, string):
3465
suite = self.get_doctest_suite_for_string(klass, string)
3467
result = tests.TextTestResult(output, 0, 1)
3469
return result, output
3471
def assertDocTestStringSucceds(self, klass, string):
3472
result, output = self.run_doctest_suite_for_string(klass, string)
3473
if not result.wasStrictlySuccessful():
3474
self.fail(output.getvalue())
3476
def assertDocTestStringFails(self, klass, string):
3477
result, output = self.run_doctest_suite_for_string(klass, string)
3478
if result.wasStrictlySuccessful():
3479
self.fail(output.getvalue())
3481
def test_injected_variable(self):
3482
self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3485
>>> os.environ['LINES']
3488
# doctest.DocTestSuite fails as it sees '25'
3489
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3490
# tests.DocTestSuite sees '42'
3491
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3493
def test_deleted_variable(self):
3494
self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3497
>>> os.environ.get('LINES')
3499
# doctest.DocTestSuite fails as it sees '25'
3500
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3501
# tests.DocTestSuite sees None
3502
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)