]> granicus.if.org Git - python/commitdiff
Changed distutils tests to avoid environment alteration
authorTarek Ziadé <ziade.tarek@gmail.com>
Sun, 18 Oct 2009 09:28:26 +0000 (09:28 +0000)
committerTarek Ziadé <ziade.tarek@gmail.com>
Sun, 18 Oct 2009 09:28:26 +0000 (09:28 +0000)
13 files changed:
Lib/distutils/tests/support.py
Lib/distutils/tests/test_bdist_dumb.py
Lib/distutils/tests/test_bdist_rpm.py
Lib/distutils/tests/test_build_ext.py
Lib/distutils/tests/test_config.py
Lib/distutils/tests/test_core.py
Lib/distutils/tests/test_dist.py
Lib/distutils/tests/test_install.py
Lib/distutils/tests/test_install_data.py
Lib/distutils/tests/test_install_headers.py
Lib/distutils/tests/test_install_lib.py
Lib/distutils/tests/test_sysconfig.py
Lib/distutils/tests/test_util.py

index 3c328cc9a64bb43369305e7c1067ff8725703e3d..41d3cd3c66fdd5949a8bfb1c911bdb2b2f34cd2b 100644 (file)
@@ -2,11 +2,11 @@
 import os
 import shutil
 import tempfile
+from copy import deepcopy
 
 from distutils import log
 from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
 from distutils.core import Distribution
-from test.test_support import EnvironmentVarGuard
 
 class LoggingSilencer(object):
 
@@ -111,8 +111,15 @@ class EnvironGuard(object):
 
     def setUp(self):
         super(EnvironGuard, self).setUp()
-        self.environ = EnvironmentVarGuard()
+        self.old_environ = deepcopy(os.environ)
 
     def tearDown(self):
-        self.environ.__exit__()
+        for key, value in self.old_environ.items():
+            if os.environ.get(key) != value:
+                os.environ[key] = value
+
+        for key in os.environ.keys():
+            if key not in self.old_environ:
+                del os.environ[key]
+
         super(EnvironGuard, self).tearDown()
index a838f73476b6989a1004ca331ec853e12d428dce..5eaef2a9d70229aacf253a1d9727dd0c889aac96 100644 (file)
@@ -26,16 +26,18 @@ setup(name='foo', version='0.1', py_modules=['foo'],
 
 class BuildDumbTestCase(support.TempdirManager,
                         support.LoggingSilencer,
+                        support.EnvironGuard,
                         unittest.TestCase):
 
     def setUp(self):
         super(BuildDumbTestCase, self).setUp()
         self.old_location = os.getcwd()
-        self.old_sys_argv = sys.argv[:]
+        self.old_sys_argv = sys.argv, sys.argv[:]
 
     def tearDown(self):
         os.chdir(self.old_location)
-        sys.argv = self.old_sys_argv[:]
+        sys.argv = self.old_sys_argv[0]
+        sys.argv[:] = self.old_sys_argv[1]
         super(BuildDumbTestCase, self).tearDown()
 
     @unittest.skipUnless(zlib, "requires zlib")
index c2715675426866efbd7d7e2d4278182549c4433a..2aa257f7e633bf9b62b2804f6c6f7c5a0cef4e3a 100644 (file)
@@ -29,11 +29,12 @@ class BuildRpmTestCase(support.TempdirManager,
     def setUp(self):
         super(BuildRpmTestCase, self).setUp()
         self.old_location = os.getcwd()
-        self.old_sys_argv = sys.argv[:]
+        self.old_sys_argv = sys.argv, sys.argv[:]
 
     def tearDown(self):
         os.chdir(self.old_location)
-        sys.argv = self.old_sys_argv[:]
+        sys.argv = self.old_sys_argv[0]
+        sys.argv[:] = self.old_sys_argv[1]
         super(BuildRpmTestCase, self).tearDown()
 
     def test_quiet(self):
index 4c09dd80a38910e4fd17ec5150aac1060840cf16..317ce2bca0c54b12bdf7378da4fef996ee5b744b 100644 (file)
@@ -34,7 +34,7 @@ class BuildExtTestCase(support.TempdirManager,
         # Note that we're making changes to sys.path
         super(BuildExtTestCase, self).setUp()
         self.tmp_dir = self.mkdtemp()
-        self.sys_path = sys.path[:]
+        self.sys_path = sys.path, sys.path[:]
         sys.path.append(self.tmp_dir)
         shutil.copy(_get_source_filename(), self.tmp_dir)
         if sys.version > "2.6":
@@ -89,7 +89,8 @@ class BuildExtTestCase(support.TempdirManager,
     def tearDown(self):
         # Get everything back to normal
         test_support.unload('xx')
-        sys.path = self.sys_path
+        sys.path = self.sys_path[0]
+        sys.path[:] = self.sys_path[1]
         if sys.version > "2.6":
             import site
             site.USER_BASE = self.old_user_base
index 694727556968eb6bc7162d83c2a7109099bd50b9..0a1bb961ff0cc06c78d56250dc99ee2c74d64929 100644 (file)
@@ -56,7 +56,7 @@ class PyPIRCCommandTestCase(support.TempdirManager,
         """Patches the environment."""
         super(PyPIRCCommandTestCase, self).setUp()
         self.tmp_dir = self.mkdtemp()
-        self.environ['HOME'] = self.tmp_dir
+        os.environ['HOME'] = self.tmp_dir
         self.rc = os.path.join(self.tmp_dir, '.pypirc')
         self.dist = Distribution()
 
index 3c26fddbc6d93daa424294c5b9977685db9000ea..48ec1b436e83025b86d2734fe0ac0700092495bc 100644 (file)
@@ -8,7 +8,7 @@ import sys
 import test.test_support
 from test.test_support import captured_stdout
 import unittest
-
+from distutils.tests import support
 
 # setup script that uses __file__
 setup_using___file__ = """\
@@ -29,17 +29,20 @@ setup()
 """
 
 
-class CoreTestCase(unittest.TestCase):
+class CoreTestCase(support.EnvironGuard, unittest.TestCase):
 
     def setUp(self):
+        super(CoreTestCase, self).setUp()
         self.old_stdout = sys.stdout
         self.cleanup_testfn()
-        self.old_argv = sys.argv[:]
+        self.old_argv = sys.argv, sys.argv[:]
 
     def tearDown(self):
         sys.stdout = self.old_stdout
         self.cleanup_testfn()
-        sys.argv = self.old_argv[:]
+        sys.argv = self.old_argv[0]
+        sys.argv[:] = self.old_argv[1]
+        super(CoreTestCase, self).tearDown()
 
     def cleanup_testfn(self):
         path = test.test_support.TESTFN
index 3d4b25f0fcea1136a43075e827c2ca6a67168023..5f032ca60fa19dde28e0396affc75353c2330661 100644 (file)
@@ -39,15 +39,17 @@ class TestDistribution(Distribution):
 
 class DistributionTestCase(support.TempdirManager,
                            support.LoggingSilencer,
+                           support.EnvironGuard,
                            unittest.TestCase):
 
     def setUp(self):
         super(DistributionTestCase, self).setUp()
-        self.argv = sys.argv[:]
+        self.argv = sys.argv, sys.argv[:]
         del sys.argv[1:]
 
     def tearDown(self):
-        sys.argv[:] = self.argv
+        sys.argv = self.argv[0]
+        sys.argv[:] = self.argv[1]
         super(DistributionTestCase, self).tearDown()
 
     def create_distribution(self, configfiles=()):
@@ -210,6 +212,15 @@ class DistributionTestCase(support.TempdirManager,
 class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
                        unittest.TestCase):
 
+    def setUp(self):
+        super(MetadataTestCase, self).setUp()
+        self.argv = sys.argv, sys.argv[:]
+
+    def tearDown(self):
+        sys.argv = self.argv[0]
+        sys.argv[:] = self.argv[1]
+        super(MetadataTestCase, self).tearDown()
+
     def test_simple_metadata(self):
         attrs = {"name": "package",
                  "version": "1.0"}
@@ -308,14 +319,14 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
 
             # linux-style
             if sys.platform in ('linux', 'darwin'):
-                self.environ['HOME'] = temp_dir
+                os.environ['HOME'] = temp_dir
                 files = dist.find_config_files()
                 self.assertTrue(user_filename in files)
 
             # win32-style
             if sys.platform == 'win32':
                 # home drive should be found
-                self.environ['HOME'] = temp_dir
+                os.environ['HOME'] = temp_dir
                 files = dist.find_config_files()
                 self.assertTrue(user_filename in files,
                              '%r not found in %r' % (user_filename, files))
@@ -331,15 +342,11 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
     def test_show_help(self):
         # smoke test, just makes sure some help is displayed
         dist = Distribution()
-        old_argv = sys.argv
         sys.argv = []
-        try:
-            dist.help = 1
-            dist.script_name = 'setup.py'
-            with captured_stdout() as s:
-                dist.parse_command_line()
-        finally:
-            sys.argv = old_argv
+        dist.help = 1
+        dist.script_name = 'setup.py'
+        with captured_stdout() as s:
+            dist.parse_command_line()
 
         output = [line for line in s.getvalue().split('\n')
                   if line.strip() != '']
index d2b8c8e9b65f598a86b6dc0092ff6b3f039989bc..d44156dd45dce35f67ffb10a761122f5634da739 100644 (file)
@@ -17,6 +17,7 @@ from distutils.errors import DistutilsOptionError
 from distutils.tests import support
 
 class InstallTestCase(support.TempdirManager,
+                      support.EnvironGuard,
                       support.LoggingSilencer,
                       unittest.TestCase):
 
index 7072136792b7aebc43c2d8532551130bcd051b63..377ae86e2f2e5168e8a1ecd32dcaf53f417c0495 100644 (file)
@@ -9,6 +9,7 @@ from distutils.tests import support
 
 class InstallDataTestCase(support.TempdirManager,
                           support.LoggingSilencer,
+                          support.EnvironGuard,
                           unittest.TestCase):
 
     def test_simple_run(self):
index 2564563faf0e18bb1a6a852fc72c5f24a7113950..5b32b13ee44ea5405422e48542e9bdb9633a0cd5 100644 (file)
@@ -9,6 +9,7 @@ from distutils.tests import support
 
 class InstallHeadersTestCase(support.TempdirManager,
                              support.LoggingSilencer,
+                             support.EnvironGuard,
                              unittest.TestCase):
 
     def test_simple_run(self):
index 793b95cad6a99d916183cbc638bae01458924d88..b2185b8442e230df4c7978a73fc5896aff915c9d 100644 (file)
@@ -10,9 +10,9 @@ from distutils.errors import DistutilsOptionError
 
 class InstallLibTestCase(support.TempdirManager,
                          support.LoggingSilencer,
+                         support.EnvironGuard,
                          unittest.TestCase):
 
-
     def test_finalize_options(self):
         pkg_dir, dist = self.create_dist()
         cmd = install_lib(dist)
index edc8fd8d60eddb0000a02cccba3308884ff16585..498714de818666c44de582f3f7c3122862d2ed7d 100644 (file)
@@ -17,8 +17,16 @@ class SysconfigTestCase(support.EnvironGuard,
     def tearDown(self):
         if self.makefile is not None:
             os.unlink(self.makefile)
+        self.cleanup_testfn()
         super(SysconfigTestCase, self).tearDown()
 
+    def cleanup_testfn(self):
+        path = test.test_support.TESTFN
+        if os.path.isfile(path):
+            os.remove(path)
+        elif os.path.isdir(path):
+            shutil.rmtree(path)
+
     def test_get_config_h_filename(self):
         config_h = sysconfig.get_config_h_filename()
         self.assertTrue(os.path.isfile(config_h), config_h)
@@ -51,8 +59,8 @@ class SysconfigTestCase(support.EnvironGuard,
         if get_default_compiler() != 'unix':
             return
 
-        self.environ['AR'] = 'my_ar'
-        self.environ['ARFLAGS'] = '-arflags'
+        os.environ['AR'] = 'my_ar'
+        os.environ['ARFLAGS'] = '-arflags'
 
         # make sure AR gets caught
         class compiler:
index 7190b2c9c69b1a9ddd75b7d436bec993e9d6d4a7..4099c950ad4f31ade7783bb3fbc2d6f48c1153d8 100644 (file)
@@ -121,7 +121,7 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
                    ('Darwin Kernel Version 8.11.1: '
                     'Wed Oct 10 18:23:28 PDT 2007; '
                     'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
-        self.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
+        os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
 
         get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
                                        '-fwrapv -O3 -Wall -Wstrict-prototypes')
@@ -129,7 +129,7 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
         self.assertEquals(get_platform(), 'macosx-10.3-i386')
 
         # macbook with fat binaries (fat, universal or fat64)
-        self.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
+        os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
         get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
                                        '/Developer/SDKs/MacOSX10.4u.sdk  '
                                        '-fno-strict-aliasing -fno-common '
@@ -250,17 +250,18 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
 
     def test_check_environ(self):
         util._environ_checked = 0
+        if 'HOME' in os.environ:
+            del os.environ['HOME']
 
         # posix without HOME
         if os.name == 'posix':  # this test won't run on windows
             check_environ()
             import pwd
-            self.assertEquals(self.environ['HOME'],
-                            pwd.getpwuid(os.getuid())[5])
+            self.assertEquals(os.environ['HOME'], pwd.getpwuid(os.getuid())[5])
         else:
             check_environ()
 
-        self.assertEquals(self.environ['PLAT'], get_platform())
+        self.assertEquals(os.environ['PLAT'], get_platform())
         self.assertEquals(util._environ_checked, 1)
 
     def test_split_quoted(self):