Skip to content
Myr

test_tracebacks.py

This file

Path

tests/test_tracebacks.py

123 lines · 4.3 KiB · Python

Last changed by

✨ Allow configuring pretty errors when creating the Typer instance (#416)Sebastián Ramírezbefore adoption95a5233

History of this file

Take it

✨ Add pretty error tracebacks for user errors and support for Rich (#412)Sebastián Ramírezbefore adoption252ed30

lines 1–14why, one step back
1import subprocess
2from pathlib import Path345def test_traceback_rich():6 file_path = Path(__file__).parent / "assets/type_error_rich.py"7 result = subprocess.run(8 ["coverage", "run", str(file_path)],9 stdout=subprocess.PIPE,10 stderr=subprocess.PIPE,11 encoding="utf-8",12 )13 assert "return get_command(self)(*args, **kwargs)" not in result.stderr14

✨ Allow configuring pretty errors when creating the Typer instance (#416)Sebastián Ramírezbefore adoption95a5233

line 15why, one step back
15 assert "typer.run(main)" not in result.stderr

✨ Add pretty error tracebacks for user errors and support for Rich (#412)Sebastián Ramírezbefore adoption252ed30

lines 16–25why, one step back
16 assert "print(name + 3)" in result.stderr
1718 # TODO: when deprecating Python 3.6, remove second option19 assert (20 'TypeError: can only concatenate str (not "int") to str' in result.stderr21 or "TypeError: must be str, not int" in result.stderr22 )23 assert "name = 'morty'" in result.stderr2425

✨ Allow configuring pretty errors when creating the Typer instance (#416)Sebastián Ramírezbefore adoption95a5233

lines 26–46why, one step back
26def test_traceback_rich_pretty_short_disable():
27 file_path = Path(__file__).parent / "assets/type_error_rich_short_disable.py"28 result = subprocess.run(29 ["coverage", "run", str(file_path)],30 stdout=subprocess.PIPE,31 stderr=subprocess.PIPE,32 encoding="utf-8",33 )34 assert "return get_command(self)(*args, **kwargs)" not in result.stderr3536 assert "app()" in result.stderr37 assert "print(name + 3)" in result.stderr3839 # TODO: when deprecating Python 3.6, remove second option40 assert (41 'TypeError: can only concatenate str (not "int") to str' in result.stderr42 or "TypeError: must be str, not int" in result.stderr43 )44 assert "name = 'morty'" in result.stderr4546

✨ Add pretty error tracebacks for user errors and support for Rich (#412)Sebastián Ramírezbefore adoption252ed30

lines 47–65why, one step back
47def test_traceback_no_rich():
48 file_path = Path(__file__).parent / "assets/type_error_no_rich.py"49 result = subprocess.run(50 ["coverage", "run", str(file_path)],51 stdout=subprocess.PIPE,52 stderr=subprocess.PIPE,53 encoding="utf-8",54 )55 assert "return get_command(self)(*args, **kwargs)" not in result.stderr5657 assert "typer.run(main)" in result.stderr58 assert "print(name + 3)" in result.stderr59 # TODO: when deprecating Python 3.6, remove second option60 assert (61 'TypeError: can only concatenate str (not "int") to str' in result.stderr62 or "TypeError: must be str, not int" in result.stderr63 )6465

✨ Allow configuring pretty errors when creating the Typer instance (#416)Sebastián Ramírezbefore adoption95a5233

lines 66–84why, one step back
66def test_traceback_no_rich_short_disable():
67 file_path = Path(__file__).parent / "assets/type_error_no_rich_short_disable.py"68 result = subprocess.run(69 ["coverage", "run", str(file_path)],70 stdout=subprocess.PIPE,71 stderr=subprocess.PIPE,72 encoding="utf-8",73 )74 assert "return get_command(self)(*args, **kwargs)" not in result.stderr7576 assert "app()" in result.stderr77 assert "print(name + 3)" in result.stderr78 # TODO: when deprecating Python 3.6, remove second option79 assert (80 'TypeError: can only concatenate str (not "int") to str' in result.stderr81 or "TypeError: must be str, not int" in result.stderr82 )8384

✨ Add pretty error tracebacks for user errors and support for Rich (#412)Sebastián Ramírezbefore adoption252ed30

lines 85–98why, one step back
85def test_unmodified_traceback():
86 file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py"87 result = subprocess.run(88 ["coverage", "run", str(file_path)],89 stdout=subprocess.PIPE,90 stderr=subprocess.PIPE,91 encoding="utf-8",92 )93 assert "morty" in result.stdout, "the call to the first app should work normally"94 assert "return callback(**use_params)" in result.stderr, (95 "calling outside of Typer should show the normal traceback, "96 "even after the hook is installed"97 )98 assert "typer.main.get_command(broken_app)()" in result.stderr

✨ Allow configuring pretty errors when creating the Typer instance (#416)Sebastián Ramírezbefore adoption95a5233

lines 99–117why, one step back
99 assert "print(name + 3)" in result.stderr
100 # TODO: when deprecating Python 3.6, remove second option101 assert (102 'TypeError: can only concatenate str (not "int") to str' in result.stderr103 or "TypeError: must be str, not int" in result.stderr104 )105106107def test_rich_pretty_errors_disable():108 file_path = Path(__file__).parent / "assets/type_error_rich_pretty_disable.py"109 result = subprocess.run(110 ["coverage", "run", str(file_path)],111 stdout=subprocess.PIPE,112 stderr=subprocess.PIPE,113 encoding="utf-8",114 )115 assert "return get_command(self)(*args, **kwargs)" in result.stderr116117 assert "app()" in result.stderr

✨ Add pretty error tracebacks for user errors and support for Rich (#412)Sebastián Ramírezbefore adoption252ed30

lines 118–123why, one step back
118 assert "print(name + 3)" in result.stderr
119 # TODO: when deprecating Python 3.6, remove second option120 assert (121 'TypeError: can only concatenate str (not "int") to str' in result.stderr122 or "TypeError: must be str, not int" in result.stderr123 )