Skip to content

Commit abef23d

Browse files
add parsing of multiline errors
1 parent 1a87abb commit abef23d

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import os
6+
import re
67
import sys
78
from collections import OrderedDict
89
from typing import TYPE_CHECKING
@@ -45,7 +46,7 @@ def pytest_runtest_makereport(item: Item, call):
4546
filesystempath = os.path.join(runpath, filesystempath)
4647

4748
# try to convert to absolute path in GitHub Actions
48-
workspace = os.environ.get("GITHUB_WORKSPACE")
49+
workspace = os.environ.get("GITHUB_WORKSPACE", "")
4950
if workspace:
5051
full_path = os.path.abspath(filesystempath)
5152
try:
@@ -79,13 +80,33 @@ def pytest_runtest_makereport(item: Item, call):
7980
_, lineno, message = report.longrepr
8081
longrepr += "\n\n" + message
8182
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
8492
print(
8593
_error_workflow_command(filesystempath, lineno, longrepr), file=sys.stderr
8694
)
8795

8896

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

0 commit comments

Comments
 (0)