From 951404e27e97677b4e62ef4d784b036f634f9779 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 19 Nov 2024 12:53:50 -0500 Subject: [PATCH 1/4] 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 b4d1f567e1c012f5e30ef9abe83f012bc2507c89 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 19 Nov 2024 12:54:59 -0500 Subject: [PATCH 2/4] 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__': From 39111081b5997ab3392479ec2501f0d4cc4a8c33 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 11 Aug 2025 15:47:39 -0400 Subject: [PATCH 3/4] test: Check stdout, not combined stdout/stderr --- templateflow/tests/test_cli.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/templateflow/tests/test_cli.py b/templateflow/tests/test_cli.py index 279b4e9a..721e7ba8 100644 --- a/templateflow/tests/test_cli.py +++ b/templateflow/tests/test_cli.py @@ -11,7 +11,7 @@ def test_ls_one(): result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w']) # One result - lines = result.output.strip().splitlines() + lines = result.stdout.strip().splitlines() assert len(lines) == 1 assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0] @@ -27,7 +27,7 @@ def test_ls_multi(): result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w']) # Two results - lines = result.output.strip().splitlines() + lines = result.stdout.strip().splitlines() assert len(lines) == 2 assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0] @@ -44,10 +44,7 @@ def test_get_one(): 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) + lines = result.stdout.strip().splitlines()[-2:] assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0] @@ -63,10 +60,7 @@ def test_get_multi(): 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) + lines = result.stdout.strip().splitlines()[-3:] 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] From e48c4cfcb5e00ace38868099d933b9a770c33e73 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Tue, 12 Aug 2025 11:41:25 -0400 Subject: [PATCH 4/4] test: Make fixture for CliRunner --- templateflow/tests/test_cli.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/templateflow/tests/test_cli.py b/templateflow/tests/test_cli.py index 721e7ba8..141b70db 100644 --- a/templateflow/tests/test_cli.py +++ b/templateflow/tests/test_cli.py @@ -1,13 +1,17 @@ from pathlib import Path import click.testing +import pytest from .. import cli -def test_ls_one(): - runner = click.testing.CliRunner() +@pytest.fixture +def runner(): + return click.testing.CliRunner() + +def test_ls_one(runner): result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w']) # One result @@ -21,9 +25,7 @@ def test_ls_one(): assert path.exists() -def test_ls_multi(): - runner = click.testing.CliRunner() - +def test_ls_multi(runner): result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w']) # Two results @@ -38,9 +40,7 @@ def test_ls_multi(): assert all(path.exists() for path in paths) -def test_get_one(): - runner = click.testing.CliRunner() - +def test_get_one(runner): result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w']) # One result, possible download status before @@ -54,9 +54,7 @@ def test_get_one(): assert stat_res.st_size == 10669511 -def test_get_multi(): - runner = click.testing.CliRunner() - +def test_get_multi(runner): result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w']) # Two result, possible download status before