Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
fslpy
Commits
f3f3d6a2
Commit
f3f3d6a2
authored
Apr 16, 2021
by
Paul McCarthy
🚵
Browse files
TEST: path tests check str/Path
parent
411a1f49
Changes
1
Hide whitespace changes
Inline
Side-by-side
tests/test_fsl_utils_path.py
View file @
f3f3d6a2
...
@@ -5,15 +5,15 @@
...
@@ -5,15 +5,15 @@
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
#
from
__future__
import
print_function
import
os
import
os
import
os.path
as
op
import
os.path
as
op
import
shutil
import
shutil
import
pathlib
import
tempfile
import
tempfile
from
unittest
import
mock
import
pytest
import
pytest
import
mock
import
fsl.utils.path
as
fslpath
import
fsl.utils.path
as
fslpath
import
fsl.data.image
as
fslimage
import
fsl.data.image
as
fslimage
...
@@ -150,7 +150,8 @@ def test_hasExt():
...
@@ -150,7 +150,8 @@ def test_hasExt():
]
]
for
path
,
aexts
,
expected
in
tests
:
for
path
,
aexts
,
expected
in
tests
:
assert
fslpath
.
hasExt
(
path
,
aexts
)
==
expected
assert
fslpath
.
hasExt
(
path
,
aexts
)
==
expected
assert
fslpath
.
hasExt
(
pathlib
.
Path
(
path
),
aexts
)
==
expected
def
test_addExt_imageFiles_mustExist_shouldPass
():
def
test_addExt_imageFiles_mustExist_shouldPass
():
...
@@ -248,18 +249,15 @@ def test_addExt_imageFiles_mustExist_shouldPass():
...
@@ -248,18 +249,15 @@ def test_addExt_imageFiles_mustExist_shouldPass():
for
f
in
files_to_create
:
for
f
in
files_to_create
:
make_dummy_image_file
(
op
.
join
(
workdir
,
f
))
make_dummy_image_file
(
op
.
join
(
workdir
,
f
))
print
(
'files_to_create: '
,
files_to_create
)
result
=
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
print
(
'workdir: '
,
os
.
listdir
(
workdir
))
allowedExts
,
print
(
'prefix: '
,
prefix
)
mustExist
=
True
,
print
(
'expected: '
,
expected
)
fileGroups
=
groups
)
assert
result
==
op
.
join
(
workdir
,
expected
)
result
=
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
result
=
fslpath
.
addExt
(
pathlib
.
Path
(
op
.
join
(
workdir
,
prefix
)),
allowedExts
,
allowedExts
,
mustExist
=
True
,
mustExist
=
True
,
fileGroups
=
groups
)
fileGroups
=
groups
)
print
(
'result: '
,
result
)
assert
result
==
op
.
join
(
workdir
,
expected
)
assert
result
==
op
.
join
(
workdir
,
expected
)
cleardir
(
workdir
)
cleardir
(
workdir
)
...
@@ -336,20 +334,15 @@ def test_addExt_otherFiles_mustExist_shouldPass():
...
@@ -336,20 +334,15 @@ def test_addExt_otherFiles_mustExist_shouldPass():
for
f
in
files_to_create
:
for
f
in
files_to_create
:
make_dummy_file
(
op
.
join
(
workdir
,
f
))
make_dummy_file
(
op
.
join
(
workdir
,
f
))
print
(
'files_to_create: '
,
files_to_create
)
result
=
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
print
(
'prefix: '
,
prefix
)
allowedExts
=
allowedExts
,
print
(
'allowedExts: '
,
allowedExts
)
mustExist
=
True
,
print
(
'fileGroups: '
,
fileGroups
)
fileGroups
=
fileGroups
)
print
(
'workdir: '
,
os
.
listdir
(
workdir
))
assert
result
==
op
.
join
(
workdir
,
expected
)
print
(
'expected: '
,
expected
)
result
=
fslpath
.
addExt
(
pathlib
.
Path
(
op
.
join
(
workdir
,
prefix
)),
allowedExts
=
allowedExts
,
result
=
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
mustExist
=
True
,
allowedExts
=
allowedExts
,
fileGroups
=
fileGroups
)
mustExist
=
True
,
fileGroups
=
fileGroups
)
print
(
'result: '
,
result
)
assert
result
==
op
.
join
(
workdir
,
expected
)
assert
result
==
op
.
join
(
workdir
,
expected
)
cleardir
(
workdir
)
cleardir
(
workdir
)
...
@@ -422,18 +415,16 @@ def test_addExt_imageFiles_mustExist_shouldFail():
...
@@ -422,18 +415,16 @@ def test_addExt_imageFiles_mustExist_shouldFail():
for
f
in
files_to_create
:
for
f
in
files_to_create
:
make_dummy_file
(
op
.
join
(
workdir
,
f
))
make_dummy_file
(
op
.
join
(
workdir
,
f
))
print
(
'files_to_create: '
,
files_to_create
)
print
(
'prefix: '
,
prefix
)
print
(
'workdir: '
,
os
.
listdir
(
workdir
))
with
pytest
.
raises
(
fslpath
.
PathError
):
with
pytest
.
raises
(
fslpath
.
PathError
):
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
result
=
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
mustExist
=
True
,
mustExist
=
True
,
fileGroups
=
fileGroups
)
fileGroups
=
fileGroups
)
with
pytest
.
raises
(
fslpath
.
PathError
):
fslpath
.
addExt
(
pathlib
.
Path
(
op
.
join
(
workdir
,
prefix
)),
print
(
'result: '
,
result
)
allowedExts
=
allowedExts
,
mustExist
=
True
,
fileGroups
=
fileGroups
)
finally
:
finally
:
shutil
.
rmtree
(
workdir
)
shutil
.
rmtree
(
workdir
)
...
@@ -481,23 +472,19 @@ def test_addExt_otherFiles_mustExist_shouldFail():
...
@@ -481,23 +472,19 @@ def test_addExt_otherFiles_mustExist_shouldFail():
for
f
in
files_to_create
:
for
f
in
files_to_create
:
make_dummy_file
(
op
.
join
(
workdir
,
f
))
make_dummy_file
(
op
.
join
(
workdir
,
f
))
print
(
'files_to_create: '
,
files_to_create
)
print
(
'prefix: '
,
prefix
)
print
(
'workdir: '
,
os
.
listdir
(
workdir
))
with
pytest
.
raises
(
fslpath
.
PathError
):
with
pytest
.
raises
(
fslpath
.
PathError
):
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
result
=
fslpath
.
addExt
(
op
.
join
(
workdir
,
prefix
),
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
mustExist
=
True
,
mustExist
=
True
,
fileGroups
=
fileGroups
)
fileGroups
=
fileGroups
)
with
pytest
.
raises
(
fslpath
.
PathError
):
fslpath
.
addExt
(
pathlib
.
Path
(
op
.
join
(
workdir
,
prefix
)),
print
(
'result: '
,
result
)
allowedExts
=
allowedExts
,
mustExist
=
True
,
fileGroups
=
fileGroups
)
finally
:
finally
:
shutil
.
rmtree
(
workdir
)
shutil
.
rmtree
(
workdir
)
pass
def
test_addExt_noExist
():
def
test_addExt_noExist
():
...
@@ -544,11 +531,14 @@ def test_addExt_noExist():
...
@@ -544,11 +531,14 @@ def test_addExt_noExist():
]
]
for
prefix
,
defaultExt
,
allowedExts
,
expected
in
tests
:
for
prefix
,
defaultExt
,
allowedExts
,
expected
in
tests
:
assert
fslpath
.
addExt
(
prefix
,
assert
fslpath
.
addExt
(
prefix
,
allowedExts
,
allowedExts
,
defaultExt
=
defaultExt
,
defaultExt
=
defaultExt
,
mustExist
=
False
)
==
expected
mustExist
=
False
)
==
expected
assert
fslpath
.
addExt
(
pathlib
.
Path
(
prefix
),
allowedExts
,
defaultExt
=
defaultExt
,
mustExist
=
False
)
==
expected
def
test_addExt_unambiguous
():
def
test_addExt_unambiguous
():
...
@@ -582,15 +572,20 @@ def test_addExt_unambiguous():
...
@@ -582,15 +572,20 @@ def test_addExt_unambiguous():
expected
=
expected
.
split
()
expected
=
expected
.
split
()
with
testdir
(
create
)
as
td
:
with
testdir
(
create
)
as
td
:
result
=
fslpath
.
addExt
(
prefix
,
result
=
fslpath
.
addExt
(
prefix
,
allowedExts
=
exts
,
allowedExts
=
exts
,
fileGroups
=
groups
,
fileGroups
=
groups
,
defaultExt
=
defaultExt
,
defaultExt
=
defaultExt
,
unambiguous
=
False
)
unambiguous
=
False
)
assert
sorted
(
expected
)
==
sorted
(
result
)
result
=
fslpath
.
addExt
(
pathlib
.
Path
(
prefix
),
allowedExts
=
exts
,
fileGroups
=
groups
,
defaultExt
=
defaultExt
,
unambiguous
=
False
)
assert
sorted
(
expected
)
==
sorted
(
result
)
assert
sorted
(
expected
)
==
sorted
(
result
)
def
test_removeExt
():
def
test_removeExt
():
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
...
@@ -622,7 +617,8 @@ def test_removeExt():
...
@@ -622,7 +617,8 @@ def test_removeExt():
if
len
(
test
)
==
2
:
allowed
=
allowedExts
if
len
(
test
)
==
2
:
allowed
=
allowedExts
else
:
allowed
=
test
[
2
]
else
:
allowed
=
test
[
2
]
assert
fslpath
.
removeExt
(
path
,
allowed
)
==
output
assert
fslpath
.
removeExt
(
path
,
allowed
)
==
output
assert
fslpath
.
removeExt
(
pathlib
.
Path
(
path
),
allowed
)
==
output
def
test_getExt
():
def
test_getExt
():
...
@@ -658,8 +654,8 @@ def test_getExt():
...
@@ -658,8 +654,8 @@ def test_getExt():
if
len
(
test
)
==
2
:
allowed
=
allowedExts
if
len
(
test
)
==
2
:
allowed
=
allowedExts
else
:
allowed
=
test
[
2
]
else
:
allowed
=
test
[
2
]
print
(
filename
,
'=='
,
output
)
assert
fslpath
.
getExt
(
filename
,
allowed
)
==
output
assert
fslpath
.
getExt
(
filename
,
allowed
)
==
output
assert
fslpath
.
getExt
(
pathlib
.
Path
(
filename
)
,
allowed
)
==
output
def
test_splitExt
():
def
test_splitExt
():
...
@@ -704,13 +700,14 @@ def test_splitExt():
...
@@ -704,13 +700,14 @@ def test_splitExt():
for
test
in
tests
:
for
test
in
tests
:
filename
=
test
[
0
]
filename
=
test
[
0
]
pfilename
=
pathlib
.
Path
(
filename
)
outbase
,
outext
=
test
[
1
]
outbase
,
outext
=
test
[
1
]
if
len
(
test
)
==
2
:
allowed
=
allowedExts
if
len
(
test
)
==
2
:
allowed
=
allowedExts
else
:
allowed
=
test
[
2
]
else
:
allowed
=
test
[
2
]
print
(
filename
,
'=='
,
(
outbase
,
outext
)
)
assert
fslpath
.
splitExt
(
filename
,
allowed
)
==
(
outbase
,
outext
)
assert
fslpath
.
splitExt
(
filename
,
allowed
)
==
(
outbase
,
outext
)
assert
fslpath
.
splitExt
(
p
filename
,
allowed
)
==
(
outbase
,
outext
)
# firstDot=True
# firstDot=True
tests
=
[
tests
=
[
...
@@ -721,7 +718,9 @@ def test_splitExt():
...
@@ -721,7 +718,9 @@ def test_splitExt():
]
]
for
f
,
exp
in
tests
:
for
f
,
exp
in
tests
:
assert
fslpath
.
splitExt
(
f
,
firstDot
=
True
)
==
exp
pf
=
pathlib
.
Path
(
f
)
assert
fslpath
.
splitExt
(
f
,
firstDot
=
True
)
==
exp
assert
fslpath
.
splitExt
(
pf
,
firstDot
=
True
)
==
exp
def
test_getFileGroup_imageFiles_shouldPass
():
def
test_getFileGroup_imageFiles_shouldPass
():
...
@@ -798,11 +797,6 @@ def test_getFileGroup_imageFiles_shouldPass():
...
@@ -798,11 +797,6 @@ def test_getFileGroup_imageFiles_shouldPass():
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
f
.
write
(
'{}
\n
'
.
format
(
fn
))
f
.
write
(
'{}
\n
'
.
format
(
fn
))
print
()
print
(
'files_to_create: '
,
files_to_create
)
print
(
'path: '
,
path
)
print
(
'files_to_expect: '
,
files_to_expect
)
fullPaths
=
fslpath
.
getFileGroup
(
fullPaths
=
fslpath
.
getFileGroup
(
op
.
join
(
workdir
,
path
),
op
.
join
(
workdir
,
path
),
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
...
@@ -918,13 +912,6 @@ def test_getFileGroup_otherFiles_shouldPass():
...
@@ -918,13 +912,6 @@ def test_getFileGroup_otherFiles_shouldPass():
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
f
.
write
(
'{}
\n
'
.
format
(
fn
))
f
.
write
(
'{}
\n
'
.
format
(
fn
))
print
()
print
(
'files_to_create: '
,
files_to_create
)
print
(
'path: '
,
path
)
print
(
'allowedExts: '
,
allowedExts
)
print
(
'fileGroups: '
,
fileGroups
)
print
(
'files_to_expect: '
,
files_to_expect
)
fullPaths
=
fslpath
.
getFileGroup
(
fullPaths
=
fslpath
.
getFileGroup
(
op
.
join
(
workdir
,
path
),
op
.
join
(
workdir
,
path
),
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
...
@@ -1006,32 +993,22 @@ def test_getFileGroup_shouldFail():
...
@@ -1006,32 +993,22 @@ def test_getFileGroup_shouldFail():
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
f
.
write
(
'{}
\n
'
.
format
(
fn
))
f
.
write
(
'{}
\n
'
.
format
(
fn
))
print
()
print
(
'files_to_create: '
,
files_to_create
)
print
(
'path: '
,
path
)
print
(
'allowedExts: '
,
allowedExts
)
print
(
'fileGroups: '
,
fileGroups
)
with
pytest
.
raises
(
fslpath
.
PathError
):
with
pytest
.
raises
(
fslpath
.
PathError
):
fullPaths
=
fslpath
.
getFileGroup
(
fslpath
.
getFileGroup
(
op
.
join
(
workdir
,
path
),
op
.
join
(
workdir
,
path
),
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
fileGroups
=
fileGroups
,
fileGroups
=
fileGroups
,
fullPaths
=
True
,
fullPaths
=
True
,
unambiguous
=
unambiguous
)
unambiguous
=
unambiguous
)
print
(
'fullPaths: '
,
fullPaths
)
with
pytest
.
raises
(
fslpath
.
PathError
):
with
pytest
.
raises
(
fslpath
.
PathError
):
exts
=
fslpath
.
getFileGroup
(
fslpath
.
getFileGroup
(
op
.
join
(
workdir
,
path
),
op
.
join
(
workdir
,
path
),
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
fileGroups
=
fileGroups
,
fileGroups
=
fileGroups
,
fullPaths
=
False
,
fullPaths
=
False
,
unambiguous
=
unambiguous
)
unambiguous
=
unambiguous
)
print
(
'exts: '
,
exts
)
cleardir
(
workdir
)
cleardir
(
workdir
)
finally
:
finally
:
...
@@ -1117,16 +1094,9 @@ def test_removeDuplicates_imageFiles_shouldPass():
...
@@ -1117,16 +1094,9 @@ def test_removeDuplicates_imageFiles_shouldPass():
paths
=
paths
.
split
()
paths
=
paths
.
split
()
expected
=
expected
.
split
()
expected
=
expected
.
split
()
print
()
print
(
'files_to_create: '
,
files_to_create
)
print
(
'paths: '
,
paths
)
print
(
'expected: '
,
expected
)
paths
=
[
op
.
join
(
workdir
,
p
)
for
p
in
paths
]
paths
=
[
op
.
join
(
workdir
,
p
)
for
p
in
paths
]
result
=
fslpath
.
removeDuplicates
(
paths
,
allowedExts
,
groups
)
result
=
fslpath
.
removeDuplicates
(
paths
,
allowedExts
,
groups
)
print
(
'result: '
,
result
)
assert
result
==
[
op
.
join
(
workdir
,
e
)
for
e
in
expected
]
assert
result
==
[
op
.
join
(
workdir
,
e
)
for
e
in
expected
]
cleardir
(
workdir
)
cleardir
(
workdir
)
...
@@ -1196,20 +1166,10 @@ def test_removeDuplicates_otherFiles_shouldPass():
...
@@ -1196,20 +1166,10 @@ def test_removeDuplicates_otherFiles_shouldPass():
for
f
in
files_to_create
:
for
f
in
files_to_create
:
make_dummy_file
(
op
.
join
(
workdir
,
f
))
make_dummy_file
(
op
.
join
(
workdir
,
f
))
print
(
'files_to_create: {}'
.
format
(
files_to_create
))
print
(
'paths: {}'
.
format
(
paths
))
print
(
'allowedExts: {}'
.
format
(
allowedExts
))
print
(
'fileGroups: {}'
.
format
(
fileGroups
))
print
(
'workdir: {}'
.
format
(
os
.
listdir
(
workdir
)))
print
(
'expected: {}'
.
format
(
expected
))
result
=
fslpath
.
removeDuplicates
([
op
.
join
(
workdir
,
p
)
for
p
in
paths
],
result
=
fslpath
.
removeDuplicates
([
op
.
join
(
workdir
,
p
)
for
p
in
paths
],
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
fileGroups
=
fileGroups
)
fileGroups
=
fileGroups
)
print
(
'result: {}'
.
format
(
result
))
assert
result
==
[
op
.
join
(
workdir
,
e
)
for
e
in
expected
]
assert
result
==
[
op
.
join
(
workdir
,
e
)
for
e
in
expected
]
cleardir
(
workdir
)
cleardir
(
workdir
)
...
@@ -1256,17 +1216,10 @@ def test_removeDuplicates_shouldFail():
...
@@ -1256,17 +1216,10 @@ def test_removeDuplicates_shouldFail():
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
with
open
(
op
.
join
(
workdir
,
fn
),
'wt'
)
as
f
:
f
.
write
(
'{}
\n
'
.
format
(
fn
))
f
.
write
(
'{}
\n
'
.
format
(
fn
))
print
()
print
(
'files_to_create: '
,
files_to_create
)
print
(
'path: '
,
path
)
print
(
'allowedExts: '
,
allowedExts
)
print
(
'fileGroups: '
,
fileGroups
)
with
pytest
.
raises
(
fslpath
.
PathError
):
with
pytest
.
raises
(
fslpath
.
PathError
):
result
=
fslpath
.
removeDuplicates
(
path
,
fslpath
.
removeDuplicates
(
path
,
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
fileGroups
=
fileGroups
)
fileGroups
=
fileGroups
)
print
(
'result: '
,
result
)
finally
:
finally
:
shutil
.
rmtree
(
workdir
)
shutil
.
rmtree
(
workdir
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment