Skip to content

Add multiline error strings #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions pytest_github_actions_annotate_failures/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import os
import re
import sys
from collections import OrderedDict
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -43,7 +44,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
filesystempath = os.path.join(runpath, filesystempath)

# try to convert to absolute path in GitHub Actions
workspace = os.environ.get("GITHUB_WORKSPACE")
workspace = os.environ.get("GITHUB_WORKSPACE", "")
if workspace:
full_path = os.path.abspath(filesystempath)
try:
Expand Down Expand Up @@ -77,13 +78,42 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
_, lineno, message = report.longrepr
longrepr += "\n\n" + message
elif isinstance(report.longrepr, str):
parsed_errors = _try_parse_multi_error_string_message(workspace, filesystempath, report.longrepr)
if parsed_errors is not None:
for _lineno, _message in parsed_errors:
print(
_error_workflow_command(filesystempath, _lineno, longrepr + "\n\n" + _message), file=sys.stderr
)
return
longrepr += "\n\n" + report.longrepr

print(
_error_workflow_command(filesystempath, lineno, longrepr), file=sys.stderr
)


def _try_parse_multi_error_string_message(workspace, filesystempath, longrepr):
pathspec_regex = re.compile(
rf"(?:(?:{re.escape(workspace)}/)?{re.escape(filesystempath)}:)?(\d+):(?:\d+:)?\s*(.+)")
matches = [
pathspec_regex.match(line)
for line in longrepr.strip().split("\n")
]
if not all(matches):
return None

errors_per_line = {}

for match in matches:
line_no = int(match.group(1))
message = match.group(2)
if line_no not in errors_per_line:
errors_per_line[line_no] = message
else:
errors_per_line[line_no] += "\n" + message

return errors_per_line.items()


def _error_workflow_command(filesystempath, lineno, longrepr):
# Build collection of arguments. Ordering is strict for easy testing
details_dict = OrderedDict()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ deps =
pytest4: pytest>=4.0.0,<5.0.0
pytest5: pytest>=5.0.0,<6.0.0
pytest6: pytest>=6.0.0,<7.0.0
pytest7: pytest>=7.0.0,<7.4.0
pytest7: pytest>=7.0.0,<8.0.0

commands = {envpython} -m pytest

Expand Down
Loading