Skip to content

Commit

Permalink
✅ Add tests for configurable pretty errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Jul 8, 2022
1 parent 510d356 commit 7f948b2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/assets/type_error_no_rich_short_disable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import typer
import typer.main

typer.main.rich = None


app = typer.Typer(pretty_errors_short=False)


@app.command()
def main(name: str = "morty"):
print(name + 3)


if __name__ == "__main__":
app()
12 changes: 12 additions & 0 deletions tests/assets/type_error_rich_pretty_disable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import typer

app = typer.Typer(pretty_errors_enable=False)


@app.command()
def main(name: str = "morty"):
print(name + 3)


if __name__ == "__main__":
app()
12 changes: 12 additions & 0 deletions tests/assets/type_error_rich_short_disable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import typer

app = typer.Typer(pretty_errors_short=False)


@app.command()
def main(name: str = "morty"):
print(name + 3)


if __name__ == "__main__":
app()
61 changes: 60 additions & 1 deletion tests/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,28 @@ def test_traceback_rich():
)
assert "return get_command(self)(*args, **kwargs)" not in result.stderr

assert "typer.run(main)" in result.stderr
assert "typer.run(main)" not 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
)
assert "name = 'morty'" in result.stderr


def test_traceback_rich_pretty_short_disable():
file_path = Path(__file__).parent / "assets/type_error_rich_short_disable.py"
result = subprocess.run(
["coverage", "run", str(file_path)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
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
Expand Down Expand Up @@ -42,6 +63,25 @@ def test_traceback_no_rich():
)


def test_traceback_no_rich_short_disable():
file_path = Path(__file__).parent / "assets/type_error_no_rich_short_disable.py"
result = subprocess.run(
["coverage", "run", str(file_path)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
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
)


def test_unmodified_traceback():
file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py"
result = subprocess.run(
Expand All @@ -62,3 +102,22 @@ def test_unmodified_traceback():
'TypeError: can only concatenate str (not "int") to str' in result.stderr
or "TypeError: must be str, not int" in result.stderr
)


def test_rich_pretty_errors_disable():
file_path = Path(__file__).parent / "assets/type_error_rich_pretty_disable.py"
result = subprocess.run(
["coverage", "run", str(file_path)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "return get_command(self)(*args, **kwargs)" 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
)

0 comments on commit 7f948b2

Please sign in to comment.