Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully exit if no profile exists #5874

Merged
merged 10 commits into from
Jan 30, 2023
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