mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Issue #10395: Added os.path.commonpath(). Implemented in posixpath and ntpath.
Based on patch by Rafik Draoui.
This commit is contained in:
parent
dd83bd2f9c
commit
3822093143
7 changed files with 255 additions and 5 deletions
|
@ -522,6 +522,60 @@ class PosixPathTest(unittest.TestCase):
|
|||
finally:
|
||||
os.getcwdb = real_getcwdb
|
||||
|
||||
def test_commonpath(self):
|
||||
def check(paths, expected):
|
||||
self.assertEqual(posixpath.commonpath(paths), expected)
|
||||
self.assertEqual(posixpath.commonpath([os.fsencode(p) for p in paths]),
|
||||
os.fsencode(expected))
|
||||
def check_error(exc, paths):
|
||||
self.assertRaises(exc, posixpath.commonpath, paths)
|
||||
self.assertRaises(exc, posixpath.commonpath,
|
||||
[os.fsencode(p) for p in paths])
|
||||
|
||||
self.assertRaises(ValueError, posixpath.commonpath, [])
|
||||
check_error(ValueError, ['/usr', 'usr'])
|
||||
check_error(ValueError, ['usr', '/usr'])
|
||||
|
||||
check(['/usr/local'], '/usr/local')
|
||||
check(['/usr/local', '/usr/local'], '/usr/local')
|
||||
check(['/usr/local/', '/usr/local'], '/usr/local')
|
||||
check(['/usr/local/', '/usr/local/'], '/usr/local')
|
||||
check(['/usr//local', '//usr/local'], '/usr/local')
|
||||
check(['/usr/./local', '/./usr/local'], '/usr/local')
|
||||
check(['/', '/dev'], '/')
|
||||
check(['/usr', '/dev'], '/')
|
||||
check(['/usr/lib/', '/usr/lib/python3'], '/usr/lib')
|
||||
check(['/usr/lib/', '/usr/lib64/'], '/usr')
|
||||
|
||||
check(['/usr/lib', '/usr/lib64'], '/usr')
|
||||
check(['/usr/lib/', '/usr/lib64'], '/usr')
|
||||
|
||||
check(['spam'], 'spam')
|
||||
check(['spam', 'spam'], 'spam')
|
||||
check(['spam', 'alot'], '')
|
||||
check(['and/jam', 'and/spam'], 'and')
|
||||
check(['and//jam', 'and/spam//'], 'and')
|
||||
check(['and/./jam', './and/spam'], 'and')
|
||||
check(['and/jam', 'and/spam', 'alot'], '')
|
||||
check(['and/jam', 'and/spam', 'and'], 'and')
|
||||
|
||||
check([''], '')
|
||||
check(['', 'spam/alot'], '')
|
||||
check_error(ValueError, ['', '/spam/alot'])
|
||||
|
||||
self.assertRaises(TypeError, posixpath.commonpath,
|
||||
[b'/usr/lib/', '/usr/lib/python3'])
|
||||
self.assertRaises(TypeError, posixpath.commonpath,
|
||||
[b'/usr/lib/', 'usr/lib/python3'])
|
||||
self.assertRaises(TypeError, posixpath.commonpath,
|
||||
[b'usr/lib/', '/usr/lib/python3'])
|
||||
self.assertRaises(TypeError, posixpath.commonpath,
|
||||
['/usr/lib/', b'/usr/lib/python3'])
|
||||
self.assertRaises(TypeError, posixpath.commonpath,
|
||||
['/usr/lib/', b'usr/lib/python3'])
|
||||
self.assertRaises(TypeError, posixpath.commonpath,
|
||||
['usr/lib/', b'/usr/lib/python3'])
|
||||
|
||||
|
||||
class PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase):
|
||||
pathmodule = posixpath
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue