Skip to content

Commit f3b2cbb

Browse files
authored
Merge pull request #2331 from bpinsard/fix/bids-filter-file_pathexist
FIX: non-existing path or JSON syntax error for `--bids-filter-file` should raise on error.
2 parents a0d40c5 + 48caaeb commit f3b2cbb

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

fmriprep/cli/parser.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@ def _filter_pybids_none_any(dct):
5555
for k, v in dct.items()
5656
}
5757

58-
def _bids_filter(value):
59-
from json import loads
60-
61-
if value and Path(value).exists():
62-
return loads(Path(value).read_text(), object_hook=_filter_pybids_none_any)
58+
def _bids_filter(value, parser):
59+
from json import loads, JSONDecodeError
60+
61+
if value:
62+
if Path(value).exists():
63+
try:
64+
return loads(Path(value).read_text(), object_hook=_filter_pybids_none_any)
65+
except JSONDecodeError as e:
66+
raise parser.error(f"JSON syntax error in: <{value}>.")
67+
else:
68+
raise parser.error(f"Path does not exist: <{value}>.")
6369

6470
verstr = f"fMRIPrep v{config.environment.version}"
6571
currentv = Version(config.environment.version)
@@ -76,6 +82,7 @@ def _bids_filter(value):
7682
PathExists = partial(_path_exists, parser=parser)
7783
IsFile = partial(_is_file, parser=parser)
7884
PositiveInt = partial(_min_one, parser=parser)
85+
BIDSFilter = partial(_bids_filter, parser=parser)
7986

8087
# Arguments as specified by BIDS-Apps
8188
# required, positional arguments
@@ -139,7 +146,7 @@ def _bids_filter(value):
139146
"--bids-filter-file",
140147
dest="bids_filters",
141148
action="store",
142-
type=_bids_filter,
149+
type=BIDSFilter,
143150
metavar="FILE",
144151
help="a JSON file describing custom BIDS input filters using PyBIDS. "
145152
"For further details, please check out "

fmriprep/cli/tests/test_parser.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,28 @@ def _mock_is_bl(*args, **kwargs):
114114
assert ("FLAGGED" in captured) is flagged[0]
115115
if flagged[0]:
116116
assert (flagged[1] or "reason: unknown") in captured
117+
118+
119+
def test_bids_filter_file(tmp_path, capsys):
120+
bids_path = tmp_path / "data"
121+
out_path = tmp_path / "out"
122+
bff = tmp_path / "filter.json"
123+
args = [str(bids_path), str(out_path), "participant",
124+
"--bids-filter-file", str(bff)]
125+
bids_path.mkdir()
126+
127+
parser = _build_parser()
128+
129+
with pytest.raises(SystemExit) as error:
130+
parser.parse_args(args)
131+
132+
err = capsys.readouterr().err
133+
assert "Path does not exist:" in err
134+
135+
bff.write_text('{"invalid json": }')
136+
137+
with pytest.raises(SystemExit) as error:
138+
parser.parse_args(args)
139+
140+
err = capsys.readouterr().err
141+
assert "JSON syntax error in:" in err

0 commit comments

Comments
 (0)