Skip to content

Commit 6c7f5b3

Browse files
authored
Merge pull request #140 from effigies/fix/get_one
fix(cli): Fix crash in templateflow get when matching one file
2 parents 15dcbc9 + e48c4cf commit 6c7f5b3

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

templateflow/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ def ls(template, **kwargs):
141141
def get(template, **kwargs):
142142
"""Fetch the assets corresponding to template and optional filters."""
143143
entities = {k: _nulls(v) for k, v in kwargs.items() if v != ''}
144-
click.echo('\n'.join(f'{match}' for match in api.get(template, **entities)))
144+
paths = api.get(template, **entities)
145+
filenames = [str(paths)] if isinstance(paths, Path) else [str(file) for file in paths]
146+
click.echo('\n'.join(filenames))
145147

146148

147149
if __name__ == '__main__':

templateflow/tests/test_cli.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from pathlib import Path
2+
3+
import click.testing
4+
import pytest
5+
6+
from .. import cli
7+
8+
9+
@pytest.fixture
10+
def runner():
11+
return click.testing.CliRunner()
12+
13+
14+
def test_ls_one(runner):
15+
result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w'])
16+
17+
# One result
18+
lines = result.stdout.strip().splitlines()
19+
assert len(lines) == 1
20+
21+
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
22+
23+
path = Path(lines[0])
24+
25+
assert path.exists()
26+
27+
28+
def test_ls_multi(runner):
29+
result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w'])
30+
31+
# Two results
32+
lines = result.stdout.strip().splitlines()
33+
assert len(lines) == 2
34+
35+
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
36+
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T2w.nii.gz' in lines[1]
37+
38+
paths = [Path(line) for line in lines]
39+
40+
assert all(path.exists() for path in paths)
41+
42+
43+
def test_get_one(runner):
44+
result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w'])
45+
46+
# One result, possible download status before
47+
lines = result.stdout.strip().splitlines()[-2:]
48+
49+
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
50+
51+
path = Path(lines[0])
52+
53+
stat_res = path.stat()
54+
assert stat_res.st_size == 10669511
55+
56+
57+
def test_get_multi(runner):
58+
result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w'])
59+
60+
# Two result, possible download status before
61+
lines = result.stdout.strip().splitlines()[-3:]
62+
63+
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
64+
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T2w.nii.gz' in lines[1]
65+
66+
paths = [Path(line) for line in lines]
67+
68+
stats = [path.stat() for path in paths]
69+
assert [stat_res.st_size for stat_res in stats] == [10669511, 10096230]

0 commit comments

Comments
 (0)