generated from ArcanaFramework/frametree-extension-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
265 lines (215 loc) · 6.75 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import logging
from warnings import warn
import pytest
import tempfile
import requests.exceptions
from pathlib import Path
from tempfile import mkdtemp
from click.testing import CliRunner
import docker
from fileformats.medimage import NiftiGzX
from pipeline2app.core.image import Pydra2AppImage
log_level = logging.WARNING
logger = logging.getLogger("arcana")
logger.setLevel(log_level)
sch = logging.StreamHandler()
sch.setLevel(log_level)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
sch.setFormatter(formatter)
logger.addHandler(sch)
PKG_DIR = Path(__file__).parent
@pytest.fixture
def cli_runner(catch_cli_exceptions):
def invoke(*args, catch_exceptions=catch_cli_exceptions, **kwargs):
runner = CliRunner()
result = runner.invoke(*args, catch_exceptions=catch_exceptions, **kwargs)
return result
return invoke
@pytest.fixture
def work_dir():
work_dir = tempfile.mkdtemp()
return Path(work_dir)
@pytest.fixture(scope="session")
def pkg_dir():
return PKG_DIR
@pytest.fixture(scope="session")
def build_cache_dir():
return Path(mkdtemp())
@pytest.fixture(scope="session")
def nifti_sample_dir(pkg_dir):
return pkg_dir / "test-data" / "nifti"
BIDS_VALIDATOR_DOCKER = "bids/validator:latest"
SUCCESS_STR = "This dataset appears to be BIDS compatible"
MOCK_BIDS_APP_IMAGE = "arcana-mock-bids-app"
BIDS_VALIDATOR_APP_IMAGE = "arcana-bids-validator-app"
@pytest.fixture(scope="session")
def bids_validator_app_script():
return f"""#!/bin/sh
# Echo inputs to get rid of any quotes
BIDS_DATASET=$(echo $1)
OUTPUTS_DIR=$(echo $2)
SUBJ_ID=$5
# Run BIDS validator to check whether BIDS dataset is created properly
output=$(/usr/local/bin/bids-validator "$BIDS_DATASET")
if [[ "$output" != *"{SUCCESS_STR}"* ]]; then
echo "BIDS validation was not successful, exiting:\n "
echo $output
exit 1;
fi
# Write mock output files to 'derivatives' Directory
mkdir -p $OUTPUTS_DIR
echo 'file1' > $OUTPUTS_DIR/sub-${{SUBJ_ID}}_file1.txt
echo 'file2' > $OUTPUTS_DIR/sub-${{SUBJ_ID}}_file2.txt
"""
# FIXME: should be converted to python script to be Windows compatible
@pytest.fixture(scope="session")
def mock_bids_app_script():
file_tests = ""
for inpt_path, datatype in [
("anat/T1w", NiftiGzX),
("anat/T2w", NiftiGzX),
("dwi/dwi", NiftiGzX),
]:
subdir, suffix = inpt_path.split("/")
fpath = f"$BIDS_DATASET/sub-${{SUBJ_ID}}/{subdir}/sub-${{SUBJ_ID}}_{suffix}{datatype.ext}"
file_tests += f"""
if [ ! -f {fpath} ]; then
echo "Did not find {suffix} file at {fpath}"
exit 1;
fi
"""
return f"""#!/bin/sh
BIDS_DATASET=$1
OUTPUTS_DIR=$2
SUBJ_ID=$5
{file_tests}
# Write mock output files to 'derivatives' Directory
mkdir -p $OUTPUTS_DIR
echo 'file1' > $OUTPUTS_DIR/sub-${{SUBJ_ID}}_file1.txt
echo 'file2' > $OUTPUTS_DIR/sub-${{SUBJ_ID}}_file2.txt
"""
@pytest.fixture(scope="session")
def mock_bids_app_executable(build_cache_dir, mock_bids_app_script):
# Create executable that runs validator then produces some mock output
# files
script_path = build_cache_dir / "mock-bids-app-executable.sh"
with open(script_path, "w") as f:
f.write(mock_bids_app_script)
os.chmod(script_path, 0o777)
return script_path
@pytest.fixture(scope="session")
def bids_success_str():
return SUCCESS_STR
@pytest.fixture(scope="session")
def bids_validator_docker():
dc = docker.from_env()
try:
dc.images.pull(BIDS_VALIDATOR_DOCKER)
except requests.exceptions.HTTPError:
warn("No internet connection, so couldn't download latest BIDS validator")
return BIDS_VALIDATOR_DOCKER
@pytest.fixture(scope="session")
def bids_validator_app_image(
bids_validator_app_script, bids_validator_docker, build_cache_dir
):
return build_app_image(
BIDS_VALIDATOR_APP_IMAGE,
bids_validator_app_script,
build_cache_dir,
base_image=bids_validator_docker,
)
@pytest.fixture(scope="session")
def mock_bids_app_image(mock_bids_app_script, build_cache_dir):
return build_app_image(
MOCK_BIDS_APP_IMAGE,
mock_bids_app_script,
build_cache_dir,
base_image=Pydra2AppImage().reference,
)
def build_app_image(tag_name, script, build_cache_dir, base_image):
dc = docker.from_env()
# Create executable that runs validator then produces some mock output
# files
build_dir = build_cache_dir / tag_name.replace(":", "__i__")
build_dir.mkdir()
launch_sh = build_dir / "launch.sh"
with open(launch_sh, "w") as f:
f.write(script)
# Build mock BIDS app image
with open(build_dir / "Dockerfile", "w") as f:
f.write(
f"""FROM {base_image}
ADD ./launch.sh /launch.sh
RUN chmod +x /launch.sh
ENTRYPOINT ["/launch.sh"]"""
)
dc.images.build(path=str(build_dir), tag=tag_name)
return tag_name
@pytest.fixture(scope="session")
def bids_command_spec(mock_bids_app_executable):
inputs = {
"T1w": {
"configuration": {
"path": "anat/T1w",
},
"datatype": "medimage:NiftiGzX",
"help": "T1-weighted image",
},
"T2w": {
"configuration": {
"path": "anat/T2w",
},
"datatype": "medimage:NiftiGzX",
"help": "T2-weighted image",
},
"DWI": {
"configuration": {
"path": "dwi/dwi",
},
"datatype": "medimage:NiftiGzXBvec",
"help": "DWI-weighted image",
},
}
outputs = {
"file1": {
"configuration": {
"path": "file1",
},
"datatype": "common:Text",
"help": "an output file",
},
"file2": {
"configuration": {
"path": "file2",
},
"datatype": "common:Text",
"help": "another output file",
},
}
return {
"task": "frametree.bids.tasks:bids_app",
"inputs": inputs,
"outputs": outputs,
"row_frequency": "session",
"configuration": {
"inputs": inputs,
"outputs": outputs,
"executable": str(mock_bids_app_executable),
},
}
# For debugging in IDE's don't catch raised exceptions and let the IDE
# break at it
if os.getenv("_PYTEST_RAISE", "0") != "0":
@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
raise call.excinfo.value
@pytest.hookimpl(tryfirst=True)
def pytest_internalerror(excinfo):
raise excinfo.value
CATCH_CLI_EXCEPTIONS = False
else:
CATCH_CLI_EXCEPTIONS = True
@pytest.fixture
def catch_cli_exceptions():
return CATCH_CLI_EXCEPTIONS