[Python-checkins] python/dist/src/Lib/test test_posixpath.py, 1.9,
1.10
jlgijsbers at users.sourceforge.net
jlgijsbers at users.sourceforge.net
Sat Aug 14 17:01:55 CEST 2004
Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27018/Lib/test
Modified Files:
test_posixpath.py
Log Message:
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
path, as normalizing the path may alter the meaning of the path if it contains
symlinks.
Also add tests for infinite symlink loops and parent symlinks that need to be
resolved.
Index: test_posixpath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_posixpath.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** test_posixpath.py 1 Jul 2003 03:33:31 -0000 1.9
--- test_posixpath.py 14 Aug 2004 15:01:53 -0000 1.10
***************
*** 3,6 ****
--- 3,12 ----
import posixpath, os
+ from posixpath import realpath, abspath, join, dirname, basename
+
+ # An absolute path to a temporary filename for testing. We can't rely on TESTFN
+ # being an absolute path, so we need this.
+
+ ABSTFN = abspath(test_support.TESTFN)
class PosixPathTest(unittest.TestCase):
***************
*** 390,396 ****
def test_realpath(self):
! self.assert_("foo" in posixpath.realpath("foo"))
!
self.assertRaises(TypeError, posixpath.realpath)
def test_main():
--- 396,489 ----
def test_realpath(self):
! self.assert_("foo" in realpath("foo"))
self.assertRaises(TypeError, posixpath.realpath)
+
+ if hasattr(os, "symlink"):
+ def test_realpath_basic(self):
+ # Basic operation.
+ try:
+ os.symlink(ABSTFN+"1", ABSTFN)
+ self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
+ finally:
+ self.safe_remove(ABSTFN)
+
+ def test_realpath_symlink_loops(self):
+ # Bug #930024, return the path unchanged if we get into an infinite
+ # symlink loop.
+ try:
+ old_path = abspath('.')
+ os.symlink(ABSTFN, ABSTFN)
+ self.assertEqual(realpath(ABSTFN), ABSTFN)
+
+ os.symlink(ABSTFN+"1", ABSTFN+"2")
+ os.symlink(ABSTFN+"2", ABSTFN+"1")
+ self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
+ self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
+
+ # Test using relative path as well.
+ os.chdir(dirname(ABSTFN))
+ self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
+ finally:
+ os.chdir(old_path)
+ self.safe_remove(ABSTFN)
+ self.safe_remove(ABSTFN+"1")
+ self.safe_remove(ABSTFN+"2")
+
+ def test_realpath_resolve_parents(self):
+ # We also need to resolve any symlinks in the parents of a relative
+ # path passed to realpath. E.g.: current working directory is
+ # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
+ # realpath("a"). This should return /usr/share/doc/a/.
+ try:
+ old_path = abspath('.')
+ os.mkdir(ABSTFN)
+ os.mkdir(ABSTFN + "/y")
+ os.symlink(ABSTFN + "/y", ABSTFN + "/k")
+
+ os.chdir(ABSTFN + "/k")
+ self.assertEqual(realpath("a"), ABSTFN + "/y/a")
+ finally:
+ os.chdir(old_path)
+ self.safe_remove(ABSTFN + "/k")
+ self.safe_rmdir(ABSTFN + "/y")
+ self.safe_rmdir(ABSTFN)
+
+ def test_realpath_resolve_before_normalizing(self):
+ # Bug #990669: Symbolic links should be resolved before we
+ # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
+ # in the following hierarchy:
+ # a/k/y
+ #
+ # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
+ # then realpath("link-y/..") should return 'k', not 'a'.
+ try:
+ old_path = abspath('.')
+ os.mkdir(ABSTFN)
+ os.mkdir(ABSTFN + "/k")
+ os.mkdir(ABSTFN + "/k/y")
+ os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
+
+ # Absolute path.
+ self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
+ # Relative path.
+ os.chdir(dirname(ABSTFN))
+ self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), ABSTFN + "/k")
+ finally:
+ os.chdir(old_path)
+ self.safe_remove(ABSTFN + "/link-y")
+ self.safe_rmdir(ABSTFN + "/k/y")
+ self.safe_rmdir(ABSTFN + "/k")
+ self.safe_rmdir(ABSTFN)
+
+ # Convenience functions for removing temporary files.
+ def pass_os_error(self, func, filename):
+ try: func(filename)
+ except OSError: pass
+
+ def safe_remove(self, filename):
+ self.pass_os_error(os.remove, filename)
+
+ def safe_rmdir(self, dirname):
+ self.pass_os_error(os.rmdir, dirname)
def test_main():
More information about the Python-checkins
mailing list