Coverage for tests / test_rich_utils.py: 100%

56 statements  

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

1import sys 1abcdefgh

2 

3import typer 1abcdefgh

4import typer.completion 1abcdefgh

5from typer.testing import CliRunner 1abcdefgh

6 

7runner = CliRunner() 1abcdefgh

8 

9 

10def test_rich_utils_click_rewrapp(): 1abcdefgh

11 app = typer.Typer(rich_markup_mode="markdown") 1abcdefgh

12 

13 @app.command() 1abcdefgh

14 def main(): 1abcdefgh

15 """ 

16 \b 

17 Some text 

18 

19 Some unwrapped text 

20 """ 

21 print("Hello World") 1abcdefgh

22 

23 @app.command() 1abcdefgh

24 def secondary(): 1abcdefgh

25 """ 

26 \b 

27 Secondary text 

28 

29 Some unwrapped text 

30 """ 

31 print("Hello Secondary World") 1abcdefgh

32 

33 result = runner.invoke(app, ["--help"]) 1abcdefgh

34 assert "Some text" in result.stdout 1abcdefgh

35 assert "Secondary text" in result.stdout 1abcdefgh

36 assert "\b" not in result.stdout 1abcdefgh

37 result = runner.invoke(app, ["main"]) 1abcdefgh

38 assert "Hello World" in result.stdout 1abcdefgh

39 result = runner.invoke(app, ["secondary"]) 1abcdefgh

40 assert "Hello Secondary World" in result.stdout 1abcdefgh

41 

42 

43def test_rich_help_no_commands(): 1abcdefgh

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") 1abcdefgh

46 

47 @app.callback(invoke_without_command=True, no_args_is_help=True) 1abcdefgh

48 def main() -> None: 1abcdefgh

49 return None # pragma: no cover 

50 

51 result = runner.invoke(app, ["--help"]) 1abcdefgh

52 

53 assert result.exit_code == 0 1abcdefgh

54 assert "Show this message" in result.stdout 1abcdefgh

55 

56 

57def test_rich_doesnt_print_None_default(): 1abcdefgh

58 app = typer.Typer(rich_markup_mode="rich") 1abcdefgh

59 

60 @app.command() 1abcdefgh

61 def main( 1abcdefgh

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}") 1abcdefgh

71 print(f"First: {option_1}") 1abcdefgh

72 print(f"Second: {option_2}") 1abcdefgh

73 

74 result = runner.invoke(app, ["--help"]) 1abcdefgh

75 assert "Usage" in result.stdout 1abcdefgh

76 assert "name" in result.stdout 1abcdefgh

77 assert "option-1" in result.stdout 1abcdefgh

78 assert "option-2" in result.stdout 1abcdefgh

79 assert result.stdout.count("[default: None]") == 0 1abcdefgh

80 result = runner.invoke(app, ["Rick", "--option-2=Morty"]) 1abcdefgh

81 assert "Hello Rick" in result.stdout 1abcdefgh

82 assert "First: option_1_default" in result.stdout 1abcdefgh

83 assert "Second: Morty" in result.stdout 1abcdefgh

84 

85 

86def test_rich_markup_import_regression(): 1abcdefgh

87 # Remove rich.markup if it was imported by other tests 

88 if "rich" in sys.modules: 1abcdefgh

89 rich_module = sys.modules["rich"] 1abcdefgh

90 if hasattr(rich_module, "markup"): 1abcdefgh

91 delattr(rich_module, "markup") 1abcdefgh

92 

93 app = typer.Typer(rich_markup_mode=None) 1abcdefgh

94 

95 @app.command() 1abcdefgh

96 def main(bar: str): 1abcdefgh

97 pass # pragma: no cover 

98 

99 result = runner.invoke(app, ["--help"]) 1abcdefgh

100 assert "Usage" in result.stdout 1abcdefgh

101 assert "BAR" in result.stdout 1abcdefgh