Merged revisions 75485 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r75485 | tarek.ziade | 2009-10-18 11:28:26 +0200 (Sun, 18 Oct 2009) | 1 line

  Changed distutils tests to avoid environment alteration
........
This commit is contained in:
Tarek Ziadé 2009-10-18 11:34:51 +00:00
parent ccb3c0946c
commit 430fb63dd2
13 changed files with 66 additions and 34 deletions

View file

@ -2,11 +2,11 @@
import os import os
import shutil import shutil
import tempfile import tempfile
from copy import deepcopy
from distutils import log from distutils import log
from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
from distutils.core import Distribution from distutils.core import Distribution
from test.support import EnvironmentVarGuard
class LoggingSilencer(object): class LoggingSilencer(object):
@ -111,8 +111,15 @@ class EnvironGuard(object):
def setUp(self): def setUp(self):
super(EnvironGuard, self).setUp() super(EnvironGuard, self).setUp()
self.environ = EnvironmentVarGuard() self.old_environ = deepcopy(os.environ)
def tearDown(self): 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 tuple(os.environ.keys()):
if key not in self.old_environ:
del os.environ[key]
super(EnvironGuard, self).tearDown() super(EnvironGuard, self).tearDown()

View file

@ -26,16 +26,18 @@ setup(name='foo', version='0.1', py_modules=['foo'],
class BuildDumbTestCase(support.TempdirManager, class BuildDumbTestCase(support.TempdirManager,
support.LoggingSilencer, support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase): unittest.TestCase):
def setUp(self): def setUp(self):
super(BuildDumbTestCase, self).setUp() super(BuildDumbTestCase, self).setUp()
self.old_location = os.getcwd() self.old_location = os.getcwd()
self.old_sys_argv = sys.argv[:] self.old_sys_argv = sys.argv, sys.argv[:]
def tearDown(self): def tearDown(self):
os.chdir(self.old_location) 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() super(BuildDumbTestCase, self).tearDown()
@unittest.skipUnless(zlib, "requires zlib") @unittest.skipUnless(zlib, "requires zlib")

View file

@ -29,11 +29,12 @@ class BuildRpmTestCase(support.TempdirManager,
def setUp(self): def setUp(self):
super(BuildRpmTestCase, self).setUp() super(BuildRpmTestCase, self).setUp()
self.old_location = os.getcwd() self.old_location = os.getcwd()
self.old_sys_argv = sys.argv[:] self.old_sys_argv = sys.argv, sys.argv[:]
def tearDown(self): def tearDown(self):
os.chdir(self.old_location) 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() super(BuildRpmTestCase, self).tearDown()
def test_quiet(self): def test_quiet(self):

View file

@ -35,7 +35,7 @@ class BuildExtTestCase(TempdirManager,
# Note that we're making changes to sys.path # Note that we're making changes to sys.path
super(BuildExtTestCase, self).setUp() super(BuildExtTestCase, self).setUp()
self.tmp_dir = self.mkdtemp() self.tmp_dir = self.mkdtemp()
self.sys_path = sys.path[:] self.sys_path = sys.path, sys.path[:]
sys.path.append(self.tmp_dir) sys.path.append(self.tmp_dir)
shutil.copy(_get_source_filename(), self.tmp_dir) shutil.copy(_get_source_filename(), self.tmp_dir)
if sys.version > "2.6": if sys.version > "2.6":
@ -90,7 +90,8 @@ class BuildExtTestCase(TempdirManager,
def tearDown(self): def tearDown(self):
# Get everything back to normal # Get everything back to normal
support.unload('xx') 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": if sys.version > "2.6":
import site import site
site.USER_BASE = self.old_user_base site.USER_BASE = self.old_user_base

View file

@ -55,7 +55,7 @@ class PyPIRCCommandTestCase(support.TempdirManager,
"""Patches the environment.""" """Patches the environment."""
super(PyPIRCCommandTestCase, self).setUp() super(PyPIRCCommandTestCase, self).setUp()
self.tmp_dir = self.mkdtemp() 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.rc = os.path.join(self.tmp_dir, '.pypirc')
self.dist = Distribution() self.dist = Distribution()

View file

@ -8,7 +8,7 @@ import sys
import test.support import test.support
from test.support import captured_stdout from test.support import captured_stdout
import unittest import unittest
from distutils.tests import support
# setup script that uses __file__ # setup script that uses __file__
setup_using___file__ = """\ setup_using___file__ = """\
@ -29,17 +29,20 @@ setup()
""" """
class CoreTestCase(unittest.TestCase): class CoreTestCase(support.EnvironGuard, unittest.TestCase):
def setUp(self): def setUp(self):
super(CoreTestCase, self).setUp()
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
self.cleanup_testfn() self.cleanup_testfn()
self.old_argv = sys.argv[:] self.old_argv = sys.argv, sys.argv[:]
def tearDown(self): def tearDown(self):
sys.stdout = self.old_stdout sys.stdout = self.old_stdout
self.cleanup_testfn() 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): def cleanup_testfn(self):
path = test.support.TESTFN path = test.support.TESTFN

View file

@ -38,15 +38,17 @@ class TestDistribution(Distribution):
class DistributionTestCase(support.LoggingSilencer, class DistributionTestCase(support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase): unittest.TestCase):
def setUp(self): def setUp(self):
super(DistributionTestCase, self).setUp() super(DistributionTestCase, self).setUp()
self.argv = sys.argv[:] self.argv = sys.argv, sys.argv[:]
del sys.argv[1:] del sys.argv[1:]
def tearDown(self): def tearDown(self):
sys.argv[:] = self.argv sys.argv = self.argv[0]
sys.argv[:] = self.argv[1]
super(DistributionTestCase, self).tearDown() super(DistributionTestCase, self).tearDown()
def create_distribution(self, configfiles=()): def create_distribution(self, configfiles=()):
@ -181,6 +183,15 @@ class DistributionTestCase(support.LoggingSilencer,
class MetadataTestCase(support.TempdirManager, support.EnvironGuard, class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
unittest.TestCase): 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): def test_simple_metadata(self):
attrs = {"name": "package", attrs = {"name": "package",
"version": "1.0"} "version": "1.0"}
@ -279,14 +290,14 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
# linux-style # linux-style
if sys.platform in ('linux', 'darwin'): if sys.platform in ('linux', 'darwin'):
self.environ['HOME'] = temp_dir os.environ['HOME'] = temp_dir
files = dist.find_config_files() files = dist.find_config_files()
self.assertTrue(user_filename in files) self.assertTrue(user_filename in files)
# win32-style # win32-style
if sys.platform == 'win32': if sys.platform == 'win32':
# home drive should be found # home drive should be found
self.environ['HOME'] = temp_dir os.environ['HOME'] = temp_dir
files = dist.find_config_files() files = dist.find_config_files()
self.assertTrue(user_filename in files, self.assertTrue(user_filename in files,
'%r not found in %r' % (user_filename, files)) '%r not found in %r' % (user_filename, files))
@ -302,15 +313,11 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
def test_show_help(self): def test_show_help(self):
# smoke test, just makes sure some help is displayed # smoke test, just makes sure some help is displayed
dist = Distribution() dist = Distribution()
old_argv = sys.argv
sys.argv = [] sys.argv = []
try: dist.help = 1
dist.help = 1 dist.script_name = 'setup.py'
dist.script_name = 'setup.py' with captured_stdout() as s:
with captured_stdout() as s: dist.parse_command_line()
dist.parse_command_line()
finally:
sys.argv = old_argv
output = [line for line in s.getvalue().split('\n') output = [line for line in s.getvalue().split('\n')
if line.strip() != ''] if line.strip() != '']

View file

@ -17,6 +17,7 @@ from distutils.errors import DistutilsOptionError
from distutils.tests import support from distutils.tests import support
class InstallTestCase(support.TempdirManager, class InstallTestCase(support.TempdirManager,
support.EnvironGuard,
support.LoggingSilencer, support.LoggingSilencer,
unittest.TestCase): unittest.TestCase):

View file

@ -9,6 +9,7 @@ from distutils.tests import support
class InstallDataTestCase(support.TempdirManager, class InstallDataTestCase(support.TempdirManager,
support.LoggingSilencer, support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase): unittest.TestCase):
def test_simple_run(self): def test_simple_run(self):

View file

@ -9,6 +9,7 @@ from distutils.tests import support
class InstallHeadersTestCase(support.TempdirManager, class InstallHeadersTestCase(support.TempdirManager,
support.LoggingSilencer, support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase): unittest.TestCase):
def test_simple_run(self): def test_simple_run(self):

View file

@ -10,9 +10,9 @@ from distutils.errors import DistutilsOptionError
class InstallLibTestCase(support.TempdirManager, class InstallLibTestCase(support.TempdirManager,
support.LoggingSilencer, support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase): unittest.TestCase):
def test_finalize_options(self): def test_finalize_options(self):
pkg_dir, dist = self.create_dist() pkg_dir, dist = self.create_dist()
cmd = install_lib(dist) cmd = install_lib(dist)

View file

@ -17,8 +17,15 @@ class SysconfigTestCase(support.EnvironGuard,
def tearDown(self): def tearDown(self):
if self.makefile is not None: if self.makefile is not None:
os.unlink(self.makefile) os.unlink(self.makefile)
self.cleanup_testfn()
super(SysconfigTestCase, self).tearDown() super(SysconfigTestCase, self).tearDown()
def cleanup_testfn(self):
if os.path.isfile(TESTFN):
os.remove(TESTFN)
elif os.path.isdir(TESTFN):
shutil.rmtree(TESTFN)
def test_get_config_h_filename(self): def test_get_config_h_filename(self):
config_h = sysconfig.get_config_h_filename() config_h = sysconfig.get_config_h_filename()
self.assertTrue(os.path.isfile(config_h), config_h) self.assertTrue(os.path.isfile(config_h), config_h)
@ -51,8 +58,8 @@ class SysconfigTestCase(support.EnvironGuard,
if get_default_compiler() != 'unix': if get_default_compiler() != 'unix':
return return
self.environ['AR'] = 'my_ar' os.environ['AR'] = 'my_ar'
self.environ['ARFLAGS'] = '-arflags' os.environ['ARFLAGS'] = '-arflags'
# make sure AR gets caught # make sure AR gets caught
class compiler: class compiler:

View file

@ -121,7 +121,7 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
('Darwin Kernel Version 8.11.1: ' ('Darwin Kernel Version 8.11.1: '
'Wed Oct 10 18:23:28 PDT 2007; ' 'Wed Oct 10 18:23:28 PDT 2007; '
'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) '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 ' get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
'-fwrapv -O3 -Wall -Wstrict-prototypes') '-fwrapv -O3 -Wall -Wstrict-prototypes')
@ -129,7 +129,7 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
self.assertEquals(get_platform(), 'macosx-10.3-i386') self.assertEquals(get_platform(), 'macosx-10.3-i386')
# macbook with fat binaries (fat, universal or fat64) # 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 ' get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
'/Developer/SDKs/MacOSX10.4u.sdk ' '/Developer/SDKs/MacOSX10.4u.sdk '
'-fno-strict-aliasing -fno-common ' '-fno-strict-aliasing -fno-common '
@ -250,17 +250,18 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
def test_check_environ(self): def test_check_environ(self):
util._environ_checked = 0 util._environ_checked = 0
if 'HOME' in os.environ:
del os.environ['HOME']
# posix without HOME # posix without HOME
if os.name == 'posix': # this test won't run on windows if os.name == 'posix': # this test won't run on windows
check_environ() check_environ()
import pwd import pwd
self.assertEquals(self.environ['HOME'], self.assertEquals(os.environ['HOME'], pwd.getpwuid(os.getuid())[5])
pwd.getpwuid(os.getuid())[5])
else: else:
check_environ() check_environ()
self.assertEquals(self.environ['PLAT'], get_platform()) self.assertEquals(os.environ['PLAT'], get_platform())
self.assertEquals(util._environ_checked, 1) self.assertEquals(util._environ_checked, 1)
def test_split_quoted(self): def test_split_quoted(self):