Skip to content

Commit

Permalink
remove test mkdtemp dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed Aug 15, 2024
1 parent 2ee5c1a commit 2fada6a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 33 deletions.
12 changes: 6 additions & 6 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 shutil
import tempfile

import numpy as np
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 4 additions & 12 deletions py/desispec/test/test_bootcalib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
from uuid import uuid1
import tempfile
import shutil
import os
import numpy as np
import glob
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion py/desispec/test/test_calibfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions py/desispec/test/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import uuid
import os
import tempfile
import shutil
from glob import glob
from importlib import resources

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

Expand Down
5 changes: 5 additions & 0 deletions py/desispec/test/test_fiberflat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import copy
import os
import tempfile
import shutil
from uuid import uuid1

import numpy as np
Expand Down Expand Up @@ -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)


Expand Down
14 changes: 13 additions & 1 deletion py/desispec/test/test_pixgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions py/desispec/test/test_qlextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import uuid
import os
import tempfile
import shutil
from glob import glob
from importlib import resources

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

Expand Down
5 changes: 5 additions & 0 deletions py/desispec/test/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import unittest
import tempfile
import shutil
from uuid import uuid4
from astropy.table import Table

Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion py/desispec/test/test_spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions py/desispec/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from uuid import uuid4
import importlib
import tempfile
import shutil

import numpy as np
from astropy.table import Table
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 2fada6a

Please sign in to comment.