Skip to content

Commit 853a593

Browse files
authored
Improve path_to_url() tests (#13496)
Use `urllib.parse.quote()` rather than `urllib.request.pathname2url()` to generate expected URLs in tests for `path_to_url()`. This makes expectations a little more explicit. (The implementation of `path_to_url()` itself calls `pathname2url()`, and so we were marking our own homework!) Also stop relying on urllib to transform Windows drive letters to uppercase (as this behaviour was removed in 3.14).
1 parent a27ebf5 commit 853a593

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

news/7dab557a-8fb5-41c5-afae-a92c8ab02aba.trivial.rst

Whitespace-only changes.

tests/unit/test_urls.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import urllib.request
3+
import urllib.parse
44

55
import pytest
66

@@ -11,15 +11,15 @@
1111
def test_path_to_url_unix() -> None:
1212
assert path_to_url("/tmp/file") == "file:///tmp/file"
1313
path = os.path.join(os.getcwd(), "file")
14-
assert path_to_url("file") == "file://" + path
14+
assert path_to_url("file") == "file://" + urllib.parse.quote(path)
1515

1616

1717
@pytest.mark.skipif("sys.platform != 'win32'")
1818
@pytest.mark.parametrize(
1919
"path, url",
2020
[
21-
pytest.param("c:/tmp/file", "file:///C:/tmp/file", id="posix-path"),
22-
pytest.param("c:\\tmp\\file", "file:///C:/tmp/file", id="nt-path"),
21+
pytest.param("C:/tmp/file", "file:///C:/tmp/file", id="posix-path"),
22+
pytest.param("C:\\tmp\\file", "file:///C:/tmp/file", id="nt-path"),
2323
],
2424
)
2525
def test_path_to_url_win(path: str, url: str) -> None:
@@ -38,21 +38,21 @@ def test_unc_path_to_url_win() -> None:
3838

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

4444

4545
@pytest.mark.parametrize(
4646
"url,win_expected,non_win_expected",
4747
[
4848
("file:tmp", "tmp", "tmp"),
49-
("file:c:/path/to/file", r"C:\path\to\file", "c:/path/to/file"),
49+
("file:C:/path/to/file", r"C:\path\to\file", "C:/path/to/file"),
5050
("file:/path/to/file", r"\path\to\file", "/path/to/file"),
5151
("file://localhost/tmp/file", r"\tmp\file", "/tmp/file"),
52-
("file://localhost/c:/tmp/file", r"C:\tmp\file", "/c:/tmp/file"),
52+
("file://localhost/C:/tmp/file", r"C:\tmp\file", "/C:/tmp/file"),
5353
("file://somehost/tmp/file", r"\\somehost\tmp\file", None),
5454
("file:///tmp/file", r"\tmp\file", "/tmp/file"),
55-
("file:///c:/tmp/file", r"C:\tmp\file", "/c:/tmp/file"),
55+
("file:///C:/tmp/file", r"C:\tmp\file", "/C:/tmp/file"),
5656
],
5757
)
5858
def test_url_to_path(url: str, win_expected: str, non_win_expected: str) -> None:

0 commit comments

Comments
 (0)