Skip to content

Commit

Permalink
CLI: Catch NotImplementedError in verdi calcjob gotocomputer (aii…
Browse files Browse the repository at this point in the history
…dateam#6525)

Not all transport plugins implement the `gotocomputer` method.
Instead of excepting, the remote working directory is now displayed.
  • Loading branch information
khsrali authored and mikibonacci committed Sep 3, 2024
1 parent d342c75 commit 3a363db
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/aiida/cmdline/commands/cmd_calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ def calcjob_gotocomputer(calcjob):
if not remote_workdir:
echo.echo_critical('no remote work directory for this calcjob, maybe the daemon did not submit it yet')

command = transport.gotocomputer_command(remote_workdir)
echo.echo_report('going to the remote work directory...')
os.system(command)
try:
command = transport.gotocomputer_command(remote_workdir)
echo.echo_report('going to the remote work directory...')
os.system(command)
except NotImplementedError:
echo.echo_report(f'gotocomputer is not implemented for {transport}')
echo.echo_report(f'remote work directory is {remote_workdir}')


@verdi_calcjob.command('res')
Expand Down
41 changes: 41 additions & 0 deletions tests/cmdline/commands/test_calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,44 @@ def test_calcjob_remotecat(self):
options = [str(self.result_job.uuid), 'fileA.txt']
result = self.cli_runner.invoke(command.calcjob_remotecat, options)
assert result.stdout == 'test stringA'

def test_calcjob_gotocomputer(self):
"""Test verdi calcjob gotocomputer"""

from unittest.mock import patch

from aiida.common.exceptions import NotExistent

options = [str(self.result_job.uuid)]

# Easy peasy no exception
with patch('os.system') as mock_os_system:
result = self.cli_runner.invoke(command.calcjob_gotocomputer, options)
mock_os_system.assert_called_once()
assert mock_os_system.call_args[0][0] is not None

def raise_(e):
raise e('something')

# Test when get_transport raises NotExistent
with patch(
'aiida.orm.nodes.process.calculation.calcjob.CalcJobNode.get_transport', new=lambda _: raise_(NotExistent)
):
result = self.cli_runner.invoke(command.calcjob_gotocomputer, options)
assert result.exit_code == 1
assert 'something' in result.output

# Test when get_remote_workdir returns None
with patch('aiida.orm.nodes.process.calculation.calcjob.CalcJobNode.get_remote_workdir', new=lambda _: None):
result = self.cli_runner.invoke(command.calcjob_gotocomputer, options)
assert result.exit_code == 1
assert 'no remote work directory for this calcjob' in result.output

# Test when gotocomputer_command raises NotImplementedError
with patch(
'aiida.transports.plugins.local.LocalTransport.gotocomputer_command',
new=lambda _, __: raise_(NotImplementedError),
):
result = self.cli_runner.invoke(command.calcjob_gotocomputer, options)
assert result.exit_code == 0
assert self.result_job.get_remote_workdir() in result.output

0 comments on commit 3a363db

Please sign in to comment.