|
1 | | - |
2 | 1 | from __future__ import annotations |
3 | 2 |
|
4 | 3 | import contextlib |
@@ -39,30 +38,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 |
39 | 38 | return |
40 | 39 |
|
41 | 40 | 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) |
66 | 42 |
|
67 | 43 | # get the name of the current failed test, with parametrize info |
68 | 44 | longrepr = report.head_line or item.name |
@@ -91,6 +67,33 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 |
91 | 67 | ) |
92 | 68 | print(workflow_command, file=sys.stderr) |
93 | 69 |
|
| 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 | + |
94 | 97 |
|
95 | 98 | class _AnnotateWarnings: |
96 | 99 | def pytest_warning_recorded(self, warning_message, when, nodeid, location): # noqa: ARG002 |
|
0 commit comments