Coverage for tests/test_rich_utils.py: 100%
56 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-19 20:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-19 20:29 +0000
1import sys 1abcdefghi
3import typer 1abcdefghi
4import typer.completion 1abcdefghi
5from typer.testing import CliRunner 1abcdefghi
7runner = CliRunner() 1abcdefghi
10def test_rich_utils_click_rewrapp(): 1abcdefghi
11 app = typer.Typer(rich_markup_mode="markdown") 1abcdefghi
13 @app.command() 1abcdefghi
14 def main(): 1abcdefghi
15 """
16 \b
17 Some text
19 Some unwrapped text
20 """
21 print("Hello World") 1abcdefghi
23 @app.command() 1abcdefghi
24 def secondary(): 1abcdefghi
25 """
26 \b
27 Secondary text
29 Some unwrapped text
30 """
31 print("Hello Secondary World") 1abcdefghi
33 result = runner.invoke(app, ["--help"]) 1abcdefghi
34 assert "Some text" in result.stdout 1abcdefghi
35 assert "Secondary text" in result.stdout 1abcdefghi
36 assert "\b" not in result.stdout 1abcdefghi
37 result = runner.invoke(app, ["main"]) 1abcdefghi
38 assert "Hello World" in result.stdout 1abcdefghi
39 result = runner.invoke(app, ["secondary"]) 1abcdefghi
40 assert "Hello Secondary World" in result.stdout 1abcdefghi
43def test_rich_help_no_commands(): 1abcdefghi
44 """Ensure that the help still works for a Typer instance with no commands, but with a callback."""
45 app = typer.Typer(help="My cool Typer app") 1abcdefghi
47 @app.callback(invoke_without_command=True, no_args_is_help=True) 1abcdefghi
48 def main() -> None: 1abcdefghi
49 return None # pragma: no cover
51 result = runner.invoke(app, ["--help"]) 1abcdefghi
53 assert result.exit_code == 0 1abcdefghi
54 assert "Show this message" in result.stdout 1abcdefghi
57def test_rich_doesnt_print_None_default(): 1abcdefghi
58 app = typer.Typer(rich_markup_mode="rich") 1abcdefghi
60 @app.command() 1abcdefghi
61 def main( 1abcdefghi
62 name: str,
63 option_1: str = typer.Option(
64 "option_1_default",
65 ),
66 option_2: str = typer.Option(
67 ...,
68 ),
69 ):
70 print(f"Hello {name}") 1abcdefghi
71 print(f"First: {option_1}") 1abcdefghi
72 print(f"Second: {option_2}") 1abcdefghi
74 result = runner.invoke(app, ["--help"]) 1abcdefghi
75 assert "Usage" in result.stdout 1abcdefghi
76 assert "name" in result.stdout 1abcdefghi
77 assert "option-1" in result.stdout 1abcdefghi
78 assert "option-2" in result.stdout 1abcdefghi
79 assert result.stdout.count("[default: None]") == 0 1abcdefghi
80 result = runner.invoke(app, ["Rick", "--option-2=Morty"]) 1abcdefghi
81 assert "Hello Rick" in result.stdout 1abcdefghi
82 assert "First: option_1_default" in result.stdout 1abcdefghi
83 assert "Second: Morty" in result.stdout 1abcdefghi
86def test_rich_markup_import_regression(): 1abcdefghi
87 # Remove rich.markup if it was imported by other tests
88 if "rich" in sys.modules: 1abcdefghi
89 rich_module = sys.modules["rich"] 1abcdefghi
90 if hasattr(rich_module, "markup"): 1abcdefghi
91 delattr(rich_module, "markup") 1abcdefghi
93 app = typer.Typer(rich_markup_mode=None) 1abcdefghi
95 @app.command() 1abcdefghi
96 def main(bar: str): 1abcdefghi
97 pass # pragma: no cover
99 result = runner.invoke(app, ["--help"]) 1abcdefghi
100 assert "Usage" in result.stdout 1abcdefghi
101 assert "BAR" in result.stdout 1abcdefghi