-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
43 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters