Rev 5573: Introduce a more robust way to override environment variables (not deployed yet). in file:///home/vila/src/bzr/bugs/690563-better-env-isolation/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Wed Dec 15 18:04:29 GMT 2010
At file:///home/vila/src/bzr/bugs/690563-better-env-isolation/
------------------------------------------------------------
revno: 5573
revision-id: v.ladeuil+lp at free.fr-20101215180429-5vh8hb6cvxxq16t1
parent: v.ladeuil+lp at free.fr-20101215172500-hcns8bi2omdzetq8
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 690563-better-env-isolation
timestamp: Wed 2010-12-15 19:04:29 +0100
message:
Introduce a more robust way to override environment variables (not deployed yet).
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2010-12-02 10:41:05 +0000
+++ b/bzrlib/tests/__init__.py 2010-12-15 18:04:29 +0000
@@ -1539,6 +1539,20 @@
setattr(obj, attr_name, new)
return value
+ def overrideEnv(self, name, value):
+ """Set an environment variable, and reset it when the test finished.
+
+ :param name: The name if the environment variable.
+ :param value: The value to set the variable to. If None, the
+ variable is deleted from the environment.
+ """
+ if name in self._old_env:
+ # We already protect this variable so we should not record its
+ # initial value
+ osutils.set_or_unset_env(name, value)
+ else:
+ self._old_env[name] = osutils.set_or_unset_env(name, value)
+
def _cleanEnvironment(self):
new_env = {
'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py 2010-12-15 17:25:00 +0000
+++ b/bzrlib/tests/test_selftest.py 2010-12-15 18:04:29 +0000
@@ -3417,16 +3417,40 @@
def test__captureVar_None_called_twice_leaks(self):
self._captureVar('MYVAR', '42')
+ # We need an embedded test to observe the bug
class Test(tests.TestCase):
def test_me(self):
- # The first call records 42
+ # The first call save the 42 value
self._captureVar('MYVAR', None)
+ self.assertEquals(None, os.environ.get('MYVAR'))
self.assertEquals('42', self._old_env.get('MYVAR'))
# But the second one erases it !
self._captureVar('MYVAR', None)
self.assertEquals(None, self._old_env.get('MYVAR'))
- result = tests.ExtendedTestResult(StringIO(), 0, 1)
+ output = StringIO()
+ result = tests.TextTestResult(output, 0, 1)
Test('test_me').run(result)
+ self.assertTrue(result.wasStrictlySuccessful())
# And we have lost all trace of the original value
self.assertEquals(None, os.environ.get('MYVAR'))
self.assertEquals(None, self._old_env.get('MYVAR'))
+
+ def test_overrideEnv_None_called_twice_doesnt_leak(self):
+ self.overrideEnv('MYVAR', '42')
+ # We use an embedded test to make sure we fix the _captureVar bug
+ class Test(tests.TestCase):
+ def test_me(self):
+ # The first call save the 42 value
+ self.overrideEnv('MYVAR', None)
+ self.assertEquals(None, os.environ.get('MYVAR'))
+ self.assertEquals('42', self._old_env.get('MYVAR'))
+ # The second one respect it
+ self.overrideEnv('MYVAR', None)
+ self.assertEquals(None, os.environ.get('MYVAR'))
+ self.assertEquals('42', self._old_env.get('MYVAR'))
+ output = StringIO()
+ result = tests.TextTestResult(output, 0, 1)
+ Test('test_me').run(result)
+ self.assertTrue(result.wasStrictlySuccessful())
+ # We get our value back
+ self.assertEquals('42', os.environ.get('MYVAR'))
=== modified file 'doc/en/release-notes/bzr-2.3.txt'
--- a/doc/en/release-notes/bzr-2.3.txt 2010-12-14 23:32:28 +0000
+++ b/doc/en/release-notes/bzr-2.3.txt 2010-12-15 18:04:29 +0000
@@ -185,6 +185,10 @@
to 0.9.5 which will allow tests that need the fixed unicode handling to be
written. (Martin [gz])
+* Introduce an ``overrideEnv()`` helper for tests that needs to change the
+ environment variables while respecting the isoltation rules.
+ (Vincent Ladeuil, #690563)
+
* Printing selftest results to a non-UTF-8 console will now escape characters
that can't be encoded rather than aborting the test run with an exception.
(Martin [gz], #633216)
More information about the bazaar-commits
mailing list