Skip to content

Commit

Permalink
tests: GitHub Actions annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
amezin committed Jan 3, 2025
1 parent 2333b20 commit b776d16
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
8 changes: 7 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from . import procutil


pytest_plugins = ('test.screenshot', 'test.syslog', 'test.logsync', 'test.gtest_output_compat')
pytest_plugins = (
'test.screenshot',
'test.syslog',
'test.logsync',
'test.gtest_output_compat',
'test.github_annotations',
)

LOGGER = logging.getLogger(__name__)

Expand Down
93 changes: 93 additions & 0 deletions test/github_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# SPDX-FileCopyrightText: 2025 Aleksandr Mezin <mezin.alexander@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import urllib.parse
import warnings


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

warning_message = warnings.formatwarning(**warning_message)

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))

0 comments on commit b776d16

Please sign in to comment.