Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Fix crash in templateflow get when matching one file #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion templateflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ def ls(template, **kwargs):
def get(template, **kwargs):
"""Fetch the assets corresponding to template and optional filters."""
entities = {k: _nulls(v) for k, v in kwargs.items() if v != ''}
click.echo('\n'.join(f'{match}' for match in api.get(template, **entities)))
paths = api.get(template, **entities)
filenames = [str(paths)] if isinstance(paths, Path) else [str(file) for file in paths]
click.echo('\n'.join(filenames))


if __name__ == '__main__':
Expand Down
77 changes: 77 additions & 0 deletions templateflow/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pathlib import Path

import click.testing

from .. import cli


def test_ls_one():
runner = click.testing.CliRunner()

result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w'])

# One result
lines = result.output.strip().splitlines()
assert len(lines) == 1

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]

path = Path(lines[0])

assert path.exists()


def test_ls_multi():
runner = click.testing.CliRunner()

result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w'])

# Two results
lines = result.output.strip().splitlines()
assert len(lines) == 2

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T2w.nii.gz' in lines[1]

paths = [Path(line) for line in lines]

assert all(path.exists() for path in paths)


def test_get_one():
runner = click.testing.CliRunner()

result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w'])

# One result, possible download status before
lines = result.output.strip().splitlines()[-2:]
if len(lines) == 2:
assert lines[0].startswith('100%')
lines.pop(0)

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]

path = Path(lines[0])

stat_res = path.stat()
assert stat_res.st_size == 10669511


def test_get_multi():
runner = click.testing.CliRunner()

result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w'])

# Two result, possible download status before
lines = result.output.strip().splitlines()[-3:]
if len(lines) == 3:
assert lines[0].startswith('100%')
lines.pop(0)

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T2w.nii.gz' in lines[1]

paths = [Path(line) for line in lines]

stats = [path.stat() for path in paths]
assert [stat_res.st_size for stat_res in stats] == [10669511, 10096230]
Loading