Commit 58b22bfb authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Test run(tee=True)

parent 8bd3ea7b
......@@ -67,6 +67,9 @@ class CaptureStdout(object):
def reset(self):
self.__mock_stdout = StringIO('')
self.__mock_stderr = StringIO('')
self.__mock_stdout.mode = 'w'
self.__mock_stderr.mode = 'w'
return self
def __enter__(self):
self.__real_stdout = sys.stdout
......
......@@ -23,7 +23,7 @@ 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, mockFSLDIR
from . import make_random_image, mockFSLDIR, CaptureStdout
def test_run():
......@@ -43,31 +43,30 @@ def test_run():
f.write(test_script.format(0))
os.chmod('script.sh', 0o755)
expstdout = "standard output - arguments: 1 2 3"
expstderr = "standard error"
expstdout = "standard output - arguments: 1 2 3\n"
expstderr = "standard error\n"
# test:
# - single string
# - packed sequence
# - unpacked sequence
assert run.run('./script.sh 1 2 3').strip() == expstdout
assert run.run(('./script.sh', '1', '2', '3')) == expstdout
assert run.run('./script.sh 1 2 3') == expstdout
assert run.run(('./script.sh', '1', '2', '3')) == expstdout
assert run.run(*('./script.sh', '1', '2', '3')) == expstdout
# test stdout/stderr
stdout, stderr = run.run('./script.sh 1 2 3', err=True)
assert stdout.strip() == expstdout
assert stderr.strip() == expstderr
assert stdout == expstdout
assert stderr == expstderr
# test return code
res = run.run('./script.sh 1 2 3', ret=True)
print(res)
stdout, ret = res
assert stdout.strip() == expstdout
assert stdout == expstdout
assert ret == 0
stdout, stderr, ret = run.run('./script.sh 1 2 3', err=True, ret=True)
assert stdout.strip() == expstdout
assert stderr.strip() == expstderr
assert stdout == expstdout
assert stderr == expstderr
assert ret == 0
# return code != 0
......@@ -79,10 +78,61 @@ def test_run():
run.run('./script.sh 1 2 3')
stdout, ret = run.run('./script.sh 1 2 3', ret=True)
assert stdout.strip() == expstdout
assert stdout == expstdout
assert ret == 255
def test_run_tee():
test_script = textwrap.dedent("""
#!/bin/bash
echo "standard output - arguments: $@"
echo "standard error" >&2
exit 0
""").strip()
with tempdir.tempdir():
with open('script.sh', 'wt') as f:
f.write(test_script)
os.chmod('script.sh', 0o755)
expstdout = "standard output - arguments: 1 2 3\n"
expstderr = "standard error\n"
capture = CaptureStdout()
with capture:
stdout = run.run('./script.sh 1 2 3', tee=True)
assert stdout == expstdout
assert capture.stdout == expstdout
with capture.reset():
stdout, stderr = run.run('./script.sh 1 2 3', err=True, tee=True)
assert stdout == expstdout
assert stderr == expstderr
assert capture.stdout == expstdout
assert capture.stderr == expstderr
with capture.reset():
stdout, stderr, ret = run.run('./script.sh 1 2 3',
err=True, ret=True, tee=True)
assert ret == 0
assert stdout == expstdout
assert stderr == expstderr
assert capture.stdout == expstdout
assert capture.stderr == expstderr
with capture.reset():
stdout, ret = run.run('./script.sh 1 2 3', ret=True, tee=True)
assert ret == 0
assert stdout == expstdout
assert capture.stdout == expstdout
def test_dryrun():
test_script = textwrap.dedent("""
......@@ -189,8 +239,8 @@ def test_run_submit():
stdout, stderr = fslsub.output(jid)
assert stdout.strip() == 'test_script running'
assert stderr.strip() == ''
assert stdout == 'test_script running\n'
assert stderr == ''
kwargs = {'name' : 'abcde', 'ram' : '4GB'}
......@@ -201,7 +251,7 @@ def test_run_submit():
stdout, stderr = fslsub.output(jid)
experr = '\n'.join(['{}: {}'.format(k, kwargs[k])
for k in sorted(kwargs.keys())])
for k in sorted(kwargs.keys())]) + '\n'
assert stdout.strip() == 'test_script running'
assert stderr.strip() == experr
assert stdout == 'test_script running\n'
assert stderr == experr
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment