Coverage for tests / test_terminal_control_characters.py: 100%

61 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1import re 1abcdefghij

2from typing import Annotated 1abcdefghij

3 

4import pytest 1abcdefghij

5import typer 1abcdefghij

6from typer._click.exceptions import NoSuchOption 1abcdefghij

7from typer.testing import CliRunner 1abcdefghij

8 

9runner = CliRunner() 1abcdefghij

10 

11 

12@pytest.fixture(params=["rich", None], ids=["rich", "plain"]) 1abcdefghij

13def app(request: pytest.FixtureRequest) -> typer.Typer: 1abcdefghij

14 app = typer.Typer(add_completion=False, rich_markup_mode=request.param) 1abcdefghij

15 

16 @app.command() 1abcdefghij

17 def no_args() -> None: 1abcdefghij

18 pass # pragma: no cover 

19 

20 @app.command() 1abcdefghij

21 def read_file(value: typer.FileText) -> None: 1abcdefghij

22 pass # pragma: no cover 

23 

24 @app.command() 1abcdefghij

25 def parse( 1abcdefghij

26 value: Annotated[int, typer.Argument(parser=lambda x: int(x, 0))], 

27 ) -> None: 

28 pass # pragma: no cover 

29 

30 def parse_value(value: str) -> str: 1abcdefghij

31 raise ValueError(f"Invalid item: {value}") 1abcdefghij

32 

33 @app.command() 1abcdefghij

34 def parse_custom( 1abcdefghij

35 value: Annotated[str, typer.Argument(parser=parse_value)], 

36 ) -> None: 

37 pass # pragma: no cover 

38 

39 return app 1abcdefghij

40 

41 

42@pytest.mark.parametrize("color", [False, True]) 1abcdefghij

43@pytest.mark.parametrize( 1abcdefghij

44 ("command", "prefix", "message"), 

45 [ 

46 ("no-args", "", "Got unexpected extra argument(s)"), 

47 ("no-args", "--", "No such option"), 

48 ("read-file", "", "Invalid value"), 

49 ("parse", "", "Invalid value"), 

50 ("parse-custom", "", "Value error, Invalid item:"), 

51 ], 

52 ids=[ 

53 "extra-argument", 

54 "unknown-option", 

55 "file-open", 

56 "custom-parser", 

57 "custom-parser-message", 

58 ], 

59) 

60@pytest.mark.parametrize( 1abcdefghij

61 ("value", "escaped"), 

62 [ 

63 ("\x1b[2J\x1b[H", r"\x1b[2J\x1b[H"), 

64 ("\x1b]52;c;YWJj\x07", r"\x1b]52;c;YWJj\x07"), 

65 ("\x1b]52;c;YWJj\x1b\\", "\\x1b]52;c;YWJj\\x1b\\"), 

66 ("\x9b2J\x9d52;c;YWJj\x9c", r"\x9b2J\x9d52;c;YWJj\x9c"), 

67 ("\r\n\t\b\x7f", r"\x0d\x0a\x09\x08\x7f"), 

68 ( 

69 "text-\u00e9-\u65e5\u672c-\U0001f469\u200d\U0001f4bb-'quoted'-\\text", 

70 "text-\u00e9-\u65e5\u672c-\U0001f469\u200d\U0001f4bb-'quoted'-\\text", 

71 ), 

72 ], 

73 ids=["screen", "osc-bel", "osc-st", "c1", "whitespace-controls", "unicode"], 

74) 

75def test_error_values( 1abcdefghij

76 app: typer.Typer, 

77 command: str, 

78 prefix: str, 

79 message: str, 

80 value: str, 

81 escaped: str, 

82 color: bool, 

83 monkeypatch: pytest.MonkeyPatch, 

84) -> None: 

85 from typer import rich_utils 1abcdefghij

86 

87 monkeypatch.delenv("NO_COLOR", raising=False) 1abcdefghij

88 monkeypatch.setenv("TERM", "xterm-256color") 1abcdefghij

89 monkeypatch.setattr(rich_utils, "FORCE_TERMINAL", color) 1abcdefghij

90 monkeypatch.setattr(rich_utils, "MAX_WIDTH", 3000) 1abcdefghij

91 result = runner.invoke(app, [command, prefix + "input-" + value], color=color) 1abcdefghij

92 

93 assert result.exit_code == 2 1abcdefghij

94 # Remove only generated color styles so other terminal controls cannot hide. 

95 output = re.sub(r"\x1b\[[0-9;]*m", "", result.stderr) 1abcdefghij

96 assert message in output 1abcdefghij

97 if command == "parse": 1abcdefghij

98 # int() already uses repr() for the input in its error message. 

99 assert repr(prefix + "input-" + value) in output 1abcdefghij

100 else: 

101 assert prefix + "input-" + escaped in output 1abcdefghij

102 assert re.search(r"[\x00-\x09\x0b-\x1f\x7f-\x9f]", output) is None 1abcdefghij

103 

104 

105def test_unknown_option_preserves_original_value() -> None: 1abcdefghij

106 value = "--input-\x00\x1f\x7f\x80\x9f" 1abcdefghij

107 error = NoSuchOption(value) 1abcdefghij

108 

109 assert error.option_name == value 1abcdefghij

110 assert error.format_message() == r"No such option: --input-\x00\x1f\x7f\x80\x9f" 1abcdefghij

111 

112 

113def test_argument_preserves_original_value() -> None: 1abcdefghij

114 app = typer.Typer(add_completion=False) 1abcdefghij

115 value = "input-\x1b[2J\x1b]52;c;YWJj\x07" 1abcdefghij

116 

117 @app.command() 1abcdefghij

118 def main(value: str) -> None: 1abcdefghij

119 typer.echo(repr(value)) 1abcdefghij

120 

121 result = runner.invoke(app, [value]) 1abcdefghij

122 

123 assert result.exit_code == 0 1abcdefghij

124 assert result.stdout == repr(value) + "\n" 1abcdefghij

125 

126 

127def test_echo_preserves_terminal_sequences() -> None: 1abcdefghij

128 app = typer.Typer(add_completion=False) 1abcdefghij

129 value = "\x1b[2J\x1b]52;c;YWJj\x07" 1abcdefghij

130 

131 @app.command() 1abcdefghij

132 def main() -> None: 1abcdefghij

133 typer.echo(value, nl=False) 1abcdefghij

134 

135 result = runner.invoke(app, color=True) 1abcdefghij

136 

137 assert result.exit_code == 0 1abcdefghij

138 assert result.stdout_bytes == value.encode() 1abcdefghij