Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

fail gracefully when no matching volume files were found #14

Merged
merged 2 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion freesurfer_volume_reader/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse
import os
import re
import sys
import typing

import pandas
Expand Down Expand Up @@ -62,8 +63,12 @@ def main():
volume_frame['source_type'] = source_type
volume_frame['source_path'] = volume_file.absolute_path
volume_frames.append(volume_frame)
if not volume_frames:
print('Did not find any volume files matching the specified criteria.', file=sys.stderr)
return os.EX_NOINPUT
united_volume_frame = concat_dataframes(volume_frames)
print(united_volume_frame.to_csv(index=False))
return os.EX_OK

if __name__ == '__main__':
main()
sys.exit(main())
31 changes: 30 additions & 1 deletion tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.D
elif 'SUBJECTS_DIR' in os.environ:
del os.environ['SUBJECTS_DIR']
with unittest.mock.patch('sys.argv', [''] + argv):
freesurfer_volume_reader.__main__.main()
assert freesurfer_volume_reader.__main__.main() == 0
out, _ = capsys.readouterr()
resulted_frame = pandas.read_csv(io.StringIO(out)).drop(columns=['source_path'])
if 'correction' in resulted_frame:
Expand Down Expand Up @@ -202,6 +202,35 @@ def test_main_root_dir_filename_regex_combined(capsys):
)


def test_main_no_files_found(capsys):
with unittest.mock.patch('sys.argv', ['', '--freesurfer-hipposf-filename-regex', r'^21$',
'--', SUBJECTS_DIR]):
assert os.EX_NOINPUT == freesurfer_volume_reader.__main__.main()
out, err = capsys.readouterr()
assert not out
assert err == 'Did not find any volume files matching the specified criteria.\n'


def test_main_module_script_no_files_found():
proc_info = subprocess.run(['python', '-m', 'freesurfer_volume_reader',
'--freesurfer-hipposf-filename-regex', r'^21$',
'--', SUBJECTS_DIR],
check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
assert os.EX_NOINPUT == proc_info.returncode
assert not proc_info.stdout
assert 'not find any volume files' in proc_info.stderr.rstrip().decode()


def test_script_no_files_found():
proc_info = subprocess.run(['freesurfer-volume-reader',
'--freesurfer-hipposf-filename-regex', r'^21$',
'--', SUBJECTS_DIR],
check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
assert os.EX_NOINPUT == proc_info.returncode
assert not proc_info.stdout
assert 'not find any volume files' in proc_info.stderr.rstrip().decode()


def test_main_module_script_help():
subprocess.run(['python', '-m', 'freesurfer_volume_reader', '--help'],
check=True)
Expand Down