Skip to content

Commit 28b2b74

Browse files
authored
GH-112675: Move path joining tests into test_posixpath and test_ntpath (#112676)
In `test_pathlib`, the `check_drive_root_parts` test methods evaluated both joining and parsing/normalisation of paths. This dates from a time when pathlib implemented both functions itself, but nowadays path joining is done with `posixpath.join()` and `ntpath.join()`. This commit moves the joining-related test cases into `test_posixpath` and `test_ntpath`.
1 parent 2c3906b commit 28b2b74

File tree

3 files changed

+96
-106
lines changed

3 files changed

+96
-106
lines changed

Lib/test/test_ntpath.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def test_join(self):
256256
tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
257257
tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
258258
tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
259+
tester('ntpath.join("a", "b", "c\\")', 'a\\b\\c\\')
259260
tester('ntpath.join("a", "b", "\\c")', '\\c')
260261
tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
261262
tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
@@ -313,6 +314,16 @@ def test_join(self):
313314
tester("ntpath.join('\\\\computer\\', 'share')", '\\\\computer\\share')
314315
tester("ntpath.join('\\\\computer\\share\\', 'a')", '\\\\computer\\share\\a')
315316
tester("ntpath.join('\\\\computer\\share\\a\\', 'b')", '\\\\computer\\share\\a\\b')
317+
# Second part is anchored, so that the first part is ignored.
318+
tester("ntpath.join('a', 'Z:b', 'c')", 'Z:b\\c')
319+
tester("ntpath.join('a', 'Z:\\b', 'c')", 'Z:\\b\\c')
320+
tester("ntpath.join('a', '\\\\b\\c', 'd')", '\\\\b\\c\\d')
321+
# Second part has a root but not drive.
322+
tester("ntpath.join('a', '\\b', 'c')", '\\b\\c')
323+
tester("ntpath.join('Z:/a', '/b', 'c')", 'Z:\\b\\c')
324+
tester("ntpath.join('//?/Z:/a', '/b', 'c')", '\\\\?\\Z:\\b\\c')
325+
tester("ntpath.join('D:a', './c:b')", 'D:a\\.\\c:b')
326+
tester("ntpath.join('D:/a', './c:b')", 'D:\\a\\.\\c:b')
316327

317328
def test_normpath(self):
318329
tester("ntpath.normpath('A//////././//.//B')", r'A\B')

Lib/test/test_pathlib.py

Lines changed: 65 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -160,45 +160,30 @@ def with_segments(self, *pathsegments):
160160
for parent in p.parents:
161161
self.assertEqual(42, parent.session_id)
162162

163-
def _get_drive_root_parts(self, parts):
164-
path = self.cls(*parts)
165-
return path.drive, path.root, path.parts
166-
167-
def _check_drive_root_parts(self, arg, *expected):
163+
def _check_parse_path(self, raw_path, *expected):
168164
sep = self.pathmod.sep
169-
actual = self._get_drive_root_parts([x.replace('/', sep) for x in arg])
165+
actual = self.cls._parse_path(raw_path.replace('/', sep))
170166
self.assertEqual(actual, expected)
171167
if altsep := self.pathmod.altsep:
172-
actual = self._get_drive_root_parts([x.replace('/', altsep) for x in arg])
168+
actual = self.cls._parse_path(raw_path.replace('/', altsep))
173169
self.assertEqual(actual, expected)
174170

175-
def test_drive_root_parts_common(self):
176-
check = self._check_drive_root_parts
171+
def test_parse_path_common(self):
172+
check = self._check_parse_path
177173
sep = self.pathmod.sep
178-
# Unanchored parts.
179-
check((), '', '', ())
180-
check(('a',), '', '', ('a',))
181-
check(('a/',), '', '', ('a',))
182-
check(('a', 'b'), '', '', ('a', 'b'))
183-
# Expansion.
184-
check(('a/b',), '', '', ('a', 'b'))
185-
check(('a/b/',), '', '', ('a', 'b'))
186-
check(('a', 'b/c', 'd'), '', '', ('a', 'b', 'c', 'd'))
187-
# Collapsing and stripping excess slashes.
188-
check(('a', 'b//c', 'd'), '', '', ('a', 'b', 'c', 'd'))
189-
check(('a', 'b/c/', 'd'), '', '', ('a', 'b', 'c', 'd'))
190-
# Eliminating standalone dots.
191-
check(('.',), '', '', ())
192-
check(('.', '.', 'b'), '', '', ('b',))
193-
check(('a', '.', 'b'), '', '', ('a', 'b'))
194-
check(('a', '.', '.'), '', '', ('a',))
195-
# The first part is anchored.
196-
check(('/a/b',), '', sep, (sep, 'a', 'b'))
197-
check(('/a', 'b'), '', sep, (sep, 'a', 'b'))
198-
check(('/a/', 'b'), '', sep, (sep, 'a', 'b'))
199-
# Ignoring parts before an anchored part.
200-
check(('a', '/b', 'c'), '', sep, (sep, 'b', 'c'))
201-
check(('a', '/b', '/c'), '', sep, (sep, 'c'))
174+
check('', '', '', [])
175+
check('a', '', '', ['a'])
176+
check('a/', '', '', ['a'])
177+
check('a/b', '', '', ['a', 'b'])
178+
check('a/b/', '', '', ['a', 'b'])
179+
check('a/b/c/d', '', '', ['a', 'b', 'c', 'd'])
180+
check('a/b//c/d', '', '', ['a', 'b', 'c', 'd'])
181+
check('a/b/c/d', '', '', ['a', 'b', 'c', 'd'])
182+
check('.', '', '', [])
183+
check('././b', '', '', ['b'])
184+
check('a/./b', '', '', ['a', 'b'])
185+
check('a/./.', '', '', ['a'])
186+
check('/a/b', '', sep, ['a', 'b'])
202187

203188
def test_join_common(self):
204189
P = self.cls
@@ -792,17 +777,17 @@ def test_repr_roundtrips(self):
792777
class PurePosixPathTest(PurePathTest):
793778
cls = pathlib.PurePosixPath
794779

795-
def test_drive_root_parts(self):
796-
check = self._check_drive_root_parts
780+
def test_parse_path(self):
781+
check = self._check_parse_path
797782
# Collapsing of excess leading slashes, except for the double-slash
798783
# special case.
799-
check(('//a', 'b'), '', '//', ('//', 'a', 'b'))
800-
check(('///a', 'b'), '', '/', ('/', 'a', 'b'))
801-
check(('////a', 'b'), '', '/', ('/', 'a', 'b'))
784+
check('//a/b', '', '//', ['a', 'b'])
785+
check('///a/b', '', '/', ['a', 'b'])
786+
check('////a/b', '', '/', ['a', 'b'])
802787
# Paths which look like NT paths aren't treated specially.
803-
check(('c:a',), '', '', ('c:a',))
804-
check(('c:\\a',), '', '', ('c:\\a',))
805-
check(('\\a',), '', '', ('\\a',))
788+
check('c:a', '', '', ['c:a',])
789+
check('c:\\a', '', '', ['c:\\a',])
790+
check('\\a', '', '', ['\\a',])
806791

807792
def test_root(self):
808793
P = self.cls
@@ -900,67 +885,53 @@ class PureWindowsPathTest(PurePathTest):
900885
],
901886
})
902887

903-
def test_drive_root_parts(self):
904-
check = self._check_drive_root_parts
888+
def test_parse_path(self):
889+
check = self._check_parse_path
905890
# First part is anchored.
906-
check(('c:',), 'c:', '', ('c:',))
907-
check(('c:/',), 'c:', '\\', ('c:\\',))
908-
check(('/',), '', '\\', ('\\',))
909-
check(('c:a',), 'c:', '', ('c:', 'a'))
910-
check(('c:/a',), 'c:', '\\', ('c:\\', 'a'))
911-
check(('/a',), '', '\\', ('\\', 'a'))
912-
# UNC paths.
913-
check(('//',), '\\\\', '', ('\\\\',))
914-
check(('//a',), '\\\\a', '', ('\\\\a',))
915-
check(('//a/',), '\\\\a\\', '', ('\\\\a\\',))
916-
check(('//a/b',), '\\\\a\\b', '\\', ('\\\\a\\b\\',))
917-
check(('//a/b/',), '\\\\a\\b', '\\', ('\\\\a\\b\\',))
918-
check(('//a/b/c',), '\\\\a\\b', '\\', ('\\\\a\\b\\', 'c'))
919-
# Second part is anchored, so that the first part is ignored.
920-
check(('a', 'Z:b', 'c'), 'Z:', '', ('Z:', 'b', 'c'))
921-
check(('a', 'Z:/b', 'c'), 'Z:', '\\', ('Z:\\', 'b', 'c'))
891+
check('c:', 'c:', '', [])
892+
check('c:/', 'c:', '\\', [])
893+
check('/', '', '\\', [])
894+
check('c:a', 'c:', '', ['a'])
895+
check('c:/a', 'c:', '\\', ['a'])
896+
check('/a', '', '\\', ['a'])
922897
# UNC paths.
923-
check(('a', '//b/c', 'd'), '\\\\b\\c', '\\', ('\\\\b\\c\\', 'd'))
898+
check('//', '\\\\', '', [])
899+
check('//a', '\\\\a', '', [])
900+
check('//a/', '\\\\a\\', '', [])
901+
check('//a/b', '\\\\a\\b', '\\', [])
902+
check('//a/b/', '\\\\a\\b', '\\', [])
903+
check('//a/b/c', '\\\\a\\b', '\\', ['c'])
924904
# Collapsing and stripping excess slashes.
925-
check(('a', 'Z://b//c/', 'd/'), 'Z:', '\\', ('Z:\\', 'b', 'c', 'd'))
905+
check('Z://b//c/d/', 'Z:', '\\', ['b', 'c', 'd'])
926906
# UNC paths.
927-
check(('a', '//b/c//', 'd'), '\\\\b\\c', '\\', ('\\\\b\\c\\', 'd'))
907+
check('//b/c//d', '\\\\b\\c', '\\', ['d'])
928908
# Extended paths.
929-
check(('//./c:',), '\\\\.\\c:', '', ('\\\\.\\c:',))
930-
check(('//?/c:/',), '\\\\?\\c:', '\\', ('\\\\?\\c:\\',))
931-
check(('//?/c:/a',), '\\\\?\\c:', '\\', ('\\\\?\\c:\\', 'a'))
932-
check(('//?/c:/a', '/b'), '\\\\?\\c:', '\\', ('\\\\?\\c:\\', 'b'))
909+
check('//./c:', '\\\\.\\c:', '', [])
910+
check('//?/c:/', '\\\\?\\c:', '\\', [])
911+
check('//?/c:/a', '\\\\?\\c:', '\\', ['a'])
933912
# Extended UNC paths (format is "\\?\UNC\server\share").
934-
check(('//?',), '\\\\?', '', ('\\\\?',))
935-
check(('//?/',), '\\\\?\\', '', ('\\\\?\\',))
936-
check(('//?/UNC',), '\\\\?\\UNC', '', ('\\\\?\\UNC',))
937-
check(('//?/UNC/',), '\\\\?\\UNC\\', '', ('\\\\?\\UNC\\',))
938-
check(('//?/UNC/b',), '\\\\?\\UNC\\b', '', ('\\\\?\\UNC\\b',))
939-
check(('//?/UNC/b/',), '\\\\?\\UNC\\b\\', '', ('\\\\?\\UNC\\b\\',))
940-
check(('//?/UNC/b/c',), '\\\\?\\UNC\\b\\c', '\\', ('\\\\?\\UNC\\b\\c\\',))
941-
check(('//?/UNC/b/c/',), '\\\\?\\UNC\\b\\c', '\\', ('\\\\?\\UNC\\b\\c\\',))
942-
check(('//?/UNC/b/c/d',), '\\\\?\\UNC\\b\\c', '\\', ('\\\\?\\UNC\\b\\c\\', 'd'))
913+
check('//?', '\\\\?', '', [])
914+
check('//?/', '\\\\?\\', '', [])
915+
check('//?/UNC', '\\\\?\\UNC', '', [])
916+
check('//?/UNC/', '\\\\?\\UNC\\', '', [])
917+
check('//?/UNC/b', '\\\\?\\UNC\\b', '', [])
918+
check('//?/UNC/b/', '\\\\?\\UNC\\b\\', '', [])
919+
check('//?/UNC/b/c', '\\\\?\\UNC\\b\\c', '\\', [])
920+
check('//?/UNC/b/c/', '\\\\?\\UNC\\b\\c', '\\', [])
921+
check('//?/UNC/b/c/d', '\\\\?\\UNC\\b\\c', '\\', ['d'])
943922
# UNC device paths
944-
check(('//./BootPartition/',), '\\\\.\\BootPartition', '\\', ('\\\\.\\BootPartition\\',))
945-
check(('//?/BootPartition/',), '\\\\?\\BootPartition', '\\', ('\\\\?\\BootPartition\\',))
946-
check(('//./PhysicalDrive0',), '\\\\.\\PhysicalDrive0', '', ('\\\\.\\PhysicalDrive0',))
947-
check(('//?/Volume{}/',), '\\\\?\\Volume{}', '\\', ('\\\\?\\Volume{}\\',))
948-
check(('//./nul',), '\\\\.\\nul', '', ('\\\\.\\nul',))
949-
# Second part has a root but not drive.
950-
check(('a', '/b', 'c'), '', '\\', ('\\', 'b', 'c'))
951-
check(('Z:/a', '/b', 'c'), 'Z:', '\\', ('Z:\\', 'b', 'c'))
952-
check(('//?/Z:/a', '/b', 'c'), '\\\\?\\Z:', '\\', ('\\\\?\\Z:\\', 'b', 'c'))
953-
# Joining with the same drive => the first path is appended to if
954-
# the second path is relative.
955-
check(('c:/a/b', 'c:x/y'), 'c:', '\\', ('c:\\', 'a', 'b', 'x', 'y'))
956-
check(('c:/a/b', 'c:/x/y'), 'c:', '\\', ('c:\\', 'x', 'y'))
923+
check('//./BootPartition/', '\\\\.\\BootPartition', '\\', [])
924+
check('//?/BootPartition/', '\\\\?\\BootPartition', '\\', [])
925+
check('//./PhysicalDrive0', '\\\\.\\PhysicalDrive0', '', [])
926+
check('//?/Volume{}/', '\\\\?\\Volume{}', '\\', [])
927+
check('//./nul', '\\\\.\\nul', '', [])
957928
# Paths to files with NTFS alternate data streams
958-
check(('./c:s',), '', '', ('c:s',))
959-
check(('cc:s',), '', '', ('cc:s',))
960-
check(('C:c:s',), 'C:', '', ('C:', 'c:s'))
961-
check(('C:/c:s',), 'C:', '\\', ('C:\\', 'c:s'))
962-
check(('D:a', './c:b'), 'D:', '', ('D:', 'a', 'c:b'))
963-
check(('D:/a', './c:b'), 'D:', '\\', ('D:\\', 'a', 'c:b'))
929+
check('./c:s', '', '', ['c:s'])
930+
check('cc:s', '', '', ['cc:s'])
931+
check('C:c:s', 'C:', '', ['c:s'])
932+
check('C:/c:s', 'C:', '\\', ['c:s'])
933+
check('D:a/c:b', 'D:', '', ['a', 'c:b'])
934+
check('D:/a/c:b', 'D:', '\\', ['a', 'c:b'])
964935

965936
def test_str(self):
966937
p = self.cls('a/b/c')

Lib/test/test_posixpath.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,26 @@ def tearDown(self):
4747
safe_rmdir(os_helper.TESTFN + suffix)
4848

4949
def test_join(self):
50-
self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"),
51-
"/bar/baz")
52-
self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
53-
self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"),
54-
"/foo/bar/baz/")
55-
56-
self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"),
57-
b"/bar/baz")
58-
self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"),
59-
b"/foo/bar/baz")
60-
self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"),
61-
b"/foo/bar/baz/")
50+
fn = posixpath.join
51+
self.assertEqual(fn("/foo", "bar", "/bar", "baz"), "/bar/baz")
52+
self.assertEqual(fn("/foo", "bar", "baz"), "/foo/bar/baz")
53+
self.assertEqual(fn("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
54+
55+
self.assertEqual(fn(b"/foo", b"bar", b"/bar", b"baz"), b"/bar/baz")
56+
self.assertEqual(fn(b"/foo", b"bar", b"baz"), b"/foo/bar/baz")
57+
self.assertEqual(fn(b"/foo/", b"bar/", b"baz/"), b"/foo/bar/baz/")
58+
59+
self.assertEqual(fn("a", "b"), "a/b")
60+
self.assertEqual(fn("a", "b/"), "a/b/")
61+
self.assertEqual(fn("a/", "b"), "a/b")
62+
self.assertEqual(fn("a/", "b/"), "a/b/")
63+
self.assertEqual(fn("a", "b/c", "d"), "a/b/c/d")
64+
self.assertEqual(fn("a", "b//c", "d"), "a/b//c/d")
65+
self.assertEqual(fn("a", "b/c/", "d"), "a/b/c/d")
66+
self.assertEqual(fn("/a", "b"), "/a/b")
67+
self.assertEqual(fn("/a/", "b"), "/a/b")
68+
self.assertEqual(fn("a", "/b", "c"), "/b/c")
69+
self.assertEqual(fn("a", "/b", "/c"), "/c")
6270

6371
def test_split(self):
6472
self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))

0 commit comments

Comments
 (0)