def test_CommandLine_output(): tmp_infile = setup_file() tmpd, name = os.path.split(tmp_infile) pwd = os.getcwd() os.chdir(tmpd) yield assert_true, os.path.exists(tmp_infile) ci = nib.CommandLine(command='ls -l') ci.inputs.terminal_output = 'allatonce' res = ci.run() yield assert_equal, res.runtime.merged, '' yield assert_true, name in res.runtime.stdout ci = nib.CommandLine(command='ls -l') ci.inputs.terminal_output = 'file' res = ci.run() yield assert_true, 'stdout.nipype' in res.runtime.stdout yield assert_true, isinstance(res.runtime.stdout, (str, bytes)) ci = nib.CommandLine(command='ls -l') ci.inputs.terminal_output = 'none' res = ci.run() yield assert_equal, res.runtime.stdout, '' ci = nib.CommandLine(command='ls -l') res = ci.run() yield assert_true, 'stdout.nipype' in res.runtime.stdout os.chdir(pwd) teardown_file(tmpd)
def test_Commandline(): yield assert_raises, Exception, nib.CommandLine ci = nib.CommandLine(command='which') yield assert_equal, ci.cmd, 'which' yield assert_equal, ci.inputs.args, Undefined ci2 = nib.CommandLine(command='which', args='ls') yield assert_equal, ci2.cmdline, 'which ls' ci3 = nib.CommandLine(command='echo') ci3.inputs.environ = {'MYENV': 'foo'} res = ci3.run() yield assert_equal, res.runtime.environ['MYENV'], 'foo' yield assert_equal, res.outputs, None class CommandLineInputSpec1(nib.CommandLineInputSpec): foo = nib.traits.Str(argstr='%s', desc='a str') goo = nib.traits.Bool(argstr='-g', desc='a bool', position=0) hoo = nib.traits.List(argstr='-l %s', desc='a list') moo = nib.traits.List(argstr='-i %d...', desc='a repeated list', position=-1) noo = nib.traits.Int(argstr='-x %d', desc='an int') roo = nib.traits.Str(desc='not on command line') soo = nib.traits.Bool(argstr="-soo") nib.CommandLine.input_spec = CommandLineInputSpec1 ci4 = nib.CommandLine(command='cmd') ci4.inputs.foo = 'foo' ci4.inputs.goo = True ci4.inputs.hoo = ['a', 'b'] ci4.inputs.moo = [1, 2, 3] ci4.inputs.noo = 0 ci4.inputs.roo = 'hello' ci4.inputs.soo = False cmd = ci4._parse_inputs() yield assert_equal, cmd[0], '-g' yield assert_equal, cmd[-1], '-i 1 -i 2 -i 3' yield assert_true, 'hello' not in ' '.join(cmd) yield assert_true, '-soo' not in ' '.join(cmd) ci4.inputs.soo = True cmd = ci4._parse_inputs() yield assert_true, '-soo' in ' '.join(cmd) class CommandLineInputSpec2(nib.CommandLineInputSpec): foo = nib.File(argstr='%s', desc='a str', genfile=True) nib.CommandLine.input_spec = CommandLineInputSpec2 ci5 = nib.CommandLine(command='cmd') yield assert_raises, NotImplementedError, ci5._parse_inputs class DerivedClass(nib.CommandLine): input_spec = CommandLineInputSpec2 def _gen_filename(self, name): return 'filename' ci6 = DerivedClass(command='cmd') yield assert_equal, ci6._parse_inputs()[0], 'filename' nib.CommandLine.input_spec = nib.CommandLineInputSpec
def test_Commandline_environ(monkeypatch, tmpdir): from nipype import config config.set_default_config() tmpdir.chdir() monkeypatch.setitem(os.environ, 'DISPLAY', ':1') # Test environment ci3 = nib.CommandLine(command='echo') res = ci3.run() assert res.runtime.environ['DISPLAY'] == ':1' # Test display_variable option monkeypatch.delitem(os.environ, 'DISPLAY', raising=False) config.set('execution', 'display_variable', ':3') res = ci3.run() assert not 'DISPLAY' in ci3.inputs.environ assert not 'DISPLAY' in res.runtime.environ # If the interface has _redirect_x then yes, it should be set ci3._redirect_x = True res = ci3.run() assert res.runtime.environ['DISPLAY'] == ':3' # Test overwrite monkeypatch.setitem(os.environ, 'DISPLAY', ':1') ci3.inputs.environ = {'DISPLAY': ':2'} res = ci3.run() assert res.runtime.environ['DISPLAY'] == ':2'
def test_CommandLine_output(tmpdir): # Create one file tmpdir.chdir() file = tmpdir.join('foo.txt') file.write('123456\n') name = os.path.basename(file.strpath) ci = nib.CommandLine(command='ls -l') ci.terminal_output = 'allatonce' res = ci.run() assert res.runtime.merged == '' assert name in res.runtime.stdout # Check stdout is written ci = nib.CommandLine(command='ls -l') ci.terminal_output = 'file_stdout' res = ci.run() assert os.path.isfile('stdout.nipype') assert name in res.runtime.stdout tmpdir.join('stdout.nipype').remove(ignore_errors=True) # Check stderr is written ci = nib.CommandLine(command='ls -l') ci.terminal_output = 'file_stderr' res = ci.run() assert os.path.isfile('stderr.nipype') tmpdir.join('stderr.nipype').remove(ignore_errors=True) # Check outputs are thrown away ci = nib.CommandLine(command='ls -l') ci.terminal_output = 'none' res = ci.run() assert res.runtime.stdout == '' and \ res.runtime.stderr == '' and \ res.runtime.merged == '' # Check that new interfaces are set to default 'stream' ci = nib.CommandLine(command='ls -l') res = ci.run() assert ci.terminal_output == 'stream' assert name in res.runtime.stdout and \ res.runtime.stderr == '' # Check only one file is generated ci = nib.CommandLine(command='ls -l') ci.terminal_output = 'file' res = ci.run() assert os.path.isfile('output.nipype') assert name in res.runtime.merged and \ res.runtime.stdout == '' and \ res.runtime.stderr == '' tmpdir.join('output.nipype').remove(ignore_errors=True) # Check split files are generated ci = nib.CommandLine(command='ls -l') ci.terminal_output = 'file_split' res = ci.run() assert os.path.isfile('stdout.nipype') assert os.path.isfile('stderr.nipype') assert name in res.runtime.stdout
def test_global_CommandLine_output(setup_file): tmp_infile = setup_file tmpd, name = os.path.split(tmp_infile) ci = nib.CommandLine(command='ls -l') res = ci.run() assert name in res.runtime.stdout assert os.path.exists(tmp_infile) nib.CommandLine.set_default_terminal_output('allatonce') ci = nib.CommandLine(command='ls -l') res = ci.run() assert res.runtime.merged == '' assert name in res.runtime.stdout nib.CommandLine.set_default_terminal_output('file') ci = nib.CommandLine(command='ls -l') res = ci.run() assert 'stdout.nipype' in res.runtime.stdout nib.CommandLine.set_default_terminal_output('none') ci = nib.CommandLine(command='ls -l') res = ci.run() assert res.runtime.stdout == ''
def test_global_CommandLine_output(tmpdir): """Ensures CommandLine.set_default_terminal_output works""" from nipype.interfaces.fsl import BET ci = nib.CommandLine(command='ls -l') assert ci.terminal_output == 'stream' # default case ci = BET() assert ci.terminal_output == 'stream' # default case nib.CommandLine.set_default_terminal_output('allatonce') ci = nib.CommandLine(command='ls -l') assert ci.terminal_output == 'allatonce' nib.CommandLine.set_default_terminal_output('file') ci = nib.CommandLine(command='ls -l') assert ci.terminal_output == 'file' # Check default affects derived interfaces ci = BET() assert ci.terminal_output == 'file'
def test_CommandLine_output(setup_file): tmp_infile = setup_file tmpd, name = os.path.split(tmp_infile) assert os.path.exists(tmp_infile) ci = nib.CommandLine(command='ls -l') ci.inputs.terminal_output = 'allatonce' res = ci.run() assert res.runtime.merged == '' assert name in res.runtime.stdout ci = nib.CommandLine(command='ls -l') ci.inputs.terminal_output = 'file' res = ci.run() assert 'stdout.nipype' in res.runtime.stdout assert isinstance(res.runtime.stdout, (str, bytes)) ci = nib.CommandLine(command='ls -l') ci.inputs.terminal_output = 'none' res = ci.run() assert res.runtime.stdout == '' ci = nib.CommandLine(command='ls -l') res = ci.run() assert 'stdout.nipype' in res.runtime.stdout
def test_Commandline_environ(): from nipype import config config.set_default_config() ci3 = nib.CommandLine(command='echo') res = ci3.run() yield assert_equal, res.runtime.environ['DISPLAY'], ':1' config.set('execution', 'display_variable', ':3') res = ci3.run() yield assert_false, 'DISPLAY' in ci3.inputs.environ yield assert_equal, res.runtime.environ['DISPLAY'], ':3' ci3.inputs.environ = {'DISPLAY': ':2'} res = ci3.run() yield assert_equal, res.runtime.environ['DISPLAY'], ':2'
def test_global_CommandLine_output(): tmp_infile = setup_file() tmpd, name = os.path.split(tmp_infile) pwd = os.getcwd() os.chdir(tmpd) ci = nib.CommandLine(command='ls -l') res = ci.run() yield assert_true, name in res.runtime.stdout yield assert_true, os.path.exists(tmp_infile) nib.CommandLine.set_default_terminal_output('allatonce') ci = nib.CommandLine(command='ls -l') res = ci.run() yield assert_equal, res.runtime.merged, '' yield assert_true, name in res.runtime.stdout nib.CommandLine.set_default_terminal_output('file') ci = nib.CommandLine(command='ls -l') res = ci.run() yield assert_true, 'stdout.nipype' in res.runtime.stdout nib.CommandLine.set_default_terminal_output('none') ci = nib.CommandLine(command='ls -l') res = ci.run() yield assert_equal, res.runtime.stdout, '' os.chdir(pwd) teardown_file(tmpd)