Skip to content

Improve path_to_url() tests #13496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
18 changes: 9 additions & 9 deletions tests/unit/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
import urllib.request
import urllib.parse

import pytest

Expand All @@ -11,15 +11,15 @@
def test_path_to_url_unix() -> None:
assert path_to_url("/tmp/file") == "file:///tmp/file"
path = os.path.join(os.getcwd(), "file")
assert path_to_url("file") == "file://" + path
assert path_to_url("file") == "file://" + urllib.parse.quote(path)
Copy link
Contributor Author

@barneygale barneygale Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, tests will fail if the working directory contains a non-urlsafe character.



@pytest.mark.skipif("sys.platform != 'win32'")
@pytest.mark.parametrize(
"path, url",
[
pytest.param("c:/tmp/file", "file:///C:/tmp/file", id="posix-path"),
pytest.param("c:\\tmp\\file", "file:///C:/tmp/file", id="nt-path"),
pytest.param("C:/tmp/file", "file:///C:/tmp/file", id="posix-path"),
pytest.param("C:\\tmp\\file", "file:///C:/tmp/file", id="nt-path"),
],
)
def test_path_to_url_win(path: str, url: str) -> None:
Expand All @@ -38,21 +38,21 @@ def test_unc_path_to_url_win() -> None:

@pytest.mark.skipif("sys.platform != 'win32'")
def test_relative_path_to_url_win() -> None:
resolved_path = os.path.join(os.getcwd(), "file")
assert path_to_url("file") == "file:" + urllib.request.pathname2url(resolved_path)
path = os.path.join(os.getcwd(), "file").replace("\\", "/")
assert path_to_url("file") == "file:///" + urllib.parse.quote(path, safe="/:")
Comment on lines +41 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, file:c:/file (and file:/c:/file) is the non-normative form of file:///c:/file but they are effectively equivalent, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, on Windows, does pathname2url() also include : in its url-safe default list? I'm on Linux, and pathname2url("C:\\Program Files") returns file:C%3A%5CProgram%20Files which is fine for Linux, but would be nonsensial for Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, file:c:/file (and file:/c:/file) is the non-normative form of file:///c:/file but they are effectively equivalent, right?

That's correct.

Also, on Windows, does pathname2url() also include : in its url-safe default list?

It does for the drive, yes: https://github.com/python/cpython/blob/6784ef7da7cbf1a944fd0685630ced54e4a0066c/Lib/urllib/request.py#L1711

(NTFS allows colons in other parts of the path, but Windows does not.)



@pytest.mark.parametrize(
"url,win_expected,non_win_expected",
[
("file:tmp", "tmp", "tmp"),
("file:c:/path/to/file", r"C:\path\to\file", "c:/path/to/file"),
("file:C:/path/to/file", r"C:\path\to\file", "C:/path/to/file"),
("file:/path/to/file", r"\path\to\file", "/path/to/file"),
("file://localhost/tmp/file", r"\tmp\file", "/tmp/file"),
("file://localhost/c:/tmp/file", r"C:\tmp\file", "/c:/tmp/file"),
("file://localhost/C:/tmp/file", r"C:\tmp\file", "/C:/tmp/file"),
("file://somehost/tmp/file", r"\\somehost\tmp\file", None),
("file:///tmp/file", r"\tmp\file", "/tmp/file"),
("file:///c:/tmp/file", r"C:\tmp\file", "/c:/tmp/file"),
("file:///C:/tmp/file", r"C:\tmp\file", "/C:/tmp/file"),
],
)
def test_url_to_path(url: str, win_expected: str, non_win_expected: str) -> None:
Expand Down
Loading