~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: John Arbash Meinel
  • Date: 2011-01-10 22:20:12 UTC
  • mfrom: (5582 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5599.
  • Revision ID: john@arbash-meinel.com-20110110222012-mtcqudkvmzwiufuc
Merge in the bzr.dev 5582

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
3251
3251
        class Test(unittest.TestCase):
3252
3252
            def runTest(self):
3253
3253
                pass
3254
 
            addCleanup = None # for when on Python 2.7 with native addCleanup
3255
3254
        result = self.LeakRecordingResult()
3256
3255
        test = Test()
3257
 
        self.assertIs(getattr(test, "addCleanup", None), None)
3258
3256
        result.startTestRun()
3259
3257
        test.run(result)
3260
3258
        result.stopTestRun()
3387
3385
        result = tests.ExtendedTestResult(StringIO(), 0, 1)
3388
3386
        post_mortem_calls = []
3389
3387
        self.overrideAttr(pdb, "post_mortem", post_mortem_calls.append)
3390
 
        self.addCleanup(osutils.set_or_unset_env, "BZR_TEST_PDB",
3391
 
            osutils.set_or_unset_env("BZR_TEST_PDB", None))
 
3388
        self.overrideEnv('BZR_TEST_PDB', None)
3392
3389
        result._post_mortem(1)
3393
 
        os.environ["BZR_TEST_PDB"] = "on"
 
3390
        self.overrideEnv('BZR_TEST_PDB', 'on')
3394
3391
        result._post_mortem(2)
3395
3392
        self.assertEqual([2], post_mortem_calls)
3396
3393
 
3411
3408
                                                self.verbosity)
3412
3409
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3413
3410
        self.assertLength(1, calls)
 
3411
 
 
3412
 
 
3413
class TestEnvironHandling(tests.TestCase):
 
3414
 
 
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):
 
3420
            def test_me(self):
 
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'))
 
3428
        output = StringIO()
 
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'))
 
3436
 
 
3437
    def test_overrideEnv_None_called_twice_doesnt_leak(self):
 
3438
        self.failIf('MYVAR' in os.environ)
 
3439
        self.overrideEnv('MYVAR', '42')
 
3440
        # We use an embedded test to make sure we fix the _captureVar bug
 
3441
        class Test(tests.TestCase):
 
3442
            def test_me(self):
 
3443
                # The first call save the 42 value
 
3444
                self.overrideEnv('MYVAR', None)
 
3445
                self.assertEquals(None, os.environ.get('MYVAR'))
 
3446
                # Make sure we can call it twice
 
3447
                self.overrideEnv('MYVAR', None)
 
3448
                self.assertEquals(None, os.environ.get('MYVAR'))
 
3449
        output = StringIO()
 
3450
        result = tests.TextTestResult(output, 0, 1)
 
3451
        Test('test_me').run(result)
 
3452
        if not result.wasStrictlySuccessful():
 
3453
            self.fail(output.getvalue())
 
3454
        # We get our value back
 
3455
        self.assertEquals('42', os.environ.get('MYVAR'))