|
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
8 | 8 | import pytest |
9 | | -from _pytest._code.code import ExceptionRepr |
| 9 | +from _pytest._code.code import ExceptionRepr, ReprEntry |
10 | 10 | from packaging import version |
11 | 11 |
|
12 | 12 | if TYPE_CHECKING: |
@@ -38,16 +38,33 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 |
38 | 38 | return |
39 | 39 |
|
40 | 40 | if report.when == "call" and report.failed: |
41 | | - if report.location is None: |
42 | | - return |
43 | | - |
44 | 41 | filesystempath, lineno, _ = report.location |
45 | 42 |
|
46 | 43 | if lineno is not None: |
47 | 44 | # 0-index to 1-index |
48 | 45 | lineno += 1 |
49 | 46 |
|
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 |
51 | 68 |
|
52 | 69 | workflow_command = _build_workflow_command( |
53 | 70 | "error", |
@@ -80,34 +97,6 @@ def compute_path(filesystempath: str) -> str: |
80 | 97 | return filesystempath |
81 | 98 |
|
82 | 99 |
|
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 | | - |
111 | 100 | class _AnnotateWarnings: |
112 | 101 | def pytest_warning_recorded(self, warning_message, when, nodeid, location): # noqa: ARG002 |
113 | 102 | # enable only in a workflow of GitHub Actions |
|
0 commit comments