]> granicus.if.org Git - python/commitdiff
Merged revisions 71984 via svnmerge from
authorWalter Dörwald <walter@livinglogic.de>
Sun, 26 Apr 2009 21:39:21 +0000 (21:39 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Sun, 26 Apr 2009 21:39:21 +0000 (21:39 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r71984 | walter.doerwald | 2009-04-26 22:51:44 +0200 (So, 26 Apr 2009) | 2 lines

  Use test.test_support.EnvironmentVarGuard where tests change environment vars.
........

Lib/test/test_optparse.py
Lib/test/test_posixpath.py
Lib/test/test_site.py
Lib/test/test_tempfile.py
Lib/test/test_urllib.py
Lib/test/test_xmlrpc.py

index be2867c5ff49795de85eeb2240f504d9b7b2c3b9..5cf77e4af3f42baacc6a6c4c986bc1fe2c71bc97 100644 (file)
@@ -1448,15 +1448,9 @@ class TestHelp(BaseTest):
         # we must restore its original value -- otherwise, this test
         # screws things up for other tests when it's part of the Python
         # test suite.
-        orig_columns = os.environ.get('COLUMNS')
-        os.environ['COLUMNS'] = str(columns)
-        try:
+        with test_support.EnvironmentVarGuard() as env:
+            env.set('COLUMNS', str(columns))
             return InterceptingOptionParser(option_list=options)
-        finally:
-            if orig_columns is None:
-                del os.environ['COLUMNS']
-            else:
-                os.environ['COLUMNS'] = orig_columns
 
     def assertHelpEquals(self, expected_output):
         save_argv = sys.argv[:]
@@ -1479,16 +1473,10 @@ class TestHelp(BaseTest):
         self.assertHelpEquals(_expected_help_long_opts_first)
 
     def test_help_title_formatter(self):
-        save = os.environ.get("COLUMNS")
-        try:
-            os.environ["COLUMNS"] = "80"
+        with test_support.EnvironmentVarGuard() as env:
+            env.set("COLUMNS", "80")
             self.parser.formatter = TitledHelpFormatter()
             self.assertHelpEquals(_expected_help_title_formatter)
-        finally:
-            if save is not None:
-                os.environ["COLUMNS"] = save
-            else:
-                del os.environ["COLUMNS"]
 
     def test_wrap_columns(self):
         # Ensure that wrapping respects $COLUMNS environment variable.
index 7de94e70d862e9d53421e66461a55f5250f313fe..17be3d2c63e1854904b608c070dad89f0bfca257 100644 (file)
@@ -419,10 +419,9 @@ class PosixPathTest(unittest.TestCase):
             self.assert_(isinstance(posixpath.expanduser(b"~root/"), bytes))
             self.assert_(isinstance(posixpath.expanduser(b"~foo/"), bytes))
 
-            orig_home = os.environ['HOME']
-            os.environ['HOME'] = '/'
-            self.assertEqual(posixpath.expanduser("~"), "/")
-            os.environ['HOME'] = orig_home
+            with test_support.EnvironmentVarGuard() as env:
+                env.set('HOME', '/')
+                self.assertEqual(posixpath.expanduser("~"), "/")
 
         self.assertRaises(TypeError, posixpath.expanduser)
 
index 1bf143a06dd297df1ac7ccfd58a5e868a6754e88..8d101b308bf8dbf9ac30c6582d8b9aaa7fdffcd7 100644 (file)
@@ -5,7 +5,7 @@ executing have not been removed.
 
 """
 import unittest
-from test.support import run_unittest, TESTFN
+from test.support import run_unittest, TESTFN, EnvironmentVarGuard
 import builtins
 import os
 import sys
index 27a2183b4bd8175b81d37b90556847402e51a547..844e608a6ff9b4790d4719bc469cb6e22f3b8eea 100644 (file)
@@ -149,13 +149,11 @@ class test__candidate_tempdir_list(TC):
         # _candidate_tempdir_list contains the expected directories
 
         # Make sure the interesting environment variables are all set.
-        added = []
-        try:
+        with test_support.EnvironmentVarGuard() as env:
             for envname in 'TMPDIR', 'TEMP', 'TMP':
                 dirname = os.getenv(envname)
                 if not dirname:
-                    os.environ[envname] = os.path.abspath(envname)
-                    added.append(envname)
+                    env.set(envname, os.path.abspath(envname))
 
             cand = tempfile._candidate_tempdir_list()
 
@@ -173,9 +171,6 @@ class test__candidate_tempdir_list(TC):
 
             # Not practical to try to verify the presence of OS-specific
             # paths in this list.
-        finally:
-            for p in added:
-                del os.environ[p]
 
 test_classes.append(test__candidate_tempdir_list)
 
index c16f30dc7d9166b108deb31c67044f4cf978ea44..d616eb171364f3d7d457057eccc52de1a3164f84 100644 (file)
@@ -117,27 +117,19 @@ class urlopen_FileTests(unittest.TestCase):
 class ProxyTests(unittest.TestCase):
 
     def setUp(self):
-        # Save all proxy related env vars
-        self._saved_environ = dict([(k, v) for k, v in os.environ.items()
-                                    if k.lower().find('proxy') >= 0])
-        # Delete all proxy related env vars
-        for k in self._saved_environ:
-            del os.environ[k]
+        # Records changes to env vars
+        self.env = support.EnvironmentVarGuard()
 
     def tearDown(self):
         # Restore all proxy related env vars
-        for k, v in self._saved_environ.items():
-            os.environ[k] = v
+        self.env.__exit__()
+        del self.env
 
     def test_getproxies_environment_keep_no_proxies(self):
-        try:
-            os.environ['NO_PROXY'] = 'localhost'
-            proxies = urllib.request.getproxies_environment()
-            # getproxies_environment use lowered case truncated (no '_proxy') keys
-            self.assertEquals('localhost', proxies['no'])
-        finally:
-            # The old value will be restored by tearDown, if applicable.
-            del os.environ['NO_PROXY']
+        self.env.set('NO_PROXY', 'localhost')
+        proxies = urllib.request.getproxies_environment()
+        # getproxies_environment use lowered case truncated (no '_proxy') keys
+        self.assertEquals('localhost', proxies['no'])
 
 
 class urlopen_HttpTests(unittest.TestCase):
index 624793da95332eb706c84587fa57ecc948f5a19c..37347500a8fb57b1ec1503fbaae0e3a517a6e8ac 100644 (file)
@@ -571,25 +571,25 @@ class CGIHandlerTestCase(unittest.TestCase):
         self.cgi = None
 
     def test_cgi_get(self):
-        os.environ['REQUEST_METHOD'] = 'GET'
-        # if the method is GET and no request_text is given, it runs handle_get
-        # get sysout output
-        tmp = sys.stdout
-        sys.stdout = open(support.TESTFN, "w")
-        self.cgi.handle_request()
-        sys.stdout.close()
-        sys.stdout = tmp
+        with support.EnvironmentVarGuard() as env:
+            env.set('REQUEST_METHOD', 'GET')
+            # if the method is GET and no request_text is given, it runs handle_get
+            # get sysout output
+            tmp = sys.stdout
+            sys.stdout = open(support.TESTFN, "w")
+            self.cgi.handle_request()
+            sys.stdout.close()
+            sys.stdout = tmp
 
-        # parse Status header
-        handle = open(support.TESTFN, "r").read()
-        status = handle.split()[1]
-        message = ' '.join(handle.split()[2:4])
+            # parse Status header
+            handle = open(support.TESTFN, "r").read()
+            status = handle.split()[1]
+            message = ' '.join(handle.split()[2:4])
 
-        self.assertEqual(status, '400')
-        self.assertEqual(message, 'Bad Request')
+            self.assertEqual(status, '400')
+            self.assertEqual(message, 'Bad Request')
 
-        os.remove(support.TESTFN)
-        os.environ['REQUEST_METHOD'] = ''
+            os.remove(support.TESTFN)
 
     def test_cgi_xmlrpc_response(self):
         data = """<?xml version='1.0'?>
@@ -612,11 +612,9 @@ class CGIHandlerTestCase(unittest.TestCase):
         sys.stdin = open("xmldata.txt", "r")
         sys.stdout = open(support.TESTFN, "w")
 
-        os.environ['CONTENT_LENGTH'] = str(len(data))
-        try:
+        with support.EnvironmentVarGuard() as env:
+            env.set('CONTENT_LENGTH', str(len(data)))
             self.cgi.handle_request()
-        finally:
-            del os.environ['CONTENT_LENGTH']
 
         sys.stdin.close()
         sys.stdout.close()