From ffcf067d69f31276dcb91c4cc9814fd31c731e8a Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Thu, 25 Jul 2024 12:11:40 +0200 Subject: [PATCH] Improve error message of commands consisting of multiple arguments 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. --- src/aiida_shell/launch.py | 9 ++++++++- tests/test_launch.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/aiida_shell/launch.py b/src/aiida_shell/launch.py index e47a325..54ad924 100644 --- a/src/aiida_shell/launch.py +++ b/src/aiida_shell/launch.py @@ -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}' @@ -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}' diff --git a/tests/test_launch.py b/tests/test_launch.py index 3e04325..567717d 100644 --- a/tests/test_launch.py +++ b/tests/test_launch.py @@ -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')