Skip to content

Commit e44dbe8

Browse files
helpmefindanamehenryiii
authored andcommitted
add parsing of multiline errors
1 parent 8358442 commit e44dbe8

File tree

1 file changed

+24
-3
lines changed
  • pytest_github_actions_annotate_failures

1 file changed

+24
-3
lines changed

pytest_github_actions_annotate_failures/plugin.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import os
5+
import re
56
import sys
67
from collections import OrderedDict
78
from typing import TYPE_CHECKING
@@ -43,7 +44,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
4344
filesystempath = os.path.join(runpath, filesystempath)
4445

4546
# try to convert to absolute path in GitHub Actions
46-
workspace = os.environ.get("GITHUB_WORKSPACE")
47+
workspace = os.environ.get("GITHUB_WORKSPACE", "")
4748
if workspace:
4849
full_path = os.path.abspath(filesystempath)
4950
try:
@@ -77,13 +78,33 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
7778
_, lineno, message = report.longrepr
7879
longrepr += "\n\n" + message
7980
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
8290
print(
8391
_error_workflow_command(filesystempath, lineno, longrepr), file=sys.stderr
8492
)
8593

8694

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+
87108
def _error_workflow_command(filesystempath, lineno, longrepr):
88109
# Build collection of arguments. Ordering is strict for easy testing
89110
details_dict = OrderedDict()

0 commit comments

Comments
 (0)