From c33d1185e786ad00840f9ec2b130f329f93ad532 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 17 May 2021 13:50:06 +0100 Subject: [PATCH 1/2] Fix shell completion (click 8) --- .github/workflows/tox.yml | 12 ++++++------ setup.cfg | 3 +-- src/molecule/console.py | 8 +++++++- src/molecule/logger.py | 4 ++-- src/molecule/shell.py | 8 +++----- src/molecule/test/unit/command/test_base.py | 17 +++++++++++++++++ 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index af350717a0..9eca0a33ae 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -25,17 +25,17 @@ jobs: - tox_env: lint - tox_env: docs - tox_env: py36 - PREFIX: PYTEST_REQPASS=435 + PREFIX: PYTEST_REQPASS=438 - tox_env: py37 - PREFIX: PYTEST_REQPASS=435 + PREFIX: PYTEST_REQPASS=438 - tox_env: py38 - PREFIX: PYTEST_REQPASS=435 + PREFIX: PYTEST_REQPASS=438 - tox_env: py39 - PREFIX: PYTEST_REQPASS=435 + PREFIX: PYTEST_REQPASS=438 - tox_env: py36-devel - PREFIX: PYTEST_REQPASS=435 + PREFIX: PYTEST_REQPASS=438 - tox_env: py39-devel - PREFIX: PYTEST_REQPASS=435 + PREFIX: PYTEST_REQPASS=438 - tox_env: packaging - tox_env: eco - tox_env: dockerfile diff --git a/setup.cfg b/setup.cfg index a7c1e8c898..a0027c46d4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -68,8 +68,7 @@ setup_requires = install_requires = ansible-lint >= 5.0.5 # only for the prerun functionality cerberus >= 1.3.1, !=1.3.3, !=1.3.4 - click >= 7.0 - click-completion >= 0.5.1 + click >= 8.0, < 9 click-help-colors >= 0.9 cookiecutter >= 1.7.3 # dependency issues in older versions dataclasses; python_version<"3.7" diff --git a/src/molecule/console.py b/src/molecule/console.py index fe02478fe8..d9b25d6d13 100644 --- a/src/molecule/console.py +++ b/src/molecule/console.py @@ -1,7 +1,7 @@ """Console and terminal utilities.""" import os import sys -from typing import Any +from typing import Any, Dict from enrich.console import Console from rich.style import Style @@ -71,9 +71,15 @@ def should_do_markup() -> bool: return sys.stdout.isatty() +console_options: Dict[str, Any] = {"emoji": False, "theme": theme, "soft_wrap": True} + console = Console( force_terminal=should_do_markup(), theme=theme, record=True, redirect=True ) +console_options_stderr = console_options.copy() +console_options_stderr["stderr"] = True +console_stderr: Console = Console(**console_options_stderr) + # Define ANSIBLE_FORCE_COLOR if markup is enabled and another value is not # already given. This assures that Ansible subprocesses are still colored, # even if they do not run with a real TTY. diff --git a/src/molecule/logger.py b/src/molecule/logger.py index 460e110d27..d98753b5f5 100644 --- a/src/molecule/logger.py +++ b/src/molecule/logger.py @@ -27,7 +27,7 @@ from enrich.logging import RichHandler -from molecule.console import console +from molecule.console import console, console_stderr from molecule.text import underscore LOG = logging.getLogger(__name__) @@ -48,7 +48,7 @@ def configure() -> None: # libraries. logger = logging.getLogger() handler = RichHandler( - console=console, show_time=False, show_path=False, markup=True + console=console_stderr, show_time=False, show_path=False, markup=True ) # type: ignore logger.addHandler(handler) logger.propagate = False diff --git a/src/molecule/shell.py b/src/molecule/shell.py index 6cc43b3191..39f5189bc5 100644 --- a/src/molecule/shell.py +++ b/src/molecule/shell.py @@ -23,7 +23,6 @@ import sys import click -import click_completion import pkg_resources import molecule @@ -34,8 +33,6 @@ from molecule.console import console from molecule.util import do_report, lookup_config_file -click_completion.init() - # Setup logging. This location of initialization is not ideal, but the code # structure does not give us much choice because config file lookup down below # uses logging facilities. Do note that verbosity level set by the @@ -115,9 +112,10 @@ def main(ctx, debug, verbose, base_config, env_file): # pragma: no cover """ Molecule aids in the development and testing of Ansible roles. - Enable autocomplete issue: + To enable autocomplete for a supported shell execute command below after + replacing SHELL with either bash, bash or fish: - eval "$(_MOLECULE_COMPLETE=source molecule)" + eval "$(_MOLECULE_COMPLETE=SHELL_source molecule)" """ ctx.obj = {} ctx.obj["args"] = {} diff --git a/src/molecule/test/unit/command/test_base.py b/src/molecule/test/unit/command/test_base.py index abab96d76f..c5d8ce1195 100644 --- a/src/molecule/test/unit/command/test_base.py +++ b/src/molecule/test/unit/command/test_base.py @@ -282,3 +282,20 @@ def test_verify_configs_raises_with_duplicate_configs( def test_get_subcommand(): assert "test_base" == base._get_subcommand(__name__) + + +@pytest.mark.parametrize( + "shell", + [ + "bash", + "zsh", + "fish", + ], +) +def test_command_completion(shell: str) -> None: + env = os.environ.copy() + env["_MOLECULE_COMPLETE"] = f"{shell}_source" + + result = util.run_command(["molecule"], env=env) + assert result.returncode == 0 + assert "Found config file" not in result.stdout From 383977a01c2b056cfcc0ad175060e170ac24c9a4 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 18 May 2021 16:32:05 +0100 Subject: [PATCH 2/2] Update src/molecule/shell.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tadej Borovšak <70951+tadeboro@users.noreply.github.com> --- src/molecule/shell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/molecule/shell.py b/src/molecule/shell.py index 39f5189bc5..6fca6103e8 100644 --- a/src/molecule/shell.py +++ b/src/molecule/shell.py @@ -113,7 +113,7 @@ def main(ctx, debug, verbose, base_config, env_file): # pragma: no cover Molecule aids in the development and testing of Ansible roles. To enable autocomplete for a supported shell execute command below after - replacing SHELL with either bash, bash or fish: + replacing SHELL with either bash, zsh, or fish: eval "$(_MOLECULE_COMPLETE=SHELL_source molecule)" """