Skip to content

Commit a75dad5

Browse files
authored
Merge pull request #13105 from ichard26/windows-paths
Accommodate for recent pathname2url() changes upstream
2 parents fe0925b + 5beed92 commit a75dad5

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

news/ba0bd1bb-2dcc-43d0-83ff-c762e7e55bf9.trivial.rst

Whitespace-only changes.

tests/lib/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
cast,
3030
)
3131
from urllib.parse import urlparse, urlunparse
32+
from urllib.request import pathname2url
3233
from zipfile import ZipFile
3334

3435
import pytest
@@ -1379,6 +1380,10 @@ def __call__(
13791380

13801381
CertFactory = Callable[[], str]
13811382

1383+
# -------------------------------------------------------------------------
1384+
# Accommodations for Windows path and URL changes in recent Python releases
1385+
# -------------------------------------------------------------------------
1386+
13821387
# versions containing fix/backport from https://github.com/python/cpython/pull/113563
13831388
# which changed the behavior of `urllib.parse.urlun{parse,split}`
13841389
url = "////path/to/file"
@@ -1394,3 +1399,15 @@ def __call__(
13941399
sys.platform != "win32" or has_new_urlun_behavior,
13951400
reason="testing windows behavior for older CPython",
13961401
)
1402+
1403+
# Trailing slashes are now preserved on Windows, matching POSIX behaviour.
1404+
# BPO: https://github.com/python/cpython/issues/126212
1405+
does_pathname2url_preserve_trailing_slash = pathname2url("C:/foo/").endswith("/")
1406+
skip_needs_new_pathname2url_trailing_slash_behavior_win = pytest.mark.skipif(
1407+
sys.platform != "win32" or not does_pathname2url_preserve_trailing_slash,
1408+
reason="testing windows (pathname2url) behavior for newer CPython",
1409+
)
1410+
skip_needs_old_pathname2url_trailing_slash_behavior_win = pytest.mark.skipif(
1411+
sys.platform != "win32" or does_pathname2url_preserve_trailing_slash,
1412+
reason="testing windows (pathname2url) behavior for older CPython",
1413+
)

tests/unit/test_collector.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
from tests.lib import (
4141
TestData,
4242
make_test_link_collector,
43+
skip_needs_new_pathname2url_trailing_slash_behavior_win,
4344
skip_needs_new_urlun_behavior_win,
45+
skip_needs_old_pathname2url_trailing_slash_behavior_win,
4446
skip_needs_old_urlun_behavior_win,
4547
)
4648

@@ -390,12 +392,26 @@ def test_clean_url_path_with_local_path(path: str, expected: str) -> None:
390392
pytest.param(
391393
"file:///T:/path/with spaces/",
392394
"file:///T:/path/with%20spaces",
393-
marks=skip_needs_old_urlun_behavior_win,
395+
marks=[
396+
skip_needs_old_urlun_behavior_win,
397+
skip_needs_old_pathname2url_trailing_slash_behavior_win,
398+
],
394399
),
395400
pytest.param(
396401
"file:///T:/path/with spaces/",
397402
"file://///T:/path/with%20spaces",
398-
marks=skip_needs_new_urlun_behavior_win,
403+
marks=[
404+
skip_needs_new_urlun_behavior_win,
405+
skip_needs_old_pathname2url_trailing_slash_behavior_win,
406+
],
407+
),
408+
pytest.param(
409+
"file:///T:/path/with spaces/",
410+
"file://///T:/path/with%20spaces/",
411+
marks=[
412+
skip_needs_new_urlun_behavior_win,
413+
skip_needs_new_pathname2url_trailing_slash_behavior_win,
414+
],
399415
),
400416
# URL with Windows drive letter, running on non-windows
401417
# platform. The `:` after the drive should be quoted.

tests/unit/test_urls.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
from pip._internal.utils.urls import path_to_url, url_to_path
88

9-
from tests.lib import (
10-
skip_needs_new_urlun_behavior_win,
11-
skip_needs_old_urlun_behavior_win,
12-
)
13-
149

1510
@pytest.mark.skipif("sys.platform == 'win32'")
1611
def test_path_to_url_unix() -> None:
@@ -25,23 +20,22 @@ def test_path_to_url_unix() -> None:
2520
[
2621
pytest.param("c:/tmp/file", "file:///C:/tmp/file", id="posix-path"),
2722
pytest.param("c:\\tmp\\file", "file:///C:/tmp/file", id="nt-path"),
28-
pytest.param(
29-
r"\\unc\as\path",
30-
"file://unc/as/path",
31-
marks=skip_needs_old_urlun_behavior_win,
32-
id="unc-path",
33-
),
34-
pytest.param(
35-
r"\\unc\as\path",
36-
"file:////unc/as/path",
37-
marks=skip_needs_new_urlun_behavior_win,
38-
),
3923
],
4024
)
4125
def test_path_to_url_win(path: str, url: str) -> None:
4226
assert path_to_url(path) == url
4327

4428

29+
@pytest.mark.skipif("sys.platform != 'win32'")
30+
def test_unc_path_to_url_win() -> None:
31+
# The two and four slash forms are both acceptable for our purposes. CPython's
32+
# behaviour has changed several times here, so blindly accept either.
33+
# - https://github.com/python/cpython/issues/78457
34+
# - https://github.com/python/cpython/issues/126205
35+
url = path_to_url(r"\\unc\as\path")
36+
assert url in ["file://unc/as/path", "file:////unc/as/path"]
37+
38+
4539
@pytest.mark.skipif("sys.platform != 'win32'")
4640
def test_relative_path_to_url_win() -> None:
4741
resolved_path = os.path.join(os.getcwd(), "file")

0 commit comments

Comments
 (0)