Skip to content

Commit 0b46df5

Browse files
refactor into _get_location_info
1 parent ba478fc commit 0b46df5

File tree

1 file changed

+28
-25
lines changed
  • pytest_github_actions_annotate_failures

1 file changed

+28
-25
lines changed

pytest_github_actions_annotate_failures/plugin.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from __future__ import annotations
32

43
import contextlib
@@ -39,30 +38,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
3938
return
4039

4140
if report.when == "call" and report.failed:
42-
# collect information to be annotated
43-
filesystempath, lineno, _ = report.location
44-
45-
runpath = os.environ.get("PYTEST_RUN_PATH")
46-
if runpath:
47-
filesystempath = os.path.join(runpath, filesystempath)
48-
49-
# try to convert to absolute path in GitHub Actions
50-
workspace = os.environ.get("GITHUB_WORKSPACE")
51-
if workspace:
52-
full_path = os.path.abspath(filesystempath)
53-
try:
54-
rel_path = os.path.relpath(full_path, workspace)
55-
except ValueError:
56-
# os.path.relpath() will raise ValueError on Windows
57-
# when full_path and workspace have different mount points.
58-
# https://github.com/utgwkk/pytest-github-actions-annotate-failures/issues/20
59-
rel_path = filesystempath
60-
if not rel_path.startswith(".."):
61-
filesystempath = rel_path
62-
63-
if lineno is not None:
64-
# 0-index to 1-index
65-
lineno += 1
41+
filesystempath, lineno = _get_location_info(report)
6642

6743
# get the name of the current failed test, with parametrize info
6844
longrepr = report.head_line or item.name
@@ -91,6 +67,33 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
9167
)
9268
print(workflow_command, file=sys.stderr)
9369

70+
def _get_location_info(report: CollectReport) -> tuple[str, int | None]:
71+
"""Extract and process location information from the report."""
72+
filesystempath, lineno, _ = report.location
73+
74+
runpath = os.environ.get("PYTEST_RUN_PATH")
75+
if runpath:
76+
filesystempath = os.path.join(runpath, filesystempath)
77+
78+
# try to convert to absolute path in GitHub Actions
79+
workspace = os.environ.get("GITHUB_WORKSPACE")
80+
if workspace:
81+
full_path = os.path.abspath(filesystempath)
82+
try:
83+
rel_path = os.path.relpath(full_path, workspace)
84+
except ValueError:
85+
# os.path.relpath() will raise ValueError on Windows
86+
# when full_path and workspace have different mount points.
87+
rel_path = filesystempath
88+
if not rel_path.startswith(".."):
89+
filesystempath = rel_path
90+
91+
if lineno is not None:
92+
# 0-index to 1-index
93+
lineno += 1
94+
95+
return filesystempath, lineno
96+
9497

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

0 commit comments

Comments
 (0)