Skip to content

Commit

Permalink
Fix UNC bug in _win32_longpath (#34)
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
bilderbuchi authored Sep 28, 2023
1 parent 0b5bb4d commit 5a054df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pytest-datadir
==============

UNRELEASED
----------

- Fix handling of UNC paths on Windows (`#33 <https://github.com/gabrielcnr/pytest-datadir/issues/33>`__).

1.4.1 (2022-10-24)
------------------

- Replace usage of ``tmpdir`` by ``tmp_path`` (`#48 <https://github.com/gabrielcnr/pytest-datadir/pull/48>`__).


Expand Down
12 changes: 11 additions & 1 deletion src/pytest_datadir/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions tests/test_pathlib.py
Original file line number Diff line number Diff line change
@@ -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\\")

0 comments on commit 5a054df

Please # to comment.