gh-101000: Add os.path.splitroot() (#101002)

Co-authored-by: Eryk Sun <eryksun@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Barney Gale 2023-01-27 00:28:27 +00:00 committed by GitHub
parent 37f15a5efa
commit e5b08ddddf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 279 additions and 165 deletions

View file

@ -115,6 +115,32 @@ class PosixPathTest(unittest.TestCase):
self.splitextTest("........", "........", "")
self.splitextTest("", "", "")
def test_splitroot(self):
f = posixpath.splitroot
self.assertEqual(f(''), ('', '', ''))
self.assertEqual(f('a'), ('', '', 'a'))
self.assertEqual(f('a/b'), ('', '', 'a/b'))
self.assertEqual(f('a/b/'), ('', '', 'a/b/'))
self.assertEqual(f('/a'), ('', '/', 'a'))
self.assertEqual(f('/a/b'), ('', '/', 'a/b'))
self.assertEqual(f('/a/b/'), ('', '/', 'a/b/'))
# The root is collapsed when there are redundant slashes
# except when there are exactly two leading slashes, which
# is a special case in POSIX.
self.assertEqual(f('//a'), ('', '//', 'a'))
self.assertEqual(f('///a'), ('', '/', '//a'))
self.assertEqual(f('///a/b'), ('', '/', '//a/b'))
# Paths which look like NT paths aren't treated specially.
self.assertEqual(f('c:/a/b'), ('', '', 'c:/a/b'))
self.assertEqual(f('\\/a/b'), ('', '', '\\/a/b'))
self.assertEqual(f('\\a\\b'), ('', '', '\\a\\b'))
# Byte paths are supported
self.assertEqual(f(b''), (b'', b'', b''))
self.assertEqual(f(b'a'), (b'', b'', b'a'))
self.assertEqual(f(b'/a'), (b'', b'/', b'a'))
self.assertEqual(f(b'//a'), (b'', b'//', b'a'))
self.assertEqual(f(b'///a'), (b'', b'/', b'//a'))
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
@ -752,6 +778,9 @@ class PathLikeTests(unittest.TestCase):
def test_path_splitdrive(self):
self.assertPathEqual(self.path.splitdrive)
def test_path_splitroot(self):
self.assertPathEqual(self.path.splitroot)
def test_path_basename(self):
self.assertPathEqual(self.path.basename)