2022-07-12 18:45:43 +02:00
|
|
|
import os
|
2022-07-06 13:32:19 +02:00
|
|
|
import subprocess
|
2022-11-07 19:01:54 +01:00
|
|
|
import sys
|
2022-07-06 13:32:19 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
def test_traceback_no_rich():
|
|
|
|
file_path = Path(__file__).parent / "assets/type_error_no_rich.py"
|
|
|
|
result = subprocess.run(
|
2022-11-07 19:01:54 +01:00
|
|
|
[sys.executable, "-m", "coverage", "run", str(file_path)],
|
2022-07-06 13:32:19 +02:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
encoding="utf-8",
|
2022-07-12 18:45:43 +02:00
|
|
|
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},
|
2022-07-06 13:32:19 +02:00
|
|
|
)
|
|
|
|
assert "return get_command(self)(*args, **kwargs)" not in result.stderr
|
|
|
|
|
|
|
|
assert "typer.run(main)" in result.stderr
|
|
|
|
assert "print(name + 3)" in result.stderr
|
|
|
|
# TODO: when deprecating Python 3.6, remove second option
|
|
|
|
assert (
|
|
|
|
'TypeError: can only concatenate str (not "int") to str' in result.stderr
|
|
|
|
or "TypeError: must be str, not int" in result.stderr
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-08 17:20:32 +02:00
|
|
|
def test_traceback_no_rich_short_disable():
|
|
|
|
file_path = Path(__file__).parent / "assets/type_error_no_rich_short_disable.py"
|
|
|
|
result = subprocess.run(
|
2022-11-07 19:01:54 +01:00
|
|
|
[sys.executable, "-m", "coverage", "run", str(file_path)],
|
2022-07-08 17:20:32 +02:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
encoding="utf-8",
|
2022-07-12 18:45:43 +02:00
|
|
|
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},
|
2022-07-08 17:20:32 +02:00
|
|
|
)
|
|
|
|
assert "return get_command(self)(*args, **kwargs)" not in result.stderr
|
|
|
|
|
|
|
|
assert "app()" in result.stderr
|
|
|
|
assert "print(name + 3)" in result.stderr
|
|
|
|
# TODO: when deprecating Python 3.6, remove second option
|
|
|
|
assert (
|
|
|
|
'TypeError: can only concatenate str (not "int") to str' in result.stderr
|
|
|
|
or "TypeError: must be str, not int" in result.stderr
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-06 13:32:19 +02:00
|
|
|
def test_unmodified_traceback():
|
|
|
|
file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py"
|
|
|
|
result = subprocess.run(
|
2022-11-07 19:01:54 +01:00
|
|
|
[sys.executable, "-m", "coverage", "run", str(file_path)],
|
2022-07-06 13:32:19 +02:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
encoding="utf-8",
|
2022-07-12 18:45:43 +02:00
|
|
|
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},
|
2022-07-06 13:32:19 +02:00
|
|
|
)
|
|
|
|
assert "morty" in result.stdout, "the call to the first app should work normally"
|
|
|
|
assert "return callback(**use_params)" in result.stderr, (
|
|
|
|
"calling outside of Typer should show the normal traceback, "
|
|
|
|
"even after the hook is installed"
|
|
|
|
)
|
|
|
|
assert "typer.main.get_command(broken_app)()" in result.stderr
|
|
|
|
assert "print(name + 3)" in result.stderr
|
|
|
|
# TODO: when deprecating Python 3.6, remove second option
|
|
|
|
assert (
|
|
|
|
'TypeError: can only concatenate str (not "int") to str' in result.stderr
|
|
|
|
or "TypeError: must be str, not int" in result.stderr
|
|
|
|
)
|