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

verdi config list: Do not except if no profiles are defined #5921

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aiida/cmdline/commands/cmd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def verdi_config_list(ctx, prefix, description: bool):
from aiida.manage.configuration import Config, Profile

config: Config = ctx.obj.config
profile: Profile = ctx.obj.profile
profile: Profile = ctx.obj.get('profile', None)

if not profile:
echo.echo_warning('no profiles configured: run `verdi setup` to create one')
Expand Down
8 changes: 8 additions & 0 deletions tests/cmdline/commands/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Tests for ``verdi config``."""
import pytest

from aiida import get_profile
from aiida.cmdline.commands import cmd_verdi


@pytest.mark.usefixtures('empty_config')
def test_config_list_no_profile(run_cli_command):
"""Test the `verdi config list` command when no profile is present in the config, it should not except."""
run_cli_command(cmd_verdi.verdi, ['config', 'list'], initialize_ctx_obj=False)


def test_config_set_option_no_profile(run_cli_command, empty_config):
"""Test the `verdi config set` command when no profile is present in the config."""
config = empty_config
Expand Down
20 changes: 15 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ def factory(
raises: bool = False,
use_subprocess: bool = False,
suppress_warnings: bool = False,
initialize_ctx_obj: bool = True,
**kwargs
) -> CliResult:
"""Run the command and check the result.
Expand All @@ -513,6 +514,12 @@ def factory(
on the output is very strict. By running in a sub process, any warnings that are emitted by the code will be
shown since they have not already been hit as would be the case when running the test through the test
runner in this interpreter.
:param initialize_ctx_obj: Boolean, if ``True``, the custom ``obj`` attribute of the ``ctx`` is initialized when
``use_subprocess == False``. When invoking the ``verdi`` command from the command line (and when running
tests with ``use_subprocess == True``), this is done by the ``VerdiContext``, but when using the test runner
in this interpreter, the object has to be initialized manually and passed to the test runner. In certain
cases, however, this initialization should not be done, to simulate for example the absence of a loaded
profile.
:returns: Instance of ``CliResult``.
:raises AssertionError: If ``raises == True`` and the command didn't except, or if ``raises == True`` and the
the command did except.
Expand All @@ -523,7 +530,7 @@ def factory(
if use_subprocess:
result = run_cli_command_subprocess(command, parameters, user_input, aiida_profile.name, suppress_warnings)
else:
result = run_cli_command_runner(command, parameters, user_input, kwargs)
result = run_cli_command_runner(command, parameters, user_input, initialize_ctx_obj, kwargs)

if raises:
assert result.exception is not None, result.output
Expand Down Expand Up @@ -574,16 +581,19 @@ def run_cli_command_subprocess(command, parameters, user_input, profile_name, su
return result


def run_cli_command_runner(command, parameters, user_input, kwargs):
def run_cli_command_runner(command, parameters, user_input, initialize_ctx_obj, kwargs):
"""Run CLI command through ``click.testing.CliRunner``."""
from click.testing import CliRunner

from aiida.cmdline.commands.cmd_verdi import VerdiCommandGroup
from aiida.common import AttributeDict

config = get_config()
profile = get_profile()
obj = AttributeDict({'config': config, 'profile': profile})
if initialize_ctx_obj:
config = get_config()
profile = get_profile()
obj = AttributeDict({'config': config, 'profile': profile})
else:
obj = None

# We need to apply the ``VERBOSITY`` option. When invoked through the command line, this is done by the logic of the
# ``VerdiCommandGroup``, but when testing commands, the command is retrieved directly from the module which
Expand Down