Issue #10395: Added os.path.commonpath(). Implemented in posixpath and ntpath.

Based on patch by Rafik Draoui.
This commit is contained in:
Serhiy Storchaka 2015-03-31 15:31:53 +03:00
parent dd83bd2f9c
commit 3822093143
7 changed files with 255 additions and 5 deletions

View file

@ -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