Skip to content

Commit

Permalink
update tests to use temp directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed Aug 14, 2024
1 parent dc3f564 commit 47f597d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 5 deletions.
11 changes: 11 additions & 0 deletions py/desispec/test/test_binscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os, sys
import unittest
from uuid import uuid4
import tempfile

import numpy as np

Expand All @@ -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)
Expand Down Expand Up @@ -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]:
Expand All @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions py/desispec/test/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import unittest
import uuid
import os
import tempfile
from glob import glob
from importlib import resources

Expand All @@ -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)
Expand All @@ -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 {}"
Expand Down
13 changes: 12 additions & 1 deletion py/desispec/test/test_fiberflat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
import copy
import os
import tempfile
from uuid import uuid1

import numpy as np
Expand Down Expand Up @@ -44,22 +45,32 @@ 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):
os.unlink(self.testflat)
if os.path.isfile(self.testfibermap):
os.unlink(self.testfibermap)

@classmethod
def tearDownClass(cls):
os.chdir(cls.origdir)


def test_interface(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions py/desispec/test/test_pixgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions py/desispec/test/test_qlextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import unittest
import uuid
import os
import tempfile
from glob import glob
from importlib import resources

Expand All @@ -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)
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion py/desispec/test/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
import unittest
import tempfile
from uuid import uuid4
from astropy.table import Table

Expand All @@ -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:
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions py/desispec/test/test_spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion py/desispec/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
from uuid import uuid4
import importlib
import tempfile

import numpy as np
from astropy.table import Table
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -276,23 +277,31 @@ 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')
time.sleep(0.1)

@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):
Expand Down

0 comments on commit 47f597d

Please sign in to comment.