Skip to content

Commit 6213c60

Browse files
undo refactor into _get_error_message
1 parent 46ff873 commit 6213c60

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

plugin_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_fail():
221221
result = testdir.runpytest_subprocess("--rootdir=foo")
222222
result.stderr.fnmatch_lines(
223223
[
224-
"::error file=test_annotation_fail_cwd.py,line=5::test_fail*assert 0*",
224+
"::error file=test_annotation_fail_cwd0/test_annotation_fail_cwd.py,line=5::test_fail*assert 0*",
225225
]
226226
)
227227

pytest_github_actions_annotate_failures/plugin.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import TYPE_CHECKING
77

88
import pytest
9-
from _pytest._code.code import ExceptionRepr
9+
from _pytest._code.code import ExceptionRepr, ReprEntry
1010
from packaging import version
1111

1212
if TYPE_CHECKING:
@@ -38,16 +38,33 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
3838
return
3939

4040
if report.when == "call" and report.failed:
41-
if report.location is None:
42-
return
43-
4441
filesystempath, lineno, _ = report.location
4542

4643
if lineno is not None:
4744
# 0-index to 1-index
4845
lineno += 1
4946

50-
longrepr, lineno = _get_error_message(item, report, lineno)
47+
longrepr = report.head_line or item.name
48+
49+
# get the error message and line number from the actual error
50+
if isinstance(report.longrepr, ExceptionRepr):
51+
if report.longrepr.reprcrash is not None:
52+
longrepr += "\n\n" + report.longrepr.reprcrash.message
53+
tb_entries = report.longrepr.reprtraceback.reprentries
54+
if tb_entries:
55+
entry = tb_entries[0]
56+
# Handle third-party exceptions
57+
if isinstance(entry, ReprEntry) and entry.reprfileloc is not None:
58+
lineno = entry.reprfileloc.lineno
59+
filesystempath = entry.reprfileloc.path
60+
61+
elif report.longrepr.reprcrash is not None:
62+
lineno = report.longrepr.reprcrash.lineno
63+
elif isinstance(report.longrepr, tuple):
64+
filesystempath, lineno, message = report.longrepr
65+
longrepr += "\n\n" + message
66+
elif isinstance(report.longrepr, str):
67+
longrepr += "\n\n" + report.longrepr
5168

5269
workflow_command = _build_workflow_command(
5370
"error",
@@ -80,34 +97,6 @@ def compute_path(filesystempath: str) -> str:
8097
return filesystempath
8198

8299

83-
def _get_error_message(
84-
item: Item,
85-
report: CollectReport,
86-
lineno: int | None,
87-
) -> tuple[str, int]:
88-
"""Extract error message and potentially updated line number from report."""
89-
# get the name of the current failed test, with parametrize info
90-
longrepr = report.head_line or item.name
91-
92-
# get the error message and line number from the actual error
93-
if isinstance(report.longrepr, ExceptionRepr):
94-
if report.longrepr.reprcrash is not None:
95-
longrepr += "\n\n" + report.longrepr.reprcrash.message
96-
tb_entries = report.longrepr.reprtraceback.reprentries
97-
if len(tb_entries) > 1 and tb_entries[0].reprfileloc is not None:
98-
# Handle third-party exceptions
99-
lineno = tb_entries[0].reprfileloc.lineno
100-
elif report.longrepr.reprcrash is not None:
101-
lineno = report.longrepr.reprcrash.lineno
102-
elif isinstance(report.longrepr, tuple):
103-
_, lineno, message = report.longrepr
104-
longrepr += "\n\n" + message
105-
elif isinstance(report.longrepr, str):
106-
longrepr += "\n\n" + report.longrepr
107-
108-
return longrepr, lineno
109-
110-
111100
class _AnnotateWarnings:
112101
def pytest_warning_recorded(self, warning_message, when, nodeid, location): # noqa: ARG002
113102
# enable only in a workflow of GitHub Actions

0 commit comments

Comments
 (0)