Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1375d5

Browse files
author
Tarek Ziadé
committed
Merged revisions 69609 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r69609 | tarek.ziade | 2009-02-14 15:10:23 +0100 (Sat, 14 Feb 2009) | 1 line Fix for #5257: refactored all tests in distutils, so they use a temporary directory. ........
1 parent 9e8dbbc commit c1375d5

8 files changed

Lines changed: 59 additions & 62 deletions

File tree

Lib/distutils/tests/support.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Support code for distutils test cases."""
2-
2+
import os
33
import shutil
44
import tempfile
55

@@ -31,7 +31,7 @@ def tearDown(self):
3131
super().tearDown()
3232
while self.tempdirs:
3333
d = self.tempdirs.pop()
34-
shutil.rmtree(d)
34+
shutil.rmtree(d, os.name in ('nt', 'cygwin'))
3535

3636
def mkdtemp(self):
3737
"""Create a temporary directory that will be cleaned up.

Lib/distutils/tests/test_build_ext.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from distutils.core import Extension, Distribution
88
from distutils.command.build_ext import build_ext
99
from distutils import sysconfig
10+
from distutils.tests.support import TempdirManager
1011

1112
import unittest
1213
from test import support
@@ -19,11 +20,12 @@ def _get_source_filename():
1920
srcdir = sysconfig.get_config_var('srcdir')
2021
return os.path.join(srcdir, 'Modules', 'xxmodule.c')
2122

22-
class BuildExtTestCase(unittest.TestCase):
23+
class BuildExtTestCase(TempdirManager, unittest.TestCase):
2324
def setUp(self):
2425
# Create a simple test environment
2526
# Note that we're making changes to sys.path
26-
self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
27+
TempdirManager.setUp(self)
28+
self.tmp_dir = self.mkdtemp()
2729
self.sys_path = sys.path[:]
2830
sys.path.append(self.tmp_dir)
2931
shutil.copy(_get_source_filename(), self.tmp_dir)
@@ -74,8 +76,7 @@ def tearDown(self):
7476
# Get everything back to normal
7577
support.unload('xx')
7678
sys.path = self.sys_path
77-
# XXX on Windows the test leaves a directory with xx module in TEMP
78-
shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')
79+
TempdirManager.tearDown(self)
7980

8081
def test_solaris_enable_shared(self):
8182
dist = Distribution({'name': 'xx'})

Lib/distutils/tests/test_config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import os
44
import unittest
5+
import tempfile
56

67
from distutils.core import PyPIRCCommand
78
from distutils.core import Distribution
@@ -49,13 +50,15 @@ class PyPIRCCommandTestCase(support.TempdirManager, unittest.TestCase):
4950

5051
def setUp(self):
5152
"""Patches the environment."""
53+
support.TempdirManager.setUp(self)
54+
5255
if 'HOME' in os.environ:
5356
self._old_home = os.environ['HOME']
5457
else:
5558
self._old_home = None
56-
curdir = os.path.dirname(__file__)
57-
os.environ['HOME'] = curdir
58-
self.rc = os.path.join(curdir, '.pypirc')
59+
self.tmp_dir = self.mkdtemp()
60+
os.environ['HOME'] = self.tmp_dir
61+
self.rc = os.path.join(self.tmp_dir, '.pypirc')
5962
self.dist = Distribution()
6063

6164
class command(PyPIRCCommand):
@@ -74,9 +77,8 @@ def tearDown(self):
7477
del os.environ['HOME']
7578
else:
7679
os.environ['HOME'] = self._old_home
77-
if os.path.exists(self.rc):
78-
os.remove(self.rc)
7980
set_threshold(self.old_threshold)
81+
support.TempdirManager.tearDown(self)
8082

8183
def test_server_registration(self):
8284
# This test makes sure PyPIRCCommand knows how to:

Lib/distutils/tests/test_dir_util.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from distutils.dir_util import copy_tree
1010

1111
from distutils import log
12+
from distutils.tests import support
1213

13-
class DirUtilTestCase(unittest.TestCase):
14+
class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
1415

1516
def _log(self, msg, *args):
1617
if len(args) > 0:
@@ -19,18 +20,18 @@ def _log(self, msg, *args):
1920
self._logs.append(msg)
2021

2122
def setUp(self):
23+
support.TempdirManager.setUp(self)
2224
self._logs = []
23-
self.root_target = os.path.join(os.path.dirname(__file__), 'deep')
25+
tmp_dir = self.mkdtemp()
26+
self.root_target = os.path.join(tmp_dir, 'deep')
2427
self.target = os.path.join(self.root_target, 'here')
25-
self.target2 = os.path.join(os.path.dirname(__file__), 'deep2')
28+
self.target2 = os.path.join(tmp_dir, 'deep2')
2629
self.old_log = log.info
2730
log.info = self._log
2831

2932
def tearDown(self):
30-
for target in (self.target, self.target2):
31-
if os.path.exists(target):
32-
shutil.rmtree(target)
3333
log.info = self.old_log
34+
support.TempdirManager.tearDown(self)
3435

3536
def test_mkpath_remove_tree_verbosity(self):
3637

Lib/distutils/tests/test_dist.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import warnings
1010

1111
from test.support import TESTFN
12+
from distutils.tests import support
1213

1314

1415
class test_dist(distutils.cmd.Command):
@@ -120,7 +121,7 @@ def _warn(msg):
120121

121122
self.assertEquals(len(warns), 0)
122123

123-
class MetadataTestCase(unittest.TestCase):
124+
class MetadataTestCase(support.TempdirManager, unittest.TestCase):
124125

125126
def test_simple_metadata(self):
126127
attrs = {"name": "package",
@@ -219,8 +220,8 @@ def test_custom_pydistutils(self):
219220
else:
220221
user_filename = "pydistutils.cfg"
221222

222-
curdir = os.path.dirname(__file__)
223-
user_filename = os.path.join(curdir, user_filename)
223+
temp_dir = self.mkdtemp()
224+
user_filename = os.path.join(temp_dir, user_filename)
224225
f = open(user_filename, 'w')
225226
f.write('.')
226227
f.close()
@@ -230,14 +231,14 @@ def test_custom_pydistutils(self):
230231

231232
# linux-style
232233
if sys.platform in ('linux', 'darwin'):
233-
os.environ['HOME'] = curdir
234+
os.environ['HOME'] = temp_dir
234235
files = dist.find_config_files()
235236
self.assert_(user_filename in files)
236237

237238
# win32-style
238239
if sys.platform == 'win32':
239240
# home drive should be found
240-
os.environ['HOME'] = curdir
241+
os.environ['HOME'] = temp_dir
241242
files = dist.find_config_files()
242243
self.assert_(user_filename in files,
243244
'%r not found in %r' % (user_filename, files))

Lib/distutils/tests/test_file_util.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
from distutils.file_util import move_file
77
from distutils import log
8+
from distutils.tests import support
89

9-
class FileUtilTestCase(unittest.TestCase):
10+
class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
1011

1112
def _log(self, msg, *args):
1213
if len(args) > 0:
@@ -15,24 +16,20 @@ def _log(self, msg, *args):
1516
self._logs.append(msg)
1617

1718
def setUp(self):
19+
support.TempdirManager.setUp(self)
1820
self._logs = []
1921
self.old_log = log.info
2022
log.info = self._log
21-
self.source = os.path.join(os.path.dirname(__file__), 'f1')
22-
self.target = os.path.join(os.path.dirname(__file__), 'f2')
23-
self.target_dir = os.path.join(os.path.dirname(__file__), 'd1')
23+
tmp_dir = self.mkdtemp()
24+
self.source = os.path.join(tmp_dir, 'f1')
25+
self.target = os.path.join(tmp_dir, 'f2')
26+
self.target_dir = os.path.join(tmp_dir, 'd1')
2427

2528
def tearDown(self):
2629
log.info = self.old_log
27-
for f in (self.source, self.target, self.target_dir):
28-
if os.path.exists(f):
29-
if os.path.isfile(f):
30-
os.remove(f)
31-
else:
32-
shutil.rmtree(f)
30+
support.TempdirManager.tearDown(self)
3331

3432
def test_move_file_verbosity(self):
35-
3633
f = open(self.source, 'w')
3734
f.write('some content')
3835
f.close()

Lib/distutils/tests/test_sdist.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
import zipfile
66
from os.path import join
77
import sys
8+
import tempfile
89

910
from distutils.command.sdist import sdist
1011
from distutils.core import Distribution
1112
from distutils.tests.test_config import PyPIRCCommandTestCase
1213
from distutils.errors import DistutilsExecError
1314
from distutils.spawn import find_executable
1415

15-
CURDIR = os.path.dirname(__file__)
16-
TEMP_PKG = join(CURDIR, 'temppkg')
17-
1816
SETUP_PY = """
1917
from distutils.core import setup
2018
import somecode
@@ -29,28 +27,25 @@
2927
class sdistTestCase(PyPIRCCommandTestCase):
3028

3129
def setUp(self):
30+
# PyPIRCCommandTestCase creates a temp dir already
31+
# and put it in self.tmp_dir
3232
PyPIRCCommandTestCase.setUp(self)
33+
# setting up an environment
3334
self.old_path = os.getcwd()
35+
os.mkdir(join(self.tmp_dir, 'somecode'))
36+
os.mkdir(join(self.tmp_dir, 'dist'))
37+
# creating a MANIFEST, a package, and a README
38+
self._write(join(self.tmp_dir, 'MANIFEST.in'), MANIFEST_IN)
39+
self._write(join(self.tmp_dir, 'README'), 'xxx')
40+
self._write(join(self.tmp_dir, 'somecode', '__init__.py'), '#')
41+
self._write(join(self.tmp_dir, 'setup.py'), SETUP_PY)
42+
os.chdir(self.tmp_dir)
3443

3544
def tearDown(self):
45+
# back to normal
3646
os.chdir(self.old_path)
37-
if os.path.exists(TEMP_PKG):
38-
shutil.rmtree(TEMP_PKG)
3947
PyPIRCCommandTestCase.tearDown(self)
4048

41-
def _init_tmp_pkg(self):
42-
if os.path.exists(TEMP_PKG):
43-
shutil.rmtree(TEMP_PKG)
44-
os.mkdir(TEMP_PKG)
45-
os.mkdir(join(TEMP_PKG, 'somecode'))
46-
os.mkdir(join(TEMP_PKG, 'dist'))
47-
# creating a MANIFEST, a package, and a README
48-
self._write(join(TEMP_PKG, 'MANIFEST.in'), MANIFEST_IN)
49-
self._write(join(TEMP_PKG, 'README'), 'xxx')
50-
self._write(join(TEMP_PKG, 'somecode', '__init__.py'), '#')
51-
self._write(join(TEMP_PKG, 'setup.py'), SETUP_PY)
52-
os.chdir(TEMP_PKG)
53-
5449
def _write(self, path, content):
5550
f = open(path, 'w')
5651
try:
@@ -62,18 +57,17 @@ def test_prune_file_list(self):
6257
# this test creates a package with some vcs dirs in it
6358
# and launch sdist to make sure they get pruned
6459
# on all systems
65-
self._init_tmp_pkg()
6660

6761
# creating VCS directories with some files in them
68-
os.mkdir(join(TEMP_PKG, 'somecode', '.svn'))
69-
self._write(join(TEMP_PKG, 'somecode', '.svn', 'ok.py'), 'xxx')
62+
os.mkdir(join(self.tmp_dir, 'somecode', '.svn'))
63+
self._write(join(self.tmp_dir, 'somecode', '.svn', 'ok.py'), 'xxx')
7064

71-
os.mkdir(join(TEMP_PKG, 'somecode', '.hg'))
72-
self._write(join(TEMP_PKG, 'somecode', '.hg',
65+
os.mkdir(join(self.tmp_dir, 'somecode', '.hg'))
66+
self._write(join(self.tmp_dir, 'somecode', '.hg',
7367
'ok'), 'xxx')
7468

75-
os.mkdir(join(TEMP_PKG, 'somecode', '.git'))
76-
self._write(join(TEMP_PKG, 'somecode', '.git',
69+
os.mkdir(join(self.tmp_dir, 'somecode', '.git'))
70+
self._write(join(self.tmp_dir, 'somecode', '.git',
7771
'ok'), 'xxx')
7872

7973
# now building a sdist
@@ -96,7 +90,7 @@ def test_prune_file_list(self):
9690
cmd.run()
9791

9892
# now let's check what we have
99-
dist_folder = join(TEMP_PKG, 'dist')
93+
dist_folder = join(self.tmp_dir, 'dist')
10094
files = os.listdir(dist_folder)
10195
self.assertEquals(files, ['fake-1.0.zip'])
10296

@@ -116,8 +110,6 @@ def test_make_distribution(self):
116110
find_executable('gzip') is None):
117111
return
118112

119-
self._init_tmp_pkg()
120-
121113
# now building a sdist
122114
dist = Distribution()
123115
dist.script_name = 'setup.py'
@@ -137,7 +129,7 @@ def test_make_distribution(self):
137129
cmd.run()
138130

139131
# making sure we have two files
140-
dist_folder = join(TEMP_PKG, 'dist')
132+
dist_folder = join(self.tmp_dir, 'dist')
141133
result = os.listdir(dist_folder)
142134
result.sort()
143135
self.assertEquals(result,

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ Core and Builtins
166166
Library
167167
-------
168168

169+
- Issue #5257: refactored all tests in distutils, so they use
170+
support.TempdirManager, to avoid writing in the tests directory.
171+
169172
- Issue #4524: distutils build_script command failed with --with-suffix=3.
170173
Initial patch by Amaury Forgeot d'Arc.
171174

0 commit comments

Comments
 (0)