Skip to content

Commit 484da5d

Browse files
committed
fix terminal_output tests
1 parent e269e74 commit 484da5d

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed

nipype/interfaces/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
PY35 = sys.version_info >= (3, 5)
5252
PY3 = sys.version_info[0] > 2
5353
VALID_TERMINAL_OUTPUT = ['stream', 'allatonce', 'file', 'file_split',
54-
'file_stdout', 'file_stderr', 'discard']
54+
'file_stdout', 'file_stderr', 'none']
5555
__docformat__ = 'restructuredtext'
5656

5757

nipype/interfaces/tests/test_base.py

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
from __future__ import print_function, unicode_literals
55
from future import standard_library
6-
standard_library.install_aliases()
7-
8-
from builtins import open, str, bytes
6+
from builtins import open, str
97
import os
108
import warnings
119
import simplejson as json
1210

1311
import pytest
12+
import traits.api as traits
1413
from nipype.testing import example_data
15-
1614
import nipype.interfaces.base as nib
1715
from nipype.utils.filemanip import split_filename
1816
from nipype.interfaces.base import Undefined, config
19-
import traits.api as traits
17+
standard_library.install_aliases()
18+
2019

2120
@pytest.mark.parametrize("args", [
22-
{},
23-
{'a' : 1, 'b' : [2, 3]}
21+
{},
22+
{'a': 1, 'b': [2, 3]}
2423
])
2524
def test_bunch(args):
2625
b = nib.Bunch(**args)
@@ -31,7 +30,7 @@ def test_bunch_attribute():
3130
b = nib.Bunch(a=1, b=[2, 3], c=None)
3231
assert b.a == 1
3332
assert b.b == [2, 3]
34-
assert b.c == None
33+
assert b.c is None
3534

3635

3736
def test_bunch_repr():
@@ -66,7 +65,7 @@ def test_bunch_hash():
6665
with open(json_pth, 'r') as fp:
6766
jshash.update(fp.read().encode('utf-8'))
6867
assert newbdict['infile'][0][1] == jshash.hexdigest()
69-
assert newbdict['yat'] == True
68+
assert newbdict['yat'] is True
7069

7170

7271
@pytest.fixture(scope="module")
@@ -654,49 +653,88 @@ def test_Commandline_environ():
654653
assert res.runtime.environ['DISPLAY'] == ':2'
655654

656655

657-
def test_CommandLine_output(setup_file):
658-
tmp_infile = setup_file
659-
tmpd, name = os.path.split(tmp_infile)
660-
assert os.path.exists(tmp_infile)
656+
def test_CommandLine_output(tmpdir):
657+
# Create a file
658+
name = 'foo.txt'
659+
tmpdir.chdir()
660+
tmpdir.join(name).write('foo')
661+
661662
ci = nib.CommandLine(command='ls -l')
662663
ci.terminal_output = 'allatonce'
663664
res = ci.run()
664665
assert res.runtime.merged == ''
665666
assert name in res.runtime.stdout
667+
668+
# Check stdout is written
666669
ci = nib.CommandLine(command='ls -l')
667-
ci.terminal_output = 'file'
670+
ci.terminal_output = 'file_stdout'
668671
res = ci.run()
669-
assert 'stdout.nipype' in res.runtime.stdout
670-
assert isinstance(res.runtime.stdout, (str, bytes))
672+
assert os.path.isfile('stdout.nipype')
673+
assert name in res.runtime.stdout
674+
tmpdir.join('stdout.nipype').remove(ignore_errors=True)
675+
676+
# Check stderr is written
677+
ci = nib.CommandLine(command='ls -l')
678+
ci.terminal_output = 'file_stderr'
679+
res = ci.run()
680+
assert os.path.isfile('stderr.nipype')
681+
tmpdir.join('stderr.nipype').remove(ignore_errors=True)
682+
683+
# Check outputs are thrown away
671684
ci = nib.CommandLine(command='ls -l')
672685
ci.terminal_output = 'none'
673686
res = ci.run()
674-
assert res.runtime.stdout == ''
687+
assert res.runtime.stdout == '' and \
688+
res.runtime.stderr == '' and \
689+
res.runtime.merged == ''
690+
691+
# Check that new interfaces are set to default 'stream'
675692
ci = nib.CommandLine(command='ls -l')
676693
res = ci.run()
677-
assert 'stdout.nipype' in res.runtime.stdout
694+
assert ci.terminal_output == 'stream'
695+
assert name in res.runtime.stdout and \
696+
res.runtime.stderr == ''
678697

698+
# Check only one file is generated
699+
ci = nib.CommandLine(command='ls -l')
700+
ci.terminal_output = 'file'
701+
res = ci.run()
702+
assert os.path.isfile('output.nipype')
703+
assert name in res.runtime.merged and \
704+
res.runtime.stdout == '' and \
705+
res.runtime.stderr == ''
706+
tmpdir.join('output.nipype').remove(ignore_errors=True)
679707

680-
def test_global_CommandLine_output(setup_file):
681-
tmp_infile = setup_file
682-
tmpd, name = os.path.split(tmp_infile)
708+
# Check split files are generated
683709
ci = nib.CommandLine(command='ls -l')
710+
ci.terminal_output = 'file_split'
684711
res = ci.run()
712+
assert os.path.isfile('stdout.nipype')
713+
assert os.path.isfile('stderr.nipype')
685714
assert name in res.runtime.stdout
686-
assert os.path.exists(tmp_infile)
715+
716+
717+
def test_global_CommandLine_output(tmpdir):
718+
"""Ensures CommandLine.set_default_terminal_output works"""
719+
from nipype.interfaces.fsl import BET
720+
721+
ci = nib.CommandLine(command='ls -l')
722+
assert ci.terminal_output == 'stream' # default case
723+
724+
ci = BET()
725+
assert ci.terminal_output == 'stream' # default case
726+
687727
nib.CommandLine.set_default_terminal_output('allatonce')
688728
ci = nib.CommandLine(command='ls -l')
689-
res = ci.run()
690-
assert res.runtime.merged == ''
691-
assert name in res.runtime.stdout
729+
assert ci.terminal_output == 'allatonce'
730+
692731
nib.CommandLine.set_default_terminal_output('file')
693732
ci = nib.CommandLine(command='ls -l')
694-
res = ci.run()
695-
assert 'stdout.nipype' in res.runtime.stdout
696-
nib.CommandLine.set_default_terminal_output('none')
697-
ci = nib.CommandLine(command='ls -l')
698-
res = ci.run()
699-
assert res.runtime.stdout == ''
733+
assert ci.terminal_output == 'file'
734+
735+
# Check default affects derived interfaces
736+
ci = BET()
737+
assert ci.terminal_output == 'file'
700738

701739

702740
def check_dict(ref_dict, tst_dict):

0 commit comments

Comments
 (0)