From a70ebff6e7a69d42946e191458667e3dd502b7f0 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Tue, 17 Sep 2024 11:51:57 +0200 Subject: [PATCH 1/2] Add env var to run completion installation tests --- scripts/test.sh | 2 ++ tests/utils.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/scripts/test.sh b/scripts/test.sh index 32fa1e73f3..893dfde823 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -7,5 +7,7 @@ set -x export TERMINAL_WIDTH=3000 # Force disable terminal for tests inside of pytest, takes precedence over GITHUB_ACTIONS env var export _TYPER_FORCE_DISABLE_TERMINAL=1 +# Run autocompletion install tests in the CI +export _TYPER_RUN_INSTALL_COMPLETION_TESTS=1 # It seems xdist-pytest ensures modified sys.path to import relative modules in examples keeps working pytest --cov --cov-report=term-missing -o console_output_style=progress --numprocesses=auto ${@} diff --git a/tests/utils.py b/tests/utils.py index 8f51332ee2..019b006fa0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,4 +1,5 @@ import sys +from os import getenv import pytest @@ -25,3 +26,8 @@ needs_bash = pytest.mark.skipif( not shellingham or not shell or "bash" not in shell, reason="Test requires Bash" ) + +requires_completion_permission = pytest.mark.skipif( + not getenv("_TYPER_RUN_INSTALL_COMPLETION_TESTS", False), + reason="Test requires permission to run completion installation tests", +) From 3980ed3eda1a44ab3be654908d43ad5002646930 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Tue, 17 Sep 2024 11:52:36 +0200 Subject: [PATCH 2/2] Add @requires_completion_permission marker to all tests running --install-completion --- tests/test_completion/test_completion.py | 3 ++- tests/test_completion/test_completion_install.py | 7 +++++++ tests/test_others.py | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_completion/test_completion.py b/tests/test_completion/test_completion.py index 36581aba09..4e1586e51d 100644 --- a/tests/test_completion/test_completion.py +++ b/tests/test_completion/test_completion.py @@ -5,7 +5,7 @@ from docs_src.commands.index import tutorial001 as mod -from ..utils import needs_bash, needs_linux +from ..utils import needs_bash, needs_linux, requires_completion_permission @needs_bash @@ -26,6 +26,7 @@ def test_show_completion(): @needs_bash @needs_linux +@requires_completion_permission def test_install_completion(): bash_completion_path: Path = Path.home() / ".bashrc" text = "" diff --git a/tests/test_completion/test_completion_install.py b/tests/test_completion/test_completion_install.py index 7c9054e250..873c1416e9 100644 --- a/tests/test_completion/test_completion_install.py +++ b/tests/test_completion/test_completion_install.py @@ -10,11 +10,14 @@ from docs_src.commands.index import tutorial001 as mod +from ..utils import requires_completion_permission + runner = CliRunner() app = typer.Typer() app.command()(mod.main) +@requires_completion_permission def test_completion_install_no_shell(): result = subprocess.run( [sys.executable, "-m", "coverage", "run", mod.__file__, "--install-completion"], @@ -28,6 +31,7 @@ def test_completion_install_no_shell(): assert "Option '--install-completion' requires an argument" in result.stderr +@requires_completion_permission def test_completion_install_bash(): bash_completion_path: Path = Path.home() / ".bashrc" text = "" @@ -67,6 +71,7 @@ def test_completion_install_bash(): ) +@requires_completion_permission def test_completion_install_zsh(): completion_path: Path = Path.home() / ".zshrc" text = "" @@ -104,6 +109,7 @@ def test_completion_install_zsh(): assert "compdef _tutorial001py_completion tutorial001.py" in install_content +@requires_completion_permission def test_completion_install_fish(): script_path = Path(mod.__file__) completion_path: Path = ( @@ -133,6 +139,7 @@ def test_completion_install_fish(): assert "Completion will take effect once you restart the terminal" in result.stdout +@requires_completion_permission def test_completion_install_powershell(): completion_path: Path = ( Path.home() / ".config/powershell/Microsoft.PowerShell_profile.ps1" diff --git a/tests/test_others.py b/tests/test_others.py index a577369b16..1078e63d1f 100644 --- a/tests/test_others.py +++ b/tests/test_others.py @@ -15,6 +15,8 @@ from typer.models import ParameterInfo, TyperInfo from typer.testing import CliRunner +from .utils import requires_completion_permission + runner = CliRunner() @@ -74,6 +76,7 @@ def convert( ParameterInfo(click_type=CustomClickParser()) +@requires_completion_permission def test_install_invalid_shell(): app = typer.Typer()