|
7 | 7 |
|
8 | 8 | from .... import config
|
9 | 9 | from ....interfaces import utility as niu
|
| 10 | +from ....interfaces import base as nib |
10 | 11 | from ... import engine as pe
|
11 | 12 | from ..utils import merge_dict
|
12 | 13 | from .test_base import EngineTestInterface
|
@@ -334,3 +335,45 @@ def _producer(num=1, deadly_num=7):
|
334 | 335 | wf.base_dir = os.path.abspath("./test_output")
|
335 | 336 | with pytest.raises(RuntimeError):
|
336 | 337 | wf.run(plugin="MultiProc")
|
| 338 | + |
| 339 | + |
| 340 | +class FailCommandLine(nib.CommandLine): |
| 341 | + input_spec = nib.CommandLineInputSpec |
| 342 | + output_spec = nib.TraitedSpec |
| 343 | + _cmd = 'nipype-node-execution-fail' |
| 344 | + |
| 345 | + |
| 346 | +def test_NodeExecutionError(tmp_path, monkeypatch): |
| 347 | + import stat |
| 348 | + |
| 349 | + monkeypatch.chdir(tmp_path) |
| 350 | + |
| 351 | + # create basic executable and add to PATH |
| 352 | + exebin = tmp_path / 'bin' |
| 353 | + exebin.mkdir() |
| 354 | + exe = exebin / 'nipype-node-execution-fail' |
| 355 | + exe.write_text('#!/bin/bash\necho "Running"\necho "This should fail" >&2\nexit 1') |
| 356 | + exe.chmod(exe.stat().st_mode | stat.S_IEXEC) |
| 357 | + monkeypatch.setenv("PATH", str(exe.parent.absolute()), prepend=os.pathsep) |
| 358 | + |
| 359 | + # Test with cmdline interface |
| 360 | + cmd = pe.Node(FailCommandLine(), name="cmd-fail", base_dir='cmd') |
| 361 | + with pytest.raises(pe.nodes.NodeExecutionError) as exc: |
| 362 | + cmd.run() |
| 363 | + error_msg = str(exc.value) |
| 364 | + |
| 365 | + for attr in ("Cmdline:", "Stdout:", "Stderr:", "Traceback:"): |
| 366 | + assert attr in error_msg |
| 367 | + assert "This should fail" in error_msg |
| 368 | + |
| 369 | + # Test with function interface |
| 370 | + def fail(): |
| 371 | + raise Exception("Functions can fail too") |
| 372 | + |
| 373 | + func = pe.Node(niu.Function(function=fail), name='func-fail', base_dir='func') |
| 374 | + with pytest.raises(pe.nodes.NodeExecutionError) as exc: |
| 375 | + func.run() |
| 376 | + error_msg = str(exc.value) |
| 377 | + assert "Traceback:" in error_msg |
| 378 | + assert "Cmdline:" not in error_msg |
| 379 | + assert "Functions can fail too" in error_msg |
0 commit comments