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