]> granicus.if.org Git - python/commitdiff
Fix for #5257: refactored all tests in distutils, so they use a temporary directory.
authorTarek Ziadé <ziade.tarek@gmail.com>
Sat, 14 Feb 2009 14:10:23 +0000 (14:10 +0000)
committerTarek Ziadé <ziade.tarek@gmail.com>
Sat, 14 Feb 2009 14:10:23 +0000 (14:10 +0000)
Lib/distutils/tests/support.py
Lib/distutils/tests/test_build_ext.py
Lib/distutils/tests/test_config.py
Lib/distutils/tests/test_dir_util.py
Lib/distutils/tests/test_dist.py
Lib/distutils/tests/test_file_util.py
Lib/distutils/tests/test_sdist.py
Misc/NEWS

index 475ceee598bd76cdc6f13b5bd9257614f235c75d..6dfc82cd5e8387a925eeedecc15d9c05e0ba81e9 100644 (file)
@@ -1,5 +1,5 @@
 """Support code for distutils test cases."""
-
+import os
 import shutil
 import tempfile
 
@@ -31,7 +31,7 @@ class TempdirManager(object):
         super(TempdirManager, self).tearDown()
         while self.tempdirs:
             d = self.tempdirs.pop()
-            shutil.rmtree(d)
+            shutil.rmtree(d, os.name in ('nt', 'cygwin'))
 
     def mkdtemp(self):
         """Create a temporary directory that will be cleaned up.
index 3c7d228da70d3078268e54ab50482685551c5a3a..a38558b00773d5a741835178e172a9ad82ef5c55 100644 (file)
@@ -7,6 +7,7 @@ from StringIO import StringIO
 from distutils.core import Extension, Distribution
 from distutils.command.build_ext import build_ext
 from distutils import sysconfig
+from distutils.tests import support
 
 import unittest
 from test import test_support
@@ -19,11 +20,12 @@ def _get_source_filename():
     srcdir = sysconfig.get_config_var('srcdir')
     return os.path.join(srcdir, 'Modules', 'xxmodule.c')
 
-class BuildExtTestCase(unittest.TestCase):
+class BuildExtTestCase(support.TempdirManager, unittest.TestCase):
     def setUp(self):
         # Create a simple test environment
         # Note that we're making changes to sys.path
-        self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
+        support.TempdirManager.setUp(self)
+        self.tmp_dir = self.mkdtemp()
         self.sys_path = sys.path[:]
         sys.path.append(self.tmp_dir)
         shutil.copy(_get_source_filename(), self.tmp_dir)
@@ -74,8 +76,7 @@ class BuildExtTestCase(unittest.TestCase):
         # Get everything back to normal
         test_support.unload('xx')
         sys.path = self.sys_path
-        # XXX on Windows the test leaves a directory with xx module in TEMP
-        shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')
+        support.TempdirManager.tearDown(self)
 
     def test_solaris_enable_shared(self):
         dist = Distribution({'name': 'xx'})
index cae7689884f6f5346ea0b86abb114edd35d3ac60..d81276928ab6ceb4e630bb2788133a1cd9bd3947 100644 (file)
@@ -2,6 +2,8 @@
 import sys
 import os
 import unittest
+import tempfile
+import shutil
 
 from distutils.core import PyPIRCCommand
 from distutils.core import Distribution
@@ -49,13 +51,15 @@ class PyPIRCCommandTestCase(support.TempdirManager, unittest.TestCase):
 
     def setUp(self):
         """Patches the environment."""
+        support.TempdirManager.setUp(self)
+
         if os.environ.has_key('HOME'):
             self._old_home = os.environ['HOME']
         else:
             self._old_home = None
-        curdir = os.path.dirname(__file__)
-        os.environ['HOME'] = curdir
-        self.rc = os.path.join(curdir, '.pypirc')
+        self.tmp_dir = self.mkdtemp()
+        os.environ['HOME'] = self.tmp_dir
+        self.rc = os.path.join(self.tmp_dir, '.pypirc')
         self.dist = Distribution()
 
         class command(PyPIRCCommand):
@@ -74,9 +78,8 @@ class PyPIRCCommandTestCase(support.TempdirManager, unittest.TestCase):
             del os.environ['HOME']
         else:
             os.environ['HOME'] = self._old_home
-        if os.path.exists(self.rc):
-            os.remove(self.rc)
         set_threshold(self.old_threshold)
+        support.TempdirManager.tearDown(self)
 
     def test_server_registration(self):
         # This test makes sure PyPIRCCommand knows how to:
index edf93c40c5b2626b95133d4580145a21d6fc6e88..bf416b6d79d38639e02299718eaa6447826d729b 100644 (file)
@@ -9,8 +9,9 @@ from distutils.dir_util import create_tree
 from distutils.dir_util import copy_tree
 
 from distutils import log
+from distutils.tests import support
 
-class DirUtilTestCase(unittest.TestCase):
+class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
 
     def _log(self, msg, *args):
         if len(args) > 0:
@@ -19,18 +20,18 @@ class DirUtilTestCase(unittest.TestCase):
             self._logs.append(msg)
 
     def setUp(self):
+        support.TempdirManager.setUp(self)
         self._logs = []
-        self.root_target = os.path.join(os.path.dirname(__file__), 'deep')
+        tmp_dir = self.mkdtemp()
+        self.root_target = os.path.join(tmp_dir, 'deep')
         self.target = os.path.join(self.root_target, 'here')
-        self.target2 = os.path.join(os.path.dirname(__file__), 'deep2')
+        self.target2 = os.path.join(tmp_dir, 'deep2')
         self.old_log = log.info
         log.info = self._log
 
     def tearDown(self):
-        for target in (self.target, self.target2):
-            if os.path.exists(target):
-                shutil.rmtree(target)
         log.info = self.old_log
+        support.TempdirManager.tearDown(self)
 
     def test_mkpath_remove_tree_verbosity(self):
 
index bf59c418442c2193717971dbe1f9128e1fbb8e23..0e9868ae672ea4b9b9db641cb6ec4b31ae2773fc 100644 (file)
@@ -11,7 +11,7 @@ import unittest
 import warnings
 
 from test.test_support import TESTFN
-
+from distutils.tests import support
 
 class test_dist(distutils.cmd.Command):
     """Sample distutils extension command."""
@@ -36,14 +36,16 @@ class TestDistribution(distutils.dist.Distribution):
         return self._config_files
 
 
-class DistributionTestCase(unittest.TestCase):
+class DistributionTestCase(support.TempdirManager, unittest.TestCase):
 
     def setUp(self):
+        support.TempdirManager.setUp(self)
         self.argv = sys.argv[:]
         del sys.argv[1:]
 
     def tearDown(self):
         sys.argv[:] = self.argv
+        support.TempdirManager.tearDown(self)
 
     def create_distribution(self, configfiles=()):
         d = TestDistribution()
@@ -100,7 +102,8 @@ class DistributionTestCase(unittest.TestCase):
 
     def test_write_pkg_file(self):
         # Check DistributionMetadata handling of Unicode fields
-        my_file = os.path.join(os.path.dirname(__file__), 'f')
+        tmp_dir = self.mkdtemp()
+        my_file = os.path.join(tmp_dir, 'f')
         klass = distutils.dist.Distribution
 
         dist = klass(attrs={'author': u'Mister Café',
@@ -113,11 +116,7 @@ class DistributionTestCase(unittest.TestCase):
         # let's make sure the file can be written
         # with Unicode fields. they are encoded with
         # PKG_INFO_ENCODING
-        try:
-            dist.metadata.write_pkg_file(open(my_file, 'w'))
-        finally:
-            if os.path.exists(my_file):
-                os.remove(my_file)
+        dist.metadata.write_pkg_file(open(my_file, 'w'))
 
         # regular ascii is of course always usable
         dist = klass(attrs={'author': 'Mister Cafe',
@@ -126,11 +125,8 @@ class DistributionTestCase(unittest.TestCase):
                             'description': 'Cafe torrefie',
                             'long_description': 'Hehehe'})
 
-        try:
-            dist.metadata.write_pkg_file(open(my_file, 'w'))
-        finally:
-            if os.path.exists(my_file):
-                os.remove(my_file)
+        my_file2 = os.path.join(tmp_dir, 'f2')
+        dist.metadata.write_pkg_file(open(my_file, 'w'))
 
     def test_empty_options(self):
         # an empty options dictionary should not stay in the
@@ -155,7 +151,7 @@ class DistributionTestCase(unittest.TestCase):
 
         self.assertEquals(len(warns), 0)
 
-class MetadataTestCase(unittest.TestCase):
+class MetadataTestCase(support.TempdirManager, unittest.TestCase):
 
     def test_simple_metadata(self):
         attrs = {"name": "package",
@@ -254,8 +250,8 @@ class MetadataTestCase(unittest.TestCase):
         else:
             user_filename = "pydistutils.cfg"
 
-        curdir = os.path.dirname(__file__)
-        user_filename = os.path.join(curdir, user_filename)
+        temp_dir = self.mkdtemp()
+        user_filename = os.path.join(temp_dir, user_filename)
         f = open(user_filename, 'w')
         f.write('.')
         f.close()
@@ -265,7 +261,7 @@ class MetadataTestCase(unittest.TestCase):
 
             # linux-style
             if sys.platform in ('linux', 'darwin'):
-                os.environ['HOME'] = curdir
+                os.environ['HOME'] = temp_dir
                 files = dist.find_config_files()
                 self.assert_(user_filename in files)
 
index 523f1aef171bc560f3fbfbb8b97857915285cfec..9373af8503a96e934e96d3db642313cf68627157 100644 (file)
@@ -5,8 +5,9 @@ import shutil
 
 from distutils.file_util import move_file
 from distutils import log
+from distutils.tests import support
 
-class FileUtilTestCase(unittest.TestCase):
+class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
 
     def _log(self, msg, *args):
         if len(args) > 0:
@@ -15,24 +16,20 @@ class FileUtilTestCase(unittest.TestCase):
             self._logs.append(msg)
 
     def setUp(self):
+        support.TempdirManager.setUp(self)
         self._logs = []
         self.old_log = log.info
         log.info = self._log
-        self.source = os.path.join(os.path.dirname(__file__), 'f1')
-        self.target = os.path.join(os.path.dirname(__file__), 'f2')
-        self.target_dir = os.path.join(os.path.dirname(__file__), 'd1')
+        tmp_dir = self.mkdtemp()
+        self.source = os.path.join(tmp_dir, 'f1')
+        self.target = os.path.join(tmp_dir, 'f2')
+        self.target_dir = os.path.join(tmp_dir, 'd1')
 
     def tearDown(self):
         log.info = self.old_log
-        for f in (self.source, self.target, self.target_dir):
-            if os.path.exists(f):
-                if os.path.isfile(f):
-                    os.remove(f)
-                else:
-                    shutil.rmtree(f)
+        support.TempdirManager.tearDown(self)
 
     def test_move_file_verbosity(self):
-
         f = open(self.source, 'w')
         f.write('some content')
         f.close()
index 0d839b5c6e9aec68108ed62db90d9c1066a86269..b2e9800888c59f5defdf696ca044b0d50c9bff7b 100644 (file)
@@ -5,6 +5,7 @@ import shutil
 import zipfile
 from os.path import join
 import sys
+import tempfile
 
 from distutils.command.sdist import sdist
 from distutils.core import Distribution
@@ -12,9 +13,6 @@ from distutils.tests.test_config import PyPIRCCommandTestCase
 from distutils.errors import DistutilsExecError
 from distutils.spawn import find_executable
 
-CURDIR = os.path.dirname(__file__)
-TEMP_PKG = join(CURDIR, 'temppkg')
-
 SETUP_PY = """
 from distutils.core import setup
 import somecode
@@ -29,28 +27,25 @@ recursive-include somecode *
 class sdistTestCase(PyPIRCCommandTestCase):
 
     def setUp(self):
+        # PyPIRCCommandTestCase creates a temp dir already
+        # and put it in self.tmp_dir
         PyPIRCCommandTestCase.setUp(self)
+        # setting up an environment
         self.old_path = os.getcwd()
+        os.mkdir(join(self.tmp_dir, 'somecode'))
+        os.mkdir(join(self.tmp_dir, 'dist'))
+        # creating a MANIFEST, a package, and a README
+        self._write(join(self.tmp_dir, 'MANIFEST.in'), MANIFEST_IN)
+        self._write(join(self.tmp_dir, 'README'), 'xxx')
+        self._write(join(self.tmp_dir, 'somecode', '__init__.py'), '#')
+        self._write(join(self.tmp_dir, 'setup.py'), SETUP_PY)
+        os.chdir(self.tmp_dir)
 
     def tearDown(self):
+        # back to normal
         os.chdir(self.old_path)
-        if os.path.exists(TEMP_PKG):
-            shutil.rmtree(TEMP_PKG)
         PyPIRCCommandTestCase.tearDown(self)
 
-    def _init_tmp_pkg(self):
-        if os.path.exists(TEMP_PKG):
-            shutil.rmtree(TEMP_PKG)
-        os.mkdir(TEMP_PKG)
-        os.mkdir(join(TEMP_PKG, 'somecode'))
-        os.mkdir(join(TEMP_PKG, 'dist'))
-        # creating a MANIFEST, a package, and a README
-        self._write(join(TEMP_PKG, 'MANIFEST.in'), MANIFEST_IN)
-        self._write(join(TEMP_PKG, 'README'), 'xxx')
-        self._write(join(TEMP_PKG, 'somecode', '__init__.py'), '#')
-        self._write(join(TEMP_PKG, 'setup.py'), SETUP_PY)
-        os.chdir(TEMP_PKG)
-
     def _write(self, path, content):
         f = open(path, 'w')
         try:
@@ -62,18 +57,17 @@ class sdistTestCase(PyPIRCCommandTestCase):
         # this test creates a package with some vcs dirs in it
         # and launch sdist to make sure they get pruned
         # on all systems
-        self._init_tmp_pkg()
 
         # creating VCS directories with some files in them
-        os.mkdir(join(TEMP_PKG, 'somecode', '.svn'))
-        self._write(join(TEMP_PKG, 'somecode', '.svn', 'ok.py'), 'xxx')
+        os.mkdir(join(self.tmp_dir, 'somecode', '.svn'))
+        self._write(join(self.tmp_dir, 'somecode', '.svn', 'ok.py'), 'xxx')
 
-        os.mkdir(join(TEMP_PKG, 'somecode', '.hg'))
-        self._write(join(TEMP_PKG, 'somecode', '.hg',
+        os.mkdir(join(self.tmp_dir, 'somecode', '.hg'))
+        self._write(join(self.tmp_dir, 'somecode', '.hg',
                          'ok'), 'xxx')
 
-        os.mkdir(join(TEMP_PKG, 'somecode', '.git'))
-        self._write(join(TEMP_PKG, 'somecode', '.git',
+        os.mkdir(join(self.tmp_dir, 'somecode', '.git'))
+        self._write(join(self.tmp_dir, 'somecode', '.git',
                          'ok'), 'xxx')
 
         # now building a sdist
@@ -96,7 +90,7 @@ class sdistTestCase(PyPIRCCommandTestCase):
         cmd.run()
 
         # now let's check what we have
-        dist_folder = join(TEMP_PKG, 'dist')
+        dist_folder = join(self.tmp_dir, 'dist')
         files = os.listdir(dist_folder)
         self.assertEquals(files, ['fake-1.0.zip'])
 
@@ -116,8 +110,6 @@ class sdistTestCase(PyPIRCCommandTestCase):
             find_executable('gzip') is None):
             return
 
-        self._init_tmp_pkg()
-
         # now building a sdist
         dist = Distribution()
         dist.script_name = 'setup.py'
@@ -137,7 +129,7 @@ class sdistTestCase(PyPIRCCommandTestCase):
         cmd.run()
 
         # making sure we have two files
-        dist_folder = join(TEMP_PKG, 'dist')
+        dist_folder = join(self.tmp_dir, 'dist')
         result = os.listdir(dist_folder)
         result.sort()
         self.assertEquals(result,
index 3c35413d6167744ab019fbf68b485d19e190417f..e1778df2a6b04e1338c28aee9dd81806700caaf3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -155,6 +155,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5257: refactored all tests in distutils, so they use 
+  support.TempdirManager, to avoid writing in the tests directory.
+
 - Issue #4524: distutils build_script command failed with --with-suffix=3.
   Initial patch by Amaury Forgeot d'Arc.