Skip to content

Commit 299c163

Browse files
refactor into __get_error_message
1 parent 0b46df5 commit 299c163

File tree

1 file changed

+24
-19
lines changed
  • pytest_github_actions_annotate_failures

1 file changed

+24
-19
lines changed

pytest_github_actions_annotate_failures/plugin.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
3939

4040
if report.when == "call" and report.failed:
4141
filesystempath, lineno = _get_location_info(report)
42-
43-
# get the name of the current failed test, with parametrize info
44-
longrepr = report.head_line or item.name
45-
46-
# get the error message and line number from the actual error
47-
if isinstance(report.longrepr, ExceptionRepr):
48-
if report.longrepr.reprcrash is not None:
49-
longrepr += "\n\n" + report.longrepr.reprcrash.message
50-
tb_entries = report.longrepr.reprtraceback.reprentries
51-
if len(tb_entries) > 1 and tb_entries[0].reprfileloc is not None:
52-
# Handle third-party exceptions
53-
lineno = tb_entries[0].reprfileloc.lineno
54-
elif report.longrepr.reprcrash is not None:
55-
lineno = report.longrepr.reprcrash.lineno
56-
elif isinstance(report.longrepr, tuple):
57-
_, lineno, message = report.longrepr
58-
longrepr += "\n\n" + message
59-
elif isinstance(report.longrepr, str):
60-
longrepr += "\n\n" + report.longrepr
42+
longrepr, lineno = _get_error_message(item, report, lineno)
6143

6244
workflow_command = _build_workflow_command(
6345
"error",
@@ -94,6 +76,29 @@ def _get_location_info(report: CollectReport) -> tuple[str, int | None]:
9476

9577
return filesystempath, lineno
9678

79+
def _get_error_message(item: Item, report: CollectReport, lineno: int | None) -> tuple[str, int | None]:
80+
"""Extract error message and potentially updated line number from report."""
81+
# get the name of the current failed test, with parametrize info
82+
longrepr = report.head_line or item.name
83+
84+
# get the error message and line number from the actual error
85+
if isinstance(report.longrepr, ExceptionRepr):
86+
if report.longrepr.reprcrash is not None:
87+
longrepr += "\n\n" + report.longrepr.reprcrash.message
88+
tb_entries = report.longrepr.reprtraceback.reprentries
89+
if len(tb_entries) > 1 and tb_entries[0].reprfileloc is not None:
90+
# Handle third-party exceptions
91+
lineno = tb_entries[0].reprfileloc.lineno
92+
elif report.longrepr.reprcrash is not None:
93+
lineno = report.longrepr.reprcrash.lineno
94+
elif isinstance(report.longrepr, tuple):
95+
_, lineno, message = report.longrepr
96+
longrepr += "\n\n" + message
97+
elif isinstance(report.longrepr, str):
98+
longrepr += "\n\n" + report.longrepr
99+
100+
return longrepr, lineno
101+
97102

98103
class _AnnotateWarnings:
99104
def pytest_warning_recorded(self, warning_message, when, nodeid, location): # noqa: ARG002

0 commit comments

Comments
 (0)