Skip to content

Commit e700a99

Browse files
committed
TEST: Skip tests when test data is missing
1 parent 304cbc6 commit e700a99

File tree

13 files changed

+97
-21
lines changed

13 files changed

+97
-21
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225

226226
apidoc_module_dir = "../niworkflows"
227227
apidoc_output_dir = "api"
228-
apidoc_excluded_paths = ["conftest.py", "*/tests/*", "tests/*", "data/*"]
228+
apidoc_excluded_paths = ["conftest.py", "*/tests/*", "tests/*", "data/*", "testing.py"]
229229
apidoc_separate_modules = True
230230
apidoc_extra_args = ["--module-first", "-d 1", "-T"]
231231

niworkflows/conftest.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@
3232
import pkg_resources
3333

3434
from .utils.bids import collect_data
35-
36-
test_data_env = os.getenv(
37-
"TEST_DATA_HOME", str(Path.home() / ".cache" / "stanford-crn")
35+
from .testing import (
36+
test_data_env, test_output_dir, test_workdir, data_dir,
37+
data_env_canary, data_dir_canary
3838
)
39-
test_output_dir = os.getenv("TEST_OUTPUT_DIR")
40-
test_workdir = os.getenv("TEST_WORK_DIR")
41-
42-
data_dir = Path(test_data_env) / "BIDS-examples-1-enh-ds054"
4339

4440

4541
@pytest.fixture(autouse=True)
@@ -52,6 +48,7 @@ def add_np(doctest_namespace):
5248
doctest_namespace["pytest"] = pytest
5349
doctest_namespace["Path"] = Path
5450
doctest_namespace["datadir"] = data_dir
51+
doctest_namespace["data_dir_canary"] = data_dir_canary
5552
doctest_namespace["bids_collect_data"] = collect_data
5653
doctest_namespace["test_data"] = pkg_resources.resource_filename(
5754
"niworkflows", "tests/data"
@@ -82,6 +79,7 @@ def testdata_dir():
8279

8380
@pytest.fixture
8481
def ds000030_dir():
82+
data_env_canary()
8583
return Path(test_data_env) / "ds000030"
8684

8785

niworkflows/interfaces/bids.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class BIDSInfo(SimpleInterface):
105105
This interface uses only the basename, not the path, to determine the
106106
subject, session, task, run, acquisition or reconstruction.
107107
108+
.. testsetup::
109+
110+
>>> data_dir_canary()
111+
108112
>>> bids_info = BIDSInfo(bids_dir=str(datadir / 'ds054'), bids_validate=False)
109113
>>> bids_info.inputs.in_file = '''\
110114
sub-01/func/ses-retest/sub-01_ses-retest_task-covertverbgeneration_bold.nii.gz'''
@@ -224,6 +228,10 @@ class BIDSDataGrabber(SimpleInterface):
224228
"""
225229
Collect files from a BIDS directory structure.
226230
231+
.. testsetup::
232+
233+
>>> data_dir_canary()
234+
227235
>>> bids_src = BIDSDataGrabber(anat_only=False)
228236
>>> bids_src.inputs.subject_data = bids_collect_data(
229237
... str(datadir / 'ds114'), '01', bids_validate=False)[0]
@@ -320,6 +328,10 @@ class DerivativesDataSink(SimpleInterface):
320328
Saves the ``in_file`` into a BIDS-Derivatives folder provided
321329
by ``base_directory``, given the input reference ``source_file``.
322330
331+
.. testsetup::
332+
333+
>>> data_dir_canary()
334+
323335
>>> import tempfile
324336
>>> tmpdir = Path(tempfile.mkdtemp())
325337
>>> tmpfile = tmpdir / 'a_temp_file.nii.gz'
@@ -732,6 +744,10 @@ class ReadSidecarJSON(SimpleInterface):
732744
"""
733745
Read JSON sidecar files of a BIDS tree.
734746
747+
.. testsetup::
748+
749+
>>> data_dir_canary()
750+
735751
>>> fmap = str(datadir / 'ds054' / 'sub-100185' / 'fmap' /
736752
... 'sub-100185_phasediff.nii.gz')
737753

niworkflows/interfaces/freesurfer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class StructuralReference(fs.RobustTemplate):
4848
"""
4949
Shortcut RobustTemplate with a copy of the source if a single volume is provided.
5050
51+
.. testsetup::
52+
53+
>>> data_dir_canary()
54+
5155
Examples
5256
--------
5357
>>> t1w = bids_collect_data(

niworkflows/interfaces/tests/test_bids.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from nipype.interfaces.base import Undefined
3333

3434
from .. import bids as bintfs
35+
from niworkflows.testing import needs_data_dir
3536

3637

3738
XFORM_CODES = {
@@ -608,6 +609,7 @@ def test_DerivativesDataSink_values(tmp_path, dtype):
608609
assert sha1(out_file.read_bytes()).hexdigest() == checksum
609610

610611

612+
@needs_data_dir
611613
@pytest.mark.parametrize("field", ["RepetitionTime", "UndefinedField"])
612614
def test_ReadSidecarJSON_connection(testdata_dir, field):
613615
"""

niworkflows/interfaces/tests/test_images.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import pytest
3030

3131
from .. import images as im
32+
from niworkflows.testing import has_afni
3233

3334

3435
@pytest.mark.parametrize(
@@ -153,6 +154,7 @@ def test_conform_set_zooms(tmpdir):
153154
assert np.allclose(out_img.header.get_zooms(), conform.inputs.target_zooms)
154155

155156

157+
@pytest.mark.skipif(not has_afni, reason="Needs AFNI")
156158
@pytest.mark.parametrize("shape", [
157159
(10, 10, 10),
158160
(10, 10, 10, 1),

niworkflows/testing.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
from functools import wraps
3+
import os
4+
from pathlib import Path
5+
from nipype.interfaces import fsl, freesurfer as fs, afni
6+
7+
test_data_env = os.getenv(
8+
"TEST_DATA_HOME", str(Path.home() / ".cache" / "stanford-crn")
9+
)
10+
test_output_dir = os.getenv("TEST_OUTPUT_DIR")
11+
test_workdir = os.getenv("TEST_WORK_DIR")
12+
13+
data_dir = Path(test_data_env) / "BIDS-examples-1-enh-ds054"
14+
15+
16+
def create_canary(predicate, message):
17+
def canary():
18+
if predicate:
19+
pytest.skip(message)
20+
21+
def decorator(f):
22+
@wraps(f)
23+
def wrapper(*args, **kwargs):
24+
canary()
25+
return f(*args, **kwargs)
26+
return wrapper
27+
28+
return canary, decorator
29+
30+
31+
data_env_canary, needs_data_env = create_canary(
32+
not os.path.isdir(test_data_env),
33+
"Test data must be made available in ~/.cache/stanford-crn or in a "
34+
"directory referenced by the TEST_DATA_HOME environment variable.")
35+
36+
data_dir_canary, needs_data_dir = create_canary(
37+
not os.path.isdir(data_dir),
38+
"Test data must be made available in ~/.cache/stanford-crn or in a "
39+
"directory referenced by the TEST_DATA_HOME environment variable.")
40+
41+
has_fsl = fsl.Info.version() is not None
42+
has_freesurfer = fs.Info.version() is not None
43+
has_afni = afni.Info.version() is not None

niworkflows/tests/conftest.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,11 @@
2626
from datetime import datetime as dt
2727
import pytest
2828
from templateflow.api import get as get_template
29-
from nipype.interfaces.fsl import Info as FSLInfo
30-
from nipype.interfaces.freesurfer import Info as FreeSurferInfo
29+
from niworkflows.testing import test_data_env, data_env_canary
3130

3231
filepath = os.path.dirname(os.path.realpath(__file__))
3332
datadir = os.path.realpath(os.path.join(filepath, "data"))
3433

35-
test_data_env = os.getenv(
36-
"TEST_DATA_HOME", str(Path.home() / ".cache" / "nipreps-data")
37-
)
38-
data_dir = Path(test_data_env) / "ds000003"
39-
40-
has_fsl = FSLInfo.version() is not None
41-
has_freesurfer = FreeSurferInfo.version() is not None
42-
4334

4435
def _run_interface_mock(objekt, runtime):
4536
runtime.returncode = 0
@@ -63,7 +54,8 @@ def reference_mask():
6354

6455
@pytest.fixture
6556
def moving():
66-
return str(data_dir / "sub-01/anat/sub-01_T1w.nii.gz")
57+
data_env_canary()
58+
return str(Path(test_data_env) / "ds000003/sub-01/anat/sub-01_T1w.nii.gz")
6759

6860

6961
@pytest.fixture

niworkflows/tests/test_registration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
ApplyXFMRPT,
3737
SimpleBeforeAfterRPT,
3838
)
39-
from .conftest import _run_interface_mock, datadir, has_fsl, has_freesurfer
39+
from ..testing import has_fsl, has_freesurfer
40+
from .conftest import _run_interface_mock, datadir
4041

4142

4243
def _smoke_test_report(report_interface, artifact_name):

niworkflows/tests/test_segmentation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
SimpleShowMaskRPT,
3636
ROIsPlot,
3737
)
38-
from .conftest import _run_interface_mock, datadir, has_fsl, has_freesurfer
38+
from ..testing import has_fsl, has_freesurfer
39+
from .conftest import _run_interface_mock, datadir
3940

4041

4142
def _smoke_test_report(report_interface, artifact_name):

0 commit comments

Comments
 (0)