Skip to content

Commit d40cc48

Browse files
author
Ben Cipollini
committed
TST: test whether MATLAB interface creates a script file or not.
1 parent 8ce1c85 commit d40cc48

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

nipype/interfaces/matlab.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class MatlabCommand(CommandLine):
6666
"""Interface that runs matlab code
6767
6868
>>> import nipype.interfaces.matlab as matlab
69-
>>> mlab = matlab.MatlabCommand()
69+
>>> mlab = matlab.MatlabCommand(mfile=False) # don't write script file
7070
>>> mlab.inputs.script = "which('who')"
71-
>>> out = mlab.run() # doctest: +SKIP
71+
>>> out = mlab.run() # doctest: +SKIP
7272
"""
7373

7474
_cmd = 'matlab'
@@ -156,6 +156,7 @@ def _format_arg(self, name, trait_spec, value):
156156
return super(MatlabCommand, self)._format_arg(name, trait_spec, value)
157157

158158
def _gen_matlab_command(self, argstr, script_lines):
159+
""" Generates commands and, if mfile specified, writes it to disk."""
159160
cwd = os.getcwd()
160161
mfile = self.inputs.mfile or self.inputs.uses_mcr
161162
paths = []

nipype/interfaces/tests/test_matlab.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
mlab.MatlabCommand.set_default_matlab_cmd(matlab_cmd)
1515

1616

17+
def clean_workspace_and_get_default_script_file():
18+
# Make sure things are clean.
19+
default_script_file = mlab.MatlabInputSpec().script_file
20+
if os.path.exists(default_script_file):
21+
os.remove(default_script_file) # raise Exception('Default script file needed for tests; please remove %s!' % default_script_file)
22+
return default_script_file
23+
1724
@skipif(no_matlab)
1825
def test_cmdline():
19-
basedir = mkdtemp()
26+
default_script_file = clean_workspace_and_get_default_script_file()
27+
2028
mi = mlab.MatlabCommand(script='whos',
2129
script_file='testscript', mfile=False)
2230

@@ -31,25 +39,27 @@ def test_cmdline():
3139

3240
yield assert_equal, mi.inputs.script, 'whos'
3341
yield assert_equal, mi.inputs.script_file, 'testscript'
34-
path_exists = os.path.exists(os.path.join(basedir, 'testscript.m'))
35-
yield assert_false, path_exists
36-
rmtree(basedir)
42+
yield assert_false, os.path.exists(mi.inputs.script_file), 'scriptfile should not exist'
43+
yield assert_false, os.path.exists(default_script_file), 'default scriptfile should not exist.'
3744

3845

3946
@skipif(no_matlab)
4047
def test_mlab_inputspec():
48+
default_script_file = clean_workspace_and_get_default_script_file()
4149
spec = mlab.MatlabInputSpec()
4250
for k in ['paths', 'script', 'nosplash', 'mfile', 'logfile', 'script_file',
4351
'nodesktop']:
4452
yield assert_true, k in spec.copyable_trait_names()
4553
yield assert_true, spec.nodesktop
4654
yield assert_true, spec.nosplash
4755
yield assert_true, spec.mfile
48-
yield assert_equal, spec.script_file, 'pyscript.m'
56+
yield assert_equal, spec.script_file, default_script_file
4957

5058

5159
@skipif(no_matlab)
5260
def test_mlab_init():
61+
default_script_file = clean_workspace_and_get_default_script_file()
62+
5363
yield assert_equal, mlab.MatlabCommand._cmd, 'matlab'
5464
yield assert_equal, mlab.MatlabCommand.input_spec, mlab.MatlabInputSpec
5565

@@ -60,26 +70,48 @@ def test_mlab_init():
6070

6171
@skipif(no_matlab)
6272
def test_run_interface():
73+
default_script_file = clean_workspace_and_get_default_script_file()
74+
6375
mc = mlab.MatlabCommand(matlab_cmd='foo_m')
76+
yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 1.'
6477
yield assert_raises, ValueError, mc.run # script is mandatory
78+
yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 2.'
79+
if os.path.exists(default_script_file): # cleanup
80+
os.remove(default_script_file)
81+
6582
mc.inputs.script = 'a=1;'
83+
yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 3.'
6684
yield assert_raises, IOError, mc.run # foo_m is not an executable
85+
yield assert_true, os.path.exists(default_script_file), 'scriptfile should exist 3.'
86+
if os.path.exists(default_script_file): # cleanup
87+
os.remove(default_script_file)
88+
6789
cwd = os.getcwd()
6890
basedir = mkdtemp()
6991
os.chdir(basedir)
92+
7093
# bypasses ubuntu dash issue
7194
mc = mlab.MatlabCommand(script='foo;', paths=[basedir], mfile=True)
95+
yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 4.'
7296
yield assert_raises, RuntimeError, mc.run
97+
yield assert_true, os.path.exists(default_script_file), 'scriptfile should exist 4.'
98+
if os.path.exists(default_script_file): # cleanup
99+
os.remove(default_script_file)
100+
73101
# bypasses ubuntu dash issue
74102
res = mlab.MatlabCommand(script='a=1;', paths=[basedir], mfile=True).run()
75103
yield assert_equal, res.runtime.returncode, 0
104+
yield assert_true, os.path.exists(default_script_file), 'scriptfile should exist 5.'
76105
os.chdir(cwd)
77106
rmtree(basedir)
78107

79108

80109
@skipif(no_matlab)
81110
def test_set_matlabcmd():
111+
default_script_file = clean_workspace_and_get_default_script_file()
112+
82113
mi = mlab.MatlabCommand()
83114
mi.set_default_matlab_cmd('foo')
115+
yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist.'
84116
yield assert_equal, mi._default_matlab_cmd, 'foo'
85117
mi.set_default_matlab_cmd(matlab_cmd)

0 commit comments

Comments
 (0)