From 01881d4d017005f64993d61b463ffb7b9f98d52d Mon Sep 17 00:00:00 2001 From: James Davies Date: Wed, 17 Jan 2024 13:58:39 +0100 Subject: [PATCH] Fix importorskip in test_hooks; add tmp_cwd fixture --- tests/conftest.py | 15 +++++++++++++++ tests/test_hooks.py | 10 ++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..01a870ba --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,15 @@ +import os +from pathlib import Path + +import pytest + + +@pytest.fixture() +def tmp_cwd(tmp_path): + """Perform test in a pristine temporary working directory.""" + old_dir = Path.cwd() + os.chdir(tmp_path) + try: + yield tmp_path + finally: + os.chdir(old_dir) diff --git a/tests/test_hooks.py b/tests/test_hooks.py index c0f9efd5..3bcd81c9 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -1,7 +1,8 @@ import pytest -Step = pytest.importorskip("jwst.stpipe.Step") -Pipeline = pytest.importorskip("jwst.stpipe.Pipeline") +pytest.importorskip("jwst") + +from jwst.stpipe import Pipeline, Step # noqa: E402 class ShovelPixelsStep(Step): @@ -35,6 +36,7 @@ def process(self, input_data): return result + class HookStep(Step): class_alias = "myhook" @@ -131,12 +133,12 @@ def test_hook_as_string_of_importable_function(caplog): assert "Running hook_function on data array of size (10, 10)" in caplog.text -def test_hook_as_subproccess(caplog, tmp_path): +def test_hook_as_subproccess(caplog, tmp_cwd): """Test a string of a terminal command""" datamodels = pytest.importorskip("stdatamodels.jwst.datamodels") model = datamodels.ImageModel((10, 10)) filename = "test_hook_as_subprocess.fits" - path = tmp_path / filename + path = tmp_cwd / filename model.save(path) # Run post_hooks of "fitsinfo" and "fitsheader" CLI scripts from astropy