From 6f86390840451247c45997b5d5a9ac0ab4028bff Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Tue, 8 Nov 2022 12:11:57 +0100 Subject: [PATCH] CLI: update to be compatible with `aiida-core==2.1` (#855) Currently the CLI is broken with `aiida-core==2.1`. The reason is that we are using the `PROFILE` option, which relies on the command that it is attached to configure certain things, such as the context. In v2.1, the behavior was changed causing the commands to except because the `PROFILE` option cannot find certain attributes it expects in the context. For the `PROFILE` option to work, the root command should use the class `aiida.cmdline.groups.VerdiCommandGroup` as its `cls`. This class defines a custom context class that is necessary. It also automatically provides the verbosity option for all subcommands, so the explicit declaration can now be removed. The problem was not detected in the unit tests because they only show up when the root command is invoked. The unit tests call the subcommands directly though, and this circumvents the root command. This is documented behavior of `click` and there is no way around it. The only solution is to call the commands through the command line interface exactly as they would be called normally. This is now done in the `test_commands.py` file. The test parametrizes over all existing (sub)commands and directly calls it as a subprocess. This guarantees that the root command is called including its specific context that needs to be setup. --- pyproject.toml | 4 +- src/aiida_quantumespresso/cli/__init__.py | 39 +----------------- tests/cli/test_commands.py | 50 ++++++++++++++++------- tests/conftest.py | 4 +- 4 files changed, 43 insertions(+), 54 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 931856c58..688d90712 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,8 +21,8 @@ classifiers = [ keywords = ['aiida', 'workflows'] requires-python = '>=3.8' dependencies = [ - 'aiida_core[atomic_tools]~=2.0,>=2.0.4', - 'aiida-pseudo~=0.7.0', + 'aiida_core[atomic_tools]~=2.1', + 'aiida-pseudo~=0.8.0', 'click~=8.0', 'importlib_resources', 'jsonschema', diff --git a/src/aiida_quantumespresso/cli/__init__.py b/src/aiida_quantumespresso/cli/__init__.py index b4838985e..758c5a8ec 100644 --- a/src/aiida_quantumespresso/cli/__init__.py +++ b/src/aiida_quantumespresso/cli/__init__.py @@ -1,48 +1,13 @@ # -*- coding: utf-8 -*- # pylint: disable=wrong-import-position """Module for the command line interface.""" +from aiida.cmdline.groups import VerdiCommandGroup from aiida.cmdline.params import options, types import click -class VerbosityGroup(click.Group): - """Custom command group that automatically adds the ``VERBOSITY`` option to all subcommands.""" - - @staticmethod - def add_verbosity_option(cmd): - """Apply the ``verbosity`` option to the command, which is common to all ``verdi`` commands.""" - if 'verbosity' not in [param.name for param in cmd.params]: - cmd = options.VERBOSITY()(cmd) - - return cmd - - def group(self, *args, **kwargs): - """Ensure that sub command groups use the same class but do not override an explicitly set value.""" - kwargs.setdefault('cls', self.__class__) - return super().group(*args, **kwargs) - - def get_command(self, ctx, cmd_name): - """Return the command that corresponds to the requested ``cmd_name``. - - This method is overridden from the base class in order to automatically add the verbosity option. - - Note that if the command is not found and ``resilient_parsing`` is set to True on the context, then the latter - feature is disabled because most likely we are operating in tab-completion mode. - """ - cmd = super().get_command(ctx, cmd_name) - - if cmd is not None: - return self.add_verbosity_option(cmd) - - if ctx.resilient_parsing: - return None - - return ctx.fail(f'`{cmd_name}` is not a {self.name} command.') - - -@click.group('aiida-quantumespresso', context_settings={'help_option_names': ['-h', '--help']}) +@click.group('aiida-quantumespresso', cls=VerdiCommandGroup, context_settings={'help_option_names': ['-h', '--help']}) @options.PROFILE(type=types.ProfileParamType(load_profile=True), expose_value=False) -@options.VERBOSITY() def cmd_root(): """CLI for the `aiida-quantumespresso` plugin.""" diff --git a/tests/cli/test_commands.py b/tests/cli/test_commands.py index 379f7d3e6..d064d86fb 100644 --- a/tests/cli/test_commands.py +++ b/tests/cli/test_commands.py @@ -1,24 +1,46 @@ # -*- coding: utf-8 -*- """Tests for CLI commands.""" -from click import Context, Group +from __future__ import annotations -from aiida_quantumespresso.cli import cmd_root +import subprocess +from aiida_pseudo.cli import cmd_root +import click +import pytest -def test_commands(): - """Test that all commands in ``cmd_root`` are reachable and can print the help message. - This doesn't guarantee that the command works but at least that it can be successfully called and there are no - import errors or other basic problems. +def recurse_commands(command: click.Command, parents: list[str] = None): + """Recursively return all subcommands that are part of ``command``. + + :param command: The click command to start with. + :param parents: A list of strings that represent the parent commands leading up to the current command. + :returns: A list of strings denoting the full path to the current command. """ + if isinstance(command, click.Group): + for command_name in command.commands: + subcommand = command.get_command(None, command_name) + if parents is not None: + subparents = parents + [command.name] + else: + subparents = [command.name] + yield from recurse_commands(subcommand, subparents) + + if parents is not None: + yield parents + [command.name] + else: + yield [command.name] - def recursively_print_help(ctx): - assert isinstance(ctx.get_help(), str) - if isinstance(ctx.command, Group): - for subcommand in ctx.command.commands.values(): - ctx.command = subcommand - recursively_print_help(ctx) +@pytest.mark.parametrize('command', recurse_commands(cmd_root)) +@pytest.mark.parametrize('help_option', ('--help', '-h')) +def test_commands_help_option(command, help_option): + """Test the help options for all subcommands of the CLI. - ctx = Context(cmd_root) - recursively_print_help(ctx) + The usage of ``subprocess.run`` is on purpose because using :meth:`click.Context.invoke`, which is used by the + ``run_cli_command`` fixture that should usually be used in testing CLI commands, does not behave exactly the same + compared to a direct invocation on the command line. The invocation through ``invoke`` does not go through all the + parent commands and so might not get all the necessary initializations. + """ + result = subprocess.run(command + [help_option], check=False, capture_output=True, text=True) + assert result.returncode == 0, result.stderr + assert 'Usage:' in result.stdout diff --git a/tests/conftest.py b/tests/conftest.py index b40861181..bd1e53881 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,7 @@ from collections.abc import Mapping import io import os +import pathlib import shutil import tempfile @@ -133,7 +134,8 @@ def sssp(aiida_profile, generate_upf_data): continue upf = generate_upf_data(element) - filename = os.path.join(dirpath, f'{element}.upf') + dirpath = pathlib.Path(dirpath) + filename = dirpath / f'{element}.upf' with open(filename, 'w+b') as handle: with upf.open(mode='rb') as source: