Coverage for tests / test_cli / test_help.py: 100%
75 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
1import subprocess 1abcdefghij
2import sys 1abcdefghij
4import typer 1abcdefghij
5from typer.testing import CliRunner 1abcdefghij
7runner = CliRunner() 1abcdefghij
10def test_script_help(): 1abcdefghij
11 result = subprocess.run( 1abcdefghij
12 [
13 sys.executable,
14 "-m",
15 "coverage",
16 "run",
17 "-m",
18 "typer",
19 "tests/assets/cli/sample.py",
20 "--help",
21 ],
22 capture_output=True,
23 encoding="utf-8",
24 )
25 assert "run" in result.stdout 1abcdefghij
28def test_not_python(): 1abcdefghij
29 result = subprocess.run( 1abcdefghij
30 [
31 sys.executable,
32 "-m",
33 "coverage",
34 "run",
35 "-m",
36 "typer",
37 "tests/assets/cli/not_python.txt",
38 "run",
39 ],
40 capture_output=True,
41 encoding="utf-8",
42 )
43 assert "Could not import as Python file" in result.stderr 1abcdefghij
46def test_short_help() -> None: 1abcdefghij
47 app = typer.Typer( 1abcdefghij
48 rich_markup_mode=None,
49 context_settings={"max_content_width": 50},
50 )
52 @app.command(help=" \n\t ") 1abcdefghij
53 def empty() -> None: 1abcdefghij
54 pass # pragma: no cover
56 @app.command(help="\b first sentence.") 1abcdefghij
57 def marker() -> None: 1abcdefghij
58 pass # pragma: no cover
60 # Forcing truncation
61 @app.command(help=f"{'x' * 30} {'y' * 5} z trailing") 1abcdefghij
62 def long() -> None: 1abcdefghij
63 pass # pragma: no cover
65 result = runner.invoke(app, ["--help"], terminal_width=50) 1abcdefghij
66 assert result.exit_code == 0 1abcdefghij
67 assert "empty" in result.output 1abcdefghij
68 assert "marker" in result.output 1abcdefghij
69 assert "long" in result.output 1abcdefghij
70 assert "first sentence." in result.output 1abcdefghij
71 assert f"{'x' * 30}..." in result.output 1abcdefghij
74def test_help_wrapping() -> None: 1abcdefghij
75 app = typer.Typer( 1abcdefghij
76 rich_markup_mode=None,
77 context_settings={"max_content_width": 50},
78 )
80 @app.command( 1abcdefghij
81 help=(
82 "Wrapped paragraph has enough words to wrap in help output.\n"
83 "\n"
84 "\n"
85 "\b\n"
86 "RAW-LINE-ONE stays on one line even with many many many words.\n"
87 "RAW-LINE-TWO keeps original formatting.\n"
88 "\n"
89 "Final paragraph wraps normally as well."
90 )
91 )
92 def cmd() -> None: 1abcdefghij
93 pass # pragma: no cover
95 result = runner.invoke(app, ["cmd", "--help"], terminal_width=50) 1abcdefghij
96 assert result.exit_code == 0 1abcdefghij
97 assert "Wrapped paragraph has enough words to wrap" in result.output 1abcdefghij
98 assert ( 1abcdefghij
99 "RAW-LINE-ONE stays on one line even with many many many words."
100 in result.output
101 )
102 assert "RAW-LINE-TWO keeps original formatting." in result.output 1abcdefghij
103 assert "Final paragraph wraps normally as well." in result.output 1abcdefghij
106def test_help_wrapping_long_name() -> None: 1abcdefghij
107 app = typer.Typer(rich_markup_mode=None) 1abcdefghij
109 @app.command() 1abcdefghij
110 def cmd(value: str) -> None: 1abcdefghij
111 pass # pragma: no cover
113 result = runner.invoke( 1abcdefghij
114 app,
115 ["cmd", "--help"],
116 terminal_width=40,
117 prog_name="very-long-program-name-that-forces-wrap",
118 )
119 assert result.exit_code == 0 1abcdefghij
121 output_lines = result.output.splitlines() 1abcdefghij
122 usage_idx = output_lines.index("Usage: very-long-program-name-that-forces-wrap ") 1abcdefghij
123 args_line = output_lines[usage_idx + 1] 1abcdefghij
124 assert args_line.lstrip() == "[OPTIONS] {value}" 1abcdefghij
125 assert args_line.startswith(" ") 1abcdefghij
128def test_format_long_help_option() -> None: 1abcdefghij
129 app = typer.Typer(rich_markup_mode=None) 1abcdefghij
131 @app.command() 1abcdefghij
132 def cmd( 1abcdefghij
133 very_long: str = typer.Option(
134 ...,
135 "--this-is-a-very-very-very-long-option-name",
136 help="Description is rendered in the next line for long option labels.",
137 ),
138 ) -> None:
139 pass # pragma: no cover
141 result = runner.invoke(app, ["cmd", "--help"], terminal_width=80) 1abcdefghij
142 assert result.exit_code == 0 1abcdefghij
144 output_lines = result.output.splitlines() 1abcdefghij
145 option_idx = next( 1abcdefghij
146 i
147 for i, line in enumerate(output_lines)
148 if "--this-is-a-very-very-very-long-option-name" in line
149 )
150 assert "Description is rendered" not in output_lines[option_idx] 1abcdefghij
151 first_desc_line = output_lines[option_idx + 1] 1abcdefghij
152 assert first_desc_line.lstrip().startswith("Description is rendered") 1abcdefghij
153 continuation_block = " ".join( 1abcdefghij
154 line.strip() for line in output_lines[option_idx + 1 :] if line.startswith(" ")
155 )
156 assert ( 1abcdefghij
157 "Description is rendered in the next line for long option labels."
158 in continuation_block
159 )
162def test_wrap_text_splits_long_word() -> None: 1abcdefghij
163 app = typer.Typer( 1abcdefghij
164 rich_markup_mode=None,
165 context_settings={"max_content_width": 10},
166 )
168 @app.command(help="supercalifragilisticexpialidocious") 1abcdefghij
169 def cmd() -> None: 1abcdefghij
170 pass # pragma: no cover
172 result = runner.invoke(app, ["cmd", "--help"], terminal_width=20) 1abcdefghij
173 assert result.exit_code == 0 1abcdefghij
174 assert "supercalifragilist" in result.output 1abcdefghij
175 assert "icexpialidocious" in result.output 1abcdefghij
178def test_typer_run_usage() -> None: 1abcdefghij
179 from typer.cli import app as typer_cli_app 1abcdefghij
181 result = runner.invoke( 1abcdefghij
182 typer_cli_app,
183 ["tests/assets/cli/minimum_main.py", "run"],
184 prog_name="typer",
185 )
187 assert result.exit_code == 2 1abcdefghij
188 usage_line = result.output.splitlines()[0] 1abcdefghij
189 assert usage_line.startswith("Usage: typer [PATH_OR_MODULE] run [OPTIONS] {name}") 1abcdefghij
190 assert "Try 'typer [PATH_OR_MODULE] run --help' for help." in result.output 1abcdefghij