3453
3454
self.fail(output.getvalue())
3454
3455
# We get our value back
3455
3456
self.assertEquals('42', os.environ.get('MYVAR'))
3459
class TestIsolatedEnv(tests.TestCase):
3460
"""Test isolating tests from os.environ.
3462
Since we use tests that are already isolated from os.environ a bit of care
3463
should be taken when designing the tests to avoid bootstrap side-effects.
3464
The tests start an already clean os.environ which allow doing valid
3465
assertions about which variables are present or not and design tests around
3469
class ScratchMonkey(tests.TestCase):
3474
def test_basics(self):
3475
# Make sure we know the definition of BZR_HOME: not part of os.environ
3476
# for tests.TestCase.
3477
self.assertTrue('BZR_HOME' in tests.isolated_environ)
3478
self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3479
# Being part of isolated_environ, BZR_HOME should not appear here
3480
self.assertFalse('BZR_HOME' in os.environ)
3481
# Make sure we know the definition of LINES: part of os.environ for
3483
self.assertTrue('LINES' in tests.isolated_environ)
3484
self.assertEquals('25', tests.isolated_environ['LINES'])
3485
self.assertEquals('25', os.environ['LINES'])
3487
def test_injecting_unknown_variable(self):
3488
# BZR_HOME is known to be absent from os.environ
3489
test = self.ScratchMonkey('test_me')
3490
tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3491
self.assertEquals('foo', os.environ['BZR_HOME'])
3492
tests.restore_os_environ(test)
3493
self.assertFalse('BZR_HOME' in os.environ)
3495
def test_injecting_known_variable(self):
3496
test = self.ScratchMonkey('test_me')
3497
# LINES is known to be present in os.environ
3498
tests.override_os_environ(test, {'LINES': '42'})
3499
self.assertEquals('42', os.environ['LINES'])
3500
tests.restore_os_environ(test)
3501
self.assertEquals('25', os.environ['LINES'])
3503
def test_deleting_variable(self):
3504
test = self.ScratchMonkey('test_me')
3505
# LINES is known to be present in os.environ
3506
tests.override_os_environ(test, {'LINES': None})
3507
self.assertTrue('LINES' not in os.environ)
3508
tests.restore_os_environ(test)
3509
self.assertEquals('25', os.environ['LINES'])
3512
class TestDocTestSuiteIsolation(tests.TestCase):
3513
"""Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3515
Since tests.TestCase alreay provides an isolation from os.environ, we use
3516
the clean environment as a base for testing. To precisely capture the
3517
isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3520
We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3521
not `os.environ` so each test overrides it to suit its needs.
3525
def get_doctest_suite_for_string(self, klass, string):
3526
class Finder(doctest.DocTestFinder):
3528
def find(*args, **kwargs):
3529
test = doctest.DocTestParser().get_doctest(
3530
string, {}, 'foo', 'foo.py', 0)
3533
suite = klass(test_finder=Finder())
3536
def run_doctest_suite_for_string(self, klass, string):
3537
suite = self.get_doctest_suite_for_string(klass, string)
3539
result = tests.TextTestResult(output, 0, 1)
3541
return result, output
3543
def assertDocTestStringSucceds(self, klass, string):
3544
result, output = self.run_doctest_suite_for_string(klass, string)
3545
if not result.wasStrictlySuccessful():
3546
self.fail(output.getvalue())
3548
def assertDocTestStringFails(self, klass, string):
3549
result, output = self.run_doctest_suite_for_string(klass, string)
3550
if result.wasStrictlySuccessful():
3551
self.fail(output.getvalue())
3553
def test_injected_variable(self):
3554
self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3557
>>> os.environ['LINES']
3560
# doctest.DocTestSuite fails as it sees '25'
3561
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3562
# tests.DocTestSuite sees '42'
3563
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3565
def test_deleted_variable(self):
3566
self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3569
>>> os.environ.get('LINES')
3571
# doctest.DocTestSuite fails as it sees '25'
3572
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3573
# tests.DocTestSuite sees None
3574
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)