From 444e54deedeb96ed0a8bc4c5926b209d5b4908ee Mon Sep 17 00:00:00 2001 From: Stephan Loyd Date: Wed, 11 May 2022 20:31:21 +0800 Subject: [PATCH] Suppress a possible relative_to() failure, fixes #9894 --- AUTHORS | 1 + changelog/9894.improvement.rst | 1 + src/_pytest/pathlib.py | 8 ++++++-- testing/test_pathlib.py | 5 +++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelog/9894.improvement.rst diff --git a/AUTHORS b/AUTHORS index 86a814a13..cdd84b9b5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -312,6 +312,7 @@ Stefan Scherfke Stefan Zimmermann Stefano Taschini Steffen Allner +Stephan Loyd Stephan Obermann Sven-Hendrik Haase Sylvain MariƩ diff --git a/changelog/9894.improvement.rst b/changelog/9894.improvement.rst new file mode 100644 index 000000000..551bbb9bd --- /dev/null +++ b/changelog/9894.improvement.rst @@ -0,0 +1 @@ +Suppress a potential pathlib relative_to() error, which is possible to be raised from ExceptionInfo.getexpr() when running Python from double-slashed path (like //usr/bin/python3) and single-slashed tests path. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index c5a411b59..5d48543ac 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -707,8 +707,12 @@ def bestrelpath(directory: Path, dest: Path) -> str: # Can be the case for a relative path and an absolute path. if not base: return str(dest) - reldirectory = directory.relative_to(base) - reldest = dest.relative_to(base) + try: + reldirectory = directory.relative_to(base) + reldest = dest.relative_to(base) + except ValueError: + # GH 9894 + return str(dest) return os.path.join( # Back from directory to base. *([os.pardir] * len(reldirectory.parts)), diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index c901dc6f4..9d15fc39b 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -413,6 +413,11 @@ def test_bestrelpath() -> None: assert bestrelpath(curdir, curdir.parent) == ".." assert bestrelpath(curdir, Path("hello")) == "hello" + # GH 9894 + directory = Path("/var/tmp/pytest-issue") + dest = Path("//usr/lib/python3.9/importlib/__init__.py") + assert bestrelpath(directory, dest) == str(dest) + def test_commonpath() -> None: path = Path("/foo/bar/baz/path")