diff --git a/tests/__init__.py b/tests/__init__.py index 723ed0f1d530b0923bbae7f7955999d1760b308c..57794123f0bbb3aa3ee448f742039781015cbb49 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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 diff --git a/tests/test_run.py b/tests/test_run.py index 5e390462268837deb9ad3183802bf7e95ff92731..849fff60e40e7830b7a4e9b85406389024eec7e2 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -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() == ''