From d0e8ed7b1bb0771aabec9c11f74ff8af38bb1340 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sun, 19 Jun 2022 16:27:55 +0100 Subject: [PATCH] Always show something when rendering exceptions --- cleo/ui/exception_trace.py | 6 ++++-- tests/ui/test_exception_trace.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cleo/ui/exception_trace.py b/cleo/ui/exception_trace.py index 1e3f02da..eb7ed439 100644 --- a/cleo/ui/exception_trace.py +++ b/cleo/ui/exception_trace.py @@ -252,9 +252,11 @@ def ignore_files_in(self, ignore: str) -> ExceptionTrace: return self def render(self, io: IO | Output, simple: bool = False) -> None: - if simple: + # If simple rendering wouldn't show anything useful, abandon it. + simple_string = str(self._exception) if simple else "" + if simple_string: io.write_line("") - io.write_line(f"{str(self._exception)}") + io.write_line(f"{simple_string}") else: self._render_exception(io, self._exception) diff --git a/tests/ui/test_exception_trace.py b/tests/ui/test_exception_trace.py index e7731f1c..abd43e3a 100644 --- a/tests/ui/test_exception_trace.py +++ b/tests/ui/test_exception_trace.py @@ -408,3 +408,34 @@ def test_simple_render_supports_solutions(): https://example2.com """ assert io.fetch_output() == expected + + +def test_simple_render_aborts_if_no_message(): + io = BufferedIO() + + with pytest.raises(Exception) as e: + raise AssertionError + + trace = ExceptionTrace(e.value) + + trace.render(io, simple=True) + lineno = 417 + + expected = f""" + AssertionError + + + + at {trace._get_relative_file_path(__file__)}:{lineno} in \ +test_simple_render_aborts_if_no_message + {lineno - 4}│ def test_simple_render_aborts_if_no_message(): + {lineno - 3}│ io = BufferedIO() + {lineno - 2}│ + {lineno - 1}│ with pytest.raises(Exception) as e: + → {lineno + 0}│ raise AssertionError + {lineno + 1}│ + {lineno + 2}│ trace = ExceptionTrace(e.value) + {lineno + 3}│ + {lineno + 4}│ trace.render(io, simple=True) +""" # noqa: W293 + assert expected == io.fetch_output()