1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
# NOTE: these tests reference line numbers from code in this file,
# so it's sensitive to refactoring
from __future__ import annotations
import re
import pytest
from cleo.io.buffered_io import BufferedIO
from cleo.io.outputs.output import Verbosity
from cleo.ui.exception_trace.component import ExceptionTrace
from tests.fixtures.exceptions import nested1
from tests.fixtures.exceptions import nested2
from tests.fixtures.exceptions import recursion
from tests.fixtures.exceptions import simple
def test_render_better_error_message() -> None:
io = BufferedIO()
try:
simple.simple_exception()
except Exception as e:
trace = ExceptionTrace(e)
trace.render(io)
expected = f"""\
Exception
Failed
at {trace._get_relative_file_path(simple.__file__)}:2 in simple_exception
1│ def simple_exception() -> None:
→ 2│ raise Exception("Failed")
3│
"""
assert expected == io.fetch_output()
def test_render_debug_better_error_message() -> None:
io = BufferedIO()
io.set_verbosity(Verbosity.DEBUG)
try:
simple.simple_exception()
except Exception as e: # Exception
trace = ExceptionTrace(e)
trace.render(io)
lineno = 47
expected = f"""
Stack trace:
1 {trace._get_relative_file_path(__file__)}:{lineno} in \
test_render_debug_better_error_message
{lineno - 2}│
{lineno - 1}│ try:
→ {lineno + 0}│ simple.simple_exception()
{lineno + 1}│ except Exception as e: # Exception
{lineno + 2}│ trace = ExceptionTrace(e)
Exception
Failed
at {trace._get_relative_file_path(simple.__file__)}:2 in simple_exception
1│ def simple_exception() -> None:
→ 2│ raise Exception("Failed")
3│
"""
assert io.fetch_output() == expected
def test_render_debug_better_error_message_recursion_error() -> None:
io = BufferedIO()
io.set_verbosity(Verbosity.DEBUG)
try:
recursion.recursion_error()
except RecursionError as e:
trace = ExceptionTrace(e)
lineno = 83
trace.render(io)
expected = rf"""^
Stack trace:
\d+ {re.escape(trace._get_relative_file_path(__file__))}:{lineno} in test_render_debug_better_error_message_recursion_error
{lineno - 2}\│
{lineno - 1}\│ try:
→ {lineno + 0}\│ recursion.recursion_error\(\)
{lineno + 1}\│ except RecursionError as e:
{lineno + 2}\│ trace = ExceptionTrace\(e\)
... Previous frame repeated \d+ times
\s*\d+ {re.escape(trace._get_relative_file_path(recursion.__file__))}:2 in recursion_error
1\│ def recursion_error\(\) -> None:
→ 2\│ recursion_error\(\)
3\│
RecursionError
maximum recursion depth exceeded
at {re.escape(trace._get_relative_file_path(recursion.__file__))}:2 in recursion_error
1\│ def recursion_error\(\) -> None:
→ 2\│ recursion_error\(\)
3\│
"""
assert re.match(expected, io.fetch_output()) is not None
def test_render_very_verbose_better_error_message() -> None:
io = BufferedIO()
io.set_verbosity(Verbosity.VERY_VERBOSE)
try:
simple.simple_exception()
except Exception as e: # Exception
trace = ExceptionTrace(e)
trace.render(io)
expected = f"""
Stack trace:
1 {trace._get_relative_file_path(__file__)}:125 in \
test_render_very_verbose_better_error_message
simple.simple_exception()
Exception
Failed
at {trace._get_relative_file_path(simple.__file__)}:2 in simple_exception
1│ def simple_exception() -> None:
→ 2│ raise Exception("Failed")
3│
"""
assert expected == io.fetch_output()
def test_render_debug_better_error_message_recursion_error_with_multiple_duplicated_frames() -> (
None
):
def first() -> None:
def second() -> None:
first()
second()
io = BufferedIO()
io.set_verbosity(Verbosity.VERY_VERBOSE)
with pytest.raises(RecursionError) as e:
first()
trace = ExceptionTrace(e.value)
trace.render(io)
expected = r"... Previous 2 frames repeated \d+ times"
assert re.search(expected, io.fetch_output()) is not None
def test_render_can_ignore_given_files() -> None:
io = BufferedIO()
io.set_verbosity(Verbosity.VERY_VERBOSE)
with pytest.raises(Exception) as e:
nested2.call()
trace = ExceptionTrace(e.value)
trace.ignore_files_in(rf"^{re.escape(nested1.__file__)}$")
trace.render(io)
lineno = 180
expected = f"""
Stack trace:
2 {trace._get_relative_file_path(__file__)}:{lineno} in \
test_render_can_ignore_given_files
nested2.call()
1 {trace._get_relative_file_path(nested2.__file__)}:8 in call
run()
Exception
Foo
at {trace._get_relative_file_path(nested1.__file__)}:3 in inner
1│ def outer() -> None:
2│ def inner() -> None:
→ 3│ raise Exception("Foo")
4│
5│ inner()
6│
"""
assert io.fetch_output() == expected
def test_render_shows_ignored_files_if_in_debug_mode() -> None:
io = BufferedIO()
io.set_verbosity(Verbosity.DEBUG)
with pytest.raises(Exception) as e:
nested2.call()
trace = ExceptionTrace(e.value)
trace.ignore_files_in(rf"^{re.escape(nested1.__file__)}$")
trace.render(io)
lineno = 218
expected = f"""
Stack trace:
4 {trace._get_relative_file_path(__file__)}:{lineno} in \
test_render_shows_ignored_files_if_in_debug_mode
{lineno - 2}│
{lineno - 1}│ with pytest.raises(Exception) as e:
→ {lineno + 0}│ nested2.call()
{lineno + 1}│
{lineno + 2}│ trace = ExceptionTrace(e.value)
3 {trace._get_relative_file_path(nested2.__file__)}:8 in call
6│ outer()
7│
→ 8│ run()
9│
2 {trace._get_relative_file_path(nested2.__file__)}:6 in run
4│ def call() -> None:
5│ def run() -> None:
→ 6│ outer()
7│
8│ run()
1 {trace._get_relative_file_path(nested1.__file__)}:5 in outer
3│ raise Exception("Foo")
4│
→ 5│ inner()
6│
Exception
Foo
at {trace._get_relative_file_path(nested1.__file__)}:3 in inner
1│ def outer() -> None:
2│ def inner() -> None:
→ 3│ raise Exception("Foo")
4│
5│ inner()
6│
"""
assert io.fetch_output() == expected
def test_render_falls_back_on_ascii_symbols() -> None:
io = BufferedIO(supports_utf8=False)
with pytest.raises(Exception) as e:
simple.simple_exception()
trace = ExceptionTrace(e.value)
trace.render(io)
expected = f"""
Exception
Failed
at {trace._get_relative_file_path(simple.__file__)}:2 in simple_exception
1| def simple_exception() -> None:
> 2| raise Exception("Failed")
3|
"""
assert io.fetch_output() == expected
def test_empty_source_file_do_not_break_highlighter() -> None:
from cleo.ui.exception_trace.component import Highlighter
highlighter = Highlighter()
highlighter.highlighted_lines("")
def test_docstrings_are_correctly_rendered() -> None:
from cleo.formatters.formatter import Formatter
from cleo.ui.exception_trace.component import Highlighter
source = '''
def test():
"""
Doctring
"""
...
'''
formatter = Formatter()
highlighter = Highlighter()
lines = highlighter.highlighted_lines(source)
assert [formatter.format(line) for line in lines] == [*source.splitlines(), ""]
def test_simple_render() -> None:
io = BufferedIO()
with pytest.raises(Exception) as e:
simple.simple_exception()
trace = ExceptionTrace(e.value)
trace.render(io, simple=True)
expected = """
Failed
"""
assert io.fetch_output() == expected
def test_simple_render_aborts_if_no_message() -> None:
io = BufferedIO()
with pytest.raises(Exception) as e:
raise AssertionError
trace = ExceptionTrace(e.value)
trace.render(io, simple=True)
lineno = 342
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() -> None:
{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)
"""
assert expected == io.fetch_output()
|