diff --git a/py/desispec/test/test_binscripts.py b/py/desispec/test/test_binscripts.py index ff0be2e7c..21523358f 100644 --- a/py/desispec/test/test_binscripts.py +++ b/py/desispec/test/test_binscripts.py @@ -2,6 +2,7 @@ import os, sys import unittest from uuid import uuid4 +import shutil import tempfile import numpy as np @@ -90,12 +91,11 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): """Cleanup in case tests crashed and left files behind""" - os.chdir(cls.testdir) - for filename in [cls.framefile, cls.fiberflatfile, cls.fibermapfile, \ - cls.skyfile, cls.calibfile, cls.stdfile, cls.qa_calib_file, - cls.qa_data_file, cls.modelfile, cls.qafig]: - if os.path.exists(filename): - os.remove(filename) + + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) + if cls.origPath is None: del os.environ['PYTHONPATH'] else: diff --git a/py/desispec/test/test_bootcalib.py b/py/desispec/test/test_bootcalib.py index bbc54d94b..699727b7c 100644 --- a/py/desispec/test/test_bootcalib.py +++ b/py/desispec/test/test_bootcalib.py @@ -5,6 +5,7 @@ import unittest from uuid import uuid1 import tempfile +import shutil import os import numpy as np import glob @@ -51,18 +52,9 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): - """We deliberately don't clean up the testarc and testflat files, - since they are useful for offline testing. - """ - os.chdir(cls.testdir) - # if os.path.exists(cls.testarc): - # os.unlink(cls.testarc) - # if os.path.exists(cls.testflat): - # os.unlink(cls.testflat) - if os.path.exists(cls.testout): - os.unlink(cls.testout) - if os.path.isfile(cls.qafile): - os.unlink(cls.qafile) + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) os.chdir(cls.origdir) diff --git a/py/desispec/test/test_calibfinder.py b/py/desispec/test/test_calibfinder.py index 334df3710..dbd379fb7 100644 --- a/py/desispec/test/test_calibfinder.py +++ b/py/desispec/test/test_calibfinder.py @@ -35,7 +35,7 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): #- remove temporary calibration directory - if os.path.isdir(cls.calibdir) : + if cls.calibdir.startswith(tempfile.gettempdir()) and os.path.isdir(cls.calibdir) : shutil.rmtree(cls.calibdir) def tearDown(self): diff --git a/py/desispec/test/test_extract.py b/py/desispec/test/test_extract.py index 1e3eee939..a1baffb11 100644 --- a/py/desispec/test/test_extract.py +++ b/py/desispec/test/test_extract.py @@ -14,6 +14,7 @@ import uuid import os import tempfile +import shutil from glob import glob from importlib import resources @@ -60,10 +61,9 @@ def setUp(self): @classmethod def tearDownClass(cls): - os.chdir(cls.testdir) - for filename in glob('test-*{}*.fits'.format(cls.testhash)): - if os.path.exists(filename): - os.remove(filename) + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) os.chdir(cls.origdir) diff --git a/py/desispec/test/test_fiberflat.py b/py/desispec/test/test_fiberflat.py index 5b2164e83..d9e7faf94 100644 --- a/py/desispec/test/test_fiberflat.py +++ b/py/desispec/test/test_fiberflat.py @@ -8,6 +8,7 @@ import copy import os import tempfile +import shutil from uuid import uuid1 import numpy as np @@ -69,6 +70,10 @@ def tearDown(self): @classmethod def tearDownClass(cls): + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) + os.chdir(cls.origdir) diff --git a/py/desispec/test/test_pixgroup.py b/py/desispec/test/test_pixgroup.py index 273be77a0..0a6ff5a44 100644 --- a/py/desispec/test/test_pixgroup.py +++ b/py/desispec/test/test_pixgroup.py @@ -19,6 +19,10 @@ class TestPixGroup(unittest.TestCase): def setUpClass(cls): cls.testdir = tempfile.mkdtemp() cls.outdir = os.path.join(cls.testdir, 'output') + + cls.origenv = dict() + for key in ['DESI_SPECTRO_REDUX', 'SPECPROD']: + cls.origenv[key] = os.getenv(key) #- will be None if not set os.environ['DESI_SPECTRO_REDUX'] = cls.testdir os.environ['SPECPROD'] = 'grouptest' @@ -136,9 +140,17 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): - if os.path.exists(cls.testdir): + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): shutil.rmtree(cls.testdir) + #- restore environment + for key, value in cls.origenv.items(): + if value is not None: + os.environ[key] = value + elif key in os.environ: + del os.environ[key] + def setUp(self): os.environ['DESI_SPECTRO_REDUX'] = self.testdir os.environ['SPECPROD'] = 'grouptest' diff --git a/py/desispec/test/test_qlextract.py b/py/desispec/test/test_qlextract.py index bc0007d16..edfe77ff4 100644 --- a/py/desispec/test/test_qlextract.py +++ b/py/desispec/test/test_qlextract.py @@ -13,6 +13,7 @@ import uuid import os import tempfile +import shutil from glob import glob from importlib import resources @@ -56,10 +57,9 @@ def setUp(self): @classmethod def tearDownClass(cls): - os.chdir(cls.testdir) - for filename in glob('test-*{}*.fits'.format(cls.testhash)): - if os.path.exists(filename): - os.remove(filename) + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) os.chdir(cls.origdir) diff --git a/py/desispec/test/test_scripts.py b/py/desispec/test/test_scripts.py index 46cde5e13..a3f3b2359 100644 --- a/py/desispec/test/test_scripts.py +++ b/py/desispec/test/test_scripts.py @@ -8,6 +8,7 @@ import os import unittest import tempfile +import shutil from uuid import uuid4 from astropy.table import Table @@ -29,6 +30,10 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) + cls.environ_cache.clear() os.chdir(cls.origdir) diff --git a/py/desispec/test/test_spectra.py b/py/desispec/test/test_spectra.py index 0328daab4..e5f8ed0fb 100644 --- a/py/desispec/test/test_spectra.py +++ b/py/desispec/test/test_spectra.py @@ -74,7 +74,8 @@ def tearDownClass(cls): else: os.environ[e] = cls.origEnv[e] - if os.path.exists(cls.testDir): + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testDir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testDir): shutil.rmtree(cls.testDir) os.chdir(cls.origDir) diff --git a/py/desispec/test/test_util.py b/py/desispec/test/test_util.py index 71ecea9c9..fa1bcb1f5 100644 --- a/py/desispec/test/test_util.py +++ b/py/desispec/test/test_util.py @@ -8,6 +8,7 @@ from uuid import uuid4 import importlib import tempfile +import shutil import numpy as np from astropy.table import Table @@ -295,10 +296,9 @@ def setUp(self): @classmethod def tearDownClass(cls): - os.chdir(cls.testdir) - for filename in [cls.infile, cls.outfile, cls.testfile]: - if os.path.exists(filename): - os.remove(filename) + #- Remove testdir only if it was created by tempfile.mkdtemp + if cls.testdir.startswith(tempfile.gettempdir()) and os.path.exists(cls.testdir): + shutil.rmtree(cls.testdir) os.chdir(cls.origdir)