-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# SPDX-FileCopyrightText: 2025 Aleksandr Mezin <mezin.alexander@gmail.com> | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import os | ||
import urllib.parse | ||
|
||
|
||
try: | ||
from xdist import is_xdist_worker | ||
|
||
except ImportError: | ||
def is_xdist_worker(request_or_session): | ||
return hasattr(request_or_session.config, 'workerinput') | ||
|
||
|
||
def command(command, message='', **properties): | ||
properties = ','.join( | ||
f'{k}={urllib.parse.quote(v)}' | ||
for k, v in properties.items() | ||
) | ||
|
||
if properties: | ||
return f'::{command} {properties}::{urllib.parse.quote(message)}' | ||
else: | ||
return f'::{command}::{urllib.parse.quote(message)}' | ||
|
||
|
||
class FailureReporter: | ||
def __init__(self, terminal_reporter): | ||
self.terminal_reporter = terminal_reporter | ||
|
||
def pytest_runtest_logreport(self, report): | ||
if not report.failed: | ||
return | ||
|
||
filesystempath, lineno, _ = report.location | ||
|
||
properties = dict( | ||
title=report.nodeid, | ||
file=filesystempath, | ||
) | ||
|
||
if lineno is not None: | ||
properties['line'] = lineno + 1 | ||
|
||
self.terminal_reporter.write_line(command('error', report.longreprtext, **properties)) | ||
|
||
|
||
class WarningReporter: | ||
def __init__(self, terminal_reporter): | ||
self.terminal_reporter = terminal_reporter | ||
|
||
def pytest_warning_recorded(self, warning_message, when, nodeid, location): | ||
properties = dict(title=nodeid) | ||
|
||
if location: | ||
filename, linenumber, _ = location | ||
|
||
if filename: | ||
properties['file'] = filename | ||
|
||
if linenumber is not None: | ||
properties['line'] = linenumber + 1 | ||
|
||
self.terminal_reporter.write_line(command('warning', warning_message, **properties)) | ||
|
||
|
||
def pytest_addoption(parser): | ||
env_value = os.environ.get('GITHUB_ACTIONS', None) | ||
|
||
parser.addoption( | ||
'--github-actions-annotations', | ||
type=bool, | ||
default=env_value == 'true', | ||
help='Generate GitHub Actions annotations for failures and warnings', | ||
) | ||
|
||
|
||
def pytest_sessionstart(session): | ||
if not session.config.option.github_actions_annotations: | ||
return | ||
|
||
if is_xdist_worker(session): | ||
return | ||
|
||
terminal_reporter = session.config.pluginmanager.getplugin('terminalreporter') | ||
|
||
session.config.pluginmanager.register(FailureReporter(terminal_reporter)) | ||
session.config.pluginmanager.register(WarningReporter(terminal_reporter)) |