diff --git a/src/aiida_shell/launch.py b/src/aiida_shell/launch.py index f59212b..edf5d68 100644 --- a/src/aiida_shell/launch.py +++ b/src/aiida_shell/launch.py @@ -124,17 +124,18 @@ def prepare_shell_job_inputs( # noqa: PLR0913 :returns: A dictionary containing prepared inputs for the ShellJob. """ metadata = metadata or {} - computer = metadata.get('options', {}).pop('computer', None) + options = metadata.get('options', {}) - if computer: + if 'computer' in options: warnings.warn( 'Specifying a computer through `metadata.options.computer` in `launch_shell_job` is deprecated. Please use ' '`metadata.computer` instead.', AiidaDeprecationWarning, stacklevel=2, ) - else: - computer = metadata.pop('computer', None) + metadata['computer'] = options.pop('computer') + + computer = metadata.get('computer') if isinstance(command, str): code = prepare_code(command, computer, resolve_command) @@ -154,7 +155,7 @@ def prepare_shell_job_inputs( # noqa: PLR0913 'arguments': arguments, 'outputs': outputs, 'parser': parser, - 'metadata': metadata or {}, + 'metadata': metadata, } if monitors: inputs['monitors'] = monitors diff --git a/tests/test_launch.py b/tests/test_launch.py index fda8ba1..a89f999 100644 --- a/tests/test_launch.py +++ b/tests/test_launch.py @@ -7,14 +7,18 @@ import pytest from aiida.engine import WorkChain, run_get_node, workfunction -from aiida.orm import AbstractCode, Computer, Float, Int, RemoteData, SinglefileData, Str +from aiida.orm import AbstractCode, Computer, Float, InstalledCode, Int, PortableCode, RemoteData, SinglefileData, Str +from aiida_shell import ShellCode from aiida_shell.calculations.shell import ShellJob from aiida_shell.launch import launch_shell_job, prepare_computer DATE_COMMAND = shutil.which('date') assert DATE_COMMAND is not None, 'The `date` command must be available in order to run the tests.' +ECHO_COMMAND = shutil.which('echo') +assert ECHO_COMMAND is not None, 'The `echo` command must be available in order to run the tests.' + class ShellWorkChain(WorkChain): """Implementation of :class:`aiida.engine.processes.workchains.workchain.WorkChain` that submits a ``ShellJob``.""" @@ -375,15 +379,29 @@ def test_preexisting_localhost_no_default_mpiprocs_per_machine( Computer.collection.delete(computer.pk) -def test_metadata_computer(generate_computer): - """Test the ``metadata.computer`` input.""" - label = 'custom-computer' - computer = generate_computer(label=label) - assert computer.label == label +@pytest.mark.parametrize('code_type', ('string', 'shell', 'installed', 'portable')) +def test_metadata_computer(code_type, generate_computer, tmp_path): + """Test that ``metadata.computer`` is respected for different code types.""" + computer = generate_computer(label=f'computer-{code_type}') + + if code_type == 'string': + command = 'echo' + elif code_type == 'shell': + command = ShellCode( + computer=computer, filepath_executable=ECHO_COMMAND, default_calc_job_plugin='core.shell' + ).store() + elif code_type == 'installed': + command = InstalledCode(computer=computer, filepath_executable=ECHO_COMMAND).store() + else: + filepath_executable = tmp_path / 'echo.sh' + filepath_executable.write_text('#!/bin/bash\necho "$@"\n') + filepath_executable.chmod(0o755) + command = PortableCode(filepath_executable='echo.sh', filepath_files=tmp_path).store() - _, node = launch_shell_job('date', metadata={'computer': computer}) + results, node = launch_shell_job(command, arguments=['hello'], metadata={'computer': computer}) assert node.is_finished_ok - assert node.inputs.code.computer.uuid == computer.uuid + assert results['stdout'].get_content().strip() == 'hello' + assert node.computer.uuid == computer.uuid def test_monitors():