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

New unit test for run(submit=True). Not going to test fslsub right now, as it

will probably change.
parent 1bd1ab69
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,10 @@ import nibabel as nib
from six import StringIO
try: from unittest import mock
except ImportError: import mock
import fsl.data.image as fslimage
from fsl.utils.tempdir import tempdir
from fsl.utils.platform import platform as fslplatform
......@@ -37,9 +41,14 @@ def mockFSLDIR():
try:
with tempdir() as td:
fsldir = op.join(td, 'fsl')
os.makedirs(fsldir)
bindir = op.join(fsldir, 'bin')
os.makedirs(bindir)
fslplatform.fsldir = fsldir
yield fsldir
path = op.pathsep.join((bindir, os.environ['PATH']))
with mock.patch.dict(os.environ, {'PATH': path}):
yield fsldir
finally:
fslplatform.fsldir = oldval
......
......@@ -20,8 +20,9 @@ import pytest
import fsl.utils.tempdir as tempdir
from fsl.utils.platform import platform as fslplatform
import fsl.utils.run as run
import fsl.utils.fslsub as fslsub
from . import make_random_image
from . import make_random_image, mockFSLDIR
def test_run():
......@@ -58,7 +59,9 @@ def test_run():
assert stderr.strip() == expstderr
# test return code
stdout, ret = run.run('./script.sh 1 2 3', ret=True)
res = run.run('./script.sh 1 2 3', ret=True)
print(res)
stdout, ret = res
assert stdout.strip() == expstdout
assert ret == 0
stdout, stderr, ret = run.run('./script.sh 1 2 3', err=True, ret=True)
......@@ -123,7 +126,6 @@ def test_runfsl():
with pytest.raises(run.FSLNotPresent):
run.runfsl('fslhd image')
# FSLDIR/bin exists - should be good
fsldir = op.abspath('./fsl')
fslhd = op.join(fsldir, 'bin', 'fslhd')
......@@ -138,3 +140,43 @@ def test_runfsl():
assert run.runfsl('fslhd image').strip() == 'image'
finally:
fslplatform.fsldir = old_fsldir
mock_fsl_sub = textwrap.dedent("""
#!/usr/bin/env bash
jid=12345
cmd=$1
name=`basename $cmd`
$cmd > "$name".o"$jid"
touch "$name".e"$jid"
echo $jid
exit 0
""").strip()
def test_run_submit():
def mkexec(path, contents):
with open(path, 'wt') as f:
f.write(contents)
os.chmod(path, 0o755)
test_script = textwrap.dedent("""
#!/usr/bin/env bash
echo test_script running
exit 0
""").strip()
with tempdir.tempdir(), mockFSLDIR():
mkexec(op.expandvars('$FSLDIR/bin/fsltest'), test_script)
mkexec(op.expandvars('$FSLDIR/bin/fsl_sub'), mock_fsl_sub)
jid = run.run('fsltest', submit=True)[0]
assert jid == '12345'
stdout, stderr = fslsub.output(jid, 'fsltest')
assert stdout.strip() == 'test_script running'
assert stderr.strip() == ''
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