Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
257f6b80
Commit
257f6b80
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
TEST: Expanded run module unit tests
parent
a34afa7c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_run.py
+112
-30
112 additions, 30 deletions
tests/test_run.py
with
112 additions
and
30 deletions
tests/test_run.py
+
112
−
30
View file @
257f6b80
...
@@ -27,6 +27,12 @@ import fsl.utils.fslsub as fslsub
...
@@ -27,6 +27,12 @@ import fsl.utils.fslsub as fslsub
from
.
import
make_random_image
,
mockFSLDIR
,
CaptureStdout
from
.
import
make_random_image
,
mockFSLDIR
,
CaptureStdout
def
mkexec
(
path
,
contents
):
with
open
(
path
,
'
wt
'
)
as
f
:
f
.
write
(
contents
)
os
.
chmod
(
path
,
0o755
)
def
test_run
():
def
test_run
():
test_script
=
textwrap
.
dedent
(
"""
test_script
=
textwrap
.
dedent
(
"""
...
@@ -40,9 +46,7 @@ def test_run():
...
@@ -40,9 +46,7 @@ def test_run():
with
tempdir
.
tempdir
():
with
tempdir
.
tempdir
():
# return code == 0
# return code == 0
with
open
(
'
script.sh
'
,
'
wt
'
)
as
f
:
mkexec
(
'
script.sh
'
,
test_script
.
format
(
0
))
f
.
write
(
test_script
.
format
(
0
))
os
.
chmod
(
'
script.sh
'
,
0o755
)
expstdout
=
"
standard output - arguments: 1 2 3
\n
"
expstdout
=
"
standard output - arguments: 1 2 3
\n
"
expstderr
=
"
standard error
\n
"
expstderr
=
"
standard error
\n
"
...
@@ -56,29 +60,34 @@ def test_run():
...
@@ -56,29 +60,34 @@ def test_run():
assert
run
.
run
(
*
(
'
./script.sh
'
,
'
1
'
,
'
2
'
,
'
3
'
))
==
expstdout
assert
run
.
run
(
*
(
'
./script.sh
'
,
'
1
'
,
'
2
'
,
'
3
'
))
==
expstdout
# test stdout/stderr
# test stdout/stderr
stdout
,
stderr
=
run
.
run
(
'
./script.sh 1 2 3
'
,
err
=
True
)
stdout
,
stderr
=
run
.
run
(
'
./script.sh 1 2 3
'
,
std
err
=
True
)
assert
stdout
==
expstdout
assert
stdout
==
expstdout
assert
stderr
==
expstderr
assert
stderr
==
expstderr
# test return code
# test return code
res
=
run
.
run
(
'
./script.sh 1 2 3
'
,
ret
=
True
)
res
=
run
.
run
(
'
./script.sh 1 2 3
'
,
exitcode
=
True
)
stdout
,
ret
=
res
stdout
,
ret
=
res
assert
stdout
==
expstdout
assert
stdout
==
expstdout
assert
ret
==
0
assert
ret
==
0
stdout
,
stderr
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
err
=
True
,
ret
=
True
)
stdout
,
stderr
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
stderr
=
True
,
exitcode
=
True
)
assert
stdout
==
expstdout
assert
stdout
==
expstdout
assert
stderr
==
expstderr
assert
stderr
==
expstderr
assert
ret
==
0
assert
ret
==
0
# stdout=False
res
=
run
.
run
(
'
./script.sh 1 2 3
'
,
stdout
=
False
)
assert
res
==
()
stderr
=
run
.
run
(
'
./script.sh 1 2 3
'
,
stdout
=
False
,
stderr
=
True
)
assert
stderr
==
expstderr
# return code != 0
# return code != 0
with
open
(
'
./script.sh
'
,
'
wt
'
)
as
f
:
mkexec
(
'
./script.sh
'
,
test_script
.
format
(
255
))
f
.
write
(
test_script
.
format
(
255
))
os
.
chmod
(
'
./script.sh
'
,
0o755
)
with
pytest
.
raises
(
RuntimeError
):
with
pytest
.
raises
(
RuntimeError
):
run
.
run
(
'
./script.sh 1 2 3
'
)
run
.
run
(
'
./script.sh 1 2 3
'
)
stdout
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
ret
=
True
)
stdout
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
exitcode
=
True
)
assert
stdout
==
expstdout
assert
stdout
==
expstdout
assert
ret
==
255
assert
ret
==
255
...
@@ -93,9 +102,7 @@ def test_run_tee():
...
@@ -93,9 +102,7 @@ def test_run_tee():
"""
).
strip
()
"""
).
strip
()
with
tempdir
.
tempdir
():
with
tempdir
.
tempdir
():
with
open
(
'
script.sh
'
,
'
wt
'
)
as
f
:
mkexec
(
'
script.sh
'
,
test_script
)
f
.
write
(
test_script
)
os
.
chmod
(
'
script.sh
'
,
0o755
)
expstdout
=
"
standard output - arguments: 1 2 3
\n
"
expstdout
=
"
standard output - arguments: 1 2 3
\n
"
expstderr
=
"
standard error
\n
"
expstderr
=
"
standard error
\n
"
...
@@ -103,13 +110,14 @@ def test_run_tee():
...
@@ -103,13 +110,14 @@ def test_run_tee():
capture
=
CaptureStdout
()
capture
=
CaptureStdout
()
with
capture
:
with
capture
:
stdout
=
run
.
run
(
'
./script.sh 1 2 3
'
,
tee
=
True
)
stdout
=
run
.
run
(
'
./script.sh 1 2 3
'
,
log
=
{
'
tee
'
:
True
}
)
assert
stdout
==
expstdout
assert
stdout
==
expstdout
assert
capture
.
stdout
==
expstdout
assert
capture
.
stdout
==
expstdout
with
capture
.
reset
():
with
capture
.
reset
():
stdout
,
stderr
=
run
.
run
(
'
./script.sh 1 2 3
'
,
err
=
True
,
tee
=
True
)
stdout
,
stderr
=
run
.
run
(
'
./script.sh 1 2 3
'
,
stderr
=
True
,
log
=
{
'
tee
'
:
True
})
assert
stdout
==
expstdout
assert
stdout
==
expstdout
assert
stderr
==
expstderr
assert
stderr
==
expstderr
...
@@ -118,7 +126,9 @@ def test_run_tee():
...
@@ -118,7 +126,9 @@ def test_run_tee():
with
capture
.
reset
():
with
capture
.
reset
():
stdout
,
stderr
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
stdout
,
stderr
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
err
=
True
,
ret
=
True
,
tee
=
True
)
stderr
=
True
,
exitcode
=
True
,
log
=
{
'
tee
'
:
True
})
assert
ret
==
0
assert
ret
==
0
assert
stdout
==
expstdout
assert
stdout
==
expstdout
...
@@ -127,7 +137,9 @@ def test_run_tee():
...
@@ -127,7 +137,9 @@ def test_run_tee():
assert
capture
.
stderr
==
expstderr
assert
capture
.
stderr
==
expstderr
with
capture
.
reset
():
with
capture
.
reset
():
stdout
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
ret
=
True
,
tee
=
True
)
stdout
,
ret
=
run
.
run
(
'
./script.sh 1 2 3
'
,
exitcode
=
True
,
log
=
{
'
tee
'
:
True
})
assert
ret
==
0
assert
ret
==
0
assert
stdout
==
expstdout
assert
stdout
==
expstdout
...
@@ -142,9 +154,7 @@ def test_dryrun():
...
@@ -142,9 +154,7 @@ def test_dryrun():
"""
).
strip
()
"""
).
strip
()
with
tempdir
.
tempdir
():
with
tempdir
.
tempdir
():
with
open
(
'
./script.sh
'
,
'
wt
'
)
as
f
:
mkexec
(
'
./script.sh
'
,
test_script
)
f
.
write
(
test_script
)
os
.
chmod
(
'
./script.sh
'
,
0o755
)
run
.
run
(
'
./script.sh
'
)
run
.
run
(
'
./script.sh
'
)
assert
op
.
exists
(
'
foo
'
)
assert
op
.
exists
(
'
foo
'
)
...
@@ -169,7 +179,6 @@ def test_runfsl():
...
@@ -169,7 +179,6 @@ def test_runfsl():
old_fsldir
=
fslplatform
.
fsldir
old_fsldir
=
fslplatform
.
fsldir
old_fsldevdir
=
fslplatform
.
fsldevdir
old_fsldevdir
=
fslplatform
.
fsldevdir
try
:
try
:
with
tempdir
.
tempdir
():
with
tempdir
.
tempdir
():
...
@@ -185,9 +194,8 @@ def test_runfsl():
...
@@ -185,9 +194,8 @@ def test_runfsl():
fsldir
=
op
.
abspath
(
'
./fsl
'
)
fsldir
=
op
.
abspath
(
'
./fsl
'
)
fslhd
=
op
.
join
(
fsldir
,
'
bin
'
,
'
fslhd
'
)
fslhd
=
op
.
join
(
fsldir
,
'
bin
'
,
'
fslhd
'
)
os
.
makedirs
(
op
.
join
(
fsldir
,
'
bin
'
))
os
.
makedirs
(
op
.
join
(
fsldir
,
'
bin
'
))
with
open
(
fslhd
,
'
wt
'
)
as
f
:
f
.
write
(
test_script
.
format
(
'
fsldir
'
))
mkexec
(
fslhd
,
test_script
.
format
(
'
fsldir
'
))
os
.
chmod
(
fslhd
,
0o777
)
fslplatform
.
fsldir
=
fsldir
fslplatform
.
fsldir
=
fsldir
assert
run
.
runfsl
(
'
fslhd
'
).
strip
()
==
'
fsldir
'
assert
run
.
runfsl
(
'
fslhd
'
).
strip
()
==
'
fsldir
'
...
@@ -196,8 +204,8 @@ def test_runfsl():
...
@@ -196,8 +204,8 @@ def test_runfsl():
fsldevdir
=
'
./fsldev
'
fsldevdir
=
'
./fsldev
'
fslhd
=
op
.
join
(
fsldevdir
,
'
bin
'
,
'
fslhd
'
)
fslhd
=
op
.
join
(
fsldevdir
,
'
bin
'
,
'
fslhd
'
)
shutil
.
copytree
(
fsldir
,
fsldevdir
)
shutil
.
copytree
(
fsldir
,
fsldevdir
)
with
open
(
fslhd
,
'
wt
'
)
as
f
:
f
.
write
(
test_script
.
format
(
'
fsldevdir
'
))
mkexec
(
fslhd
,
test_script
.
format
(
'
fsldevdir
'
))
fslplatform
.
fsldevdir
=
fsldevdir
fslplatform
.
fsldevdir
=
fsldevdir
fslplatform
.
fsldir
=
None
fslplatform
.
fsldir
=
None
...
@@ -207,10 +215,7 @@ def test_runfsl():
...
@@ -207,10 +215,7 @@ def test_runfsl():
override
=
'
./override
'
override
=
'
./override
'
fslhd
=
op
.
join
(
override
,
'
fslhd
'
)
fslhd
=
op
.
join
(
override
,
'
fslhd
'
)
os
.
makedirs
(
override
)
os
.
makedirs
(
override
)
with
open
(
fslhd
,
'
wt
'
)
as
f
:
mkexec
(
fslhd
,
test_script
.
format
(
'
override
'
))
f
.
write
(
test_script
.
format
(
'
override
'
))
os
.
chmod
(
fslhd
,
0o777
)
fslplatform
.
fsldir
=
None
fslplatform
.
fsldir
=
None
fslplatform
.
fsldevdir
=
None
fslplatform
.
fsldevdir
=
None
...
@@ -285,3 +290,80 @@ def test_run_submit():
...
@@ -285,3 +290,80 @@ def test_run_submit():
assert
stdout
==
'
test_script running
\n
'
assert
stdout
==
'
test_script running
\n
'
assert
stderr
==
experr
assert
stderr
==
experr
def
test_run_streams
():
"""
"""
test_script
=
textwrap
.
dedent
(
"""
#!/usr/bin/env bash
echo standard output
echo standard error >&2
exit 0
"""
).
strip
()
expstdout
=
'
standard output
\n
'
expstderr
=
'
standard error
\n
'
with
tempdir
.
tempdir
():
mkexec
(
'
./script.sh
'
,
test_script
)
with
open
(
'
my_stdout
'
,
'
wt
'
)
as
stdout
,
\
open
(
'
my_stderr
'
,
'
wt
'
)
as
stderr
:
stdout
,
stderr
=
run
.
run
(
'
./script.sh
'
,
stderr
=
True
,
log
=
{
'
stdout
'
:
stdout
,
'
stderr
'
:
stderr
})
assert
stdout
==
expstdout
assert
stderr
==
expstderr
assert
open
(
'
my_stdout
'
,
'
rt
'
).
read
()
==
expstdout
assert
open
(
'
my_stderr
'
,
'
rt
'
).
read
()
==
expstderr
capture
=
CaptureStdout
()
with
open
(
'
my_stdout
'
,
'
wt
'
)
as
stdout
,
\
open
(
'
my_stderr
'
,
'
wt
'
)
as
stderr
,
\
capture
.
reset
():
stdout
,
stderr
=
run
.
run
(
'
./script.sh
'
,
stderr
=
True
,
log
=
{
'
tee
'
:
True
,
'
stdout
'
:
stdout
,
'
stderr
'
:
stderr
})
assert
stdout
==
expstdout
assert
stderr
==
expstderr
assert
capture
.
stdout
==
expstdout
assert
capture
.
stderr
==
expstderr
assert
open
(
'
my_stdout
'
,
'
rt
'
).
read
()
==
expstdout
assert
open
(
'
my_stderr
'
,
'
rt
'
).
read
()
==
expstderr
def
test_run_logcmd
():
test_script
=
textwrap
.
dedent
(
"""
#!/usr/bin/env bash
echo output $@
exit 0
"""
).
strip
()
expstdout
=
'
./script.sh 1 2 3
\n
output 1 2 3
\n
'
with
tempdir
.
tempdir
():
mkexec
(
'
script.sh
'
,
test_script
)
stdout
=
run
.
run
(
'
./script.sh 1 2 3
'
,
log
=
{
'
cmd
'
:
True
})
assert
stdout
==
expstdout
mkexec
(
'
script.sh
'
,
test_script
)
stdout
=
run
.
run
(
'
./script.sh 1 2 3
'
,
log
=
{
'
cmd
'
:
True
})
assert
stdout
==
expstdout
with
open
(
'
my_stdout
'
,
'
wt
'
)
as
stdoutf
:
stdout
=
run
.
run
(
'
./script.sh 1 2 3
'
,
log
=
{
'
cmd
'
:
True
,
'
stdout
'
:
stdoutf
})
assert
stdout
==
expstdout
assert
open
(
'
my_stdout
'
,
'
rt
'
).
read
()
==
expstdout
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment