From f1855a2bec72934f1d819474906b91ff7ef229f1 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Thu, 19 Apr 2018 14:29:42 +0100 Subject: [PATCH] New unit test for run(submit=True). Not going to test fslsub right now, as it will probably change. --- tests/__init__.py | 13 +++++++++++-- tests/test_run.py | 48 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 723ed0f1d..57794123f 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 5e3904622..849fff60e 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() == '' -- GitLab