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