Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JP-3552: use tmp_path instead of tmpdir, enable no:legacypath as a tox factor #8327

Merged
merged 16 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ general

- Remove unused asdf-transform-schemas dependency [#8337]

- Replaced all instances of pytest ``tmpdir`` and ``tmpdir_factory``
fixtures with ``tmp_path`` and ``tmp_path_factory``. [#8327]

- Replaced the ``_jail`` fixture from ``ci_watson`` with custom
``tmp_cwd`` to enforce ``no:legacypath`` in the CI tests. [#8327]

- Renamed the ``jail`` fixture with ``tmp_cwd_module``. [#8327]

jump
----

Expand Down
10 changes: 7 additions & 3 deletions docs/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import pytest
import os


@pytest.fixture(autouse=True)
def _docdir(request):

# Trigger ONLY for doctestplus.
doctest_plugin = request.config.pluginmanager.getplugin("doctestplus")
if isinstance(request.node.parent, doctest_plugin._doctest_textfile_item_cls):
tmpdir = request.getfixturevalue('tmpdir')
with tmpdir.as_cwd():
yield
tmp_path = request.getfixturevalue('tmp_path')
old_cwd = os.getcwd()
os.chdir(tmp_path)
yield
os.chdir(old_cwd)
else:
yield
4 changes: 2 additions & 2 deletions jwst/assign_wcs/tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def distortion_model():
return dist


def test_distortion_schema(distortion_model, tmpdir):
def test_distortion_schema(distortion_model, tmp_path):
"""Make sure DistortionModel roundtrips"""
path = str(tmpdir.join("test_dist.asdf"))
path = tmp_path / "test_dist.asdf"
dist = distortion_model
dist.save(path)

Expand Down
12 changes: 6 additions & 6 deletions jwst/assign_wcs/tests/test_wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_v23_to_sky():
assert_allclose(radec, expected_ra_dec, atol=1e-10)


def test_frame_from_model_3d(tmpdir, create_model_3d):
def test_frame_from_model_3d(tmp_path, create_model_3d):
""" Tests creating a frame from a data model. """
# Test CompositeFrame initialization (celestial and spectral)
im = create_model_3d
Expand All @@ -163,7 +163,7 @@ def test_frame_from_model_3d(tmpdir, create_model_3d):
assert frame.frames[1].axes_names == ('ALPHA1A', 'BETA1A')


def test_frame_from_model_2d(tmpdir, create_model_2d):
def test_frame_from_model_2d(tmp_path, create_model_2d):
""" Tests creating a frame from a data model. """
# Test 2D spatial custom frame
im = create_model_2d
Expand All @@ -173,13 +173,13 @@ def test_frame_from_model_2d(tmpdir, create_model_2d):
assert frame.axes_names == ("RA", "DEC")


def test_create_fitswcs(tmpdir, create_model_3d):
def test_create_fitswcs(tmp_path, create_model_3d):
"""GWCS from create_fitswcs function and astropy.wcs give same result"""
im = create_model_3d
w3d = pointing.create_fitswcs(im)
gra, gdec, glam = w3d(1, 1, 1)

path = str(tmpdir.join("fitswcs.fits"))
path = tmp_path / "fitswcs.fits"
im.save(path)
with fits.open(path) as hdulist:
hdu = hdulist["SCI"]
Expand All @@ -191,7 +191,7 @@ def test_create_fitswcs(tmpdir, create_model_3d):
assert_allclose((ra, dec), (gra, gdec))


def test_sip_approx(tmpdir):
def test_sip_approx(tmp_path):
# some of the wcs info
true_wcs = {
'ctype1': 'RA---TAN-SIP',
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_sip_approx(tmpdir):
assert_allclose(fitswcs_res.dec.deg, gwcs_dec, atol=1.5e-6)

# now write the file out, read it back in, and check that the fit values are preserved
path = str(tmpdir.join("tmp_sip_wcs.fits"))
path = tmp_path / "tmp_sip_wcs.fits"
result.write(path)

with open(path) as result_read:
Expand Down
2 changes: 1 addition & 1 deletion jwst/associations/lib/tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_fails(mismatched, standard=standard_asn):
asn_diff.compare_asn_lists(left_asns, right_asns)


@pytest.mark.usefixtures('_jail')
@pytest.mark.usefixtures('tmp_cwd')
def test_fromfiles():
"""Test from files

Expand Down
26 changes: 14 additions & 12 deletions jwst/associations/tests/test_asn_from_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_level2():
assert name.startswith('jwnoprogram-o999_none')
assert isinstance(serialized, str)


def test_level2_tuple():
"""Test level 2 association when passing in a tuple"""
items = [('file_1.fits', 'science'), ('file_2.fits', 'background'),
Expand All @@ -48,6 +49,7 @@ def test_level2_tuple():
if not items[3][1]:
assert product['members'][0]['exptype'] == 'science'


def test_file_ext():
"""check that the filename extension is correctly appended"""
items = ['a', 'b', 'c']
Expand All @@ -63,18 +65,18 @@ def test_file_ext():
assert name.endswith('yaml')


def test_level2_from_cmdline(tmpdir):
def test_level2_from_cmdline(tmp_path):
"""Create a level2 association from the command line"""
rule = 'DMSLevel2bBase'
path = tmpdir.join('test_asn.json')
path = tmp_path / 'test_asn.json'
inlist = ['a', 'b', 'c']
args = [
'-o', path.strpath,
'-o', str(path),
'-r', rule,
]
args = args + inlist
Main.cli(args)
with open(path.strpath, 'r') as fp:
with open(path, 'r') as fp:
asn = load_asn(fp, registry=AssociationRegistry(include_bases=True))
assert asn['asn_rule'] == 'DMSLevel2bBase'
assert asn['asn_type'] == 'None'
Expand Down Expand Up @@ -195,19 +197,19 @@ def test_cmdline_fails():
"format",
['json', 'yaml']
)
def test_cmdline_success(format, tmpdir):
def test_cmdline_success(format, tmp_path):
"""Create Level3 associations in different formats"""
path = tmpdir.join('test_asn.json')
path = tmp_path / 'test_asn.json'
product_name = 'test_product'
inlist = ['a', 'b', 'c']
args = [
'-o', path.strpath,
'-o', str(path),
'--product-name', product_name,
'--format', format
]
args = args + inlist
Main.cli(args)
with open(path.strpath, 'r') as fp:
with open(path, 'r') as fp:
asn = load_asn(fp, format=format)
assert len(asn['products']) == 1
assert asn['products'][0]['name'] == product_name
Expand All @@ -219,18 +221,18 @@ def test_cmdline_success(format, tmpdir):
assert inlist == expnames


def test_cmdline_change_rules(tmpdir):
def test_cmdline_change_rules(tmp_path):
"""Command line change the rule"""
rule = 'Association'
path = tmpdir.join('test_asn.json')
path = tmp_path / 'test_asn.json'
inlist = ['a', 'b', 'c']
args = [
'-o', path.strpath,
'-o', str(path),
'-r', rule,
]
args = args + inlist
Main.cli(args)
with open(path.strpath, 'r') as fp:
with open(path, 'r') as fp:
asn = load_asn(fp, registry=AssociationRegistry(include_bases=True))
assert inlist == asn['members']

Expand Down
2 changes: 1 addition & 1 deletion jwst/associations/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_asn_candidates(pool, all_candidates, case):
(['nosuchpool.csv'], 1),
]
)
def test_cmdline_status(args, expected, _jail):
def test_cmdline_status(args, expected, tmp_cwd):
"""Ensure command line status are as expected."""
full_args = ['asn_generate'] + args
status = subprocess.run(full_args)
Expand Down
7 changes: 4 additions & 3 deletions jwst/associations/tests/test_pool.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import os
from jwst.associations.tests.helpers import t_path

from jwst.associations import AssociationPool

POOL_FILE = t_path('data/jw93060_20150312T160130_pool.csv')


def test_pool(tmpdir):
def test_pool(tmp_path_factory):
pool = AssociationPool.read(POOL_FILE)
assert len(pool) == 636

tmp_pool = str(tmpdir.mkdir(__name__).join('tmp_pool.csv'))
tmp_path = tmp_path_factory.mktemp(__name__)
tmp_pool = tmp_path / 'tmp_pool.csv'
pool.write(tmp_pool)

roundtrip = AssociationPool.read(tmp_pool)
Expand Down
30 changes: 13 additions & 17 deletions jwst/background/tests/test_background.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Unit tests for background subtraction
"""
import os
import pathlib

from astropy.stats import sigma_clipped_stats
import pytest
Expand All @@ -12,25 +12,21 @@

from jwst.assign_wcs import AssignWcsStep
from jwst.background import BackgroundStep
from jwst.background.tests import data as data_directory
from jwst.stpipe import Step
from jwst.background.background_sub import robust_mean, mask_from_source_cat, no_NaN


data_path = os.path.split(os.path.abspath(data_directory.__file__))[0]


def get_file_path(filename):
"""Construct an absolute path."""
return os.path.join(data_path, filename)
@pytest.fixture(scope="module")
def data_path():
return pathlib.Path(__file__).parent / "data"


@pytest.fixture(scope='module')
def background(tmpdir_factory):
def background(tmp_path_factory):
"""Generate a background image to feed to background step"""

filename = tmpdir_factory.mktemp('background_input')
filename = str(filename.join('background.fits'))
filename = tmp_path_factory.mktemp('background_input')
filename = filename / 'background.fits'
with datamodels.IFUImageModel((10, 10)) as image:
image.data[:, :] = 10
image.meta.instrument.name = 'NIRSPEC'
Expand Down Expand Up @@ -84,7 +80,7 @@ def science_image():
return image


def test_nirspec_gwa(_jail, background, science_image):
def test_nirspec_gwa(tmp_cwd, background, science_image):
"""Verify NIRSPEC GWA logic for in the science and background"""

# open the background to read in the GWA values
Expand All @@ -106,7 +102,7 @@ def test_nirspec_gwa(_jail, background, science_image):
back_image.close()


def test_nirspec_gwa_xtilt(_jail, background, science_image):
def test_nirspec_gwa_xtilt(tmp_cwd, background, science_image):
"""Verify NIRSPEC GWA Xtilt must be the same in the science and background image"""

# open the background to read in the GWA values
Expand All @@ -128,7 +124,7 @@ def test_nirspec_gwa_xtilt(_jail, background, science_image):
back_image.close()


def test_nirspec_gwa_ytilt(_jail, background, science_image):
def test_nirspec_gwa_ytilt(tmp_cwd, background, science_image):
"""Verify NIRSPEC GWA Ytilt must be the same in the science and background image"""

# open the background to read in the GWA values
Expand All @@ -152,7 +148,7 @@ def test_nirspec_gwa_ytilt(_jail, background, science_image):


@pytest.fixture(scope='module')
def make_wfss_datamodel():
def make_wfss_datamodel(data_path):
"""Generate WFSS Observation"""
wcsinfo = {
'dec_ref': -27.79156387419731,
Expand Down Expand Up @@ -201,7 +197,7 @@ def make_wfss_datamodel():
image.meta.subarray._instance.update(subarray)
image.meta.exposure._instance.update(exposure)
image.data = np.random.rand(2048, 2048)
image.meta.source_catalog = get_file_path('test_cat.ecsv')
image.meta.source_catalog = str(data_path / "test_cat.ecsv")

return image

Expand All @@ -213,7 +209,7 @@ def make_wfss_datamodel():
@pytest.mark.parametrize("pupils", ['GRISMC', 'GRISMR'])
@pytest.mark.parametrize("filters", filter_list)
@pytest.mark.parametrize("detectors", ['NRCALONG', 'NRCBLONG'])
def test_nrc_wfss_background(filters, pupils, detectors, make_wfss_datamodel):
def test_nrc_wfss_background(tmp_cwd, filters, pupils, detectors, make_wfss_datamodel):
"""Test background subtraction for NIRCAM WFSS modes."""
data = make_wfss_datamodel

Expand Down
23 changes: 17 additions & 6 deletions jwst/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import pytest
import inspect
from pathlib import Path

from jwst.associations import (AssociationRegistry, AssociationPool)
from jwst.associations.tests.helpers import t_path
Expand Down Expand Up @@ -52,23 +53,33 @@ def slow(request):


@pytest.fixture(scope="module")
def jail(request, tmpdir_factory):
"""Run test in a pristine temporary working directory, scoped to module.

This fixture is the same as _jail in ci_watson, but scoped to module
instead of function. This allows a fixture using it to produce files in a
def tmp_cwd_module(request, tmp_path_factory):
"""
Run test in a pristine temporary working directory, scoped to module.
This allows a fixture using it to produce files in a
temporary directory, and then have the tests access them.
"""
old_dir = os.getcwd()
path = request.module.__name__.split('.')[-1]
if request._parent_request.fixturename is not None:
path = path + "_" + request._parent_request.fixturename
newpath = tmpdir_factory.mktemp(path)
newpath = tmp_path_factory.mktemp(path)
os.chdir(str(newpath))
yield newpath
os.chdir(old_dir)


@pytest.fixture
def tmp_cwd(tmp_path):
"""Perform test in a pristine temporary working directory, scoped to function."""
old_dir = Path.cwd()
os.chdir(tmp_path)
try:
yield tmp_path
finally:
os.chdir(old_dir)


@pytest.hookimpl(trylast=True)
def pytest_configure(config):
terminal_reporter = config.pluginmanager.getplugin('terminalreporter')
Expand Down
Loading