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 tests for completion flags #866

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 44 additions & 15 deletions tests/test_completion/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,73 @@

from docs_src.commands.index import tutorial001 as mod

from ..utils import needs_linux
from ..utils import needs_cleaning, needs_linux


@needs_linux
def test_show_completion():
def test_show_completion_flag():
result = subprocess.run(
[
"bash",
"-c",
f"'{sys.executable}' -m coverage run '{mod.__file__}' --show-completion",
"typer --show-completion",
],
capture_output=True,
text=True,
encoding="utf-8",
env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
)
assert "_TUTORIAL001.PY_COMPLETE=complete_bash" in result.stdout
curr_shell_path = os.environ["SHELL"]
# check for default user shell
shell = "bash"
if "zsh" in curr_shell_path:
shell = "zsh"
if "fish" in curr_shell_path:
shell = "fish"
if "pwsh" in curr_shell_path:
shell = "pwsh"
assert f"_TYPER_COMPLETE=complete_{shell}" in result.stdout


@needs_cleaning
@needs_linux
def test_install_completion_flag():
# NOTE: This test causes side-effects at runtime on your system shell config
curr_shell_path = os.environ["SHELL"]

completion_path: Path = Path.home() / ".bashrc"
if "zsh" in curr_shell_path:
completion_path: Path = Path.home() / ".zshrc"
if "fish" in curr_shell_path:
completion_path: Path = Path.home() / ".config/fish/completions/"

@needs_linux
def test_install_completion():
bash_completion_path: Path = Path.home() / ".bashrc"
text = ""
if bash_completion_path.is_file(): # pragma: no cover
text = bash_completion_path.read_text()
if completion_path.is_file(): # pragma: no cover
text = completion_path.read_text()
result = subprocess.run(
[
"bash",
"-c",
f"'{sys.executable}' -m coverage run '{mod.__file__}' --install-completion",
"typer --install-completion",
],
capture_output=True,
encoding="utf-8",
env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
)
new_text = bash_completion_path.read_text()
bash_completion_path.write_text(text)
assert "source" in new_text
assert str(Path(".bash_completions/tutorial001.py.sh")) in new_text

shell_config_file = completion_path.read_text()
completion_path.write_text(text)

if "bash" in curr_shell_path:
# check .bashrc
assert "source" in shell_config_file
assert str(Path(".bash_completions/tutorial001.py.sh")) in shell_config_file
if "zsh" in curr_shell_path:
# check .zshrc
assert (
"\n\nautoload -Uz compinit\nzstyle ':completion:*' menu select\nfpath+=~/.zfunc\n"
in shell_config_file
)

assert "completion installed in" in result.stdout
assert "Completion will take effect once you restart the terminal" in result.stdout

Expand Down
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
needs_linux = pytest.mark.skipif(
not sys.platform.startswith("linux"), reason="Test requires Linux"
)

needs_cleaning = pytest.mark.skip(
reason="needs decouple from system, causes side-effects"
)
Loading