From 450ca11a58c222bcf7a225e947b5f9fbcb45d311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarek=20Ziad=C3=A9?= Date: Sun, 10 May 2009 12:17:30 +0000 Subject: [PATCH] now using EnvironGuard everywhere --- Lib/distutils/tests/test_config.py | 29 +++---------------- Lib/distutils/tests/test_dist.py | 22 ++++----------- Lib/distutils/tests/test_util.py | 45 ++++++++++-------------------- 3 files changed, 25 insertions(+), 71 deletions(-) diff --git a/Lib/distutils/tests/test_config.py b/Lib/distutils/tests/test_config.py index 7737538bf8f..fd778d1ba53 100644 --- a/Lib/distutils/tests/test_config.py +++ b/Lib/distutils/tests/test_config.py @@ -49,18 +49,14 @@ password:xxx class PyPIRCCommandTestCase(support.TempdirManager, support.LoggingSilencer, + support.EnvironGuard, unittest.TestCase): def setUp(self): """Patches the environment.""" super(PyPIRCCommandTestCase, self).setUp() - - if os.environ.has_key('HOME'): - self._old_home = os.environ['HOME'] - else: - self._old_home = None self.tmp_dir = self.mkdtemp() - os.environ['HOME'] = self.tmp_dir + self.environ['HOME'] = self.tmp_dir self.rc = os.path.join(self.tmp_dir, '.pypirc') self.dist = Distribution() @@ -76,10 +72,6 @@ class PyPIRCCommandTestCase(support.TempdirManager, def tearDown(self): """Removes the patch.""" - if self._old_home is None: - del os.environ['HOME'] - else: - os.environ['HOME'] = self._old_home set_threshold(self.old_threshold) super(PyPIRCCommandTestCase, self).tearDown() @@ -89,12 +81,7 @@ class PyPIRCCommandTestCase(support.TempdirManager, # 2. handle the old format # new format - f = open(self.rc, 'w') - try: - f.write(PYPIRC) - finally: - f.close() - + self.write_file(self.rc, PYPIRC) cmd = self._cmd(self.dist) config = cmd._read_pypirc() @@ -106,10 +93,7 @@ class PyPIRCCommandTestCase(support.TempdirManager, self.assertEquals(config, waited) # old format - f = open(self.rc, 'w') - f.write(PYPIRC_OLD) - f.close() - + self.write_file(self.rc, PYPIRC_OLD) config = cmd._read_pypirc() config = config.items() config.sort() @@ -119,19 +103,14 @@ class PyPIRCCommandTestCase(support.TempdirManager, self.assertEquals(config, waited) def test_server_empty_registration(self): - cmd = self._cmd(self.dist) rc = cmd._get_rc_file() self.assert_(not os.path.exists(rc)) - cmd._store_pypirc('tarek', 'xxx') - self.assert_(os.path.exists(rc)) content = open(rc).read() - self.assertEquals(content, WANTED) - def test_suite(): return unittest.makeSuite(PyPIRCCommandTestCase) diff --git a/Lib/distutils/tests/test_dist.py b/Lib/distutils/tests/test_dist.py index 847df7bb60d..3304790b51f 100644 --- a/Lib/distutils/tests/test_dist.py +++ b/Lib/distutils/tests/test_dist.py @@ -39,13 +39,13 @@ class TestDistribution(distutils.dist.Distribution): class DistributionTestCase(support.TempdirManager, unittest.TestCase): def setUp(self): - support.TempdirManager.setUp(self) + super(DistributionTestCase, self).setUp() self.argv = sys.argv[:] del sys.argv[1:] def tearDown(self): sys.argv[:] = self.argv - support.TempdirManager.tearDown(self) + super(DistributionTestCase, self).tearDown() def create_distribution(self, configfiles=()): d = TestDistribution() @@ -151,7 +151,8 @@ class DistributionTestCase(support.TempdirManager, unittest.TestCase): self.assertEquals(len(warns), 0) -class MetadataTestCase(support.TempdirManager, unittest.TestCase): +class MetadataTestCase(support.TempdirManager, support.EnvironGuard, + unittest.TestCase): def test_simple_metadata(self): attrs = {"name": "package", @@ -238,13 +239,6 @@ class MetadataTestCase(support.TempdirManager, unittest.TestCase): def test_custom_pydistutils(self): # fixes #2166 # make sure pydistutils.cfg is found - old = {} - for env in ('HOME', 'HOMEPATH', 'HOMEDRIVE'): - value = os.environ.get(env) - old[env] = value - if value is not None: - del os.environ[env] - if os.name == 'posix': user_filename = ".pydistutils.cfg" else: @@ -261,22 +255,18 @@ class MetadataTestCase(support.TempdirManager, unittest.TestCase): # linux-style if sys.platform in ('linux', 'darwin'): - os.environ['HOME'] = temp_dir + self.environ['HOME'] = temp_dir files = dist.find_config_files() self.assert_(user_filename in files) # win32-style if sys.platform == 'win32': # home drive should be found - os.environ['HOME'] = temp_dir + self.environ['HOME'] = temp_dir files = dist.find_config_files() self.assert_(user_filename in files, '%r not found in %r' % (user_filename, files)) finally: - for key, value in old.items(): - if value is None: - continue - os.environ[key] = value os.remove(user_filename) def test_suite(): diff --git a/Lib/distutils/tests/test_util.py b/Lib/distutils/tests/test_util.py index ea7c5925b79..cee7d5263b8 100644 --- a/Lib/distutils/tests/test_util.py +++ b/Lib/distutils/tests/test_util.py @@ -8,28 +8,23 @@ import unittest from copy import copy from distutils.errors import DistutilsPlatformError - -from distutils.util import get_platform -from distutils.util import convert_path -from distutils.util import change_root -from distutils.util import check_environ -from distutils.util import split_quoted -from distutils.util import strtobool -from distutils.util import rfc822_escape - +from distutils.util import (get_platform, convert_path, change_root, + check_environ, split_quoted, strtobool, + rfc822_escape) from distutils import util # used to patch _environ_checked from distutils.sysconfig import get_config_vars from distutils import sysconfig +from distutils.tests import support -class utilTestCase(unittest.TestCase): +class UtilTestCase(support.EnvironGuard, unittest.TestCase): def setUp(self): + super(UtilTestCase, self).setUp() # saving the environment self.name = os.name self.platform = sys.platform self.version = sys.version self.sep = os.sep - self.environ = dict(os.environ) self.join = os.path.join self.isabs = os.path.isabs self.splitdrive = os.path.splitdrive @@ -51,10 +46,6 @@ class utilTestCase(unittest.TestCase): sys.platform = self.platform sys.version = self.version os.sep = self.sep - for k, v in self.environ.items(): - os.environ[k] = v - for k in set(os.environ) - set(self.environ): - del os.environ[k] os.path.join = self.join os.path.isabs = self.isabs os.path.splitdrive = self.splitdrive @@ -63,6 +54,7 @@ class utilTestCase(unittest.TestCase): else: del os.uname sysconfig._config_vars = copy(self._config_vars) + super(UtilTestCase, self).tearDown() def _set_uname(self, uname): self._uname = uname @@ -102,7 +94,7 @@ class utilTestCase(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')) - os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3' + self.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3' get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' '-fwrapv -O3 -Wall -Wstrict-prototypes') @@ -110,7 +102,7 @@ class utilTestCase(unittest.TestCase): self.assertEquals(get_platform(), 'macosx-10.3-i386') # macbook with fat binaries (fat, universal or fat64) - os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4' + self.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 ' @@ -214,21 +206,14 @@ class utilTestCase(unittest.TestCase): # posix without HOME if os.name == 'posix': # this test won't run on windows - old_home = os.environ.get('HOME') - try: - check_environ() - import pwd - self.assertEquals(os.environ['HOME'], - pwd.getpwuid(os.getuid())[5]) - finally: - if old_home is not None: - os.environ['HOME'] = old_home - else: - del os.environ['HOME'] + check_environ() + import pwd + self.assertEquals(self.environ['HOME'], + pwd.getpwuid(os.getuid())[5]) else: check_environ() - self.assertEquals(os.environ['PLAT'], get_platform()) + self.assertEquals(self.environ['PLAT'], get_platform()) self.assertEquals(util._environ_checked, 1) def test_split_quoted(self): @@ -253,7 +238,7 @@ class utilTestCase(unittest.TestCase): self.assertEquals(res, wanted) def test_suite(): - return unittest.makeSuite(utilTestCase) + return unittest.makeSuite(UtilTestCase) if __name__ == "__main__": unittest.main(defaultTest="test_suite")