Skip to content

Commit

Permalink
Replace tmpdir with tmp_path (#1095)
Browse files Browse the repository at this point in the history
* Replace tmpdir with tmp_path

* Fix path construction

* Removed unused tmp_path

Co-authored-by: P. L. Lim <[email protected]>

---------

Co-authored-by: P. L. Lim <[email protected]>
  • Loading branch information
rosteen and pllim authored Nov 14, 2023
1 parent 5098f23 commit 8707443
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
12 changes: 6 additions & 6 deletions specutils/io/default_loaders/tests/test_apogee.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@


@pytest.mark.remote_data
def test_apStar_loader(tmpdir):
def test_apStar_loader(tmp_path):
apstar_url = ("https://data.sdss.org/sas/dr16/apogee/spectro/redux/r12/"
"stars/apo25m/N7789/apStar-r12-2M00005414+5522241.fits")
with set_temp_cache(path=str(tmpdir)):
with set_temp_cache(path=str(tmp_path)):
filename = download_file(apstar_url, cache=True)
spectrum = apStar_loader(filename) # noqa


@pytest.mark.remote_data
def test_apVisit_loader(tmpdir):
def test_apVisit_loader(tmp_path):
apvisit_url = ("https://data.sdss.org/sas/dr16/apogee/spectro/redux/r12/"
"visit/apo25m/N7789/5094/55874/"
"apVisit-r12-5094-55874-123.fits")
with set_temp_cache(path=str(tmpdir)):
with set_temp_cache(path=str(tmp_path)):
filename = download_file(apvisit_url, cache=True)
spectrum = apVisit_loader(filename) # noqa


@pytest.mark.remote_data
def test_aspcapStar_loader(tmpdir):
def test_aspcapStar_loader(tmp_path):
aspcap_url = ("https://data.sdss.org/sas/dr16/apogee/spectro/aspcap/r12/"
"l33/apo25m/N7789/aspcapStar-r12-2M00005414+5522241.fits")
with set_temp_cache(path=str(tmpdir)):
with set_temp_cache(path=str(tmp_path)):
filename = download_file(aspcap_url, cache=True)
spectrum = aspcapStar_loader(filename) # noqa
50 changes: 25 additions & 25 deletions specutils/io/default_loaders/tests/test_jwst_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def spec_multi(request):
@pytest.mark.parametrize('spec_multi, format',
[('EXTRACT1D', 'JWST x1d multi'),
('COMBINE1D', 'JWST c1d multi')], indirect=['spec_multi'])
def test_jwst_1d_multi_reader(tmpdir, spec_multi, format):
def test_jwst_1d_multi_reader(tmp_path, spec_multi, format):
"""Test SpectrumList.read for JWST c1d/x1d multi data"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_multi.writeto(tmpfile)

data = SpectrumList.read(tmpfile, format=format)
Expand All @@ -114,9 +114,9 @@ def test_jwst_1d_multi_reader(tmpdir, spec_multi, format):
@pytest.mark.parametrize('spec_single, format',
[('EXTRACT1D', 'JWST x1d'),
('COMBINE1D', 'JWST c1d')], indirect=['spec_single'])
def test_jwst_1d_single_reader(tmpdir, spec_single, format):
def test_jwst_1d_single_reader(tmp_path, spec_single, format):
"""Test Spectrum1D.read for JWST x1d data"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_single.writeto(tmpfile)

data = Spectrum1D.read(tmpfile, format=format)
Expand All @@ -125,9 +125,9 @@ def test_jwst_1d_single_reader(tmpdir, spec_single, format):


@pytest.mark.parametrize("srctype", [None, "UNKNOWN"])
def test_jwst_srctpye_defaults(tmpdir, x1d_single, srctype):
def test_jwst_srctpye_defaults(tmp_path, x1d_single, srctype):
""" Test """
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')

# Add a spectrum with missing or UNKNOWN SRCTYPE (mutate the fixture)
x1d_single['EXTRACT1D'].header['SRCTYPE'] == srctype
Expand All @@ -140,9 +140,9 @@ def test_jwst_srctpye_defaults(tmpdir, x1d_single, srctype):


@pytest.mark.parametrize('spec_single', ['EXTRACT1D', 'COMBINE1D'], indirect=['spec_single'])
def test_jwst_1d_single_reader_no_format(tmpdir, spec_single):
def test_jwst_1d_single_reader_no_format(tmp_path, spec_single):
"""Test Spectrum1D.read for JWST c1d/x1d data without format arg"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_single.writeto(tmpfile)

data = Spectrum1D.read(tmpfile)
Expand All @@ -153,9 +153,9 @@ def test_jwst_1d_single_reader_no_format(tmpdir, spec_single):


@pytest.mark.parametrize('spec_multi', ['EXTRACT1D', 'COMBINE1D'], indirect=['spec_multi'])
def test_jwst_1d_multi_reader_no_format(tmpdir, spec_multi):
def test_jwst_1d_multi_reader_no_format(tmp_path, spec_multi):
"""Test Spectrum1D.read for JWST c1d/x1d data without format arg"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_multi.writeto(tmpfile)

data = SpectrumList.read(tmpfile)
Expand All @@ -167,9 +167,9 @@ def test_jwst_1d_multi_reader_no_format(tmpdir, spec_multi):


@pytest.mark.parametrize('spec_multi', ['EXTRACT1D', 'COMBINE1D'], indirect=['spec_multi'])
def test_jwst_1d_multi_reader_check_units(tmpdir, spec_multi):
def test_jwst_1d_multi_reader_check_units(tmp_path, spec_multi):
"""Test units for Spectrum1D.read for JWST c1d/x1d data"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_multi.writeto(tmpfile)

data = SpectrumList.read(tmpfile)
Expand All @@ -179,9 +179,9 @@ def test_jwst_1d_multi_reader_check_units(tmpdir, spec_multi):


@pytest.mark.parametrize('spec_single', ['EXTRACT1D', 'COMBINE1D'], indirect=['spec_single'])
def test_jwst_1d_reader_meta(tmpdir, spec_single):
def test_jwst_1d_reader_meta(tmp_path, spec_single):
"""Test that the Primary and COMBINE1D/EXTRACT1D extension headers are merged in meta"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_single.writeto(tmpfile)

data = Spectrum1D.read(tmpfile)
Expand All @@ -190,19 +190,19 @@ def test_jwst_1d_reader_meta(tmpdir, spec_single):


@pytest.mark.parametrize('spec_multi', ['EXTRACT1D', 'COMBINE1D'], indirect=['spec_multi'])
def test_jwst_1d_single_reader_fail_on_multi(tmpdir, spec_multi):
def test_jwst_1d_single_reader_fail_on_multi(tmp_path, spec_multi):
"""Make sure Spectrum1D.read on JWST c1d/x1d with many spectra errors out"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
spec_multi.writeto(tmpfile)

with pytest.raises(IORegistryError):
Spectrum1D.read(tmpfile)


@pytest.mark.parametrize("srctype", ["BADVAL"])
def test_jwst_reader_fail(tmpdir, x1d_single, srctype):
def test_jwst_reader_fail(tmp_path, x1d_single, srctype):
"""Check that the reader fails when SRCTYPE is a BADVAL"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
hdulist = x1d_single
# Add a spectrum with bad SRCTYPE (mutate the fixture)
hdulist.append(create_spectrum_hdu(100, srctype, ver=2))
Expand All @@ -213,9 +213,9 @@ def test_jwst_reader_fail(tmpdir, x1d_single, srctype):


@pytest.mark.xfail(reason="JWST loader no longer attempts to auto-find flux column.")
def test_jwst_reader_warning_stddev(tmpdir, x1d_single):
def test_jwst_reader_warning_stddev(tmp_path, x1d_single):
"""Check that the reader raises warning when stddev is zeros"""
tmpfile = str(tmpdir.join('jwst.fits'))
tmpfile = str(tmp_path / 'jwst.fits')
hdulist = x1d_single
# Put zeros in ERROR column
hdulist["EXTRACT1D"].data["ERROR"] = 0
Expand Down Expand Up @@ -308,8 +308,8 @@ def s2d_multi(generate_wcs_transform, request):


@pytest.mark.xfail(reason="Needs investigation! See #717")
def test_jwst_s2d_reader(tmpdir, s2d_single):
path = str(tmpdir.join("test.fits"))
def test_jwst_s2d_reader(tmp_path, s2d_single):
path = str(tmp_path / "test.fits")
model = s2d_single
model.save(path)

Expand All @@ -318,8 +318,8 @@ def test_jwst_s2d_reader(tmpdir, s2d_single):
assert spec.unit == u.dimensionless_unscaled


def test_jwst_s2d_multi_reader(tmpdir, s2d_multi):
path = str(tmpdir.join("test.fits"))
def test_jwst_s2d_multi_reader(tmp_path, s2d_multi):
path = str(tmp_path / "test.fits")
model = s2d_multi
model.save(path)

Expand Down Expand Up @@ -366,7 +366,7 @@ def generate_s3d_wcs():


@pytest.fixture()
def tmp_asdf(tmpdir):
def tmp_asdf():
# Create some data
sequence = np.arange(100)
squares = sequence**2
Expand Down
20 changes: 10 additions & 10 deletions specutils/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def test_speclist_autoidentify():


@pytest.mark.filterwarnings(r'ignore:.*data loader provided for Spectrum1D without explicit identifier')
def test_default_identifier(tmpdir):
def test_default_identifier(tmp_path):

fname = str(tmpdir.join('empty.txt'))
fname = str(tmp_path / 'empty.txt')
with open(fname, 'w') as ff:
ff.write('\n')

Expand All @@ -97,10 +97,10 @@ def reader(*args, **kwargs):
registry.unregister_identifier(format_name, datatype)


def test_default_identifier_extension(tmpdir):
def test_default_identifier_extension(tmp_path):

good_fname = str(tmpdir.join('empty.fits'))
bad_fname = str(tmpdir.join('empty.txt'))
good_fname = str(tmp_path / 'empty.fits')
bad_fname = str(tmp_path / 'empty.txt')

# Create test data files.
for name in [good_fname, bad_fname]:
Expand All @@ -126,10 +126,10 @@ def reader(*args, **kwargs):
registry.unregister_identifier(format_name, datatype)


def test_custom_identifier(tmpdir):
def test_custom_identifier(tmp_path):

good_fname = str(tmpdir.join('good.txt'))
bad_fname = str(tmpdir.join('bad.txt'))
good_fname = str(tmp_path / 'good.txt')
bad_fname = str(tmp_path / 'bad.txt')

# Create test data files.
for name in [good_fname, bad_fname]:
Expand Down Expand Up @@ -164,9 +164,9 @@ def reader(*args, **kwargs):
reason="Test requires priorities to be implemented in astropy",
raises=registry.IORegistryError,
)
def test_loader_uses_priority(tmpdir):
def test_loader_uses_priority(tmp_path):
counter = Counter()
fname = str(tmpdir.join('good.txt'))
fname = str(tmp_path / 'good.txt')

with open(fname, 'w') as ff:
ff.write('\n')
Expand Down

0 comments on commit 8707443

Please sign in to comment.