|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import os |
| 5 | +import re |
5 | 6 | import sys |
6 | 7 | from collections import OrderedDict |
7 | 8 | from typing import TYPE_CHECKING |
@@ -43,7 +44,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 |
43 | 44 | filesystempath = os.path.join(runpath, filesystempath) |
44 | 45 |
|
45 | 46 | # try to convert to absolute path in GitHub Actions |
46 | | - workspace = os.environ.get("GITHUB_WORKSPACE") |
| 47 | + workspace = os.environ.get("GITHUB_WORKSPACE", "") |
47 | 48 | if workspace: |
48 | 49 | full_path = os.path.abspath(filesystempath) |
49 | 50 | try: |
@@ -77,13 +78,33 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 |
77 | 78 | _, lineno, message = report.longrepr |
78 | 79 | longrepr += "\n\n" + message |
79 | 80 | elif isinstance(report.longrepr, str): |
80 | | - longrepr += "\n\n" + report.longrepr |
81 | | - |
| 81 | + parsed_errors = _try_parse_multi_error_string_message(workspace, filesystempath, report.longrepr) |
| 82 | + if parsed_errors is not None: |
| 83 | + for _lineno, _message in parsed_errors: |
| 84 | + print( |
| 85 | + _error_workflow_command(filesystempath, lineno, _lineno + "\n\n" + _message), file=sys.stderr |
| 86 | + ) |
| 87 | + return |
| 88 | + else: |
| 89 | + longrepr += "\n\n" + report.longrepr |
82 | 90 | print( |
83 | 91 | _error_workflow_command(filesystempath, lineno, longrepr), file=sys.stderr |
84 | 92 | ) |
85 | 93 |
|
86 | 94 |
|
| 95 | +def _try_parse_multi_error_string_message(workspace, filesystempath, longrepr): |
| 96 | + pathspec_regex = re.compile( |
| 97 | + rf"(?:(?:{re.escape(workspace)}/)?{re.escape(filesystempath)}:)?(\d+):(?:\d+:)?\s*(.+)") |
| 98 | + matches = [ |
| 99 | + pathspec_regex.match(line) |
| 100 | + for line in longrepr.strip().split("\n") |
| 101 | + ] |
| 102 | + if not all(matches): |
| 103 | + return None |
| 104 | + |
| 105 | + return [(int(match.group(1)), match.group(2)) for match in matches] |
| 106 | + |
| 107 | + |
87 | 108 | def _error_workflow_command(filesystempath, lineno, longrepr): |
88 | 109 | # Build collection of arguments. Ordering is strict for easy testing |
89 | 110 | details_dict = OrderedDict() |
|
0 commit comments