Skip to content

Commit

Permalink
Improve error message of commands consisting of multiple arguments
Browse files Browse the repository at this point in the history
Commands like `git diff`, `verdi status` or `python script.py` will fail
with different opaque error messages. We add a check that the command
consists only out of one argument.
  • Loading branch information
agoscinski committed Jul 25, 2024
1 parent 2ab8219 commit ffcf067
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/aiida_shell/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ def prepare_code(command: str, computer: Computer | None = None, resolve_command
:return: A :class:`aiida.orm.nodes.code.abstract.AbstractCode` instance.
:raises ValueError: If ``resolve_command=True`` and the code fails to determine the absolute path of the command.
"""
splitted_command = command.strip().split(' ')
if len(splitted_command) > 1:
raise ValueError(
f'Command should only consist of one argument containing the executable '
f'but found {len(splitted_command)} arguments {splitted_command}. '
'Please add any further arguments to the `arguments` parameter in `launch_shell_job`.'
)

computer = prepare_computer(computer)
code_label = f'{command}@{computer.label}'

Expand All @@ -130,7 +138,6 @@ def prepare_code(command: str, computer: Computer | None = None, resolve_command
with computer.get_transport() as transport:
status, stdout, stderr = transport.exec_command_wait(f'which {command}')
executable = stdout.strip()

if status != 0:
raise ValueError(
f'failed to determine the absolute path of the command on the computer: {stderr}'
Expand Down
9 changes: 9 additions & 0 deletions tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def test_error_command_failed():
assert node.exit_status == ShellJob.exit_codes.ERROR_COMMAND_FAILED.status


def test_multi_command_raise():
"""Test that the shellfunction process fails if the command consists of more than one argument.
Running ``git diff`` raises an error message.
"""
with pytest.raises(ValueError, match=r'.* Command should only consist of one argument containing .*'):
launch_shell_job('git diff')


def test_default():
"""Test :func:`aiida_shell.launch_shell_job` with default arguments."""
results, node = launch_shell_job('date')
Expand Down

0 comments on commit ffcf067

Please sign in to comment.