Coverage for tests / test_tracebacks.py: 100%

36 statements  

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

1import os 1abcdefg

2import subprocess 1abcdefg

3import sys 1abcdefg

4from pathlib import Path 1abcdefg

5 

6 

7def test_traceback_no_rich(): 1abcdefg

8 file_path = Path(__file__).parent / "assets/type_error_no_rich.py" 1abcdefg

9 result = subprocess.run( 1abcdefg

10 [sys.executable, "-m", "coverage", "run", str(file_path)], 

11 capture_output=True, 

12 encoding="utf-8", 

13 env={ 

14 **os.environ, 

15 "TYPER_STANDARD_TRACEBACK": "", 

16 "_TYPER_STANDARD_TRACEBACK": "", 

17 }, 

18 ) 

19 assert "return get_command(self)(*args, **kwargs)" not in result.stderr 1abcdefg

20 

21 assert "app()" in result.stderr 1abcdefg

22 assert "print(name + 3)" in result.stderr 1abcdefg

23 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr 1abcdefg

24 

25 

26def test_traceback_no_rich_short_disable(): 1abcdefg

27 file_path = Path(__file__).parent / "assets/type_error_no_rich_short_disable.py" 1abcdefg

28 result = subprocess.run( 1abcdefg

29 [sys.executable, "-m", "coverage", "run", str(file_path)], 

30 capture_output=True, 

31 encoding="utf-8", 

32 env={ 

33 **os.environ, 

34 "TYPER_STANDARD_TRACEBACK": "", 

35 "_TYPER_STANDARD_TRACEBACK": "", 

36 }, 

37 ) 

38 assert "return get_command(self)(*args, **kwargs)" not in result.stderr 1abcdefg

39 

40 assert "app()" in result.stderr 1abcdefg

41 assert "print(name + 3)" in result.stderr 1abcdefg

42 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr 1abcdefg

43 

44 

45def test_unmodified_traceback(): 1abcdefg

46 file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py" 1abcdefg

47 result = subprocess.run( 1abcdefg

48 [sys.executable, "-m", "coverage", "run", str(file_path)], 

49 capture_output=True, 

50 encoding="utf-8", 

51 env={ 

52 **os.environ, 

53 "TYPER_STANDARD_TRACEBACK": "", 

54 "_TYPER_STANDARD_TRACEBACK": "", 

55 }, 

56 ) 

57 assert "morty" in result.stdout, "the call to the first app should work normally" 1abcdefg

58 assert "return callback(**use_params)" in result.stderr, ( 1abcdefg

59 "calling outside of Typer should show the normal traceback, " 

60 "even after the hook is installed" 

61 ) 

62 assert "typer.main.get_command(broken_app)()" in result.stderr 1abcdefg

63 assert "print(name + 3)" in result.stderr 1abcdefg

64 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr 1abcdefg

65 

66 

67def test_rich_exceptions_dont_truncate_code_on_wide_terminal(): 1abcdefg

68 file_path = Path(__file__).parent / "assets/type_error_rich.py" 1abcdefg

69 result = subprocess.run( 1abcdefg

70 [sys.executable, "-m", "coverage", "run", str(file_path)], 

71 capture_output=True, 

72 encoding="utf-8", 

73 env={ 

74 **os.environ, 

75 "TERMINAL_WIDTH": "120", 

76 "TYPER_STANDARD_TRACEBACK": "", 

77 "_TYPER_STANDARD_TRACEBACK": "", 

78 }, 

79 ) 

80 

81 assert ( 1abcdefg

82 "# The line below will cause TypeError because you cannot concatenate string and integer." 

83 in result.stderr 

84 ) 

85 

86 

87def test_rich_exceptions_wrap_words_on_small_width_terminal(): 1abcdefg

88 file_path = Path(__file__).parent / "assets/type_error_rich.py" 1abcdefg

89 result = subprocess.run( 1abcdefg

90 [sys.executable, "-m", "coverage", "run", str(file_path)], 

91 capture_output=True, 

92 encoding="utf-8", 

93 env={ 

94 **os.environ, 

95 "TERMINAL_WIDTH": "80", 

96 "TYPER_STANDARD_TRACEBACK": "", 

97 "_TYPER_STANDARD_TRACEBACK": "", 

98 }, 

99 ) 

100 

101 # Long line is wrapped, so the full line is not in the output, but parts of it are. 

102 assert "# The line below will cause TypeError because you cannot" in result.stderr 1abcdefg

103 assert "concatenate string and integer." in result.stderr 1abcdefg

104 assert ( 1abcdefg

105 "# The line below will cause TypeError because you cannot concatenate string and integer." 

106 not in result.stderr 

107 )