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

Delta Between Two Patch Sets: Lib/test/test_posixpath.py

Issue 10395: new os.path function to extract common prefix based on path components
Left Patch Set: Created 6 years, 8 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_ntpath.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import itertools 1 import itertools
2 import os 2 import os
3 import posixpath 3 import posixpath
4 import sys 4 import sys
5 import unittest 5 import unittest
6 import warnings 6 import warnings
7 from posixpath import realpath, abspath, dirname, basename 7 from posixpath import realpath, abspath, dirname, basename
8 from test import support, test_genericpath 8 from test import support, test_genericpath
9 9
10 try: 10 try:
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 if support.can_symlink(): 178 if support.can_symlink():
179 os.symlink(support.TESTFN + "1", support.TESTFN + "2") 179 os.symlink(support.TESTFN + "1", support.TESTFN + "2")
180 self.assertIs(posixpath.islink(support.TESTFN + "2"), True) 180 self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
181 os.remove(support.TESTFN + "1") 181 os.remove(support.TESTFN + "1")
182 self.assertIs(posixpath.islink(support.TESTFN + "2"), True) 182 self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
183 self.assertIs(posixpath.exists(support.TESTFN + "2"), False) 183 self.assertIs(posixpath.exists(support.TESTFN + "2"), False)
184 self.assertIs(posixpath.lexists(support.TESTFN + "2"), True) 184 self.assertIs(posixpath.lexists(support.TESTFN + "2"), True)
185 finally: 185 finally:
186 if not f.close(): 186 if not f.close():
187 f.close() 187 f.close()
188
189 @staticmethod
190 def _create_file(filename):
191 with open(filename, 'wb') as f:
192 f.write(b'foo')
193
194 def test_samefile(self):
195 test_fn = support.TESTFN + "1"
196 self._create_file(test_fn)
197 self.assertTrue(posixpath.samefile(test_fn, test_fn))
198 self.assertRaises(TypeError, posixpath.samefile)
199
200 @unittest.skipIf(
201 sys.platform.startswith('win'),
202 "posixpath.samefile does not work on links in Windows")
203 @unittest.skipUnless(hasattr(os, "symlink"),
204 "Missing symlink implementation")
205 def test_samefile_on_links(self):
206 test_fn1 = support.TESTFN + "1"
207 test_fn2 = support.TESTFN + "2"
208 self._create_file(test_fn1)
209
210 os.symlink(test_fn1, test_fn2)
211 self.assertTrue(posixpath.samefile(test_fn1, test_fn2))
212 os.remove(test_fn2)
213
214 self._create_file(test_fn2)
215 self.assertFalse(posixpath.samefile(test_fn1, test_fn2))
216
217
218 def test_samestat(self):
219 test_fn = support.TESTFN + "1"
220 self._create_file(test_fn)
221 test_fns = [test_fn]*2
222 stats = map(os.stat, test_fns)
223 self.assertTrue(posixpath.samestat(*stats))
224
225 @unittest.skipIf(
226 sys.platform.startswith('win'),
227 "posixpath.samestat does not work on links in Windows")
228 @unittest.skipUnless(hasattr(os, "symlink"),
229 "Missing symlink implementation")
230 def test_samestat_on_links(self):
231 test_fn1 = support.TESTFN + "1"
232 test_fn2 = support.TESTFN + "2"
233 self._create_file(test_fn1)
234 test_fns = (test_fn1, test_fn2)
235 os.symlink(*test_fns)
236 stats = map(os.stat, test_fns)
237 self.assertTrue(posixpath.samestat(*stats))
238 os.remove(test_fn2)
239
240 self._create_file(test_fn2)
241 stats = map(os.stat, test_fns)
242 self.assertFalse(posixpath.samestat(*stats))
243
244 self.assertRaises(TypeError, posixpath.samestat)
245 188
246 def test_ismount(self): 189 def test_ismount(self):
247 self.assertIs(posixpath.ismount("/"), True) 190 self.assertIs(posixpath.ismount("/"), True)
248 with warnings.catch_warnings(): 191 with warnings.catch_warnings():
249 warnings.simplefilter("ignore", DeprecationWarning) 192 warnings.simplefilter("ignore", DeprecationWarning)
250 self.assertIs(posixpath.ismount(b"/"), True) 193 self.assertIs(posixpath.ismount(b"/"), True)
251 194
252 def test_ismount_non_existent(self): 195 def test_ismount_non_existent(self):
253 # Non-existent mountpoint. 196 # Non-existent mountpoint.
254 self.assertIs(posixpath.ismount(ABSTFN), False) 197 self.assertIs(posixpath.ismount(ABSTFN), False)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) 254 self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes)
312 self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) 255 self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
313 256
314 with support.EnvironmentVarGuard() as env: 257 with support.EnvironmentVarGuard() as env:
315 env['HOME'] = '/' 258 env['HOME'] = '/'
316 self.assertEqual(posixpath.expanduser("~"), "/") 259 self.assertEqual(posixpath.expanduser("~"), "/")
317 self.assertEqual(posixpath.expanduser("~/foo"), "/foo") 260 self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
318 # expanduser should fall back to using the password database 261 # expanduser should fall back to using the password database
319 del env['HOME'] 262 del env['HOME']
320 home = pwd.getpwuid(os.getuid()).pw_dir 263 home = pwd.getpwuid(os.getuid()).pw_dir
321 self.assertEqual(posixpath.expanduser("~"), home) 264 # $HOME can end with a trailing /, so strip it (see #17809)
265 self.assertEqual(posixpath.expanduser("~"), home.rstrip("/"))
322 266
323 def test_normpath(self): 267 def test_normpath(self):
324 self.assertEqual(posixpath.normpath(""), ".") 268 self.assertEqual(posixpath.normpath(""), ".")
325 self.assertEqual(posixpath.normpath("/"), "/") 269 self.assertEqual(posixpath.normpath("/"), "/")
326 self.assertEqual(posixpath.normpath("//"), "//") 270 self.assertEqual(posixpath.normpath("//"), "//")
327 self.assertEqual(posixpath.normpath("///"), "/") 271 self.assertEqual(posixpath.normpath("///"), "/")
328 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") 272 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
329 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), 273 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"),
330 "/foo/baz") 274 "/foo/baz")
331 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") 275 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
332 276
333 self.assertEqual(posixpath.normpath(b""), b".") 277 self.assertEqual(posixpath.normpath(b""), b".")
334 self.assertEqual(posixpath.normpath(b"/"), b"/") 278 self.assertEqual(posixpath.normpath(b"/"), b"/")
335 self.assertEqual(posixpath.normpath(b"//"), b"//") 279 self.assertEqual(posixpath.normpath(b"//"), b"//")
336 self.assertEqual(posixpath.normpath(b"///"), b"/") 280 self.assertEqual(posixpath.normpath(b"///"), b"/")
337 self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") 281 self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar")
338 self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), 282 self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"),
339 b"/foo/baz") 283 b"/foo/baz")
340 self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), 284 self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"),
341 b"/foo/bar") 285 b"/foo/bar")
342 286
287 @skip_if_ABSTFN_contains_backslash
288 def test_realpath_curdir(self):
289 self.assertEqual(realpath('.'), os.getcwd())
290 self.assertEqual(realpath('./.'), os.getcwd())
291 self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())
292
293 self.assertEqual(realpath(b'.'), os.getcwdb())
294 self.assertEqual(realpath(b'./.'), os.getcwdb())
295 self.assertEqual(realpath(b'/'.join([b'.'] * 100)), os.getcwdb())
296
297 @skip_if_ABSTFN_contains_backslash
298 def test_realpath_pardir(self):
299 self.assertEqual(realpath('..'), dirname(os.getcwd()))
300 self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))
301 self.assertEqual(realpath('/'.join(['..'] * 100)), '/')
302
303 self.assertEqual(realpath(b'..'), dirname(os.getcwdb()))
304 self.assertEqual(realpath(b'../..'), dirname(dirname(os.getcwdb())))
305 self.assertEqual(realpath(b'/'.join([b'..'] * 100)), b'/')
306
343 @unittest.skipUnless(hasattr(os, "symlink"), 307 @unittest.skipUnless(hasattr(os, "symlink"),
344 "Missing symlink implementation") 308 "Missing symlink implementation")
345 @skip_if_ABSTFN_contains_backslash 309 @skip_if_ABSTFN_contains_backslash
346 def test_realpath_basic(self): 310 def test_realpath_basic(self):
347 # Basic operation. 311 # Basic operation.
348 try: 312 try:
349 os.symlink(ABSTFN+"1", ABSTFN) 313 os.symlink(ABSTFN+"1", ABSTFN)
350 self.assertEqual(realpath(ABSTFN), ABSTFN+"1") 314 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
351 finally: 315 finally:
352 support.unlink(ABSTFN) 316 support.unlink(ABSTFN)
(...skipping 17 matching lines...) Expand all
370 try: 334 try:
371 old_path = abspath('.') 335 old_path = abspath('.')
372 os.symlink(ABSTFN, ABSTFN) 336 os.symlink(ABSTFN, ABSTFN)
373 self.assertEqual(realpath(ABSTFN), ABSTFN) 337 self.assertEqual(realpath(ABSTFN), ABSTFN)
374 338
375 os.symlink(ABSTFN+"1", ABSTFN+"2") 339 os.symlink(ABSTFN+"1", ABSTFN+"2")
376 os.symlink(ABSTFN+"2", ABSTFN+"1") 340 os.symlink(ABSTFN+"2", ABSTFN+"1")
377 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") 341 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
378 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") 342 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
379 343
344 self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x")
345 self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN))
346 self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")
347 os.symlink(ABSTFN+"x", ABSTFN+"y")
348 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),
349 ABSTFN + "y")
350 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),
351 ABSTFN + "1")
352
353 os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
354 self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b")
355
356 os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
357 basename(ABSTFN) + "c", ABSTFN+"c")
358 self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
359
380 # Test using relative path as well. 360 # Test using relative path as well.
381 os.chdir(dirname(ABSTFN)) 361 os.chdir(dirname(ABSTFN))
382 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) 362 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
383 finally: 363 finally:
384 os.chdir(old_path) 364 os.chdir(old_path)
385 support.unlink(ABSTFN) 365 support.unlink(ABSTFN)
386 support.unlink(ABSTFN+"1") 366 support.unlink(ABSTFN+"1")
387 support.unlink(ABSTFN+"2") 367 support.unlink(ABSTFN+"2")
368 support.unlink(ABSTFN+"y")
369 support.unlink(ABSTFN+"c")
370 support.unlink(ABSTFN+"a")
371
372 @unittest.skipUnless(hasattr(os, "symlink"),
373 "Missing symlink implementation")
374 @skip_if_ABSTFN_contains_backslash
375 def test_realpath_repeated_indirect_symlinks(self):
376 # Issue #6975.
377 try:
378 os.mkdir(ABSTFN)
379 os.symlink('../' + basename(ABSTFN), ABSTFN + '/self')
380 os.symlink('self/self/self', ABSTFN + '/link')
381 self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN)
382 finally:
383 support.unlink(ABSTFN + '/self')
384 support.unlink(ABSTFN + '/link')
385 safe_rmdir(ABSTFN)
386
387 @unittest.skipUnless(hasattr(os, "symlink"),
388 "Missing symlink implementation")
389 @skip_if_ABSTFN_contains_backslash
390 def test_realpath_deep_recursion(self):
391 depth = 10
392 old_path = abspath('.')
393 try:
394 os.mkdir(ABSTFN)
395 for i in range(depth):
396 os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1))
397 os.symlink('.', ABSTFN + '/0')
398 self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
399
400 # Test using relative path as well.
401 os.chdir(ABSTFN)
402 self.assertEqual(realpath('%d' % depth), ABSTFN)
403 finally:
404 os.chdir(old_path)
405 for i in range(depth + 1):
406 support.unlink(ABSTFN + '/%d' % i)
407 safe_rmdir(ABSTFN)
388 408
389 @unittest.skipUnless(hasattr(os, "symlink"), 409 @unittest.skipUnless(hasattr(os, "symlink"),
390 "Missing symlink implementation") 410 "Missing symlink implementation")
391 @skip_if_ABSTFN_contains_backslash 411 @skip_if_ABSTFN_contains_backslash
392 def test_realpath_resolve_parents(self): 412 def test_realpath_resolve_parents(self):
393 # We also need to resolve any symlinks in the parents of a relative 413 # We also need to resolve any symlinks in the parents of a relative
394 # path passed to realpath. E.g.: current working directory is 414 # path passed to realpath. E.g.: current working directory is
395 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call 415 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
396 # realpath("a"). This should return /usr/share/doc/a/. 416 # realpath("a"). This should return /usr/share/doc/a/.
397 try: 417 try:
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../ ../x') 531 self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../ ../x')
512 self.assertEqual(posixpath.relpath(b"/", b"/"), b'.') 532 self.assertEqual(posixpath.relpath(b"/", b"/"), b'.')
513 self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.') 533 self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.')
514 self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.') 534 self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.')
515 535
516 self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") 536 self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str")
517 self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") 537 self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes")
518 finally: 538 finally:
519 os.getcwdb = real_getcwdb 539 os.getcwdb = real_getcwdb
520 540
521 def test_sameopenfile(self):
522 fname = support.TESTFN + "1"
523 with open(fname, "wb") as a, open(fname, "wb") as b:
524 self.assertTrue(posixpath.sameopenfile(a.fileno(), b.fileno()))
525
526 def test_commonpath(self): 541 def test_commonpath(self):
527 542 def check(paths, expected):
528 self.assertIsNone(posixpath.commonpath([])) 543 self.assertEqual(posixpath.commonpath(paths), expected)
529 self.assertIsNone(posixpath.commonpath(['/usr', 'spam'])) 544 self.assertEqual(posixpath.commonpath([os.fsencode(p) for p in paths ]),
530 self.assertEqual(posixpath.commonpath(['/usr/local']), '/usr/local') 545 os.fsencode(expected))
531 self.assertEqual(posixpath.commonpath(['/usr/local', '/usr/local']), 546 def check_error(exc, paths):
532 '/usr/local') 547 self.assertRaises(exc, posixpath.commonpath, paths)
533 self.assertEqual(posixpath.commonpath(['/usr/local/', '/usr/local']), 548 self.assertRaises(exc, posixpath.commonpath,
534 '/usr/local') 549 [os.fsencode(p) for p in paths])
535 self.assertEqual(posixpath.commonpath(['/usr/local/', '/usr/local/']), 550
536 '/usr/local') 551 self.assertRaises(ValueError, posixpath.commonpath, [])
537 self.assertEqual(posixpath.commonpath(['/', '/dev']), '/') 552 check_error(ValueError, ['/usr', 'usr'])
538 self.assertEqual(posixpath.commonpath(['/usr', '/dev']), '/') 553 check_error(ValueError, ['usr', '/usr'])
539 self.assertEqual( 554
540 posixpath.commonpath(['/usr/lib/', '/usr/lib/python3']), 555 check(['/usr/local'], '/usr/local')
541 '/usr/lib') 556 check(['/usr/local', '/usr/local'], '/usr/local')
542 self.assertEqual( 557 check(['/usr/local/', '/usr/local'], '/usr/local')
543 posixpath.commonpath([b'/usr/lib/', b'/usr/lib/python3']), 558 check(['/usr/local/', '/usr/local/'], '/usr/local')
544 b'/usr/lib') 559 check(['/usr//local', '//usr/local'], '/usr/local')
545 self.assertEqual(posixpath.commonpath(['/usr/lib/', '/usr/lib64/']), 560 check(['/usr/./local', '/./usr/local'], '/usr/local')
storchaka 2012/11/06 15:45:05 Test without trailing '/' will be more interesting
546 '/usr') 561 check(['/', '/dev'], '/')
547 562 check(['/usr', '/dev'], '/')
548 self.assertEqual(posixpath.commonpath(['spam']), 'spam') 563 check(['/usr/lib/', '/usr/lib/python3'], '/usr/lib')
549 self.assertEqual(posixpath.commonpath(['spam', 'spam']), 'spam') 564 check(['/usr/lib/', '/usr/lib64/'], '/usr')
550 self.assertEqual(posixpath.commonpath(['spam', 'alot']), '') 565
551 self.assertEqual(posixpath.commonpath(['and/jam', 'and/spam']), 'and') 566 check(['/usr/lib', '/usr/lib64'], '/usr')
552 self.assertEqual( 567 check(['/usr/lib/', '/usr/lib64'], '/usr')
553 posixpath.commonpath(['and/jam', 'and/spam', 'alot']), '') 568
554 self.assertEqual( 569 check(['spam'], 'spam')
555 posixpath.commonpath(['and/jam', 'and/spam', 'and']), 'and') 570 check(['spam', 'spam'], 'spam')
571 check(['spam', 'alot'], '')
572 check(['and/jam', 'and/spam'], 'and')
573 check(['and//jam', 'and/spam//'], 'and')
574 check(['and/./jam', './and/spam'], 'and')
575 check(['and/jam', 'and/spam', 'alot'], '')
576 check(['and/jam', 'and/spam', 'and'], 'and')
577
578 check([''], '')
579 check(['', 'spam/alot'], '')
580 check_error(ValueError, ['', '/spam/alot'])
556 581
557 self.assertRaises(TypeError, posixpath.commonpath, 582 self.assertRaises(TypeError, posixpath.commonpath,
558 [b'/usr/lib/', '/usr/lib/python3']) 583 [b'/usr/lib/', '/usr/lib/python3'])
559 584 self.assertRaises(TypeError, posixpath.commonpath,
storchaka 2012/11/06 15:45:05 Tests with empty path ('') needed.
560 585 [b'/usr/lib/', 'usr/lib/python3'])
561 class PosixCommonTest(test_genericpath.CommonTest): 586 self.assertRaises(TypeError, posixpath.commonpath,
587 [b'usr/lib/', '/usr/lib/python3'])
588 self.assertRaises(TypeError, posixpath.commonpath,
589 ['/usr/lib/', b'/usr/lib/python3'])
590 self.assertRaises(TypeError, posixpath.commonpath,
591 ['/usr/lib/', b'usr/lib/python3'])
592 self.assertRaises(TypeError, posixpath.commonpath,
593 ['usr/lib/', b'/usr/lib/python3'])
594
595
596 class PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase):
562 pathmodule = posixpath 597 pathmodule = posixpath
563 attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] 598 attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
564 599
565 600
566 def test_main():
567 support.run_unittest(PosixPathTest, PosixCommonTest)
568
569
570 if __name__=="__main__": 601 if __name__=="__main__":
571 test_main() 602 unittest.main()
LEFTRIGHT

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