7
7
from tempfile import mkdtemp
8
8
from shutil import rmtree
9
9
10
- from ...testing import (assert_equal , assert_true ,
11
- assert_false )
10
+ from ...testing import (assert_equal , assert_true , assert_false )
12
11
import nipype .pipeline .engine as pe
13
12
import nipype .interfaces .base as nib
14
13
import nipype .interfaces .utility as niu
15
14
from ... import config
16
- from ..utils import merge_dict
17
-
15
+ from ..utils import merge_dict , clean_working_directory
18
16
19
17
def test_identitynode_removal ():
20
18
@@ -46,6 +44,48 @@ def test_function(arg1, arg2, arg3):
46
44
yield assert_equal , len (eg .nodes ()), 8
47
45
48
46
47
+ def test_clean_working_directory ():
48
+ class OutputSpec (nib .TraitedSpec ):
49
+ files = nib .traits .List (nib .File )
50
+ others = nib .File ()
51
+ class InputSpec (nib .TraitedSpec ):
52
+ infile = nib .File ()
53
+ outputs = OutputSpec ()
54
+ inputs = InputSpec ()
55
+
56
+ wd = mkdtemp ()
57
+ filenames = ['file.hdr' , 'file.img' , 'file.BRIK' , 'file.HEAD' ,
58
+ '_0x1234.json' , 'foo.txt' ]
59
+ outfiles = []
60
+ for filename in filenames :
61
+ outfile = os .path .join (wd , filename )
62
+ with open (outfile , 'wt' ) as fp :
63
+ fp .writelines ('dummy' )
64
+ outfiles .append (outfile )
65
+ outputs .files = outfiles [:4 :2 ]
66
+ outputs .others = outfiles [5 ]
67
+ inputs .infile = outfiles [- 1 ]
68
+ needed_outputs = ['files' ]
69
+ config .set_default_config ()
70
+ yield assert_true , os .path .exists (outfiles [5 ])
71
+ config .set_default_config ()
72
+ config .set ('execution' , 'remove_unnecessary_outputs' , False )
73
+ out = clean_working_directory (outputs , wd , inputs , needed_outputs ,
74
+ deepcopy (config ._sections ))
75
+ yield assert_true , os .path .exists (outfiles [5 ])
76
+ yield assert_equal , out .others , outfiles [5 ]
77
+ config .set ('execution' , 'remove_unnecessary_outputs' , True )
78
+ out = clean_working_directory (outputs , wd , inputs , needed_outputs ,
79
+ deepcopy (config ._sections ))
80
+ yield assert_true , os .path .exists (outfiles [1 ])
81
+ yield assert_true , os .path .exists (outfiles [3 ])
82
+ yield assert_true , os .path .exists (outfiles [4 ])
83
+ yield assert_false , os .path .exists (outfiles [5 ])
84
+ yield assert_equal , out .others , nib .Undefined
85
+ yield assert_equal , len (out .files ), 2
86
+ config .set_default_config ()
87
+ rmtree (wd )
88
+
49
89
def test_outputs_removal ():
50
90
51
91
def test_function (arg1 ):
@@ -140,19 +180,12 @@ def test_function(arg1):
140
180
file1 = os .path .join (os .getcwd (), 'file1.txt' )
141
181
file2 = os .path .join (os .getcwd (), 'file2.txt' )
142
182
file3 = os .path .join (os .getcwd (), 'file3.txt' )
143
- fp = open (file1 , 'wt' )
144
- fp .write ('%d' % arg1 )
145
- fp .close ()
146
- fp = open (file2 , 'wt' )
147
- fp .write ('%d' % arg1 )
148
- fp .close ()
149
- fp = open (file3 , 'wt' )
150
- fp .write ('%d' % arg1 )
151
- fp .close ()
183
+ file4 = os .path .join (os .getcwd (), 'subdir' , 'file1.txt' )
184
+ files = [file1 , file2 , file3 , file4 ]
152
185
os .mkdir ("subdir" )
153
- fp = open ( "subdir/file1.txt" , 'wt' )
154
- fp . write ( '%d' % arg1 )
155
- fp .close ( )
186
+ for filename in files :
187
+ with open ( filename , 'wt' ) as fp :
188
+ fp .write ( '%d' % arg1 )
156
189
return file1 , file2 , os .path .join (os .getcwd (),"subdir" )
157
190
158
191
def test_function2 (in_file , arg ):
@@ -161,15 +194,10 @@ def test_function2(in_file, arg):
161
194
file1 = os .path .join (os .getcwd (), 'file1.txt' )
162
195
file2 = os .path .join (os .getcwd (), 'file2.txt' )
163
196
file3 = os .path .join (os .getcwd (), 'file3.txt' )
164
- fp = open (file1 , 'wt' )
165
- fp .write ('%d' % arg + in_arg )
166
- fp .close ()
167
- fp = open (file2 , 'wt' )
168
- fp .write ('%d' % arg + in_arg )
169
- fp .close ()
170
- fp = open (file3 , 'wt' )
171
- fp .write ('%d' % arg + in_arg )
172
- fp .close ()
197
+ files = [file1 , file2 , file3 ]
198
+ for filename in files :
199
+ with open (filename , 'wt' ) as fp :
200
+ fp .write ('%d' % arg + in_arg )
173
201
return file1 , file2 , 1
174
202
175
203
def test_function3 (arg ):
0 commit comments