From 47f597d39ae450cd3421f31568ed6f027bc374fe Mon Sep 17 00:00:00 2001 From: Stephen Bailey Date: Wed, 14 Aug 2024 13:27:43 -0700 Subject: [PATCH] update tests to use temp directories --- py/desispec/test/test_binscripts.py | 11 +++++++++++ py/desispec/test/test_extract.py | 8 ++++++++ py/desispec/test/test_fiberflat.py | 13 ++++++++++++- py/desispec/test/test_pixgroup.py | 4 ++-- py/desispec/test/test_qlextract.py | 8 ++++++++ py/desispec/test/test_scripts.py | 7 ++++++- py/desispec/test/test_spectra.py | 5 +++++ py/desispec/test/test_util.py | 11 ++++++++++- 8 files changed, 62 insertions(+), 5 deletions(-) diff --git a/py/desispec/test/test_binscripts.py b/py/desispec/test/test_binscripts.py index e8048ee8c..ff0be2e7c 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 tempfile import numpy as np @@ -26,6 +27,9 @@ class TestBinScripts(unittest.TestCase): @classmethod def setUpClass(cls): + cls.origdir = os.getcwd() + cls.testdir = tempfile.mkdtemp() + os.chdir(cls.testdir) cls.nspec = 6 cls.nwave = 2000 # Needed for QA cls.wave = 4000+np.arange(cls.nwave) @@ -86,6 +90,7 @@ 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]: @@ -96,6 +101,12 @@ def tearDownClass(cls): else: os.environ['PYTHONPATH'] = cls.origPath + #- back to where we started + os.chdir(cls.origdir) + + def setUp(self): + os.chdir(self.testdir) + def _write_frame(self, flavor='none', camera='b3', expid=1, night='20160607',gaia_only=False): """Write a fake frame""" flux = np.ones((self.nspec, self.nwave)) diff --git a/py/desispec/test/test_extract.py b/py/desispec/test/test_extract.py index caaaad600..1e3eee939 100644 --- a/py/desispec/test/test_extract.py +++ b/py/desispec/test/test_extract.py @@ -13,6 +13,7 @@ import unittest import uuid import os +import tempfile from glob import glob from importlib import resources @@ -27,6 +28,9 @@ class TestExtract(unittest.TestCase): @classmethod def setUpClass(cls): + cls.origdir = os.getcwd() + cls.testdir = tempfile.mkdtemp() + os.chdir(cls.testdir) cls.testhash = uuid.uuid4() cls.imgfile = 'test-img-{}.fits'.format(cls.testhash) cls.outfile = 'test-out-{}.fits'.format(cls.testhash) @@ -49,16 +53,20 @@ def setUpClass(cls): cls.img = img def setUp(self): + os.chdir(self.testdir) for filename in (self.outfile, self.outmodel): if os.path.exists(filename): os.remove(filename) @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) + os.chdir(cls.origdir) + @unittest.skipIf(nospecter, 'specter not installed; skipping extraction test') def test_extract(self): template = "desi_extract_spectra -i {} -p {} -w 7500,7600,0.75 -f {} -s 0 -n 5 --bundlesize 5 -o {} -m {}" diff --git a/py/desispec/test/test_fiberflat.py b/py/desispec/test/test_fiberflat.py index b63cb7c1d..5b2164e83 100644 --- a/py/desispec/test/test_fiberflat.py +++ b/py/desispec/test/test_fiberflat.py @@ -7,6 +7,7 @@ import unittest import copy import os +import tempfile from uuid import uuid1 import numpy as np @@ -44,15 +45,21 @@ def _get_data(): class TestFiberFlat(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.origdir = os.getcwd() + cls.testdir = tempfile.mkdtemp() + os.chdir(cls.testdir) def setUp(self): + os.chdir(self.testdir) id = uuid1() self.testfibermap = 'test_fibermap_{}.fits'.format(id) self.testframe = 'test_frame_{}.fits'.format(id) self.testflat = 'test_fiberflat_{}.fits'.format(id) - def tearDown(self): + os.chdir(self.testdir) if os.path.isfile(self.testframe): os.unlink(self.testframe) if os.path.isfile(self.testflat): @@ -60,6 +67,10 @@ def tearDown(self): if os.path.isfile(self.testfibermap): os.unlink(self.testfibermap) + @classmethod + def tearDownClass(cls): + os.chdir(cls.origdir) + def test_interface(self): """ diff --git a/py/desispec/test/test_pixgroup.py b/py/desispec/test/test_pixgroup.py index 3893accdd..273be77a0 100644 --- a/py/desispec/test/test_pixgroup.py +++ b/py/desispec/test/test_pixgroup.py @@ -92,8 +92,8 @@ def setUpClass(cls): cls.exptable.write(cls.expfile) # Setup a dummy SpectraLite for I/O tests - cls.fileio = 'test_spectralite.fits' - cls.fileiogz = 'test_spectralite.fits.gz' + cls.fileio = os.path.join(cls.testdir, 'test_spectralite.fits') + cls.fileiogz = os.path.join(cls.testdir, 'test_spectralite.fits.gz') cls.nwave = 100 cls.nspec = 5 diff --git a/py/desispec/test/test_qlextract.py b/py/desispec/test/test_qlextract.py index dfc67dcd9..bc0007d16 100644 --- a/py/desispec/test/test_qlextract.py +++ b/py/desispec/test/test_qlextract.py @@ -12,6 +12,7 @@ import unittest import uuid import os +import tempfile from glob import glob from importlib import resources @@ -26,6 +27,9 @@ class TestExtract(unittest.TestCase): @classmethod def setUpClass(cls): + cls.origdir = os.getcwd() + cls.testdir = tempfile.mkdtemp() + os.chdir(cls.testdir) cls.testhash = uuid.uuid4() cls.imgfile = 'test-img-{}.fits'.format(cls.testhash) cls.outfile = 'test-out-{}.fits'.format(cls.testhash) @@ -45,16 +49,20 @@ def setUpClass(cls): desispec.io.write_fibermap(cls.fibermapfile, fibermap) def setUp(self): + os.chdir(self.testdir) for filename in (self.outfile, self.outmodel): if os.path.exists(filename): os.remove(filename) @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) + os.chdir(cls.origdir) + def test_boxcar(self): from desispec.quicklook.qlboxcar import do_boxcar from desispec.io import read_xytraceset diff --git a/py/desispec/test/test_scripts.py b/py/desispec/test/test_scripts.py index 36a44a3b5..46cde5e13 100644 --- a/py/desispec/test/test_scripts.py +++ b/py/desispec/test/test_scripts.py @@ -7,6 +7,7 @@ import os import unittest +import tempfile from uuid import uuid4 from astropy.table import Table @@ -17,6 +18,9 @@ class TestScripts(unittest.TestCase): @classmethod def setUpClass(cls): + cls.origdir = os.getcwd() + cls.testdir = tempfile.mkdtemp() + os.chdir(cls.testdir) # from os import environ # for k in ('DESI_SPECTRO_REDUX', 'SPECPROD'): # if k in environ: @@ -26,9 +30,10 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): cls.environ_cache.clear() + os.chdir(cls.origdir) def setUp(self): - pass + os.chdir(self.testdir) def tearDown(self): pass diff --git a/py/desispec/test/test_spectra.py b/py/desispec/test/test_spectra.py index b8b771f3d..0328daab4 100644 --- a/py/desispec/test/test_spectra.py +++ b/py/desispec/test/test_spectra.py @@ -40,7 +40,9 @@ class TestSpectra(unittest.TestCase): @classmethod def setUpClass(cls): """Create specprod directory structure""" + cls.origDir = os.getcwd() cls.testDir = tempfile.mkdtemp() + os.chdir(cls.testDir) cls.origEnv = { "SPECPROD": None, "DESI_SPECTRO_REDUX": None, @@ -75,12 +77,15 @@ def tearDownClass(cls): if os.path.exists(cls.testDir): shutil.rmtree(cls.testDir) + os.chdir(cls.origDir) + def setUp(self): #- catch specific warnings so that we can find and fix # warnings.filterwarnings("error", ".*did not parse as fits unit.*") #- Test data and files to work with + os.chdir(self.testDir) self.fileio = "test_spectra.fits" self.fileappend = "test_spectra_append.fits" self.filebuild = "test_spectra_build.fits" diff --git a/py/desispec/test/test_util.py b/py/desispec/test/test_util.py index 32b43d4c7..71ecea9c9 100644 --- a/py/desispec/test/test_util.py +++ b/py/desispec/test/test_util.py @@ -7,6 +7,7 @@ import unittest from uuid import uuid4 import importlib +import tempfile import numpy as np from astropy.table import Table @@ -135,7 +136,7 @@ def test_parse_fibers(self): #- TODO: override log level to quiet down error messages that are supposed #- to be there from these tests class TestRunCmd(unittest.TestCase): - + def test_runcmd(self): """Test calling a script""" result, success = util.runcmd('echo hello > /dev/null') @@ -276,12 +277,17 @@ def test_newer_input(self): @classmethod def setUpClass(cls): + cls.origdir = os.getcwd() + cls.testdir = tempfile.mkdtemp() + os.chdir(cls.testdir) + cls.infile = 'test-'+uuid4().hex cls.outfile = 'test-'+uuid4().hex cls.testfile = 'test-'+uuid4().hex def setUp(self): # refresh timestamps so that outfile is older than infile + os.chdir(self.testdir) for filename in [self.infile, self.outfile]: with open(filename, 'w') as fx: fx.write('This file is leftover from a test; you can remove it\n') @@ -289,10 +295,13 @@ 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) + os.chdir(cls.origdir) + class TestUtil(unittest.TestCase): def test_utils_default_nproc(self):