3
3
# vi: set ft=python sts=4 ts=4 sw=4 et:
4
4
from __future__ import print_function , unicode_literals
5
5
from future import standard_library
6
- standard_library .install_aliases ()
7
-
8
- from builtins import open , str , bytes
6
+ from builtins import open , str
9
7
import os
10
8
import warnings
11
9
import simplejson as json
12
10
13
11
import pytest
12
+ import traits .api as traits
14
13
from nipype .testing import example_data
15
-
16
14
import nipype .interfaces .base as nib
17
15
from nipype .utils .filemanip import split_filename
18
16
from nipype .interfaces .base import Undefined , config
19
- import traits .api as traits
17
+ standard_library .install_aliases ()
18
+
20
19
21
20
@pytest .mark .parametrize ("args" , [
22
- {},
23
- {'a' : 1 , 'b' : [2 , 3 ]}
21
+ {},
22
+ {'a' : 1 , 'b' : [2 , 3 ]}
24
23
])
25
24
def test_bunch (args ):
26
25
b = nib .Bunch (** args )
@@ -31,7 +30,7 @@ def test_bunch_attribute():
31
30
b = nib .Bunch (a = 1 , b = [2 , 3 ], c = None )
32
31
assert b .a == 1
33
32
assert b .b == [2 , 3 ]
34
- assert b .c == None
33
+ assert b .c is None
35
34
36
35
37
36
def test_bunch_repr ():
@@ -66,7 +65,7 @@ def test_bunch_hash():
66
65
with open (json_pth , 'r' ) as fp :
67
66
jshash .update (fp .read ().encode ('utf-8' ))
68
67
assert newbdict ['infile' ][0 ][1 ] == jshash .hexdigest ()
69
- assert newbdict ['yat' ] == True
68
+ assert newbdict ['yat' ] is True
70
69
71
70
72
71
@pytest .fixture (scope = "module" )
@@ -654,49 +653,88 @@ def test_Commandline_environ():
654
653
assert res .runtime .environ ['DISPLAY' ] == ':2'
655
654
656
655
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
+
661
662
ci = nib .CommandLine (command = 'ls -l' )
662
663
ci .terminal_output = 'allatonce'
663
664
res = ci .run ()
664
665
assert res .runtime .merged == ''
665
666
assert name in res .runtime .stdout
667
+
668
+ # Check stdout is written
666
669
ci = nib .CommandLine (command = 'ls -l' )
667
- ci .terminal_output = 'file '
670
+ ci .terminal_output = 'file_stdout '
668
671
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
671
684
ci = nib .CommandLine (command = 'ls -l' )
672
685
ci .terminal_output = 'none'
673
686
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'
675
692
ci = nib .CommandLine (command = 'ls -l' )
676
693
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 == ''
678
697
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 )
679
707
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
683
709
ci = nib .CommandLine (command = 'ls -l' )
710
+ ci .terminal_output = 'file_split'
684
711
res = ci .run ()
712
+ assert os .path .isfile ('stdout.nipype' )
713
+ assert os .path .isfile ('stderr.nipype' )
685
714
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
+
687
727
nib .CommandLine .set_default_terminal_output ('allatonce' )
688
728
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
+
692
731
nib .CommandLine .set_default_terminal_output ('file' )
693
732
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'
700
738
701
739
702
740
def check_dict (ref_dict , tst_dict ):
0 commit comments