From a5094cb90b3fefd2d9642b121b476001ce19fdd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Fri, 19 Jun 2026 07:47:12 -0600 Subject: [PATCH] fix: Handle warning filename when the file and the CWD are on different Windows drives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Edgar Ramírez Mondragón --- plugin_test.py | 41 +++++++++++++++++++ .../plugin.py | 7 +++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/plugin_test.py b/plugin_test.py index 7184717..089091f 100644 --- a/plugin_test.py +++ b/plugin_test.py @@ -1,10 +1,15 @@ from __future__ import annotations +import io +import sys +import warnings from collections import Counter import pytest from packaging import version +from pytest_github_actions_annotate_failures.plugin import _AnnotateWarnings + PYTEST_VERSION = version.parse(pytest.__version__) pytest_plugins = "pytester" @@ -252,6 +257,42 @@ def test_warning(): ) +def test_annotation_warning_cross_drive(monkeypatch: pytest.MonkeyPatch): + """Regression: os.path.relpath() raises ValueError on Windows when warning + filename and CWD are on different drive letters; must not produce INTERNALERROR.""" + + monkeypatch.setenv("GITHUB_ACTIONS", "true") + monkeypatch.delenv("GITHUB_WORKSPACE", raising=False) + monkeypatch.delenv("PYTEST_RUN_PATH", raising=False) + + def _cross_drive_relpath(path, start=None): # noqa: ARG001 + msg = "path is on mount 'C:', start on mount 'D:'" + raise ValueError(msg) + + monkeypatch.setattr("os.path.relpath", _cross_drive_relpath) + + w = warnings.WarningMessage( + UserWarning("beware"), + UserWarning, + r"C:\some\path\test_file.py", + 42, + ) + + captured = io.StringIO() + monkeypatch.setattr(sys, "stderr", captured) + + _AnnotateWarnings().pytest_warning_recorded( + w, + when="call", + nodeid="test_file.py::test_fn", + location=("test_file.py", 42, "test_fn"), + ) + + output = captured.getvalue() + assert "::warning" in output + assert "beware" in output + + def test_annotation_fail_disabled_outside_workflow( pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch, diff --git a/pytest_github_actions_annotate_failures/plugin.py b/pytest_github_actions_annotate_failures/plugin.py index 589c9cb..977d830 100644 --- a/pytest_github_actions_annotate_failures/plugin.py +++ b/pytest_github_actions_annotate_failures/plugin.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import os import sys from typing import TYPE_CHECKING @@ -107,9 +108,13 @@ def pytest_warning_recorded( if os.environ.get("GITHUB_ACTIONS") != "true": return + filesystempath = warning_message.filename + with contextlib.suppress(ValueError): + filesystempath = os.path.relpath(filesystempath) + workflow_command = _build_workflow_command( "warning", - compute_path(os.path.relpath(warning_message.filename)), + compute_path(filesystempath), warning_message.lineno, message=str(warning_message.message), )