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