Commit a5b288e5 authored by Jim Fulton's avatar Jim Fulton

Improved the handling of temporary directories in tests.

Now the test namespace has a helper for creating temporary directories
that are automatically cleaned up.
parent f3f3bd91
...@@ -36,7 +36,6 @@ def setUp(test): ...@@ -36,7 +36,6 @@ def setUp(test):
def tearDown(test): def tearDown(test):
shutil.rmtree(test.globs['_sample_eggs_container'])
zc.buildout.testing.buildoutTearDown(test) zc.buildout.testing.buildoutTearDown(test)
zc.buildout.testing.stop_server(test.globs['link_server']) zc.buildout.testing.stop_server(test.globs['link_server'])
......
...@@ -500,8 +500,7 @@ customization. ...@@ -500,8 +500,7 @@ customization.
Here is a more elaborate example. Here is a more elaborate example.
>>> import tempfile >>> extensions = mkdtemp()
>>> extensions = tempfile.mkdtemp()
>>> write(sample_buildout, 'buildout.cfg', >>> write(sample_buildout, 'buildout.cfg',
... """ ... """
...@@ -626,7 +625,7 @@ reading the configuration file. ($HOME is the value of the HOME ...@@ -626,7 +625,7 @@ reading the configuration file. ($HOME is the value of the HOME
enviornment variable. The '/' is replaced by the operating system file enviornment variable. The '/' is replaced by the operating system file
delimiter.) delimiter.)
>>> home = tempfile.mkdtemp() >>> home = mkdtemp()
>>> mkdir(home, '.buildout') >>> mkdir(home, '.buildout')
>>> write(home, '.buildout', 'default.cfg', >>> write(home, '.buildout', 'default.cfg',
... """ ... """
...@@ -934,7 +933,7 @@ The buildout normally puts the bin, eggs, and parts directories in the ...@@ -934,7 +933,7 @@ The buildout normally puts the bin, eggs, and parts directories in the
directory in the directory containing the configuration file. You can directory in the directory containing the configuration file. You can
provide alternate locations, and even names for these directories. provide alternate locations, and even names for these directories.
>>> alt = tempfile.mkdtemp('sample-alt') >>> alt = mkdtemp('sample-alt')
>>> write(sample_buildout, 'buildout.cfg', >>> write(sample_buildout, 'buildout.cfg',
... """ ... """
...@@ -972,12 +971,9 @@ provide alternate locations, and even names for these directories. ...@@ -972,12 +971,9 @@ provide alternate locations, and even names for these directories.
>>> ls(alt, 'developbasket') >>> ls(alt, 'developbasket')
- recipes.egg-link - recipes.egg-link
>>> import shutil
>>> shutil.rmtree(alt)
You can also specify an alternate buildout directory: You can also specify an alternate buildout directory:
>>> alt = tempfile.mkdtemp('sample-alt') >>> alt = mkdtemp('sample-alt')
>>> write(sample_buildout, 'buildout.cfg', >>> write(sample_buildout, 'buildout.cfg',
... """ ... """
...@@ -1007,9 +1003,6 @@ You can also specify an alternate buildout directory: ...@@ -1007,9 +1003,6 @@ You can also specify an alternate buildout directory:
>>> ls(alt, 'develop-eggs') >>> ls(alt, 'develop-eggs')
- recipes.egg-link - recipes.egg-link
>>> import shutil
>>> shutil.rmtree(alt)
Logging control Logging control
--------------- ---------------
...@@ -1150,7 +1143,7 @@ If zc.buildout is installed, you can use it to create a new buildout ...@@ -1150,7 +1143,7 @@ If zc.buildout is installed, you can use it to create a new buildout
with it's own local copies of zc.buildout and setuptools and with with it's own local copies of zc.buildout and setuptools and with
local buildout scripts. There must be an existing setup.cfg: local buildout scripts. There must be an existing setup.cfg:
>>> sample_bootstrapped = tempfile.mkdtemp('sample-bootstrapped') >>> sample_bootstrapped = mkdtemp('sample-bootstrapped')
>>> write(sample_bootstrapped, 'setup.cfg', >>> write(sample_bootstrapped, 'setup.cfg',
... ''' ... '''
... [buildout] ... [buildout]
......
...@@ -80,8 +80,7 @@ We have a link server that has a number of eggs: ...@@ -80,8 +80,7 @@ We have a link server that has a number of eggs:
let's make directory and install the demo egg to it, using the demo: let's make directory and install the demo egg to it, using the demo:
>>> import tempfile >>> dest = mkdtemp('sample-install')
>>> dest = tempfile.mkdtemp('sample-install')
>>> import zc.buildout.easy_install >>> import zc.buildout.easy_install
>>> ws = zc.buildout.easy_install.install( >>> ws = zc.buildout.easy_install.install(
... ['demo==0.2'], dest, ... ['demo==0.2'], dest,
...@@ -139,9 +138,7 @@ dependencies. We might do this to specify a sprcific version. ...@@ -139,9 +138,7 @@ dependencies. We might do this to specify a sprcific version.
We can specify an alternate Python executable, and we can specify We can specify an alternate Python executable, and we can specify
that, when we retrieve (or create) an egg, it should be unzipped. that, when we retrieve (or create) an egg, it should be unzipped.
>>> import shutil >>> dest = mkdtemp('sample-install')
>>> shutil.rmtree(dest)
>>> dest = tempfile.mkdtemp('sample-install')
>>> ws = zc.buildout.easy_install.install( >>> ws = zc.buildout.easy_install.install(
... ['demo'], dest, links=[link_server], index=link_server+'index/', ... ['demo'], dest, links=[link_server], index=link_server+'index/',
... always_unzip=True, executable= python2_3_executable) ... always_unzip=True, executable= python2_3_executable)
...@@ -150,8 +147,7 @@ that, when we retrieve (or create) an egg, it should be unzipped. ...@@ -150,8 +147,7 @@ that, when we retrieve (or create) an egg, it should be unzipped.
d demo-0.3-py2.3.egg d demo-0.3-py2.3.egg
d demoneeded-1.1-py2.3.egg d demoneeded-1.1-py2.3.egg
>>> shutil.rmtree(dest) >>> dest = mkdtemp('sample-install')
>>> dest = tempfile.mkdtemp('sample-install')
>>> ws = zc.buildout.easy_install.install( >>> ws = zc.buildout.easy_install.install(
... ['demo'], dest, links=[link_server], index=link_server+'index/', ... ['demo'], dest, links=[link_server], index=link_server+'index/',
... always_unzip=True, executable=python2_4_executable) ... always_unzip=True, executable=python2_4_executable)
...@@ -179,7 +175,7 @@ The scripts method can be used to generate scripts. Let's create a ...@@ -179,7 +175,7 @@ The scripts method can be used to generate scripts. Let's create a
destination directory for it to place them in: destination directory for it to place them in:
>>> import tempfile >>> import tempfile
>>> bin = tempfile.mkdtemp() >>> bin = mkdtemp()
Now, we'll use the scripts method to generate scripts in this directory Now, we'll use the scripts method to generate scripts in this directory
from the demo egg: from the demo egg:
...@@ -261,9 +257,7 @@ An additional argumnet can be passed to define which scripts to install ...@@ -261,9 +257,7 @@ An additional argumnet can be passed to define which scripts to install
and to provie script names. The argument is a dictionary mapping and to provie script names. The argument is a dictionary mapping
original script names to new script names. original script names to new script names.
>>> import shutil >>> bin = mkdtemp()
>>> shutil.rmtree(bin)
>>> bin = tempfile.mkdtemp()
>>> scripts = zc.buildout.easy_install.scripts( >>> scripts = zc.buildout.easy_install.scripts(
... ['demo==0.1'], ws, python2_4_executable, bin, dict(demo='run')) ... ['demo==0.1'], ws, python2_4_executable, bin, dict(demo='run'))
>>> scripts == [os.path.join(bin, 'run')] >>> scripts == [os.path.join(bin, 'run')]
......
...@@ -63,7 +63,13 @@ def buildoutSetUp(test, clear_home=True): ...@@ -63,7 +63,13 @@ def buildoutSetUp(test, clear_home=True):
# to restore whatever it was after the test. # to restore whatever it was after the test.
test.globs['_oldhome'] = os.environ.pop('HOME', None) test.globs['_oldhome'] = os.environ.pop('HOME', None)
sample = tempfile.mkdtemp('sample-buildout') temporary_directories = []
def mkdtemp(*args):
d = tempfile.mkdtemp(*args)
temporary_directories.append(d)
return d
sample = mkdtemp('sample-buildout')
for name in ('bin', 'eggs', 'develop-eggs', 'parts'): for name in ('bin', 'eggs', 'develop-eggs', 'parts'):
os.mkdir(os.path.join(sample, name)) os.mkdir(os.path.join(sample, name))
...@@ -98,10 +104,13 @@ def buildoutSetUp(test, clear_home=True): ...@@ -98,10 +104,13 @@ def buildoutSetUp(test, clear_home=True):
system = system, system = system,
get = get, get = get,
__original_wd__ = os.getcwd(), __original_wd__ = os.getcwd(),
__temporary_directories__ = temporary_directories,
mkdtemp = mkdtemp,
)) ))
def buildoutTearDown(test): def buildoutTearDown(test):
shutil.rmtree(test.globs['sample_buildout']) for d in test.globs['__temporary_directories__']:
shutil.rmtree(d)
os.chdir(test.globs['__original_wd__']) os.chdir(test.globs['__original_wd__'])
if test.globs.get('_oldhome') is not None: if test.globs.get('_oldhome') is not None:
os.environ['HOME'] = test.globs['_oldhome'] os.environ['HOME'] = test.globs['_oldhome']
...@@ -131,11 +140,10 @@ def runsetup(d, executable): ...@@ -131,11 +140,10 @@ def runsetup(d, executable):
os.chdir(here) os.chdir(here)
def create_sample_eggs(test, executable=sys.executable): def create_sample_eggs(test, executable=sys.executable):
if '_sample_eggs_container' in test.globs: if 'sample_eggs' in test.globs:
sample = test.globs['_sample_eggs_container'] sample = os.path.dirname(test.globs['sample_eggs'])
else: else:
sample = tempfile.mkdtemp('sample-eggs') sample = test.globs['mkdtemp']('sample-eggs')
test.globs['_sample_eggs_container'] = sample
test.globs['sample_eggs'] = os.path.join(sample, 'dist') test.globs['sample_eggs'] = os.path.join(sample, 'dist')
write(sample, 'README.txt', '') write(sample, 'README.txt', '')
......
...@@ -72,16 +72,9 @@ def linkerSetUp(test): ...@@ -72,16 +72,9 @@ def linkerSetUp(test):
) )
def linkerTearDown(test): def linkerTearDown(test):
shutil.rmtree(test.globs['_sample_eggs_container'])
zc.buildout.testing.buildoutTearDown(test) zc.buildout.testing.buildoutTearDown(test)
zc.buildout.testing.stop_server(test.globs['link_server']) zc.buildout.testing.stop_server(test.globs['link_server'])
def buildoutTearDown(test):
shutil.rmtree(test.globs['extensions'])
shutil.rmtree(test.globs['home'])
zc.buildout.testing.buildoutTearDown(test)
class PythonNormalizing(renormalizing.RENormalizing): class PythonNormalizing(renormalizing.RENormalizing):
def _transform(self, want, got): def _transform(self, want, got):
...@@ -141,7 +134,7 @@ def test_suite(): ...@@ -141,7 +134,7 @@ def test_suite():
doctest.DocFileSuite( doctest.DocFileSuite(
'buildout.txt', 'buildout.txt',
setUp=zc.buildout.testing.buildoutSetUp, setUp=zc.buildout.testing.buildoutSetUp,
tearDown=buildoutTearDown, tearDown=zc.buildout.testing.buildoutTearDown,
checker=renormalizing.RENormalizing([ checker=renormalizing.RENormalizing([
(re.compile('__buildout_signature__ = recipes-\S+'), (re.compile('__buildout_signature__ = recipes-\S+'),
'__buildout_signature__ = recipes-SSSSSSSSSSS'), '__buildout_signature__ = recipes-SSSSSSSSSSS'),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment