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

Skip to content

Commit 6742f14

Browse files
authored
GH-125866: Improve tests for pathname2url() and url2pathname() (#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()`.
1 parent 802d405 commit 6742f14

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
@@ -1520,39 +1519,86 @@ def test_quoting(self):
15201519
(expect, result))
15211520

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

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

15571603
class Utility_Tests(unittest.TestCase):
15581604
"""Testcase to test the various utility functions in the urllib."""
@@ -1636,60 +1682,5 @@ def test_with_method_arg(self):
16361682
self.assertEqual(request.get_method(), 'HEAD')
16371683

16381684

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

0 commit comments

Comments
 (0)