Skip to content
Myr

test_completion.py

This file

Path

tests/test_completion.py

51 lines · 1.4 KiB · Python

Last changed by

:white_check_mark: Update completion tests, checking for printed messageSebastián Ramírezbefore adoption1a3db11

History of this file

Take it

1import os2import subprocess3import sys4from pathlib import Path56import typer7from typer.testing import CliRunner89from first_steps import tutorial001 as mod1011runner = CliRunner()12app = typer.Typer()13app.command()(mod.main)141516def test_show_completion():17    result = subprocess.run(18        [19            "bash",20            "-c",21            f"{sys.executable}  -m coverage run {mod.__file__} --show-completion",22        ],23        stdout=subprocess.PIPE,24        stderr=subprocess.PIPE,25        encoding="utf-8",26        env={**os.environ, "SHELL": "/bin/bash"},27    )28    assert "_TUTORIAL001.PY_COMPLETE=complete-bash" in result.stdout293031def test_install_completion():32    bash_completion_path: Path = Path.home() / ".bash_completion"33    text = ""34    if bash_completion_path.is_file():35        text = bash_completion_path.read_text()36    result = subprocess.run(37        [38            "bash",39            "-c",40            f"{sys.executable} -m coverage run {mod.__file__} --install-completion",41        ],42        stdout=subprocess.PIPE,43        stderr=subprocess.PIPE,44        encoding="utf-8",45        env={**os.environ, "SHELL": "/bin/bash"},46    )47    new_text = bash_completion_path.read_text()48    bash_completion_path.write_text(text)49    assert "_TUTORIAL001.PY_COMPLETE=complete-bash" in new_text50    assert "completion installed in" in result.stdout51    assert "Completion will take effect once you restart the terminal." in result.stdout