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

Tests: Add test to guarantee CLI is reachable #334

Merged
merged 2 commits into from
Feb 26, 2024
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = [
'aiida-vasp~=3.1',
'aiida-wien2k~=0.2.0',
'ase!=3.20.*',
'click~=8.0',
'masci-tools~=0.9',
'pint~=0.16',
'pymatgen>=2022.1.20'
Expand Down
7 changes: 3 additions & 4 deletions src/aiida_common_workflows/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Module for the command line interface."""
import click_completion

# Activate the completion of parameter types provided by the click_completion package
click_completion.init()
from .launch import cmd_launch # noqa: F401
from .plot import cmd_plot # noqa: F401
from .root import cmd_root # noqa: F401
45 changes: 45 additions & 0 deletions tests/cli/test_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Tests for CLI commands."""
from __future__ import annotations

import subprocess

import click
import pytest
from aiida_common_workflows.cli import cmd_root


def recurse_commands(command: click.Command, parents: list[str] | None = 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]


@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.

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
Loading