Skip to content
Myr

test_tracebacks.py

This file

Path

tests/test_tracebacks.py

67 lines · 2.5 KiB · Python

Last changed by

👷‍♂️ Consistently use `sys.executable` to run subprocesses, needed by OpenSUSE (#408)Matthias Bachbefore adoption416c612

History of this file

Take it

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 1why, one step back
1import os

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

line 2why, one step back
2import subprocess

👷‍♂️ Consistently use `sys.executable` to run subprocesses, needed by OpenSUSE (#408)Matthias Bachbefore adoption416c612

line 3why, one step back
3import sys

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

lines 4–9why, one step back
4from pathlib import Path
567def test_traceback_no_rich():8 file_path = Path(__file__).parent / "assets/type_error_no_rich.py"9 result = subprocess.run(

👷‍♂️ Consistently use `sys.executable` to run subprocesses, needed by OpenSUSE (#408)Matthias Bachbefore adoption416c612

line 10why, one step back
10 [sys.executable, "-m", "coverage", "run", str(file_path)],

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

lines 11–13why, one step back
11 stdout=subprocess.PIPE,
12 stderr=subprocess.PIPE,13 encoding="utf-8",

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 14why, one step back
14 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},

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

lines 15–26why, one step back
15 )
16 assert "return get_command(self)(*args, **kwargs)" not in result.stderr1718 assert "typer.run(main)" in result.stderr19 assert "print(name + 3)" in result.stderr20 # TODO: when deprecating Python 3.6, remove second option21 assert (22 'TypeError: can only concatenate str (not "int") to str' in result.stderr23 or "TypeError: must be str, not int" in result.stderr24 )2526

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

lines 27–29why, one step back
27def test_traceback_no_rich_short_disable():
28 file_path = Path(__file__).parent / "assets/type_error_no_rich_short_disable.py"29 result = subprocess.run(

👷‍♂️ Consistently use `sys.executable` to run subprocesses, needed by OpenSUSE (#408)Matthias Bachbefore adoption416c612

line 30why, one step back
30 [sys.executable, "-m", "coverage", "run", str(file_path)],

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

lines 31–33why, one step back
31 stdout=subprocess.PIPE,
32 stderr=subprocess.PIPE,33 encoding="utf-8",

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 34why, one step back
34 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},

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

lines 35–46why, one step back
35 )
36 assert "return get_command(self)(*args, **kwargs)" not in result.stderr3738 assert "app()" in result.stderr39 assert "print(name + 3)" in result.stderr40 # TODO: when deprecating Python 3.6, remove second option41 assert (42 'TypeError: can only concatenate str (not "int") to str' in result.stderr43 or "TypeError: must be str, not int" in result.stderr44 )4546

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

lines 47–49why, one step back
47def test_unmodified_traceback():
48 file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py"49 result = subprocess.run(

👷‍♂️ Consistently use `sys.executable` to run subprocesses, needed by OpenSUSE (#408)Matthias Bachbefore adoption416c612

line 50why, one step back
50 [sys.executable, "-m", "coverage", "run", str(file_path)],

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

lines 51–53why, one step back
51 stdout=subprocess.PIPE,
52 stderr=subprocess.PIPE,53 encoding="utf-8",

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 54why, one step back
54 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},

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

lines 55–67why, one step back
55 )
56 assert "morty" in result.stdout, "the call to the first app should work normally"57 assert "return callback(**use_params)" in result.stderr, (58 "calling outside of Typer should show the normal traceback, "59 "even after the hook is installed"60 )61 assert "typer.main.get_command(broken_app)()" in result.stderr62 assert "print(name + 3)" in result.stderr63 # TODO: when deprecating Python 3.6, remove second option64 assert (65 'TypeError: can only concatenate str (not "int") to str' in result.stderr66 or "TypeError: must be str, not int" in result.stderr67 )