summaryrefslogtreecommitdiff
path: root/Lib/test/test_ntpath.py
diff options
context:
space:
mode:
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 dacddded3d..580f2030a3 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