Skip to content

Commit 3358704

Browse files
authored
Merge pull request #60 from edgarrmondragon/fix/third-party-exceptions
fix: Reference the correct line number in third-party exceptions
2 parents 55bba59 + c2626f8 commit 3358704

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

plugin_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@ def test_fail():
6969
)
7070

7171

72+
def test_annotation_third_party_exception(testdir):
73+
testdir.makepyfile(
74+
my_module="""
75+
def fn():
76+
raise Exception('oops')
77+
"""
78+
)
79+
80+
testdir.makepyfile(
81+
"""
82+
import pytest
83+
from my_module import fn
84+
pytest_plugins = 'pytest_github_actions_annotate_failures'
85+
86+
def test_fail():
87+
fn()
88+
"""
89+
)
90+
testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true")
91+
result = testdir.runpytest_subprocess()
92+
result.stderr.fnmatch_lines(
93+
["::error file=test_annotation_third_party_exception.py,line=6::test_fail*oops*",]
94+
)
95+
96+
7297
def test_annotation_fail_disabled_outside_workflow(testdir):
7398
testdir.makepyfile(
7499
"""

pytest_github_actions_annotate_failures/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ def pytest_runtest_makereport(item, call):
5959
# get the error message and line number from the actual error
6060
if hasattr(report.longrepr, "reprcrash"):
6161
longrepr += "\n\n" + report.longrepr.reprcrash.message
62-
lineno = report.longrepr.reprcrash.lineno
62+
traceback_entries = report.longrepr.reprtraceback.reprentries
63+
if len(traceback_entries) > 1:
64+
# Handle third-party exceptions
65+
lineno = traceback_entries[0].reprfileloc.lineno
66+
else:
67+
lineno = report.longrepr.reprcrash.lineno
6368
elif isinstance(report.longrepr, tuple):
6469
_, lineno, message = report.longrepr
6570
longrepr += "\n\n" + message

0 commit comments

Comments
 (0)