-
Notifications
You must be signed in to change notification settings - Fork 9
feat: better annotation structure #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,71 @@ | ||
from __future__ import print_function | ||
import os | ||
import pytest | ||
from collections import OrderedDict | ||
|
||
# Reference: | ||
# https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks | ||
# https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example | ||
# https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_runtest_makereport | ||
# | ||
# Inspired by: | ||
# https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py | ||
|
||
@pytest.hookimpl(tryfirst=True, hookwrapper=True) | ||
def pytest_runtest_makereport(item, call): | ||
henryiii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# execute all other hooks to obtain the report object | ||
outcome = yield | ||
report = outcome.get_result() | ||
|
||
def pytest_runtest_logreport(report): | ||
# enable only in a workflow of GitHub Actions | ||
# ref: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables | ||
if os.environ.get('GITHUB_ACTIONS') != 'true': | ||
return | ||
|
||
if report.outcome != 'failed': | ||
return | ||
if report.when == "call" and report.failed: | ||
# collect information to be annotated | ||
filesystempath, lineno, _ = report.location | ||
|
||
# try to convert to absolute path in GitHub Actions | ||
workspace = os.environ.get('GITHUB_WORKSPACE') | ||
if workspace: | ||
full_path = os.path.abspath(filesystempath) | ||
rel_path = os.path.relpath(full_path, workspace) | ||
if not rel_path.startswith('..'): | ||
filesystempath = rel_path | ||
|
||
# collect information to be annotated | ||
filesystempath, lineno, _ = report.location | ||
# 0-index to 1-index | ||
lineno += 1 | ||
|
||
# try to convert to absolute path in GitHub Actions | ||
workspace = os.environ.get('GITHUB_WORKSPACE') | ||
if workspace: | ||
full_path = os.path.abspath(filesystempath) | ||
rel_path = os.path.relpath(full_path, workspace) | ||
if not rel_path.startswith('..'): | ||
filesystempath = rel_path | ||
|
||
# 0-index to 1-index | ||
lineno += 1 | ||
# get the name of the current failed test, with parametrize info | ||
longrepr = report.head_line or item.name | ||
|
||
longrepr = str(report.longrepr) | ||
# get the error message and line number from the actual error | ||
try: | ||
longrepr += "\n\n" + report.longrepr.reprcrash.message | ||
lineno = report.longrepr.reprcrash.lineno | ||
|
||
except AttributeError: | ||
pass | ||
|
||
print(_error_workflow_command(filesystempath, lineno, longrepr)) | ||
|
||
print(_error_workflow_command(filesystempath, lineno, longrepr)) | ||
|
||
def _error_workflow_command(filesystempath, lineno, longrepr): | ||
if lineno is None: | ||
if longrepr is None: | ||
return '\n::error file={}'.format(filesystempath) | ||
else: | ||
longrepr = _escape(longrepr) | ||
return '\n::error file={}::{}'.format(filesystempath, longrepr) | ||
# Build collection of arguments. Ordering is strict for easy testing | ||
details_dict = OrderedDict() | ||
details_dict["file"] = filesystempath | ||
if lineno is not None: | ||
details_dict["line"] = lineno | ||
|
||
details = ",".join("{}={}".format(k,v) for k,v in details_dict.items()) | ||
|
||
if longrepr is None: | ||
return '\n::error {}'.format(details) | ||
else: | ||
if longrepr is None: | ||
return '\n::error file={},line={}'.format(filesystempath, lineno) | ||
else: | ||
longrepr = _escape(longrepr) | ||
return '\n::error file={},line={}::{}'.format(filesystempath, lineno, longrepr) | ||
longrepr = _escape(longrepr) | ||
return '\n::error {}::{}'.format(details, longrepr) | ||
|
||
def _escape(s): | ||
return s.replace('%', '%25').replace('\r', '%0D').replace('\n', '%0A') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.