Skip to content
Myr

test_tracebacks.py

This file

Path

tests/test_tracebacks.py

66 lines · 2.4 KiB · Python

Last changed by

✨ Refactor and document handling pretty exceptions (#422)Sebastián Ramírezbefore adoptionc750f82

History of this file

Take it

1import os2import subprocess3from pathlib import Path456def test_traceback_no_rich():7    file_path = Path(__file__).parent / "assets/type_error_no_rich.py"8    result = subprocess.run(9        ["coverage", "run", str(file_path)],10        stdout=subprocess.PIPE,11        stderr=subprocess.PIPE,12        encoding="utf-8",13        env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},14    )15    assert "return get_command(self)(*args, **kwargs)" not in result.stderr1617    assert "typer.run(main)" in result.stderr18    assert "print(name + 3)" in result.stderr19    # TODO: when deprecating Python 3.6, remove second option20    assert (21        'TypeError: can only concatenate str (not "int") to str' in result.stderr22        or "TypeError: must be str, not int" in result.stderr23    )242526def test_traceback_no_rich_short_disable():27    file_path = Path(__file__).parent / "assets/type_error_no_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        env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},34    )35    assert "return get_command(self)(*args, **kwargs)" not in result.stderr3637    assert "app()" in result.stderr38    assert "print(name + 3)" in result.stderr39    # 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    )444546def test_unmodified_traceback():47    file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py"48    result = subprocess.run(49        ["coverage", "run", str(file_path)],50        stdout=subprocess.PIPE,51        stderr=subprocess.PIPE,52        encoding="utf-8",53        env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},54    )55    assert "morty" in result.stdout, "the call to the first app should work normally"56    assert "return callback(**use_params)" in result.stderr, (57        "calling outside of Typer should show the normal traceback, "58        "even after the hook is installed"59    )60    assert "typer.main.get_command(broken_app)()" in result.stderr61    assert "print(name + 3)" in result.stderr62    # TODO: when deprecating Python 3.6, remove second option63    assert (64        'TypeError: can only concatenate str (not "int") to str' in result.stderr65        or "TypeError: must be str, not int" in result.stderr66    )