Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(192725)

Side by Side Diff: Lib/test/test_ntpath.py

Issue 10395: new os.path function to extract common prefix based on path components
Patch Set: Created 5 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/posixpath.py ('k') | Lib/test/test_posixpath.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import ntpath 1 import ntpath
2 import os 2 import os
3 import sys 3 import sys
4 import unittest 4 import unittest
5 import warnings 5 import warnings
6 from test.support import TestFailed 6 from test.support import TestFailed
7 from test import support, test_genericpath 7 from test import support, test_genericpath
8 from tempfile import TemporaryFile 8 from tempfile import TemporaryFile
9 9
10 10
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar \\bat') 320 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar \\bat')
321 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') 321 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
322 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') 322 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
323 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') 323 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
324 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') 324 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
325 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') 325 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
326 tester('ntpath.relpath("/", "/")', '.') 326 tester('ntpath.relpath("/", "/")', '.')
327 tester('ntpath.relpath("/a", "/a")', '.') 327 tester('ntpath.relpath("/a", "/a")', '.')
328 tester('ntpath.relpath("/a/b", "/a/b")', '.') 328 tester('ntpath.relpath("/a/b", "/a/b")', '.')
329 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') 329 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
330
331 def test_commonpath(self):
332 def check(paths, expected):
333 tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
334 expected)
335 def check_error(exc, paths):
336 self.assertRaises(exc, ntpath.commonpath, paths)
337 self.assertRaises(exc, ntpath.commonpath,
338 [os.fsencode(p) for p in paths])
339
340 self.assertRaises(ValueError, ntpath.commonpath, [])
341 check_error(ValueError, ['C:\\Program Files', 'Program Files'])
342 check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
343 check_error(ValueError, ['\\Program Files', 'Program Files'])
344 check_error(ValueError, ['Program Files', 'C:\\Program Files'])
345 check(['C:\\Program Files'], 'C:\\Program Files')
346 check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
347 check(['C:\\Program Files\\', 'C:\\Program Files'],
348 'C:\\Program Files')
349 check(['C:\\Program Files\\', 'C:\\Program Files\\'],
350 'C:\\Program Files')
351 check(['C:\\\\Program Files', 'C:\\Program Files\\\\'],
352 'C:\\Program Files')
353 check(['C:\\.\\Program Files', 'C:\\Program Files\\.'],
354 'C:\\Program Files')
355 check(['C:\\', 'C:\\bin'], 'C:\\')
356 check(['C:\\Program Files', 'C:\\bin'], 'C:\\')
357 check(['C:\\Program Files', 'C:\\Program Files\\Bar'],
358 'C:\\Program Files')
359 check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'],
360 'C:\\Program Files')
361 check(['C:\\Program Files', 'C:\\Projects'], 'C:\\')
362 check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\')
363
364 check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'],
365 'C:\\Program Files')
366 check(['C:\\Program Files\\Foo', 'c:/program files/bar'],
367 'C:\\Program Files')
368 check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
369 'c:\\program files')
370
371 check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
372
373 check(['spam'], 'spam')
374 check(['spam', 'spam'], 'spam')
375 check(['spam', 'alot'], '')
376 check(['and\\jam', 'and\\spam'], 'and')
377 check(['and\\\\jam', 'and\\spam\\\\'], 'and')
378 check(['and\\.\\jam', '.\\and\\spam'], 'and')
379 check(['and\\jam', 'and\\spam', 'alot'], '')
380 check(['and\\jam', 'and\\spam', 'and'], 'and')
381 check(['C:and\\jam', 'C:and\\spam'], 'C:and')
382
383 check([''], '')
384 check(['', 'spam\\alot'], '')
385 check_error(ValueError, ['', '\\spam\\alot'])
386
387 self.assertRaises(TypeError, ntpath.commonpath,
388 [b'C:\\Program Files', 'C:\\Program Files\\Foo'])
389 self.assertRaises(TypeError, ntpath.commonpath,
390 [b'C:\\Program Files', 'Program Files\\Foo'])
391 self.assertRaises(TypeError, ntpath.commonpath,
392 [b'Program Files', 'C:\\Program Files\\Foo'])
393 self.assertRaises(TypeError, ntpath.commonpath,
394 ['C:\\Program Files', b'C:\\Program Files\\Foo'])
395 self.assertRaises(TypeError, ntpath.commonpath,
396 ['C:\\Program Files', b'Program Files\\Foo'])
397 self.assertRaises(TypeError, ntpath.commonpath,
398 ['Program Files', b'C:\\Program Files\\Foo'])
330 399
331 def test_sameopenfile(self): 400 def test_sameopenfile(self):
332 with TemporaryFile() as tf1, TemporaryFile() as tf2: 401 with TemporaryFile() as tf1, TemporaryFile() as tf2:
333 # Make sure the same file is really the same 402 # Make sure the same file is really the same
334 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) 403 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
335 # Make sure different files are really different 404 # Make sure different files are really different
336 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) 405 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
337 # Make sure invalid values don't cause issues on win32 406 # Make sure invalid values don't cause issues on win32
338 if sys.platform == "win32": 407 if sys.platform == "win32":
339 with self.assertRaises(OSError): 408 with self.assertRaises(OSError):
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$")) 445 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$"))
377 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\")) 446 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))
378 447
379 class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase): 448 class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
380 pathmodule = ntpath 449 pathmodule = ntpath
381 attributes = ['relpath', 'splitunc'] 450 attributes = ['relpath', 'splitunc']
382 451
383 452
384 if __name__ == "__main__": 453 if __name__ == "__main__":
385 unittest.main() 454 unittest.main()
OLDNEW
« no previous file with comments | « Lib/posixpath.py ('k') | Lib/test/test_posixpath.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+