Skip to content
Snippets Groups Projects
Commit ce2d5b52 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

fsl.utils.path test suite updated.

parent 5d644d61
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,12 @@
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
from __future__ import print_function
import os
import os.path as op
import shutil
import tempfile
import pytest
......@@ -18,7 +23,7 @@ def test_addExt_exists_shouldPass(testdir):
are valid.
"""
replacements = fslimage.REPLACEMENTS
groups = fslimage.FILE_GROUPS
allowedExts = fslimage.ALLOWED_EXTENSIONS
tests = [
......@@ -41,7 +46,7 @@ def test_addExt_exists_shouldPass(testdir):
assert fslpath.addExt(prefix,
allowedExts,
mustExist=True,
replace=replacements) == output
fileGroups=groups) == output
def test_addExt_exists_shouldFail(testdir):
......@@ -49,8 +54,8 @@ def test_addExt_exists_shouldFail(testdir):
error.
"""
replacements = fslimage.REPLACEMENTS
allowedExts = fslimage.ALLOWED_EXTENSIONS
groups = fslimage.FILE_GROUPS
allowedExts = fslimage.ALLOWED_EXTENSIONS
shouldFail = [
......@@ -83,7 +88,7 @@ def test_addExt_exists_shouldFail(testdir):
for test in shouldFail:
prefix = op.join(testdir, 'nifti_formats', test[0])
allowed = allowedExts
replace = replacements
group = groups
if len(test) >= 2:
if not (test[1] == False):
......@@ -91,14 +96,14 @@ def test_addExt_exists_shouldFail(testdir):
if len(test) == 3:
if not (test[2] == False):
replace = test[2]
group = test[2]
with pytest.raises(fslpath.PathError):
fslpath.addExt(prefix,
allowed,
mustExist=True,
replace=replace)
fileGroups=group)
def test_addExt_noExist(testdir):
......@@ -136,7 +141,7 @@ def test_addExt_noExist(testdir):
def test_removeExt(testdir):
def test_removeExt():
allowedExts = fslimage.ALLOWED_EXTENSIONS
......@@ -170,13 +175,13 @@ def test_removeExt(testdir):
assert fslpath.removeExt(path, allowed) == output
def test_getExt(testdir):
def test_getExt():
allowedExts = fslimage.ALLOWED_EXTENSIONS
# len(test) == 2 -> allowedExts set from above
# Otherwise, allowedExts set from test tuple
shouldPass = [
tests = [
('blah.blah', '.blah', None),
('blah.blah', '.blah', ['.blah']),
('blah.blah', 'blah', ['blah']),
......@@ -189,36 +194,74 @@ def test_getExt(testdir):
('blah.hdr', '.hdr'),
('blah.img', '.img'),
('blah.img.gz', '.img.gz'),
]
shouldRaise = [
('blah', ''),
('blah.blah', ''),
('blah.blah', '', ['bla']),
('blah.nii.gz', '', ['.nii']),
]
for test in shouldPass:
for test in tests:
filename = test[0]
output = test[1]
if len(test) == 2: allowed = allowedExts
else: allowed = test[2]
print filename, '==', output
print(filename, '==', output)
assert fslpath.getExt(filename, allowed) == output
def test_splitExt():
allowedExts = fslimage.ALLOWED_EXTENSIONS
for test in shouldRaise:
filename = test[0]
output = test[1]
# len(test) == 2 -> allowedExts set from above
# Otherwise, allowedExts set from test tuple
tests = [
('blah', ('blah', '')),
('blah.blah', ('blah.blah', '')),
('blah.blah', ('blah', '.blah'), ['.blah']),
('blah.blah', ('blah.', 'blah'), ['blah']),
('blah.nii', ('blah', '.nii')),
('blah.nii.gz', ('blah', '.nii.gz')),
('blah.img', ('blah', '.img')),
('blah.hdr', ('blah', '.hdr')),
('blah.img.gz', ('blah', '.img.gz')),
('blah.nii.gz', ('blah.nii.gz', ''), []),
('blah.nii.gz', ('blah.nii', '.gz'), ['.gz']),
('blah.nii.gz', ('blah.nii.gz', ''), ['.nii']),
('blah.nii.gz', ('blah', '.nii.gz'), ['.nii.gz']),
('blah.nii.gz', ('blah.', 'nii.gz'), ['nii.gz']),
('blah.blah', ('blah', '.blah'), None),
('blah.blah', ('blah', '.blah'), ['.blah']),
('blah.blah', ('blah.', 'blah'), ['blah']),
('blah', ('blah', ''), None),
('blah.nii', ('blah', '.nii'), None),
('blah.nii.gz', ('blah.nii', '.gz'), None),
('blah.nii', ('blah', '.nii')),
('blah.nii.gz', ('blah', '.nii.gz')),
('blah.hdr', ('blah', '.hdr')),
('blah.img', ('blah', '.img')),
('blah.img.gz', ('blah', '.img.gz')),
('blah', ('blah', '')),
('blah.blah', ('blah.blah', '')),
('blah.blah', ('blah.blah', ''), ['bla']),
('blah.nii.gz', ('blah.nii.gz', ''), ['.nii']),
]
for test in tests:
filename = test[0]
outbase, outext = test[1]
if len(test) == 2: allowed = allowedExts
else: allowed = test[2]
with pytest.raises(fslpath.PathError):
fslpath.getExt(filename, allowed)
print(filename, '==', (outbase, outext))
assert fslpath.splitExt(filename, allowed) == (outbase, outext)
def test_deepest():
......@@ -281,4 +324,284 @@ def test_shallowest():
]
for path, suffixes, output in tests:
assert fslpath.shallowest(path, suffixes) == output
assert fslpath.shallowest(path, suffixes) == output
def test_getFileGroup():
allowedExts = fslimage.ALLOWED_EXTENSIONS
groups = fslimage.FILE_GROUPS
# (files_to_create,
# [(path, expected),
# ...
# ]
# )
#
# expected == 'all' is equivalent to expected == files_to_create
allTests = [
(['file.hdr', 'file.img'], [
('file.hdr', 'all'),
('file.img', 'all')]),
(['file.hdr.gz', 'file.img.gz'], [
('file.hdr.gz', 'all'),
('file.img.gz', 'all')]),
(['file.hdr', 'file.img', 'file.nii'], [
('file.hdr', ['file.hdr', 'file.img']),
('file.img', ['file.hdr', 'file.img'])]),
(['file.hdr', 'file.img', 'file.blob'], [
('file.hdr', ['file.hdr', 'file.img']),
('file.img', ['file.hdr', 'file.img'])]),
(['file.hdr'], [
('file.hdr', ['file.hdr']),
('file.img', ['file.img'])]),
(['file.img'], [
('file.hdr', ['file.hdr']),
('file.img', ['file.img'])]),
]
workdir = tempfile.mkdtemp()
try:
for files_to_create, tests in allTests:
for fn in files_to_create:
with open(op.join(workdir, fn), 'wt') as f:
f.write('{}\n'.format(fn))
for path, expected in tests:
if expected == 'all':
expected = list(files_to_create)
fullPaths = fslpath.getFileGroup(
op.join(workdir, path),
allowedExts=allowedExts,
fileGroups=groups,
fullPaths=True)
exts = fslpath.getFileGroup(
op.join(workdir, path),
allowedExts=allowedExts,
fileGroups=groups,
fullPaths=False)
assert sorted(fullPaths) == sorted([op.join(workdir, e) for e in expected])
assert sorted(exts) == sorted([fslpath.getExt(e, allowedExts) for e in expected])
for f in files_to_create:
try: os.remove(op.join(workdir, f))
except: pass
finally:
shutil.rmtree(workdir)
def test_imcp_shouldPass(move=False):
allowedExts = fslimage.ALLOWED_EXTENSIONS
groups = fslimage.FILE_GROUPS
#
# (files_to_create,
# [( imcp_src, imcp_dest, files_which_should_exist),
# ( imcp_src, imcp_dest, [files_which_should_exist]),
# ([imcp_srcs], imcp_dest, files_which_should_exist),
# ([imcp_srcs], imcp_dest, [files_which_should_exist]),
# ...
# ]
# )
#
# if icmp_dest == '', it means to copy to the directory
# files_which_should_exist == 'all' is equivalent to files_which_should_exist == files_to_create
shouldPass = [
(['file.hdr', 'file.img'], [
('file', 'file', 'all'),
('file', 'file.img', 'all'),
('file', 'file.hdr', 'all'),
('file', '', 'all'),
('file.img', 'file', 'all'),
('file.img', 'file.img', 'all'),
('file.img', 'file.hdr', 'all'),
('file.img', '', 'all'),
('file.hdr', 'file', 'all'),
('file.hdr', 'file.img', 'all'),
('file.hdr', 'file.hdr', 'all'),
('file.hdr', '', 'all'),
]),
(['file.hdr', 'file.img', 'file.blob'], [
('file', 'file', ['file.hdr', 'file.img']),
('file', 'file.img', ['file.hdr', 'file.img']),
('file', 'file.hdr', ['file.hdr', 'file.img']),
('file', '', ['file.hdr', 'file.img']),
('file.img', 'file', ['file.hdr', 'file.img']),
('file.img', 'file.img', ['file.hdr', 'file.img']),
('file.img', 'file.hdr', ['file.hdr', 'file.img']),
('file.img', '', ['file.hdr', 'file.img']),
('file.hdr', 'file', ['file.hdr', 'file.img']),
('file.hdr', 'file.img', ['file.hdr', 'file.img']),
('file.hdr', 'file.hdr', ['file.hdr', 'file.img']),
('file.hdr', '', ['file.hdr', 'file.img']),
]),
(['file.hdr', 'file.img', 'file.nii'], [
('file.img', 'file', ['file.hdr', 'file.img']),
('file.img', 'file.img', ['file.hdr', 'file.img']),
('file.img', 'file.hdr', ['file.hdr', 'file.img']),
('file.img', '', ['file.hdr', 'file.img']),
('file.hdr', 'file', ['file.hdr', 'file.img']),
('file.hdr', 'file.img', ['file.hdr', 'file.img']),
('file.hdr', 'file.hdr', ['file.hdr', 'file.img']),
('file.hdr', '', ['file.hdr', 'file.img']),
('file.nii', 'file', 'file.nii'),
('file.nii', 'file.nii', 'file.nii'),
('file.nii', '', 'file.nii'),
]),
(['file.nii'], [
('file', 'file', 'all'),
('file', 'file.nii', 'all'),
('file', '', 'all'),
('file.nii', 'file', 'all'),
('file.nii', 'file.nii', 'all'),
('file.nii', '', 'all'),
]),
(['file.nii.gz'], [
('file', 'file', 'all'),
('file', 'file.nii.gz', 'all'),
('file', '', 'all'),
('file.nii.gz', 'file', 'all'),
('file.nii.gz', 'file.nii.gz', 'all'),
('file.nii.gz', '', 'all'),
]),
(['file.nii', 'file.blob'], [
('file', 'file', 'file.nii'),
('file', 'file.nii', 'file.nii'),
('file', '', 'file.nii'),
('file.nii', 'file', 'file.nii'),
('file.nii', 'file.nii', 'file.nii'),
('file.nii', '', 'file.nii'),
]),
(['file.nii', 'file.nii.gz'], [
('file.nii', 'file', 'file.nii'),
('file.nii', 'file.nii', 'file.nii'),
('file.nii', '', 'file.nii'),
('file.nii.gz', 'file', 'file.nii.gz'),
('file.nii.gz', 'file.nii.gz', 'file.nii.gz'),
('file.nii.gz', '', 'file.nii.gz'),
]),
(['file.hdr', 'file.img', 'file.nii', 'file.nii.gz'], [
(['file.img', 'file.nii', 'file.nii.gz'], '', 'all'),
('file.img', '', ['file.hdr', 'file.img']),
(['file.hdr', 'file.img'], '', ['file.hdr', 'file.img']),
('file.nii', '', 'file.nii'),
(['file.nii', 'file.nii.gz'], '', ['file.nii', 'file.nii.gz']),
]),
(['001.hdr', '001.img', '002.hdr', '002.img', '003.hdr', '003.img'], [
(['001', '002', '003'], '', 'all'),
(['001.img', '002.img', '003.img'], '', 'all'),
(['001.hdr', '002.hdr', '003.hdr'], '', 'all'),
(['001.img', '002', '003'], '', 'all'),
(['001.hdr', '002', '003'], '', 'all'),
(['001.img', '002.hdr', '003.img'], '', 'all'),
(['001.hdr', '002.img', '003.hdr'], '', 'all'),
(['001', '003'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.img', '003.img'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.hdr', '003.hdr'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.img', '003'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.hdr', '003'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.img', '003.img'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.hdr', '003.hdr'], '', ['001.hdr', '001.img', '003.hdr', '003.img']),
(['001.img', '001.hdr', '002.img', '002.hdr', '003.img', '003.hdr'], '', 'all'),
]),
]
indir = tempfile.mkdtemp()
outdir = tempfile.mkdtemp()
try:
for files_to_create, tests in shouldPass:
if not isinstance(files_to_create, list):
files_to_create = [files_to_create]
for imcp_src, imcp_dest, should_exist in tests:
if not isinstance(imcp_src, list): imcp_src = [imcp_src]
if should_exist == 'all': should_exist = list(files_to_create)
elif not isinstance(should_exist, list): should_exist = [should_exist]
imcp_dest = op.join(outdir, imcp_dest)
# Each input file contains
# its name in plain text,
# so we can verify that the
# files were correctly copied
for fn in files_to_create:
with open(op.join(indir, fn), 'wt') as f:
f.write('{}\n'.format(fn))
for src in imcp_src:
src = op.join(indir, src)
if move: fslpath.immv(src, imcp_dest, allowedExts=allowedExts, fileGroups=groups)
else: fslpath.imcp(src, imcp_dest, allowedExts=allowedExts, fileGroups=groups)
copied = os.listdir(outdir)
copied = [f for f in copied if op.isfile(op.join(outdir, f))]
assert sorted(copied) == sorted(should_exist)
# check file contents
for fn in should_exist:
with open(op.join(outdir, fn), 'rt') as f:
assert f.read() == '{}\n'.format(fn)
# If move, check that
# input files are gone
if move:
for f in should_exist:
assert not op.exists(op.join(indir, f))
for f in files_to_create:
try: os.remove(op.join(indir, f))
except: pass
for f in should_exist:
os.remove(op.join(outdir, f))
finally:
shutil.rmtree(indir)
shutil.rmtree(outdir)
def test_immv_shouldPass():
test_imcp_shouldPass(move=True)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment