Commit 653766d2 authored by Kai Lautaportti's avatar Kai Lautaportti

Restore the original working directory after the recipe finishes.

This change makes sure that the working directory is restored to the location
it was before running the recipe, regardless whether the execution of the
recipe is successful or not.

Thanks to Jonathan Ballet for pointing out this problem.
parent 3f728307
......@@ -112,9 +112,11 @@ class Recipe(object):
log.info('Using local source directory: %s' % self.options['path'])
compile_dir = self.options['path']
current_dir = os.getcwd()
os.mkdir(self.options['location'])
os.chdir(compile_dir)
try:
try:
if not self.is_build_dir():
contents = os.listdir(compile_dir)
......@@ -153,6 +155,8 @@ class Recipe(object):
log.error('Compilation error. The package is left as is at %s where '
'you can inspect what went wrong' % os.getcwd())
raise
finally:
os.chdir(current_dir)
if self.options['url']:
if self.options.get('keep-compile-dir', '').lower() in ('true', 'yes', '1', 'on'):
......
......@@ -6,6 +6,7 @@ import re
import shutil
import tempfile
import unittest
import zc.buildout
import zc.buildout.testing
import zc.buildout.tests
......@@ -21,7 +22,7 @@ def setUp(test):
class NonInformativeTests(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
self.dir = os.path.realpath(tempfile.mkdtemp())
def tearDown(self):
shutil.rmtree(self.dir)
......@@ -35,9 +36,11 @@ class NonInformativeTests(unittest.TestCase):
def make_recipe(self, buildout, name, options):
from hexagonit.recipe.cmmi import Recipe
parts_directory_path = os.path.join(self.dir, 'test_parts')
os.mkdir(parts_directory_path)
bo = {
'buildout' : {
'parts-directory' : '',
'parts-directory' : parts_directory_path,
}
}
bo.update(buildout)
......@@ -61,6 +64,24 @@ class NonInformativeTests(unittest.TestCase):
self.failUnless(os.path.exists(makefile))
self.failUnless(recipe.is_build_dir())
def test_working_directory_restored_after_failure(self):
compile_directory = os.path.join(self.dir, 'compile_directory')
os.makedirs(compile_directory)
recipe = self.make_recipe({}, 'test', {'path' : compile_directory})
os.chdir(self.dir)
self.assertRaises(zc.buildout.UserError, recipe.install)
self.assertEquals(self.dir, os.getcwd())
def test_working_directory_restored_after_success(self):
compile_directory = os.path.join(self.dir, 'compile_directory')
os.makedirs(compile_directory)
self.write_file(os.path.join(compile_directory, 'configure'), 'Dummy configure')
recipe = self.make_recipe({}, 'test', {'path' : compile_directory})
os.chdir(self.dir)
self.assertEquals(self.dir, os.getcwd())
def test_suite():
suite = unittest.TestSuite((
......
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