3411
3405
self.verbosity)
3412
3406
tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3413
3407
self.assertLength(1, calls)
3410
class TestEnvironHandling(tests.TestCase):
3412
def test_overrideEnv_None_called_twice_doesnt_leak(self):
3413
self.failIf('MYVAR' in os.environ)
3414
self.overrideEnv('MYVAR', '42')
3415
# We use an embedded test to make sure we fix the _captureVar bug
3416
class Test(tests.TestCase):
3418
# The first call save the 42 value
3419
self.overrideEnv('MYVAR', None)
3420
self.assertEquals(None, os.environ.get('MYVAR'))
3421
# Make sure we can call it twice
3422
self.overrideEnv('MYVAR', None)
3423
self.assertEquals(None, os.environ.get('MYVAR'))
3425
result = tests.TextTestResult(output, 0, 1)
3426
Test('test_me').run(result)
3427
if not result.wasStrictlySuccessful():
3428
self.fail(output.getvalue())
3429
# We get our value back
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)