From 7caaf5d43ca33526ba2ede16bd528134742eade6 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 21 Jul 2025 19:15:04 +0100 Subject: [PATCH 1/4] Improve `path_to_url()` tests 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!) --- news/7dab557a-8fb5-41c5-afae-a92c8ab02aba.trivial.rst | 0 tests/unit/test_urls.py | 9 +++++---- 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 news/7dab557a-8fb5-41c5-afae-a92c8ab02aba.trivial.rst diff --git a/news/7dab557a-8fb5-41c5-afae-a92c8ab02aba.trivial.rst b/news/7dab557a-8fb5-41c5-afae-a92c8ab02aba.trivial.rst new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/test_urls.py b/tests/unit/test_urls.py index 0c145255080..ea529616978 100644 --- a/tests/unit/test_urls.py +++ b/tests/unit/test_urls.py @@ -1,6 +1,6 @@ import os import sys -import urllib.request +import urllib.parse import pytest @@ -11,7 +11,7 @@ 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://" + urllib.request.pathname2url(path) + assert path_to_url("file") == "file://" + urllib.parse.quote(path) @pytest.mark.skipif("sys.platform != 'win32'") @@ -38,8 +38,9 @@ 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) + resolved_path = os.path.join(os.getcwd(), "file").replace("\\", "/") + quoted_path = urllib.parse.quote(resolved_path, safe="/:") + assert path_to_url("file") == "file:///" + quoted_path @pytest.mark.parametrize( From d0570644b7a14c8cb8e2832f56c071baadc716bf Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 24 Jul 2025 00:12:38 +0100 Subject: [PATCH 2/4] Reduce diff --- tests/unit/test_urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_urls.py b/tests/unit/test_urls.py index ea529616978..4cfb0b8e141 100644 --- a/tests/unit/test_urls.py +++ b/tests/unit/test_urls.py @@ -38,8 +38,8 @@ 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").replace("\\", "/") - quoted_path = urllib.parse.quote(resolved_path, safe="/:") + resolved_path = os.path.join(os.getcwd(), "file") + quoted_path = urllib.parse.quote(resolved_path.replace("\\", "/"), "/:") assert path_to_url("file") == "file:///" + quoted_path From 781d45232bad4dc7d2965e829d89d45a3356e331 Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 24 Jul 2025 00:52:37 +0100 Subject: [PATCH 3/4] Match unix test naming --- tests/unit/test_urls.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_urls.py b/tests/unit/test_urls.py index 4cfb0b8e141..6a04e8101d0 100644 --- a/tests/unit/test_urls.py +++ b/tests/unit/test_urls.py @@ -38,9 +38,8 @@ 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") - quoted_path = urllib.parse.quote(resolved_path.replace("\\", "/"), "/:") - assert path_to_url("file") == "file:///" + quoted_path + path = os.path.join(os.getcwd(), "file").replace("\\", "/") + assert path_to_url("file") == "file:///" + urllib.parse.quote(path, safe="/:") @pytest.mark.parametrize( From 508998bd957acbb6d1411b0d6e98f06105632511 Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 24 Jul 2025 18:02:28 +0100 Subject: [PATCH 4/4] Use uppercase drive letters --- tests/unit/test_urls.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_urls.py b/tests/unit/test_urls.py index 6a04e8101d0..9a82f0f84f1 100644 --- a/tests/unit/test_urls.py +++ b/tests/unit/test_urls.py @@ -18,8 +18,8 @@ def test_path_to_url_unix() -> None: @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: @@ -46,13 +46,13 @@ def test_relative_path_to_url_win() -> None: "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: