from test
import test_support
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
):
def assertIs(self
, a
, b
):
# Check that normcase() is idempotent
p
= posixpath
.normcase(p
)
self
.assertEqual(p
, posixpath
.normcase(p
))
self
.assertRaises(TypeError, posixpath
.normcase
)
self
.assertEqual(posixpath
.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
self
.assertEqual(posixpath
.join("/foo", "bar", "baz"), "/foo/bar/baz")
self
.assertEqual(posixpath
.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
self
.assertRaises(TypeError, posixpath
.join
)
def test_splitdrive(self
):
self
.assertEqual(posixpath
.splitdrive("/foo/bar"), ("", "/foo/bar"))
self
.assertRaises(TypeError, posixpath
.splitdrive
)
self
.assertEqual(posixpath
.split("/foo/bar"), ("/foo", "bar"))
self
.assertEqual(posixpath
.split("/"), ("/", ""))
self
.assertEqual(posixpath
.split("foo"), ("", "foo"))
self
.assertEqual(posixpath
.split("////foo"), ("////", "foo"))
self
.assertEqual(posixpath
.split("//foo//bar"), ("//foo", "bar"))
self
.assertRaises(TypeError, posixpath
.split
)
self
.assertEqual(posixpath
.splitext("foo.ext"), ("foo", ".ext"))
self
.assertEqual(posixpath
.splitext("/foo/foo.ext"), ("/foo/foo", ".ext"))
self
.assertEqual(posixpath
.splitext(".ext"), ("", ".ext"))
self
.assertEqual(posixpath
.splitext("/foo.ext/foo"), ("/foo.ext/foo", ""))
self
.assertEqual(posixpath
.splitext("foo.ext/"), ("foo.ext/", ""))
self
.assertEqual(posixpath
.splitext(""), ("", ""))
self
.assertEqual(posixpath
.splitext("foo.bar.ext"), ("foo.bar", ".ext"))
self
.assertRaises(TypeError, posixpath
.splitext
)
self
.assertIs(posixpath
.isabs(""), False)
self
.assertIs(posixpath
.isabs("/"), True)
self
.assertIs(posixpath
.isabs("/foo"), True)
self
.assertIs(posixpath
.isabs("/foo/bar"), True)
self
.assertIs(posixpath
.isabs("foo/bar"), False)
self
.assertRaises(TypeError, posixpath
.isabs
)
def test_splitdrive(self
):
self
.assertEqual(posixpath
.splitdrive("/foo/bar"), ("", "/foo/bar"))
self
.assertRaises(TypeError, posixpath
.splitdrive
)
self
.assertEqual(posixpath
.basename("/foo/bar"), "bar")
self
.assertEqual(posixpath
.basename("/"), "")
self
.assertEqual(posixpath
.basename("foo"), "foo")
self
.assertEqual(posixpath
.basename("////foo"), "foo")
self
.assertEqual(posixpath
.basename("//foo//bar"), "bar")
self
.assertRaises(TypeError, posixpath
.basename
)
self
.assertEqual(posixpath
.dirname("/foo/bar"), "/foo")
self
.assertEqual(posixpath
.dirname("/"), "/")
self
.assertEqual(posixpath
.dirname("foo"), "")
self
.assertEqual(posixpath
.dirname("////foo"), "////")
self
.assertEqual(posixpath
.dirname("//foo//bar"), "//foo")
self
.assertRaises(TypeError, posixpath
.dirname
)
def test_commonprefix(self
):
posixpath
.commonprefix([]),
posixpath
.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
posixpath
.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
posixpath
.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
f
= open(test_support
.TESTFN
, "wb")
self
.assertEqual(posixpath
.getsize(test_support
.TESTFN
), 3)
os
.remove(test_support
.TESTFN
)
f
= open(test_support
.TESTFN
, "wb")
f
= open(test_support
.TESTFN
, "ab")
f
= open(test_support
.TESTFN
, "rb")
self
.assertEqual(d
, "foobar")
posixpath
.getctime(test_support
.TESTFN
) <=
posixpath
.getmtime(test_support
.TESTFN
)
os
.remove(test_support
.TESTFN
)
self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "1"), False)
f
= open(test_support
.TESTFN
+ "1", "wb")
self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "1"), False)
if hasattr(os
, "symlink"):
os
.symlink(test_support
.TESTFN
+ "1", test_support
.TESTFN
+ "2")
self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "2"), True)
os
.remove(test_support
.TESTFN
+ "1")
self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "2"), True)
self
.assertIs(posixpath
.exists(test_support
.TESTFN
+ "2"), False)
self
.assertIs(posixpath
.lexists(test_support
.TESTFN
+ "2"), True)
os
.remove(test_support
.TESTFN
+ "1")
os
.remove(test_support
.TESTFN
+ "2")
self
.assertRaises(TypeError, posixpath
.islink
)
self
.assertIs(posixpath
.exists(test_support
.TESTFN
), False)
f
= open(test_support
.TESTFN
, "wb")
self
.assertIs(posixpath
.exists(test_support
.TESTFN
), True)
self
.assertIs(posixpath
.lexists(test_support
.TESTFN
), True)
os
.remove(test_support
.TESTFN
)
self
.assertRaises(TypeError, posixpath
.exists
)
self
.assertIs(posixpath
.isdir(test_support
.TESTFN
), False)
f
= open(test_support
.TESTFN
, "wb")
self
.assertIs(posixpath
.isdir(test_support
.TESTFN
), False)
os
.remove(test_support
.TESTFN
)
os
.mkdir(test_support
.TESTFN
)
self
.assertIs(posixpath
.isdir(test_support
.TESTFN
), True)
os
.rmdir(test_support
.TESTFN
)
os
.remove(test_support
.TESTFN
)
os
.rmdir(test_support
.TESTFN
)
self
.assertRaises(TypeError, posixpath
.isdir
)
self
.assertIs(posixpath
.isfile(test_support
.TESTFN
), False)
f
= open(test_support
.TESTFN
, "wb")
self
.assertIs(posixpath
.isfile(test_support
.TESTFN
), True)
os
.remove(test_support
.TESTFN
)
os
.mkdir(test_support
.TESTFN
)
self
.assertIs(posixpath
.isfile(test_support
.TESTFN
), False)
os
.rmdir(test_support
.TESTFN
)
os
.remove(test_support
.TESTFN
)
os
.rmdir(test_support
.TESTFN
)
self
.assertRaises(TypeError, posixpath
.isdir
)
f
= open(test_support
.TESTFN
+ "1", "wb")
test_support
.TESTFN
+ "1",
test_support
.TESTFN
+ "1"
# If we don't have links, assume that os.stat doesn't return resonable
# inode information and thus, that samefile() doesn't work
if hasattr(os
, "symlink"):
test_support
.TESTFN
+ "1",
test_support
.TESTFN
+ "2"
test_support
.TESTFN
+ "1",
test_support
.TESTFN
+ "2"
os
.remove(test_support
.TESTFN
+ "2")
f
= open(test_support
.TESTFN
+ "2", "wb")
test_support
.TESTFN
+ "1",
test_support
.TESTFN
+ "2"
os
.remove(test_support
.TESTFN
+ "1")
os
.remove(test_support
.TESTFN
+ "2")
self
.assertRaises(TypeError, posixpath
.samefile
)
f
= open(test_support
.TESTFN
+ "1", "wb")
os
.stat(test_support
.TESTFN
+ "1"),
os
.stat(test_support
.TESTFN
+ "1")
# If we don't have links, assume that os.stat() doesn't return resonable
# inode information and thus, that samefile() doesn't work
if hasattr(os
, "symlink"):
if hasattr(os
, "symlink"):
os
.symlink(test_support
.TESTFN
+ "1", test_support
.TESTFN
+ "2")
os
.stat(test_support
.TESTFN
+ "1"),
os
.stat(test_support
.TESTFN
+ "2")
os
.remove(test_support
.TESTFN
+ "2")
f
= open(test_support
.TESTFN
+ "2", "wb")
os
.stat(test_support
.TESTFN
+ "1"),
os
.stat(test_support
.TESTFN
+ "2")
os
.remove(test_support
.TESTFN
+ "1")
os
.remove(test_support
.TESTFN
+ "2")
self
.assertRaises(TypeError, posixpath
.samestat
)
self
.assertIs(posixpath
.ismount("/"), True)
self
.assertRaises(TypeError, posixpath
.ismount
)
def test_expanduser(self
):
self
.assertEqual(posixpath
.expanduser("foo"), "foo")
self
.assert_(isinstance(posixpath
.expanduser("~/"), basestring
))
# if home directory == root directory, this test makes no sense
if posixpath
.expanduser("~") != '/':
posixpath
.expanduser("~") + "/",
posixpath
.expanduser("~/")
self
.assert_(isinstance(posixpath
.expanduser("~root/"), basestring
))
self
.assert_(isinstance(posixpath
.expanduser("~foo/"), basestring
))
self
.assertRaises(TypeError, posixpath
.expanduser
)
def test_expandvars(self
):
oldenv
= os
.environ
.copy()
os
.environ
["foo"] = "bar"
os
.environ
["{foo"] = "baz1"
os
.environ
["{foo}"] = "baz2"
self
.assertEqual(posixpath
.expandvars("foo"), "foo")
self
.assertEqual(posixpath
.expandvars("$foo bar"), "bar bar")
self
.assertEqual(posixpath
.expandvars("${foo}bar"), "barbar")
self
.assertEqual(posixpath
.expandvars("$[foo]bar"), "$[foo]bar")
self
.assertEqual(posixpath
.expandvars("$bar bar"), "$bar bar")
self
.assertEqual(posixpath
.expandvars("$?bar"), "$?bar")
self
.assertEqual(posixpath
.expandvars("${foo}bar"), "barbar")
self
.assertEqual(posixpath
.expandvars("$foo}bar"), "bar}bar")
self
.assertEqual(posixpath
.expandvars("${foo"), "${foo")
self
.assertEqual(posixpath
.expandvars("${{foo}}"), "baz1}")
os
.environ
.update(oldenv
)
self
.assertRaises(TypeError, posixpath
.expandvars
)
self
.assertEqual(posixpath
.normpath(""), ".")
self
.assertEqual(posixpath
.normpath("/"), "/")
self
.assertEqual(posixpath
.normpath("//"), "//")
self
.assertEqual(posixpath
.normpath("///"), "/")
self
.assertEqual(posixpath
.normpath("///foo/.//bar//"), "/foo/bar")
self
.assertEqual(posixpath
.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
self
.assertEqual(posixpath
.normpath("///..//./foo/.//bar"), "/foo/bar")
self
.assertRaises(TypeError, posixpath
.normpath
)
self
.assert_("foo" in posixpath
.abspath("foo"))
self
.assertRaises(TypeError, posixpath
.abspath
)
self
.assert_("foo" in realpath("foo"))
self
.assertRaises(TypeError, posixpath
.realpath
)
if hasattr(os
, "symlink"):
def test_realpath_basic(self
):
os
.symlink(ABSTFN
+"1", ABSTFN
)
self
.assertEqual(realpath(ABSTFN
), ABSTFN
+"1")
def test_realpath_symlink_loops(self
):
# Bug #930024, return the path unchanged if we get into an infinite
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
)
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/.
os
.symlink(ABSTFN
+ "/y", ABSTFN
+ "/k")
self
.assertEqual(realpath("a"), ABSTFN
+ "/y/a")
self
.safe_remove(ABSTFN
+ "/k")
self
.safe_rmdir(ABSTFN
+ "/y")
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:
# and a symbolic link 'link-y' pointing to 'y' in directory 'a',
# then realpath("link-y/..") should return 'k', not 'a'.
os
.mkdir(ABSTFN
+ "/k/y")
os
.symlink(ABSTFN
+ "/k/y", ABSTFN
+ "/link-y")
self
.assertEqual(realpath(ABSTFN
+ "/link-y/.."), ABSTFN
+ "/k")
os
.chdir(dirname(ABSTFN
))
self
.assertEqual(realpath(basename(ABSTFN
) + "/link-y/.."), ABSTFN
+ "/k")
self
.safe_remove(ABSTFN
+ "/link-y")
self
.safe_rmdir(ABSTFN
+ "/k/y")
self
.safe_rmdir(ABSTFN
+ "/k")
def test_realpath_resolve_first(self
):
# Bug #1213894: The first component of the path, if not absolute,
os
.symlink(ABSTFN
, ABSTFN
+ "link")
os
.chdir(dirname(ABSTFN
))
self
.assertEqual(realpath(base
+ "link"), ABSTFN
)
self
.assertEqual(realpath(base
+ "link/k"), ABSTFN
+ "/k")
self
.safe_remove(ABSTFN
+ "link")
self
.safe_rmdir(ABSTFN
+ "/k")
# Convenience functions for removing temporary files.
def pass_os_error(self
, func
, filename
):
def safe_remove(self
, filename
):
self
.pass_os_error(os
.remove
, filename
)
def safe_rmdir(self
, dirname
):
self
.pass_os_error(os
.rmdir
, dirname
)
test_support
.run_unittest(PosixPathTest
)