Skip to content

Commit 0b42abc

Browse files
authored
fix: respect PYTEST_RUN_PATH in warning annotations
Signed-off-by: Tom Plant <tom.plant@devicie.com>
1 parent 10e283c commit 0b42abc

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

plugin_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ def test_warning():
213213
)
214214

215215

216+
def test_annotation_warning_runpath(testdir: pytest.Testdir):
217+
testdir.makepyfile(
218+
"""
219+
import warnings
220+
import pytest
221+
pytest_plugins = 'pytest_github_actions_annotate_failures'
222+
223+
def test_warning():
224+
warnings.warn('beware', Warning)
225+
assert 1
226+
"""
227+
)
228+
testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true")
229+
testdir.monkeypatch.setenv("PYTEST_RUN_PATH", "some_path")
230+
result = testdir.runpytest_subprocess()
231+
result.stderr.fnmatch_lines(
232+
[
233+
"::warning file=some_path/test_annotation_warning_runpath.py,line=6::beware",
234+
]
235+
)
236+
237+
216238
def test_annotation_fail_disabled_outside_workflow(testdir: pytest.Testdir):
217239
testdir.makepyfile(
218240
"""

pytest_github_actions_annotate_failures/plugin.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import contextlib
43
import os
54
import sys
65
from typing import TYPE_CHECKING
@@ -110,25 +109,9 @@ def pytest_warning_recorded(
110109
if os.environ.get("GITHUB_ACTIONS") != "true":
111110
return
112111

113-
filesystempath = warning_message.filename
114-
workspace = os.environ.get("GITHUB_WORKSPACE")
115-
116-
if workspace:
117-
try:
118-
rel_path = os.path.relpath(filesystempath, workspace)
119-
except ValueError:
120-
# os.path.relpath() will raise ValueError on Windows
121-
# when full_path and workspace have different mount points.
122-
rel_path = filesystempath
123-
if not rel_path.startswith(".."):
124-
filesystempath = rel_path
125-
else:
126-
with contextlib.suppress(ValueError):
127-
filesystempath = os.path.relpath(filesystempath)
128-
129112
workflow_command = _build_workflow_command(
130113
"warning",
131-
filesystempath,
114+
compute_path(os.path.relpath(warning_message.filename)),
132115
warning_message.lineno,
133116
message=str(warning_message.message),
134117
)

0 commit comments

Comments
 (0)