Coverage for tests / utils.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1import re 1abcdefg

2import sys 1abcdefg

3from os import getenv 1abcdefg

4 

5import pytest 1abcdefg

6from typer._completion_shared import _get_shell_name 1abcdefg

7from typer.core import HAS_RICH 1abcdefg

8 

9needs_linux = pytest.mark.skipif( 1abcdefg

10 not sys.platform.startswith("linux"), reason="Test requires Linux" 

11) 

12 

13needs_rich = pytest.mark.skipif(not HAS_RICH, reason="Test requires Rich") 1abcdefg

14 

15shell = _get_shell_name() 1abcdefg

16needs_bash = pytest.mark.skipif( 1abcdefg

17 shell is None or "bash" not in shell, reason="Test requires Bash" 

18) 

19 

20requires_completion_permission = pytest.mark.skipif( 1abcdefg

21 not getenv("_TYPER_RUN_INSTALL_COMPLETION_TESTS", False), 

22 reason="Test requires permission to run completion installation tests", 

23) 

24 

25 

26def strip_double_spaces(text: str) -> str: 1abcdefg

27 return re.sub(r" {2,}", " ", text) 1abcdefg

28 

29 

30def normalize_rich_output( 1abcdefg

31 text: str, replace_with: str = "*", squash_whitespaces: bool = True 

32) -> str: 

33 """ 

34 Replace all rich formatting characters with a simple placeholder. 

35 """ 

36 text = re.sub(r"\x1b\[[0-9;]*m", replace_with, text) 1abcdefg

37 text = re.sub(r"[\u2500-\u257F]", replace_with, text) 1abcdefg

38 text = re.sub(r"[\U0001F300-\U0001F6FF]", replace_with, text) 1abcdefg

39 text = re.sub(f"{re.escape(replace_with)}+", replace_with, text) 1abcdefg

40 if squash_whitespaces: 1abcdefg

41 text = strip_double_spaces(text) 1abcdefg

42 return text 1abcdefg