summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_ntpath.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-31 12:31:53 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-31 12:31:53 (GMT)
commit38220931433ab2d83892170e96a4d66764ce5338 (patch)
tree29fe3fe23b61b7627a30da3de0bc218fa9cdaa04 /Lib/test/test_ntpath.py
parentdd83bd2f9c8c328dc6b7655f33581e09df0e3611 (diff)
downloadcpython-38220931433ab2d83892170e96a4d66764ce5338.zip
cpython-38220931433ab2d83892170e96a4d66764ce5338.tar.gz
cpython-38220931433ab2d83892170e96a4d66764ce5338.tar.bz2
Issue #10395: Added os.path.commonpath(). Implemented in posixpath and ntpath.
Based on patch by Rafik Draoui.
Diffstat (limited to 'Lib/test/test_ntpath.py')
-rw-r--r--Lib/test/test_ntpath.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index dacddde..580f203 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -330,6 +330,75 @@ class TestNtpath(unittest.TestCase):
tester('ntpath.relpath("/a/b", "/a/b")', '.')
tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
+ def test_commonpath(self):
+ def check(paths, expected):
+ tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
+ expected)
+ def check_error(exc, paths):
+ self.assertRaises(exc, ntpath.commonpath, paths)
+ self.assertRaises(exc, ntpath.commonpath,
+ [os.fsencode(p) for p in paths])
+
+ self.assertRaises(ValueError, ntpath.commonpath, [])
+ check_error(ValueError, ['C:\\Program Files', 'Program Files'])
+ check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
+ check_error(ValueError, ['\\Program Files', 'Program Files'])
+ check_error(ValueError, ['Program Files', 'C:\\Program Files'])
+ check(['C:\\Program Files'], 'C:\\Program Files')
+ check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
+ check(['C:\\Program Files\\', 'C:\\Program Files'],
+ 'C:\\Program Files')
+ check(['C:\\Program Files\\', 'C:\\Program Files\\'],
+ 'C:\\Program Files')
+ check(['C:\\\\Program Files', 'C:\\Program Files\\\\'],
+ 'C:\\Program Files')
+ check(['C:\\.\\Program Files', 'C:\\Program Files\\.'],
+ 'C:\\Program Files')
+ check(['C:\\', 'C:\\bin'], 'C:\\')
+ check(['C:\\Program Files', 'C:\\bin'], 'C:\\')
+ check(['C:\\Program Files', 'C:\\Program Files\\Bar'],
+ 'C:\\Program Files')
+ check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'],
+ 'C:\\Program Files')
+ check(['C:\\Program Files', 'C:\\Projects'], 'C:\\')
+ check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\')
+
+ check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'],
+ 'C:\\Program Files')
+ check(['C:\\Program Files\\Foo', 'c:/program files/bar'],
+ 'C:\\Program Files')
+ check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
+ 'c:\\program files')
+
+ check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
+
+ 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(['C:and\\jam', 'C:and\\spam'], 'C:and')
+
+ check([''], '')
+ check(['', 'spam\\alot'], '')
+ check_error(ValueError, ['', '\\spam\\alot'])
+
+ self.assertRaises(TypeError, ntpath.commonpath,
+ [b'C:\\Program Files', 'C:\\Program Files\\Foo'])
+ self.assertRaises(TypeError, ntpath.commonpath,
+ [b'C:\\Program Files', 'Program Files\\Foo'])
+ self.assertRaises(TypeError, ntpath.commonpath,
+ [b'Program Files', 'C:\\Program Files\\Foo'])
+ self.assertRaises(TypeError, ntpath.commonpath,
+ ['C:\\Program Files', b'C:\\Program Files\\Foo'])
+ self.assertRaises(TypeError, ntpath.commonpath,
+ ['C:\\Program Files', b'Program Files\\Foo'])
+ self.assertRaises(TypeError, ntpath.commonpath,
+ ['Program Files', b'C:\\Program Files\\Foo'])
+
def test_sameopenfile(self):
with TemporaryFile() as tf1, TemporaryFile() as tf2:
# Make sure the same file is really the same