3415
3412
class TestEnvironHandling(tests.TestCase):
3417
def test__captureVar_None_called_twice_leaks(self):
3418
self.failIf('MYVAR' in os.environ)
3419
self._captureVar('MYVAR', '42')
3420
# We need an embedded test to observe the bug
3421
class Test(tests.TestCase):
3423
# The first call save the 42 value
3424
self._captureVar('MYVAR', None)
3425
self.assertEquals(None, os.environ.get('MYVAR'))
3426
self.assertEquals('42', self._old_env.get('MYVAR'))
3427
# But the second one erases it !
3428
self._captureVar('MYVAR', None)
3429
self.assertEquals(None, self._old_env.get('MYVAR'))
3431
result = tests.TextTestResult(output, 0, 1)
3432
Test('test_me').run(result)
3433
if not result.wasStrictlySuccessful():
3434
self.fail(output.getvalue())
3435
# And we have lost all trace of the original value
3436
self.assertEquals(None, os.environ.get('MYVAR'))
3437
self.assertEquals(None, self._old_env.get('MYVAR'))
3439
3414
def test_overrideEnv_None_called_twice_doesnt_leak(self):
3440
3415
self.failIf('MYVAR' in os.environ)
3441
3416
self.overrideEnv('MYVAR', '42')
3455
3430
self.fail(output.getvalue())
3456
3431
# We get our value back
3457
3432
self.assertEquals('42', os.environ.get('MYVAR'))
3435
class TestIsolatedEnv(tests.TestCase):
3436
"""Test isolating tests from os.environ.
3438
Since we use tests that are already isolated from os.environ a bit of care
3439
should be taken when designing the tests to avoid bootstrap side-effects.
3440
The tests start an already clean os.environ which allow doing valid
3441
assertions about which variables are present or not and design tests around
3445
class ScratchMonkey(tests.TestCase):
3450
def test_basics(self):
3451
# Make sure we know the definition of BZR_HOME: not part of os.environ
3452
# for tests.TestCase.
3453
self.assertTrue('BZR_HOME' in tests.isolated_environ)
3454
self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3455
# Being part of isolated_environ, BZR_HOME should not appear here
3456
self.assertFalse('BZR_HOME' in os.environ)
3457
# Make sure we know the definition of LINES: part of os.environ for
3459
self.assertTrue('LINES' in tests.isolated_environ)
3460
self.assertEquals('25', tests.isolated_environ['LINES'])
3461
self.assertEquals('25', os.environ['LINES'])
3463
def test_injecting_unknown_variable(self):
3464
# BZR_HOME is known to be absent from os.environ
3465
test = self.ScratchMonkey('test_me')
3466
tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3467
self.assertEquals('foo', os.environ['BZR_HOME'])
3468
tests.restore_os_environ(test)
3469
self.assertFalse('BZR_HOME' in os.environ)
3471
def test_injecting_known_variable(self):
3472
test = self.ScratchMonkey('test_me')
3473
# LINES is known to be present in os.environ
3474
tests.override_os_environ(test, {'LINES': '42'})
3475
self.assertEquals('42', os.environ['LINES'])
3476
tests.restore_os_environ(test)
3477
self.assertEquals('25', os.environ['LINES'])
3479
def test_deleting_variable(self):
3480
test = self.ScratchMonkey('test_me')
3481
# LINES is known to be present in os.environ
3482
tests.override_os_environ(test, {'LINES': None})
3483
self.assertTrue('LINES' not in os.environ)
3484
tests.restore_os_environ(test)
3485
self.assertEquals('25', os.environ['LINES'])
3488
class TestDocTestSuiteIsolation(tests.TestCase):
3489
"""Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3491
Since tests.TestCase alreay provides an isolation from os.environ, we use
3492
the clean environment as a base for testing. To precisely capture the
3493
isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3496
We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3497
not `os.environ` so each test overrides it to suit its needs.
3501
def get_doctest_suite_for_string(self, klass, string):
3502
class Finder(doctest.DocTestFinder):
3504
def find(*args, **kwargs):
3505
test = doctest.DocTestParser().get_doctest(
3506
string, {}, 'foo', 'foo.py', 0)
3509
suite = klass(test_finder=Finder())
3512
def run_doctest_suite_for_string(self, klass, string):
3513
suite = self.get_doctest_suite_for_string(klass, string)
3515
result = tests.TextTestResult(output, 0, 1)
3517
return result, output
3519
def assertDocTestStringSucceds(self, klass, string):
3520
result, output = self.run_doctest_suite_for_string(klass, string)
3521
if not result.wasStrictlySuccessful():
3522
self.fail(output.getvalue())
3524
def assertDocTestStringFails(self, klass, string):
3525
result, output = self.run_doctest_suite_for_string(klass, string)
3526
if result.wasStrictlySuccessful():
3527
self.fail(output.getvalue())
3529
def test_injected_variable(self):
3530
self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3533
>>> os.environ['LINES']
3536
# doctest.DocTestSuite fails as it sees '25'
3537
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3538
# tests.DocTestSuite sees '42'
3539
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3541
def test_deleted_variable(self):
3542
self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3545
>>> os.environ.get('LINES')
3547
# doctest.DocTestSuite fails as it sees '25'
3548
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3549
# tests.DocTestSuite sees None
3550
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)