diff --git a/src/_pytest/reports.py b/src/_pytest/reports.py index 657e06833..b4013f6a2 100644 --- a/src/_pytest/reports.py +++ b/src/_pytest/reports.py @@ -1,5 +1,5 @@ +import os from io import StringIO -from pathlib import Path from pprint import pprint from typing import Any from typing import cast @@ -29,7 +29,6 @@ from _pytest._code.code import ReprTraceback from _pytest._code.code import TerminalRepr from _pytest._io import TerminalWriter from _pytest.compat import final -from _pytest.compat import LEGACY_PATH from _pytest.config import Config from _pytest.nodes import Collector from _pytest.nodes import Item @@ -500,8 +499,8 @@ def _report_to_json(report: BaseReport) -> Dict[str, Any]: else: d["longrepr"] = report.longrepr for name in d: - if isinstance(d[name], (LEGACY_PATH, Path)): - d[name] = str(d[name]) + if isinstance(d[name], os.PathLike): + d[name] = os.fspath(d[name]) elif name == "result": d[name] = None # for now return d diff --git a/testing/test_reports.py b/testing/test_reports.py index 3da63c2c8..31b6cf1af 100644 --- a/testing/test_reports.py +++ b/testing/test_reports.py @@ -4,7 +4,6 @@ from typing import Union import pytest from _pytest._code.code import ExceptionChainRepr from _pytest._code.code import ExceptionRepr -from _pytest.compat import legacy_path from _pytest.config import Config from _pytest.pytester import Pytester from _pytest.reports import CollectReport @@ -225,18 +224,26 @@ class TestReportSerialization: assert newrep.longrepr == str(rep.longrepr) def test_paths_support(self, pytester: Pytester) -> None: - """Report attributes which are py.path or pathlib objects should become strings.""" + """Report attributes which are path-like should become strings.""" pytester.makepyfile( """ def test_a(): assert False """ ) + + class MyPathLike: + def __init__(self, path: str) -> None: + self.path = path + + def __fspath__(self) -> str: + return self.path + reprec = pytester.inline_run() reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3 test_a_call = reports[1] - test_a_call.path1 = legacy_path(pytester.path) # type: ignore[attr-defined] + test_a_call.path1 = MyPathLike(str(pytester.path)) # type: ignore[attr-defined] test_a_call.path2 = pytester.path # type: ignore[attr-defined] data = test_a_call._to_json() assert data["path1"] == str(pytester.path)