2010-03-06 14:07:18 -04:00
|
|
|
"""
|
|
|
|
Tests common to genericpath, macpath, ntpath and posixpath
|
|
|
|
"""
|
|
|
|
|
2006-08-26 15:42:06 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
import os
|
|
|
|
import genericpath
|
2010-05-13 13:18:14 -03:00
|
|
|
import sys
|
2006-08-26 15:42:06 -03:00
|
|
|
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def safe_rmdir(dirname):
|
|
|
|
try:
|
|
|
|
os.rmdir(dirname)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class GenericTest(unittest.TestCase):
|
|
|
|
# The path module to be tested
|
2010-03-06 14:52:52 -04:00
|
|
|
pathmodule = genericpath
|
2010-03-06 14:07:18 -04:00
|
|
|
common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
|
|
|
|
'getmtime', 'exists', 'isdir', 'isfile']
|
|
|
|
attributes = []
|
|
|
|
|
|
|
|
def test_no_argument(self):
|
|
|
|
for attr in self.common_attributes + self.attributes:
|
|
|
|
with self.assertRaises(TypeError):
|
2010-03-06 14:52:52 -04:00
|
|
|
getattr(self.pathmodule, attr)()
|
|
|
|
raise self.fail("{}.{}() did not raise a TypeError"
|
|
|
|
.format(self.pathmodule.__name__, attr))
|
2006-08-26 15:42:06 -03:00
|
|
|
|
|
|
|
def test_commonprefix(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
commonprefix = self.pathmodule.commonprefix
|
2006-08-26 15:42:06 -03:00
|
|
|
self.assertEqual(
|
2010-03-06 14:52:52 -04:00
|
|
|
commonprefix([]),
|
2006-08-26 15:42:06 -03:00
|
|
|
""
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2010-03-06 14:52:52 -04:00
|
|
|
commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
|
2006-08-26 15:42:06 -03:00
|
|
|
"/home/swen"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2010-03-06 14:52:52 -04:00
|
|
|
commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
|
2006-08-26 15:42:06 -03:00
|
|
|
"/home/swen/"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2010-03-06 14:52:52 -04:00
|
|
|
commonprefix(["/home/swen/spam", "/home/swen/spam"]),
|
2006-08-26 15:42:06 -03:00
|
|
|
"/home/swen/spam"
|
|
|
|
)
|
2010-03-08 08:39:35 -04:00
|
|
|
self.assertEqual(
|
|
|
|
commonprefix(["home:swenson:spam", "home:swen:spam"]),
|
|
|
|
"home:swen"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
commonprefix([":home:swen:spam", ":home:swen:eggs"]),
|
|
|
|
":home:swen:"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
commonprefix([":home:swen:spam", ":home:swen:spam"]),
|
|
|
|
":home:swen:spam"
|
|
|
|
)
|
2006-08-26 15:42:06 -03:00
|
|
|
|
2010-03-06 14:52:52 -04:00
|
|
|
testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
|
|
|
|
'aXc', 'abd', 'ab', 'aX', 'abcX']
|
2010-03-06 14:07:18 -04:00
|
|
|
for s1 in testlist:
|
|
|
|
for s2 in testlist:
|
2010-03-06 14:52:52 -04:00
|
|
|
p = commonprefix([s1, s2])
|
2010-03-06 14:07:18 -04:00
|
|
|
self.assertTrue(s1.startswith(p))
|
|
|
|
self.assertTrue(s2.startswith(p))
|
|
|
|
if s1 != s2:
|
|
|
|
n = len(p)
|
|
|
|
self.assertNotEqual(s1[n:n+1], s2[n:n+1])
|
|
|
|
|
2006-08-26 15:42:06 -03:00
|
|
|
def test_getsize(self):
|
|
|
|
f = open(test_support.TESTFN, "wb")
|
|
|
|
try:
|
|
|
|
f.write("foo")
|
|
|
|
f.close()
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3)
|
2006-08-26 15:42:06 -03:00
|
|
|
finally:
|
|
|
|
if not f.closed:
|
|
|
|
f.close()
|
2010-03-06 14:07:18 -04:00
|
|
|
test_support.unlink(test_support.TESTFN)
|
2006-08-26 15:42:06 -03:00
|
|
|
|
|
|
|
def test_time(self):
|
|
|
|
f = open(test_support.TESTFN, "wb")
|
|
|
|
try:
|
|
|
|
f.write("foo")
|
|
|
|
f.close()
|
|
|
|
f = open(test_support.TESTFN, "ab")
|
|
|
|
f.write("bar")
|
|
|
|
f.close()
|
|
|
|
f = open(test_support.TESTFN, "rb")
|
|
|
|
d = f.read()
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(d, "foobar")
|
|
|
|
|
2010-02-20 05:40:07 -04:00
|
|
|
self.assertLessEqual(
|
2010-03-06 14:52:52 -04:00
|
|
|
self.pathmodule.getctime(test_support.TESTFN),
|
|
|
|
self.pathmodule.getmtime(test_support.TESTFN)
|
2006-08-26 15:42:06 -03:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if not f.closed:
|
|
|
|
f.close()
|
2010-03-06 14:07:18 -04:00
|
|
|
test_support.unlink(test_support.TESTFN)
|
2006-08-26 15:42:06 -03:00
|
|
|
|
|
|
|
def test_exists(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.exists(test_support.TESTFN), False)
|
2006-08-26 15:42:06 -03:00
|
|
|
f = open(test_support.TESTFN, "wb")
|
|
|
|
try:
|
|
|
|
f.write("foo")
|
|
|
|
f.close()
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.exists(test_support.TESTFN), True)
|
|
|
|
if not self.pathmodule == genericpath:
|
|
|
|
self.assertIs(self.pathmodule.lexists(test_support.TESTFN),
|
|
|
|
True)
|
2006-08-26 15:42:06 -03:00
|
|
|
finally:
|
|
|
|
if not f.close():
|
|
|
|
f.close()
|
2010-03-06 14:07:18 -04:00
|
|
|
test_support.unlink(test_support.TESTFN)
|
2006-08-26 15:42:06 -03:00
|
|
|
|
|
|
|
def test_isdir(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
|
2006-08-26 15:42:06 -03:00
|
|
|
f = open(test_support.TESTFN, "wb")
|
|
|
|
try:
|
|
|
|
f.write("foo")
|
|
|
|
f.close()
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
|
2006-08-26 15:42:06 -03:00
|
|
|
os.remove(test_support.TESTFN)
|
|
|
|
os.mkdir(test_support.TESTFN)
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True)
|
2006-08-26 15:42:06 -03:00
|
|
|
os.rmdir(test_support.TESTFN)
|
|
|
|
finally:
|
|
|
|
if not f.close():
|
|
|
|
f.close()
|
2010-03-06 14:07:18 -04:00
|
|
|
test_support.unlink(test_support.TESTFN)
|
|
|
|
safe_rmdir(test_support.TESTFN)
|
2006-08-26 15:42:06 -03:00
|
|
|
|
|
|
|
def test_isfile(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
|
2006-08-26 15:42:06 -03:00
|
|
|
f = open(test_support.TESTFN, "wb")
|
|
|
|
try:
|
|
|
|
f.write("foo")
|
|
|
|
f.close()
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True)
|
2006-08-26 15:42:06 -03:00
|
|
|
os.remove(test_support.TESTFN)
|
|
|
|
os.mkdir(test_support.TESTFN)
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
|
2006-08-26 15:42:06 -03:00
|
|
|
os.rmdir(test_support.TESTFN)
|
|
|
|
finally:
|
|
|
|
if not f.close():
|
|
|
|
f.close()
|
2010-03-06 14:07:18 -04:00
|
|
|
test_support.unlink(test_support.TESTFN)
|
|
|
|
safe_rmdir(test_support.TESTFN)
|
|
|
|
|
|
|
|
|
2010-06-17 17:30:56 -03:00
|
|
|
# Following TestCase is not supposed to be run from test_genericpath.
|
|
|
|
# It is inherited by other test modules (macpath, ntpath, posixpath).
|
|
|
|
|
2010-03-06 14:07:18 -04:00
|
|
|
class CommonTest(GenericTest):
|
|
|
|
# The path module to be tested
|
2010-03-06 14:52:52 -04:00
|
|
|
pathmodule = None
|
2010-03-06 14:07:18 -04:00
|
|
|
common_attributes = GenericTest.common_attributes + [
|
|
|
|
# Properties
|
|
|
|
'curdir', 'pardir', 'extsep', 'sep',
|
|
|
|
'pathsep', 'defpath', 'altsep', 'devnull',
|
|
|
|
# Methods
|
|
|
|
'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
|
|
|
|
'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
|
|
|
|
'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_normcase(self):
|
|
|
|
# Check that normcase() is idempotent
|
|
|
|
p = "FoO/./BaR"
|
2010-03-06 14:52:52 -04:00
|
|
|
p = self.pathmodule.normcase(p)
|
|
|
|
self.assertEqual(p, self.pathmodule.normcase(p))
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def test_splitdrive(self):
|
|
|
|
# splitdrive for non-NT paths
|
2010-03-06 14:52:52 -04:00
|
|
|
splitdrive = self.pathmodule.splitdrive
|
|
|
|
self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
|
|
|
|
self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
|
|
|
|
self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def test_expandvars(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
if self.pathmodule.__name__ == 'macpath':
|
|
|
|
self.skipTest('macpath.expandvars is a stub')
|
|
|
|
expandvars = self.pathmodule.expandvars
|
2010-03-06 14:07:18 -04:00
|
|
|
with test_support.EnvironmentVarGuard() as env:
|
|
|
|
env.clear()
|
|
|
|
env["foo"] = "bar"
|
|
|
|
env["{foo"] = "baz1"
|
|
|
|
env["{foo}"] = "baz2"
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertEqual(expandvars("foo"), "foo")
|
|
|
|
self.assertEqual(expandvars("$foo bar"), "bar bar")
|
|
|
|
self.assertEqual(expandvars("${foo}bar"), "barbar")
|
|
|
|
self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
|
|
|
|
self.assertEqual(expandvars("$bar bar"), "$bar bar")
|
|
|
|
self.assertEqual(expandvars("$?bar"), "$?bar")
|
|
|
|
self.assertEqual(expandvars("${foo}bar"), "barbar")
|
|
|
|
self.assertEqual(expandvars("$foo}bar"), "bar}bar")
|
|
|
|
self.assertEqual(expandvars("${foo"), "${foo")
|
|
|
|
self.assertEqual(expandvars("${{foo}}"), "baz1}")
|
|
|
|
self.assertEqual(expandvars("$foo$foo"), "barbar")
|
|
|
|
self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def test_abspath(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIn("foo", self.pathmodule.abspath("foo"))
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
# Abspath returns bytes when the arg is bytes
|
2010-02-20 18:34:21 -04:00
|
|
|
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIsInstance(self.pathmodule.abspath(path), str)
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def test_realpath(self):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIn("foo", self.pathmodule.realpath("foo"))
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def test_normpath_issue5827(self):
|
|
|
|
# Make sure normpath preserves unicode
|
|
|
|
for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIsInstance(self.pathmodule.normpath(path), unicode)
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
def test_abspath_issue3426(self):
|
|
|
|
# Check that abspath returns unicode when the arg is unicode
|
|
|
|
# with both ASCII and non-ASCII cwds.
|
2010-03-06 14:52:52 -04:00
|
|
|
abspath = self.pathmodule.abspath
|
2010-03-06 14:07:18 -04:00
|
|
|
for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIsInstance(abspath(path), unicode)
|
2010-03-06 14:07:18 -04:00
|
|
|
|
|
|
|
unicwd = u'\xe7w\xf0'
|
|
|
|
try:
|
|
|
|
fsencoding = test_support.TESTFN_ENCODING or "ascii"
|
|
|
|
unicwd.encode(fsencoding)
|
|
|
|
except (AttributeError, UnicodeEncodeError):
|
|
|
|
# FS encoding is probably ASCII
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
with test_support.temp_cwd(unicwd):
|
|
|
|
for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
|
2010-03-06 14:52:52 -04:00
|
|
|
self.assertIsInstance(abspath(path), unicode)
|
2010-03-06 14:07:18 -04:00
|
|
|
|
2010-05-13 13:18:14 -03:00
|
|
|
@unittest.skipIf(sys.platform == 'darwin',
|
2010-05-13 13:22:15 -03:00
|
|
|
"Mac OS X denies the creation of a directory with an invalid utf8 name")
|
2010-05-13 13:18:14 -03:00
|
|
|
def test_nonascii_abspath(self):
|
2010-03-06 14:07:18 -04:00
|
|
|
# Test non-ASCII, non-UTF8 bytes in the path.
|
|
|
|
with test_support.temp_cwd('\xe7w\xf0'):
|
|
|
|
self.test_abspath()
|
2010-02-20 18:34:21 -04:00
|
|
|
|
|
|
|
|
2006-08-26 15:42:06 -03:00
|
|
|
def test_main():
|
2010-03-06 14:07:18 -04:00
|
|
|
test_support.run_unittest(GenericTest)
|
|
|
|
|
2006-08-26 15:42:06 -03:00
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
test_main()
|