From d17d903d98e246cb76f5c4b65e4ddd054e5c2d61 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 19 Nov 2024 12:53:50 -0500 Subject: [PATCH 1/2] test(cli): Verify get and ls behavior for 1 and multiple files --- templateflow/tests/test_cli.py | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 templateflow/tests/test_cli.py diff --git a/templateflow/tests/test_cli.py b/templateflow/tests/test_cli.py new file mode 100644 index 00000000..279b4e9a --- /dev/null +++ b/templateflow/tests/test_cli.py @@ -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] From 6daa0a88c7c7b8a1d18f62f6e8fa52f3b71e4991 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 19 Nov 2024 12:54:59 -0500 Subject: [PATCH 2/2] fix(cli): Fix crash in templateflow get when matching one file --- templateflow/cli.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templateflow/cli.py b/templateflow/cli.py index f536eed1..ce04d26e 100644 --- a/templateflow/cli.py +++ b/templateflow/cli.py @@ -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__':