diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 789750d..403b9f5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,8 +1,14 @@ pytest-datadir ============== +UNRELEASED +---------- + +- Fix handling of UNC paths on Windows (`#33 `__). + 1.4.1 (2022-10-24) ------------------ + - Replace usage of ``tmpdir`` by ``tmp_path`` (`#48 `__). diff --git a/src/pytest_datadir/plugin.py b/src/pytest_datadir/plugin.py index 1b6af4a..8e50086 100644 --- a/src/pytest_datadir/plugin.py +++ b/src/pytest_datadir/plugin.py @@ -16,7 +16,17 @@ def _win32_longpath(path): # to a path string tells the Windows APIs to disable all string parsing # and to send the string that follows it straight to the file system". # (See https://docs.microsoft.com/pt-br/windows/desktop/FileIO/naming-a-file) - return "\\\\?\\" + os.path.normpath(path) + normalized = os.path.normpath(path) + if not normalized.startswith("\\\\?\\"): + is_unc = normalized.startswith("\\\\") + # see https://en.wikipedia.org/wiki/Path_(computing)#Universal_Naming_Convention # noqa: E501 + if ( + is_unc + ): # then we need to insert an additional "UNC\" to the longpath prefix + normalized = normalized.replace("\\\\", "\\\\?\\UNC\\") + else: + normalized = "\\\\?\\" + normalized + return normalized else: return path diff --git a/tests/test_pathlib.py b/tests/test_pathlib.py new file mode 100644 index 0000000..9c57904 --- /dev/null +++ b/tests/test_pathlib.py @@ -0,0 +1,20 @@ +import sys + +import pytest +from pytest_datadir.plugin import _win32_longpath + + +def test_win32_longpath_idempotent(datadir): + """Double application should not prepend twice.""" + first = _win32_longpath(str(datadir)) + second = _win32_longpath(first) + assert first == second + + +@pytest.mark.skipif( + not sys.platform.startswith("win"), reason="Only makes sense on Windows" +) +def test_win32_longpath_unc(datadir): + unc_path = r"\\ComputerName\SharedFolder\Resource" + longpath = _win32_longpath(unc_path) + assert longpath.startswith("\\\\?\\UNC\\")