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

Fix shell completion (click 8) #3118

Merged
merged 2 commits into from
May 18, 2021
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
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion src/molecule/console.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/molecule/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import sys

import click
import click_completion
import pkg_resources

import molecule
Expand All @@ -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
Expand Down Expand Up @@ -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, zsh, or fish:

eval "$(_MOLECULE_COMPLETE=source molecule)"
eval "$(_MOLECULE_COMPLETE=SHELL_source molecule)"
"""
ctx.obj = {}
ctx.obj["args"] = {}
Expand Down
17 changes: 17 additions & 0 deletions src/molecule/test/unit/command/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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