From c1dcb3f77d5b39d624afda1d67d4c3362f20379b Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Fri, 19 Apr 2013 16:30:04 +0300 Subject: [PATCH] tests: more isolation for test cases Run every test case in its own temp directory. Similarly, use test case specific cache directory that lies outside the temp directory. Also, create the test case specific orig repo by copying rather than cloning in order to get an identical copy with all refs. Change-Id: I42e1ae889118924b4d4482b3780efd13632e6567 Signed-off-by: Markus Lehtonen --- tests/test_obs_service_gbp.py | 47 +++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/tests/test_obs_service_gbp.py b/tests/test_obs_service_gbp.py index 9609cd0..1857420 100644 --- a/tests/test_obs_service_gbp.py +++ b/tests/test_obs_service_gbp.py @@ -53,12 +53,9 @@ class UnitTestsBase(object): os.environ['GIT_CEILING_DIRECTORIES'] = os.getcwd() # Create temporary workdir cls.workdir = os.path.abspath(tempfile.mkdtemp(prefix='%s_' % - __name__, dir='.')) + cls.__name__, dir='.')) cls.orig_dir = os.getcwd() os.chdir(cls.workdir) - # Use cache in our workdir - cls.cachedir = os.path.join(cls.workdir, 'cache') - os.environ['CACHEDIR'] = cls.cachedir # Create an orig repo for testing cls._template_repo = cls.create_orig_repo('orig') @@ -79,46 +76,38 @@ class UnitTestsBase(object): def __init__(self): self.orig_repo = None + self.tmpdir = None + self.cachedir = None def setup(self): """Test class setup""" + # Change to a temporary directory + self.tmpdir = os.path.abspath(tempfile.mkdtemp(prefix='test_', + dir=self.workdir)) + os.chdir(self.tmpdir) + # Use cache in our tmpdir + suffix = os.path.basename(self.tmpdir).replace('test', '') + self.cachedir = os.path.join(self.workdir, 'cache' + suffix) + os.environ['CACHEDIR'] = self.cachedir # Create temporary "orig" repository - repo_path = os.path.abspath(tempfile.mkdtemp(prefix='orig_', - dir=self.workdir)) - self.orig_repo = GitRepository.clone(repo_path, - self._template_repo.path, - auto_name=False) + repo_dir = os.path.join(self.workdir, 'orig' + suffix) + shutil.copytree(self._template_repo.path, repo_dir) + self.orig_repo = GitRepository(repo_dir) def teardown(self): """Test case teardown""" + # Restore original working dir os.chdir(self.workdir) if not 'DEBUG_NOSETESTS' in os.environ: shutil.rmtree(self.orig_repo.path) + if os.path.exists(self.cachedir): + shutil.rmtree(self.cachedir) + shutil.rmtree(self.tmpdir) class TestBasicFunctionality(UnitTestsBase): """Base class for unit tests""" - def __init__(self): - self.tmpdir = None - super(TestBasicFunctionality, self).__init__() - - def setup(self): - """Test case setup""" - super(TestBasicFunctionality, self).setup() - # Change to a temporary directory - self.tmpdir = self.orig_repo.path.replace('orig', 'test') - os.makedirs(self.tmpdir) - os.chdir(self.tmpdir) - - def teardown(self): - """Test case teardown""" - # Restore original working dir - os.chdir(self.workdir) - if not 'DEBUG_NOSETESTS' in os.environ: - shutil.rmtree(self.tmpdir) - super(TestBasicFunctionality, self).teardown() - def test_invalid_options(self): """Test invalid options""" # Non-existing option -- 2.7.4