Skip to content

Commit

Permalink
verdi daemon status: Do not except when no profiles are defined (#5874
Browse files Browse the repository at this point in the history
)

The command assumed that at least one profile would be defined and loaded
but if this wasn't the case, for example after a clean install of a new
instance, the command would except.

A new utility decorator `requires_loaded_profile` is introduced that
checks that a profile is loaded or a critical error is printed and the
command is exited. This decorator is applied to `verdi daemon status`
to fix the issue.

Co-authored-by: Sebastiaan Huber <mail@sphuber.net>
  • Loading branch information
zahid47 and sphuber authored Jan 30, 2023
1 parent dc41653 commit ff3ae82
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions aiida/cmdline/commands/cmd_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def start(foreground, number):

@verdi_daemon.command()
@click.option('--all', 'all_profiles', is_flag=True, help='Show status of all daemons.')
@decorators.requires_loaded_profile()
def status(all_profiles):
"""Print the status of the current daemon or all daemons.
Expand Down
26 changes: 26 additions & 0 deletions aiida/cmdline/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,29 @@ def wrapper(wrapped, _, args, kwargs):
return wrapped(*args, **kwargs)

return wrapper


def requires_loaded_profile():
"""Function decorator for CLI command that requires a profile to be loaded.
Example::
@requires_loaded_profile()
def create_node():
pass
If no profile has been loaded, the command will exit with a critical error. Most ``verdi`` commands will
automatically load the default profile. So if this error is hit, it is most likely that either no profile have been
defined at all or the default is unspecified.
"""

@decorator
def wrapper(wrapped, _, args, kwargs):
from aiida.manage.configuration import get_profile

if get_profile() is None:
echo.echo_critical('No profile loaded: make sure at least one profile is configured and a default is set.')

return wrapped(*args, **kwargs)

return wrapper
7 changes: 7 additions & 0 deletions tests/cmdline/commands/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,10 @@ def test_daemon_status(run_cli_command):

assert f'Profile: {get_profile().name}' in result.output
assert last_line == 'Use verdi daemon [incr | decr] [num] to increase / decrease the amount of workers'


@pytest.mark.usefixtures('empty_config')
def test_daemon_status_no_profile(run_cli_command):
"""Test ``verdi daemon status`` when no profiles are defined."""
result = run_cli_command(cmd_daemon.status, raises=True)
assert 'No profile loaded: make sure at least one profile is configured and a default is set.' in result.output

0 comments on commit ff3ae82

Please sign in to comment.