Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b267b62

Browse files
[3.12] GH-125866: Improve tests for pathname2url() and url2pathname() (GH-125993) (#126145)
GH-125866: Improve tests for `pathname2url()` and `url2pathname()` (GH-125993) Merge `URL2PathNameTests` and `PathName2URLTests` test cases (which test only the Windows-specific implementations from `nturl2path`) into the main `Pathname_Tests` test case for these functions. Copy/port some test cases for `pathlib.Path.as_uri()` and `from_uri()`. (cherry picked from commit 6742f14) Co-authored-by: Barney Gale <[email protected]>
1 parent db0a1b8 commit b267b62

File tree

1 file changed

+77
-86
lines changed

1 file changed

+77
-86
lines changed

Lib/test/test_urllib.py

Lines changed: 77 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
ssl = None
2020
import sys
2121
import tempfile
22-
from nturl2path import url2pathname, pathname2url
2322

2423
from base64 import b64encode
2524
import collections
@@ -1529,39 +1528,86 @@ def test_quoting(self):
15291528
(expect, result))
15301529

15311530
@unittest.skipUnless(sys.platform == 'win32',
1532-
'test specific to the nturl2path functions.')
1533-
def test_prefixes(self):
1531+
'test specific to Windows pathnames.')
1532+
def test_pathname2url_win(self):
15341533
# Test special prefixes are correctly handled in pathname2url()
1535-
given = '\\\\?\\C:\\dir'
1536-
expect = '///C:/dir'
1537-
result = urllib.request.pathname2url(given)
1538-
self.assertEqual(expect, result,
1539-
"pathname2url() failed; %s != %s" %
1540-
(expect, result))
1541-
given = '\\\\?\\unc\\server\\share\\dir'
1542-
expect = '/server/share/dir'
1543-
result = urllib.request.pathname2url(given)
1544-
self.assertEqual(expect, result,
1545-
"pathname2url() failed; %s != %s" %
1546-
(expect, result))
1547-
1534+
fn = urllib.request.pathname2url
1535+
self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir')
1536+
self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '/server/share/dir')
1537+
self.assertEqual(fn("C:"), '///C:')
1538+
self.assertEqual(fn("C:\\"), '///C:')
1539+
self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c')
1540+
self.assertEqual(fn('C:\\a\\b%#c'), '///C:/a/b%25%23c')
1541+
self.assertEqual(fn('C:\\a\\b\xe9'), '///C:/a/b%C3%A9')
1542+
self.assertEqual(fn('C:\\foo\\bar\\spam.foo'), "///C:/foo/bar/spam.foo")
1543+
# Long drive letter
1544+
self.assertRaises(IOError, fn, "XX:\\")
1545+
# No drive letter
1546+
self.assertEqual(fn("\\folder\\test\\"), '/folder/test/')
1547+
self.assertEqual(fn("\\\\folder\\test\\"), '////folder/test/')
1548+
self.assertEqual(fn("\\\\\\folder\\test\\"), '/////folder/test/')
1549+
self.assertEqual(fn('\\\\some\\share\\'), '////some/share/')
1550+
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '////some/share/a/b.c')
1551+
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '////some/share/a/b%25%23c%C3%A9')
1552+
# Round-tripping
1553+
urls = ['///C:',
1554+
'/////folder/test/',
1555+
'///C:/foo/bar/spam.foo']
1556+
for url in urls:
1557+
self.assertEqual(fn(urllib.request.url2pathname(url)), url)
1558+
1559+
@unittest.skipIf(sys.platform == 'win32',
1560+
'test specific to POSIX pathnames')
1561+
def test_pathname2url_posix(self):
1562+
fn = urllib.request.pathname2url
1563+
self.assertEqual(fn('/'), '/')
1564+
self.assertEqual(fn('/a/b.c'), '/a/b.c')
1565+
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
15481566

15491567
@unittest.skipUnless(sys.platform == 'win32',
1550-
'test specific to the urllib.url2path function.')
1551-
def test_ntpath(self):
1552-
given = ('/C:/', '///C:/', '/C|//')
1553-
expect = 'C:\\'
1554-
for url in given:
1555-
result = urllib.request.url2pathname(url)
1556-
self.assertEqual(expect, result,
1557-
'urllib.request..url2pathname() failed; %s != %s' %
1558-
(expect, result))
1559-
given = '///C|/path'
1560-
expect = 'C:\\path'
1561-
result = urllib.request.url2pathname(given)
1562-
self.assertEqual(expect, result,
1563-
'urllib.request.url2pathname() failed; %s != %s' %
1564-
(expect, result))
1568+
'test specific to Windows pathnames.')
1569+
def test_url2pathname_win(self):
1570+
fn = urllib.request.url2pathname
1571+
self.assertEqual(fn('/C:/'), 'C:\\')
1572+
self.assertEqual(fn("///C|"), 'C:')
1573+
self.assertEqual(fn("///C:"), 'C:')
1574+
self.assertEqual(fn('///C:/'), 'C:\\')
1575+
self.assertEqual(fn('/C|//'), 'C:\\')
1576+
self.assertEqual(fn('///C|/path'), 'C:\\path')
1577+
# No DOS drive
1578+
self.assertEqual(fn("///C/test/"), '\\\\\\C\\test\\')
1579+
self.assertEqual(fn("////C/test/"), '\\\\C\\test\\')
1580+
# DOS drive paths
1581+
self.assertEqual(fn('C:/path/to/file'), 'C:\\path\\to\\file')
1582+
self.assertEqual(fn('C|/path/to/file'), 'C:\\path\\to\\file')
1583+
self.assertEqual(fn('/C|/path/to/file'), 'C:\\path\\to\\file')
1584+
self.assertEqual(fn('///C|/path/to/file'), 'C:\\path\\to\\file')
1585+
self.assertEqual(fn("///C|/foo/bar/spam.foo"), 'C:\\foo\\bar\\spam.foo')
1586+
# Non-ASCII drive letter
1587+
self.assertRaises(IOError, fn, "///\u00e8|/")
1588+
# UNC paths
1589+
self.assertEqual(fn('//server/path/to/file'), '\\\\server\\path\\to\\file')
1590+
self.assertEqual(fn('////server/path/to/file'), '\\\\server\\path\\to\\file')
1591+
self.assertEqual(fn('/////server/path/to/file'), '\\\\\\server\\path\\to\\file')
1592+
# Localhost paths
1593+
self.assertEqual(fn('//localhost/C:/path/to/file'), 'C:\\path\\to\\file')
1594+
self.assertEqual(fn('//localhost/C|/path/to/file'), 'C:\\path\\to\\file')
1595+
# Round-tripping
1596+
paths = ['C:',
1597+
r'\\\C\test\\',
1598+
r'C:\foo\bar\spam.foo']
1599+
for path in paths:
1600+
self.assertEqual(fn(urllib.request.pathname2url(path)), path)
1601+
1602+
@unittest.skipIf(sys.platform == 'win32',
1603+
'test specific to POSIX pathnames')
1604+
def test_url2pathname_posix(self):
1605+
fn = urllib.request.url2pathname
1606+
self.assertEqual(fn('/foo/bar'), '/foo/bar')
1607+
self.assertEqual(fn('//foo/bar'), '//foo/bar')
1608+
self.assertEqual(fn('///foo/bar'), '///foo/bar')
1609+
self.assertEqual(fn('////foo/bar'), '////foo/bar')
1610+
self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar')
15651611

15661612
class Utility_Tests(unittest.TestCase):
15671613
"""Testcase to test the various utility functions in the urllib."""
@@ -1645,60 +1691,5 @@ def test_with_method_arg(self):
16451691
self.assertEqual(request.get_method(), 'HEAD')
16461692

16471693

1648-
class URL2PathNameTests(unittest.TestCase):
1649-
1650-
def test_converting_drive_letter(self):
1651-
self.assertEqual(url2pathname("///C|"), 'C:')
1652-
self.assertEqual(url2pathname("///C:"), 'C:')
1653-
self.assertEqual(url2pathname("///C|/"), 'C:\\')
1654-
1655-
def test_converting_when_no_drive_letter(self):
1656-
# cannot end a raw string in \
1657-
self.assertEqual(url2pathname("///C/test/"), r'\\\C\test' '\\')
1658-
self.assertEqual(url2pathname("////C/test/"), r'\\C\test' '\\')
1659-
1660-
def test_simple_compare(self):
1661-
self.assertEqual(url2pathname("///C|/foo/bar/spam.foo"),
1662-
r'C:\foo\bar\spam.foo')
1663-
1664-
def test_non_ascii_drive_letter(self):
1665-
self.assertRaises(IOError, url2pathname, "///\u00e8|/")
1666-
1667-
def test_roundtrip_url2pathname(self):
1668-
list_of_paths = ['C:',
1669-
r'\\\C\test\\',
1670-
r'C:\foo\bar\spam.foo'
1671-
]
1672-
for path in list_of_paths:
1673-
self.assertEqual(url2pathname(pathname2url(path)), path)
1674-
1675-
class PathName2URLTests(unittest.TestCase):
1676-
1677-
def test_converting_drive_letter(self):
1678-
self.assertEqual(pathname2url("C:"), '///C:')
1679-
self.assertEqual(pathname2url("C:\\"), '///C:')
1680-
1681-
def test_converting_when_no_drive_letter(self):
1682-
self.assertEqual(pathname2url(r"\\\folder\test" "\\"),
1683-
'/////folder/test/')
1684-
self.assertEqual(pathname2url(r"\\folder\test" "\\"),
1685-
'////folder/test/')
1686-
self.assertEqual(pathname2url(r"\folder\test" "\\"),
1687-
'/folder/test/')
1688-
1689-
def test_simple_compare(self):
1690-
self.assertEqual(pathname2url(r'C:\foo\bar\spam.foo'),
1691-
"///C:/foo/bar/spam.foo" )
1692-
1693-
def test_long_drive_letter(self):
1694-
self.assertRaises(IOError, pathname2url, "XX:\\")
1695-
1696-
def test_roundtrip_pathname2url(self):
1697-
list_of_paths = ['///C:',
1698-
'/////folder/test/',
1699-
'///C:/foo/bar/spam.foo']
1700-
for path in list_of_paths:
1701-
self.assertEqual(pathname2url(url2pathname(path)), path)
1702-
17031694
if __name__ == '__main__':
17041695
unittest.main()

0 commit comments

Comments
 (0)