Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions plugin_test.py
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion pytest_github_actions_annotate_failures/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import os
import sys
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -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),
)
Expand Down
Loading