Skip to content
Myr

utils.py

This file

Path

tests/utils.py

45 lines 路 1.3 KiB 路 Python

Last changed by

馃悰 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b

History of this file

Take it

1import re2import sys3from os import getenv45import pytest6from typer._completion_shared import _get_shell_name7from typer.core import HAS_RICH89needs_linux = pytest.mark.skipif(10    not sys.platform.startswith("linux"), reason="Test requires Linux"11)12needs_windows = pytest.mark.skipif(13    not sys.platform.startswith("win"), reason="Test requires Windows"14)1516needs_rich = pytest.mark.skipif(not HAS_RICH, reason="Test requires Rich")1718shell = _get_shell_name()19needs_bash = pytest.mark.skipif(20    shell is None or "bash" not in shell, reason="Test requires Bash"21)2223requires_completion_permission = pytest.mark.skipif(24    not getenv("_TYPER_RUN_INSTALL_COMPLETION_TESTS", False),25    reason="Test requires permission to run completion installation tests",26)272829def strip_double_spaces(text: str) -> str:30    return re.sub(r" {2,}", " ", text)313233def normalize_rich_output(34    text: str, replace_with: str = "*", squash_whitespaces: bool = True35) -> str:36    """37    Replace all rich formatting characters with a simple placeholder.38    """39    text = re.sub(r"\x1b\[[0-9;]*m", replace_with, text)40    text = re.sub(r"[\u2500-\u257F]", replace_with, text)41    text = re.sub(r"[\U0001F300-\U0001F6FF]", replace_with, text)42    text = re.sub(f"{re.escape(replace_with)}+", replace_with, text)43    if squash_whitespaces:44        text = strip_double_spaces(text)45    return text