From f6f13e4da9431ad1efcd73127dc8240999ada198 Mon Sep 17 00:00:00 2001 From: David Foster Date: Sun, 7 Jan 2024 20:26:01 -0500 Subject: [PATCH] SQ -> Continuous Integration: Export warnings as warning annotations in GitHub Actions --- src/crystal/tests/index.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/crystal/tests/index.py b/src/crystal/tests/index.py index 8c0cd973..0b011f2c 100644 --- a/src/crystal/tests/index.py +++ b/src/crystal/tests/index.py @@ -245,8 +245,25 @@ def _warnings_sent_to_ci() -> Iterator[None]: super_showwarning = warnings.showwarning # capture def showwarning(message, category, filename, lineno, file=None, line=None): + # Try to reformat `filename` to use the Linux format so that warning + # annotations are associated with the correct file + # + # Depending on OS, `filename` initially looks like: + # - macOS: setup/dist/Crystal Web Archiver.app/Contents/Resources/lib/python38.zip/crystal/tests/index.py + # - Linux: src/crystal/tests/index.py + # - Windows: crystal\tests\index.pyc + filename_parts = filename.split(os.path.sep) + if 'crystal' in filename_parts: + filename_parts = ['src'] + filename_parts[filename_parts.index('crystal'):] + if filename_parts[-1].endswith('.pyc'): + filename_parts[-1] = filename_parts[-1][:-1] # convert .pyc ending to .py + filename = '/'.join(filename_parts) # reinterpret + + # Create warning annotation in GitHub Action's [Summary > Annotations] section + # # Syntax: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-setting-a-warning-message print(f'::warning file={filename},line={lineno}::{message}') + return super_showwarning(message, category, filename, lineno, file, line) warnings.showwarning = showwarning