Skip to content

Commit f7cb9b2

Browse files
committed
R interface tests
1 parent 2638489 commit f7cb9b2

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

nipype/interfaces/tests/test_r.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# -*- coding: utf-8 -*-
2+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
import os
5+
6+
import pytest
7+
import nipype.interfaces.r as r
8+
9+
r_cmd = r.get_r_command()
10+
no_r = r_cmd is None
11+
if not no_r:
12+
r.RCommand.set_default_r_cmd(r_cmd)
13+
14+
15+
def clean_workspace_and_get_default_script_file():
16+
# Make sure things are clean.
17+
default_script_file = r.RInputSpec().script_file
18+
if os.path.exists(default_script_file):
19+
os.remove(
20+
default_script_file
21+
) # raise Exception('Default script file needed for tests; please remove %s!' % default_script_file)
22+
return default_script_file
23+
24+
25+
@pytest.mark.skipif(no_r, reason="R is not available")
26+
def test_cmdline():
27+
default_script_file = clean_workspace_and_get_default_script_file()
28+
29+
ri = r.RCommand(script="1 + 1", script_file="testscript", rfile=False)
30+
31+
assert ri.cmdline == r_cmd + (
32+
' -e "1 + 1"'
33+
)
34+
35+
assert ri.inputs.script == "1 + 1"
36+
assert ri.inputs.script_file == "testscript"
37+
assert not os.path.exists(ri.inputs.script_file), "scriptfile should not exist"
38+
assert not os.path.exists(
39+
default_script_file
40+
), "default scriptfile should not exist."
41+
42+
@pytest.mark.skipif(no_r, reason="R is not available")
43+
def test_r_init():
44+
default_script_file = clean_workspace_and_get_default_script_file()
45+
46+
assert r.RCommand._cmd == "R"
47+
assert r.RCommand.input_spec == r.RInputSpec
48+
49+
assert r.RCommand().cmd == r_cmd
50+
rc = r.RCommand(r_cmd="foo_m")
51+
assert rc.cmd == "foo_m"
52+
53+
54+
@pytest.mark.skipif(no_r, reason="R is not available")
55+
def test_run_interface(tmpdir):
56+
default_script_file = clean_workspace_and_get_default_script_file()
57+
58+
rc = r.RCommand(r_cmd="foo_m")
59+
assert not os.path.exists(default_script_file), "scriptfile should not exist 1."
60+
with pytest.raises(ValueError):
61+
rc.run() # script is mandatory
62+
assert not os.path.exists(default_script_file), "scriptfile should not exist 2."
63+
if os.path.exists(default_script_file): # cleanup
64+
os.remove(default_script_file)
65+
66+
rc.inputs.script = "a=1;"
67+
assert not os.path.exists(default_script_file), "scriptfile should not exist 3."
68+
with pytest.raises(IOError):
69+
rc.run() # foo_m is not an executable
70+
assert os.path.exists(default_script_file), "scriptfile should exist 3."
71+
if os.path.exists(default_script_file): # cleanup
72+
os.remove(default_script_file)
73+
74+
cwd = tmpdir.chdir()
75+
76+
# bypasses ubuntu dash issue
77+
rc = r.RCommand(script="foo;", rfile=True)
78+
assert not os.path.exists(default_script_file), "scriptfile should not exist 4."
79+
with pytest.raises(RuntimeError):
80+
rc.run()
81+
assert os.path.exists(default_script_file), "scriptfile should exist 4."
82+
if os.path.exists(default_script_file): # cleanup
83+
os.remove(default_script_file)
84+
85+
# bypasses ubuntu dash issue
86+
res = r.RCommand(script="a=1;", rfile=True).run()
87+
assert res.runtime.returncode == 0
88+
assert os.path.exists(default_script_file), "scriptfile should exist 5."
89+
cwd.chdir()
90+
91+
92+
@pytest.mark.skipif(no_r, reason="R is not available")
93+
def test_set_rcmd():
94+
default_script_file = clean_workspace_and_get_default_script_file()
95+
96+
ri = r.RCommand()
97+
ri.set_default_r_cmd("foo")
98+
assert not os.path.exists(default_script_file), "scriptfile should not exist."
99+
assert ri._default_r_cmd == "foo"
100+
ri.set_default_r_cmd(r_cmd)

0 commit comments

Comments
 (0)