mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
Create test_genericpath.CommonTest and reuse it to test other path modules.
This commit is contained in:
parent
58526417ed
commit
dc1531c5c4
4 changed files with 163 additions and 323 deletions
|
@ -1,38 +1,72 @@
|
||||||
|
"""
|
||||||
|
Tests common to genericpath, macpath, ntpath and posixpath
|
||||||
|
"""
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from test import test_support
|
from test import test_support
|
||||||
import os
|
import os
|
||||||
import genericpath
|
import genericpath
|
||||||
|
|
||||||
class AllCommonTest(unittest.TestCase):
|
|
||||||
|
def safe_rmdir(dirname):
|
||||||
|
try:
|
||||||
|
os.rmdir(dirname)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class GenericTest(unittest.TestCase):
|
||||||
|
# The path module to be tested
|
||||||
|
path = genericpath
|
||||||
|
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):
|
||||||
|
getattr(self.path, attr)()
|
||||||
|
raise self.fail("{}.{}() do not raise a TypeError"
|
||||||
|
.format(self.path.__name__, attr))
|
||||||
|
|
||||||
def test_commonprefix(self):
|
def test_commonprefix(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
genericpath.commonprefix([]),
|
self.path.commonprefix([]),
|
||||||
""
|
""
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
genericpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
|
self.path.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
|
||||||
"/home/swen"
|
"/home/swen"
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
genericpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
|
self.path.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
|
||||||
"/home/swen/"
|
"/home/swen/"
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
genericpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
|
self.path.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
|
||||||
"/home/swen/spam"
|
"/home/swen/spam"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', 'aXc', 'abd', 'ab', 'aX', 'abcX']
|
||||||
|
for s1 in testlist:
|
||||||
|
for s2 in testlist:
|
||||||
|
p = self.path.commonprefix([s1, s2])
|
||||||
|
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])
|
||||||
|
|
||||||
def test_getsize(self):
|
def test_getsize(self):
|
||||||
f = open(test_support.TESTFN, "wb")
|
f = open(test_support.TESTFN, "wb")
|
||||||
try:
|
try:
|
||||||
f.write("foo")
|
f.write("foo")
|
||||||
f.close()
|
f.close()
|
||||||
self.assertEqual(genericpath.getsize(test_support.TESTFN), 3)
|
self.assertEqual(self.path.getsize(test_support.TESTFN), 3)
|
||||||
finally:
|
finally:
|
||||||
if not f.closed:
|
if not f.closed:
|
||||||
f.close()
|
f.close()
|
||||||
os.remove(test_support.TESTFN)
|
test_support.unlink(test_support.TESTFN)
|
||||||
|
|
||||||
def test_time(self):
|
def test_time(self):
|
||||||
f = open(test_support.TESTFN, "wb")
|
f = open(test_support.TESTFN, "wb")
|
||||||
|
@ -48,147 +82,150 @@ class AllCommonTest(unittest.TestCase):
|
||||||
self.assertEqual(d, "foobar")
|
self.assertEqual(d, "foobar")
|
||||||
|
|
||||||
self.assertLessEqual(
|
self.assertLessEqual(
|
||||||
genericpath.getctime(test_support.TESTFN),
|
self.path.getctime(test_support.TESTFN),
|
||||||
genericpath.getmtime(test_support.TESTFN)
|
self.path.getmtime(test_support.TESTFN)
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
if not f.closed:
|
if not f.closed:
|
||||||
f.close()
|
f.close()
|
||||||
os.remove(test_support.TESTFN)
|
test_support.unlink(test_support.TESTFN)
|
||||||
|
|
||||||
def test_exists(self):
|
def test_exists(self):
|
||||||
self.assertIs(genericpath.exists(test_support.TESTFN), False)
|
self.assertIs(self.path.exists(test_support.TESTFN), False)
|
||||||
f = open(test_support.TESTFN, "wb")
|
f = open(test_support.TESTFN, "wb")
|
||||||
try:
|
try:
|
||||||
f.write("foo")
|
f.write("foo")
|
||||||
f.close()
|
f.close()
|
||||||
self.assertIs(genericpath.exists(test_support.TESTFN), True)
|
self.assertIs(self.path.exists(test_support.TESTFN), True)
|
||||||
|
if not self.path == genericpath:
|
||||||
|
self.assertIs(self.path.lexists(test_support.TESTFN), True)
|
||||||
finally:
|
finally:
|
||||||
if not f.close():
|
if not f.close():
|
||||||
f.close()
|
f.close()
|
||||||
try:
|
test_support.unlink(test_support.TESTFN)
|
||||||
os.remove(test_support.TESTFN)
|
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, genericpath.exists)
|
|
||||||
|
|
||||||
def test_isdir(self):
|
def test_isdir(self):
|
||||||
self.assertIs(genericpath.isdir(test_support.TESTFN), False)
|
self.assertIs(self.path.isdir(test_support.TESTFN), False)
|
||||||
f = open(test_support.TESTFN, "wb")
|
f = open(test_support.TESTFN, "wb")
|
||||||
try:
|
try:
|
||||||
f.write("foo")
|
f.write("foo")
|
||||||
f.close()
|
f.close()
|
||||||
self.assertIs(genericpath.isdir(test_support.TESTFN), False)
|
self.assertIs(self.path.isdir(test_support.TESTFN), False)
|
||||||
os.remove(test_support.TESTFN)
|
os.remove(test_support.TESTFN)
|
||||||
os.mkdir(test_support.TESTFN)
|
os.mkdir(test_support.TESTFN)
|
||||||
self.assertIs(genericpath.isdir(test_support.TESTFN), True)
|
self.assertIs(self.path.isdir(test_support.TESTFN), True)
|
||||||
os.rmdir(test_support.TESTFN)
|
os.rmdir(test_support.TESTFN)
|
||||||
finally:
|
finally:
|
||||||
if not f.close():
|
if not f.close():
|
||||||
f.close()
|
f.close()
|
||||||
try:
|
test_support.unlink(test_support.TESTFN)
|
||||||
os.remove(test_support.TESTFN)
|
safe_rmdir(test_support.TESTFN)
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
os.rmdir(test_support.TESTFN)
|
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, genericpath.isdir)
|
|
||||||
|
|
||||||
def test_isfile(self):
|
def test_isfile(self):
|
||||||
self.assertIs(genericpath.isfile(test_support.TESTFN), False)
|
self.assertIs(self.path.isfile(test_support.TESTFN), False)
|
||||||
f = open(test_support.TESTFN, "wb")
|
f = open(test_support.TESTFN, "wb")
|
||||||
try:
|
try:
|
||||||
f.write("foo")
|
f.write("foo")
|
||||||
f.close()
|
f.close()
|
||||||
self.assertIs(genericpath.isfile(test_support.TESTFN), True)
|
self.assertIs(self.path.isfile(test_support.TESTFN), True)
|
||||||
os.remove(test_support.TESTFN)
|
os.remove(test_support.TESTFN)
|
||||||
os.mkdir(test_support.TESTFN)
|
os.mkdir(test_support.TESTFN)
|
||||||
self.assertIs(genericpath.isfile(test_support.TESTFN), False)
|
self.assertIs(self.path.isfile(test_support.TESTFN), False)
|
||||||
os.rmdir(test_support.TESTFN)
|
os.rmdir(test_support.TESTFN)
|
||||||
finally:
|
finally:
|
||||||
if not f.close():
|
if not f.close():
|
||||||
f.close()
|
f.close()
|
||||||
try:
|
test_support.unlink(test_support.TESTFN)
|
||||||
os.remove(test_support.TESTFN)
|
safe_rmdir(test_support.TESTFN)
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
os.rmdir(test_support.TESTFN)
|
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, genericpath.isdir)
|
|
||||||
|
|
||||||
def test_samefile(self):
|
|
||||||
f = open(test_support.TESTFN + "1", "wb")
|
|
||||||
try:
|
|
||||||
f.write("foo")
|
|
||||||
f.close()
|
|
||||||
self.assertIs(
|
|
||||||
genericpath.samefile(
|
|
||||||
test_support.TESTFN + "1",
|
|
||||||
test_support.TESTFN + "1"
|
|
||||||
),
|
|
||||||
True
|
|
||||||
)
|
|
||||||
# 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"):
|
|
||||||
os.symlink(
|
|
||||||
test_support.TESTFN + "1",
|
|
||||||
test_support.TESTFN + "2"
|
|
||||||
)
|
|
||||||
self.assertIs(
|
|
||||||
genericpath.samefile(
|
|
||||||
test_support.TESTFN + "1",
|
|
||||||
test_support.TESTFN + "2"
|
|
||||||
),
|
|
||||||
True
|
|
||||||
)
|
|
||||||
os.remove(test_support.TESTFN + "2")
|
|
||||||
f = open(test_support.TESTFN + "2", "wb")
|
|
||||||
f.write("bar")
|
|
||||||
f.close()
|
|
||||||
self.assertIs(
|
|
||||||
genericpath.samefile(
|
|
||||||
test_support.TESTFN + "1",
|
|
||||||
test_support.TESTFN + "2"
|
|
||||||
),
|
|
||||||
False
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
try:
|
|
||||||
os.remove(test_support.TESTFN + "1")
|
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
os.remove(test_support.TESTFN + "2")
|
|
||||||
except os.error:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, genericpath.samefile)
|
|
||||||
|
|
||||||
|
|
||||||
# XXX at some point this should probably go in some class that contains common
|
class CommonTest(GenericTest):
|
||||||
# tests for all test_*path modules.
|
# The path module to be tested
|
||||||
def _issue3426(self, cwd, abspath):
|
path = None
|
||||||
# Issue 3426: check that abspath retuns unicode when the arg is unicode
|
common_attributes = GenericTest.common_attributes + [
|
||||||
# and str when it's str, with both ASCII and non-ASCII cwds
|
# Properties
|
||||||
with test_support.temp_cwd(cwd):
|
'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"
|
||||||
|
p = self.path.normcase(p)
|
||||||
|
self.assertEqual(p, self.path.normcase(p))
|
||||||
|
|
||||||
|
def test_splitdrive(self):
|
||||||
|
# splitdrive for non-NT paths
|
||||||
|
self.assertEqual(self.path.splitdrive("/foo/bar"), ("", "/foo/bar"))
|
||||||
|
self.assertEqual(self.path.splitdrive("foo:bar"), ('', 'foo:bar'))
|
||||||
|
self.assertEqual(self.path.splitdrive(":foo:bar"), ('', ':foo:bar'))
|
||||||
|
|
||||||
|
def test_expandvars(self):
|
||||||
|
if self.path.__name__ == 'macpath':
|
||||||
|
raise unittest.SkipTest('macpath.expandvars is a stub')
|
||||||
|
with test_support.EnvironmentVarGuard() as env:
|
||||||
|
env.clear()
|
||||||
|
env["foo"] = "bar"
|
||||||
|
env["{foo"] = "baz1"
|
||||||
|
env["{foo}"] = "baz2"
|
||||||
|
self.assertEqual(self.path.expandvars("foo"), "foo")
|
||||||
|
self.assertEqual(self.path.expandvars("$foo bar"), "bar bar")
|
||||||
|
self.assertEqual(self.path.expandvars("${foo}bar"), "barbar")
|
||||||
|
self.assertEqual(self.path.expandvars("$[foo]bar"), "$[foo]bar")
|
||||||
|
self.assertEqual(self.path.expandvars("$bar bar"), "$bar bar")
|
||||||
|
self.assertEqual(self.path.expandvars("$?bar"), "$?bar")
|
||||||
|
self.assertEqual(self.path.expandvars("${foo}bar"), "barbar")
|
||||||
|
self.assertEqual(self.path.expandvars("$foo}bar"), "bar}bar")
|
||||||
|
self.assertEqual(self.path.expandvars("${foo"), "${foo")
|
||||||
|
self.assertEqual(self.path.expandvars("${{foo}}"), "baz1}")
|
||||||
|
self.assertEqual(self.path.expandvars("$foo$foo"), "barbar")
|
||||||
|
self.assertEqual(self.path.expandvars("$bar$bar"), "$bar$bar")
|
||||||
|
|
||||||
|
def test_abspath(self):
|
||||||
|
self.assertIn("foo", self.path.abspath("foo"))
|
||||||
|
|
||||||
|
# Abspath returns bytes when the arg is bytes
|
||||||
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
|
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
|
||||||
self.assertIsInstance(abspath(path), str)
|
self.assertIsInstance(self.path.abspath(path), str)
|
||||||
for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
|
|
||||||
self.assertIsInstance(abspath(upath), unicode)
|
def test_realpath(self):
|
||||||
|
self.assertIn("foo", self.path.realpath("foo"))
|
||||||
|
|
||||||
|
def test_normpath_issue5827(self):
|
||||||
|
# Make sure normpath preserves unicode
|
||||||
|
for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
|
||||||
|
self.assertIsInstance(self.path.normpath(path), unicode)
|
||||||
|
|
||||||
|
def test_abspath_issue3426(self):
|
||||||
|
# Check that abspath returns unicode when the arg is unicode
|
||||||
|
# with both ASCII and non-ASCII cwds.
|
||||||
|
for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
|
||||||
|
self.assertIsInstance(self.path.abspath(path), unicode)
|
||||||
|
|
||||||
|
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:\\'):
|
||||||
|
self.assertIsInstance(self.path.abspath(path), unicode)
|
||||||
|
|
||||||
|
# Test non-ASCII, non-UTF8 bytes in the path.
|
||||||
|
with test_support.temp_cwd('\xe7w\xf0'):
|
||||||
|
self.test_abspath()
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(AllCommonTest)
|
test_support.run_unittest(GenericTest)
|
||||||
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import macpath
|
import macpath
|
||||||
from test import test_support
|
from test import test_support, test_genericpath
|
||||||
import unittest
|
import unittest
|
||||||
import test_genericpath
|
|
||||||
|
|
||||||
|
|
||||||
class MacPathTestCase(unittest.TestCase):
|
class MacPathTestCase(unittest.TestCase):
|
||||||
|
@ -9,12 +8,6 @@ class MacPathTestCase(unittest.TestCase):
|
||||||
def test_abspath(self):
|
def test_abspath(self):
|
||||||
self.assertEqual(macpath.abspath("xx:yy"), "xx:yy")
|
self.assertEqual(macpath.abspath("xx:yy"), "xx:yy")
|
||||||
|
|
||||||
def test_abspath_with_ascii_cwd(self):
|
|
||||||
test_genericpath._issue3426(self, u'cwd', macpath.abspath)
|
|
||||||
|
|
||||||
def test_abspath_with_nonascii_cwd(self):
|
|
||||||
test_genericpath._issue3426(self, u'\xe7w\xf0', macpath.abspath)
|
|
||||||
|
|
||||||
def test_isabs(self):
|
def test_isabs(self):
|
||||||
isabs = macpath.isabs
|
isabs = macpath.isabs
|
||||||
self.assertTrue(isabs("xx:yy"))
|
self.assertTrue(isabs("xx:yy"))
|
||||||
|
@ -46,11 +39,6 @@ class MacPathTestCase(unittest.TestCase):
|
||||||
self.assertEqual(split(":conky:mountpoint:"),
|
self.assertEqual(split(":conky:mountpoint:"),
|
||||||
(':conky:mountpoint', ''))
|
(':conky:mountpoint', ''))
|
||||||
|
|
||||||
def test_splitdrive(self):
|
|
||||||
splitdrive = macpath.splitdrive
|
|
||||||
self.assertEqual(splitdrive("foo:bar"), ('', 'foo:bar'))
|
|
||||||
self.assertEqual(splitdrive(":foo:bar"), ('', ':foo:bar'))
|
|
||||||
|
|
||||||
def test_splitext(self):
|
def test_splitext(self):
|
||||||
splitext = macpath.splitext
|
splitext = macpath.splitext
|
||||||
self.assertEqual(splitext(":foo.ext"), (':foo', '.ext'))
|
self.assertEqual(splitext(":foo.ext"), (':foo', '.ext'))
|
||||||
|
@ -67,9 +55,12 @@ class MacPathTestCase(unittest.TestCase):
|
||||||
self.assertIsInstance(macpath.normpath(path), unicode,
|
self.assertIsInstance(macpath.normpath(path), unicode,
|
||||||
'normpath() returned str instead of unicode')
|
'normpath() returned str instead of unicode')
|
||||||
|
|
||||||
|
class MacCommonTest(test_genericpath.CommonTest):
|
||||||
|
path = macpath
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(MacPathTestCase)
|
test_support.run_unittest(MacPathTestCase, MacCommonTest)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import ntpath
|
import ntpath
|
||||||
import os
|
import os
|
||||||
from test.test_support import TestFailed
|
from test.test_support import TestFailed
|
||||||
import test.test_support as test_support
|
from test import test_support, test_genericpath
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,11 +123,6 @@ class TestNtpath(unittest.TestCase):
|
||||||
tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
|
tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
|
||||||
tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
|
tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
|
||||||
|
|
||||||
# Issue 5827: Make sure normpath preserves unicode
|
|
||||||
for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
|
|
||||||
self.assertIsInstance(ntpath.normpath(path), unicode,
|
|
||||||
'normpath() returned str instead of unicode')
|
|
||||||
|
|
||||||
def test_expandvars(self):
|
def test_expandvars(self):
|
||||||
with test_support.EnvironmentVarGuard() as env:
|
with test_support.EnvironmentVarGuard() as env:
|
||||||
env.clear()
|
env.clear()
|
||||||
|
@ -169,16 +164,6 @@ class TestNtpath(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
tester('ntpath.abspath("C:\\")', "C:\\")
|
tester('ntpath.abspath("C:\\")', "C:\\")
|
||||||
|
|
||||||
# Issue 3426: check that abspath retuns unicode when the arg is
|
|
||||||
# unicode and str when it's str, with both ASCII and non-ASCII cwds
|
|
||||||
for cwd in (u'cwd', u'\xe7w\xf0'):
|
|
||||||
with test_support.temp_cwd(cwd):
|
|
||||||
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
|
|
||||||
self.assertIsInstance(ntpath.abspath(path), str)
|
|
||||||
for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
|
|
||||||
self.assertIsInstance(ntpath.abspath(upath), unicode)
|
|
||||||
|
|
||||||
|
|
||||||
def test_relpath(self):
|
def test_relpath(self):
|
||||||
currentdir = os.path.split(os.getcwd())[-1]
|
currentdir = os.path.split(os.getcwd())[-1]
|
||||||
tester('ntpath.relpath("a")', 'a')
|
tester('ntpath.relpath("a")', 'a')
|
||||||
|
@ -192,8 +177,13 @@ class TestNtpath(unittest.TestCase):
|
||||||
tester('ntpath.relpath("a", "a")', '.')
|
tester('ntpath.relpath("a", "a")', '.')
|
||||||
|
|
||||||
|
|
||||||
|
class NtCommonTest(test_genericpath.CommonTest):
|
||||||
|
path = ntpath
|
||||||
|
attributes = ['relpath', 'splitunc']
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(TestNtpath)
|
test_support.run_unittest(TestNtpath, NtCommonTest)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import unittest
|
import unittest
|
||||||
from test import test_support
|
from test import test_support, test_genericpath
|
||||||
|
|
||||||
import test_genericpath
|
|
||||||
|
|
||||||
import posixpath, os
|
import posixpath, os
|
||||||
from posixpath import realpath, abspath, dirname, basename
|
from posixpath import realpath, abspath, dirname, basename
|
||||||
|
@ -27,26 +25,11 @@ class PosixPathTest(unittest.TestCase):
|
||||||
test_support.unlink(test_support.TESTFN + suffix)
|
test_support.unlink(test_support.TESTFN + suffix)
|
||||||
safe_rmdir(test_support.TESTFN + suffix)
|
safe_rmdir(test_support.TESTFN + suffix)
|
||||||
|
|
||||||
def test_normcase(self):
|
|
||||||
# Check that normcase() is idempotent
|
|
||||||
p = "FoO/./BaR"
|
|
||||||
p = posixpath.normcase(p)
|
|
||||||
self.assertEqual(p, posixpath.normcase(p))
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.normcase)
|
|
||||||
|
|
||||||
def test_join(self):
|
def test_join(self):
|
||||||
self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
|
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.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)
|
|
||||||
|
|
||||||
def test_split(self):
|
def test_split(self):
|
||||||
self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
|
self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
|
||||||
self.assertEqual(posixpath.split("/"), ("/", ""))
|
self.assertEqual(posixpath.split("/"), ("/", ""))
|
||||||
|
@ -54,8 +37,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
|
self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
|
||||||
self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
|
self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.split)
|
|
||||||
|
|
||||||
def splitextTest(self, path, filename, ext):
|
def splitextTest(self, path, filename, ext):
|
||||||
self.assertEqual(posixpath.splitext(path), (filename, ext))
|
self.assertEqual(posixpath.splitext(path), (filename, ext))
|
||||||
self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
|
self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
|
||||||
|
@ -77,7 +58,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
self.splitextTest("..", "..", "")
|
self.splitextTest("..", "..", "")
|
||||||
self.splitextTest("........", "........", "")
|
self.splitextTest("........", "........", "")
|
||||||
self.splitextTest("", "", "")
|
self.splitextTest("", "", "")
|
||||||
self.assertRaises(TypeError, posixpath.splitext)
|
|
||||||
|
|
||||||
def test_isabs(self):
|
def test_isabs(self):
|
||||||
self.assertIs(posixpath.isabs(""), False)
|
self.assertIs(posixpath.isabs(""), False)
|
||||||
|
@ -86,8 +66,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
self.assertIs(posixpath.isabs("/foo/bar"), True)
|
self.assertIs(posixpath.isabs("/foo/bar"), True)
|
||||||
self.assertIs(posixpath.isabs("foo/bar"), False)
|
self.assertIs(posixpath.isabs("foo/bar"), False)
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.isabs)
|
|
||||||
|
|
||||||
def test_basename(self):
|
def test_basename(self):
|
||||||
self.assertEqual(posixpath.basename("/foo/bar"), "bar")
|
self.assertEqual(posixpath.basename("/foo/bar"), "bar")
|
||||||
self.assertEqual(posixpath.basename("/"), "")
|
self.assertEqual(posixpath.basename("/"), "")
|
||||||
|
@ -95,8 +73,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
self.assertEqual(posixpath.basename("////foo"), "foo")
|
self.assertEqual(posixpath.basename("////foo"), "foo")
|
||||||
self.assertEqual(posixpath.basename("//foo//bar"), "bar")
|
self.assertEqual(posixpath.basename("//foo//bar"), "bar")
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.basename)
|
|
||||||
|
|
||||||
def test_dirname(self):
|
def test_dirname(self):
|
||||||
self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
|
self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
|
||||||
self.assertEqual(posixpath.dirname("/"), "/")
|
self.assertEqual(posixpath.dirname("/"), "/")
|
||||||
|
@ -104,67 +80,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
self.assertEqual(posixpath.dirname("////foo"), "////")
|
self.assertEqual(posixpath.dirname("////foo"), "////")
|
||||||
self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
|
self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.dirname)
|
|
||||||
|
|
||||||
def test_commonprefix(self):
|
|
||||||
self.assertEqual(
|
|
||||||
posixpath.commonprefix([]),
|
|
||||||
""
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
|
|
||||||
"/home/swen"
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
|
|
||||||
"/home/swen/"
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
|
|
||||||
"/home/swen/spam"
|
|
||||||
)
|
|
||||||
|
|
||||||
testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', 'aXc', 'abd', 'ab', 'aX', 'abcX']
|
|
||||||
for s1 in testlist:
|
|
||||||
for s2 in testlist:
|
|
||||||
p = posixpath.commonprefix([s1, s2])
|
|
||||||
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])
|
|
||||||
|
|
||||||
def test_getsize(self):
|
|
||||||
f = open(test_support.TESTFN, "wb")
|
|
||||||
try:
|
|
||||||
f.write("foo")
|
|
||||||
f.close()
|
|
||||||
self.assertEqual(posixpath.getsize(test_support.TESTFN), 3)
|
|
||||||
finally:
|
|
||||||
if not f.closed:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
self.assertLessEqual(
|
|
||||||
posixpath.getctime(test_support.TESTFN),
|
|
||||||
posixpath.getmtime(test_support.TESTFN)
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
if not f.closed:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_islink(self):
|
def test_islink(self):
|
||||||
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
|
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
|
||||||
f = open(test_support.TESTFN + "1", "wb")
|
f = open(test_support.TESTFN + "1", "wb")
|
||||||
|
@ -183,56 +98,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
if not f.close():
|
if not f.close():
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.islink)
|
|
||||||
|
|
||||||
def test_exists(self):
|
|
||||||
self.assertIs(posixpath.exists(test_support.TESTFN), False)
|
|
||||||
f = open(test_support.TESTFN, "wb")
|
|
||||||
try:
|
|
||||||
f.write("foo")
|
|
||||||
f.close()
|
|
||||||
self.assertIs(posixpath.exists(test_support.TESTFN), True)
|
|
||||||
self.assertIs(posixpath.lexists(test_support.TESTFN), True)
|
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.exists)
|
|
||||||
|
|
||||||
def test_isdir(self):
|
|
||||||
self.assertIs(posixpath.isdir(test_support.TESTFN), False)
|
|
||||||
f = open(test_support.TESTFN, "wb")
|
|
||||||
try:
|
|
||||||
f.write("foo")
|
|
||||||
f.close()
|
|
||||||
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)
|
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.isdir)
|
|
||||||
|
|
||||||
def test_isfile(self):
|
|
||||||
self.assertIs(posixpath.isfile(test_support.TESTFN), False)
|
|
||||||
f = open(test_support.TESTFN, "wb")
|
|
||||||
try:
|
|
||||||
f.write("foo")
|
|
||||||
f.close()
|
|
||||||
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)
|
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.isdir)
|
|
||||||
|
|
||||||
def test_samefile(self):
|
def test_samefile(self):
|
||||||
f = open(test_support.TESTFN + "1", "wb")
|
f = open(test_support.TESTFN + "1", "wb")
|
||||||
try:
|
try:
|
||||||
|
@ -274,8 +139,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
if not f.close():
|
if not f.close():
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.samefile)
|
|
||||||
|
|
||||||
def test_samestat(self):
|
def test_samestat(self):
|
||||||
f = open(test_support.TESTFN + "1", "wb")
|
f = open(test_support.TESTFN + "1", "wb")
|
||||||
try:
|
try:
|
||||||
|
@ -315,13 +178,9 @@ class PosixPathTest(unittest.TestCase):
|
||||||
if not f.close():
|
if not f.close():
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.samestat)
|
|
||||||
|
|
||||||
def test_ismount(self):
|
def test_ismount(self):
|
||||||
self.assertIs(posixpath.ismount("/"), True)
|
self.assertIs(posixpath.ismount("/"), True)
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.ismount)
|
|
||||||
|
|
||||||
def test_expanduser(self):
|
def test_expanduser(self):
|
||||||
self.assertEqual(posixpath.expanduser("foo"), "foo")
|
self.assertEqual(posixpath.expanduser("foo"), "foo")
|
||||||
try:
|
try:
|
||||||
|
@ -343,29 +202,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
env['HOME'] = '/'
|
env['HOME'] = '/'
|
||||||
self.assertEqual(posixpath.expanduser("~"), "/")
|
self.assertEqual(posixpath.expanduser("~"), "/")
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.expanduser)
|
|
||||||
|
|
||||||
def test_expandvars(self):
|
|
||||||
with test_support.EnvironmentVarGuard() as env:
|
|
||||||
env.clear()
|
|
||||||
env["foo"] = "bar"
|
|
||||||
env["{foo"] = "baz1"
|
|
||||||
env["{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}")
|
|
||||||
self.assertEqual(posixpath.expandvars("$foo$foo"), "barbar")
|
|
||||||
self.assertEqual(posixpath.expandvars("$bar$bar"), "$bar$bar")
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.expandvars)
|
|
||||||
|
|
||||||
def test_normpath(self):
|
def test_normpath(self):
|
||||||
self.assertEqual(posixpath.normpath(""), ".")
|
self.assertEqual(posixpath.normpath(""), ".")
|
||||||
self.assertEqual(posixpath.normpath("/"), "/")
|
self.assertEqual(posixpath.normpath("/"), "/")
|
||||||
|
@ -375,27 +211,6 @@ class PosixPathTest(unittest.TestCase):
|
||||||
self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
|
self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
|
||||||
self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
|
self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
|
||||||
|
|
||||||
# Issue 5827: Make sure normpath preserves unicode
|
|
||||||
for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
|
|
||||||
self.assertIsInstance(posixpath.normpath(path), unicode,
|
|
||||||
'normpath() returned str instead of unicode')
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, posixpath.normpath)
|
|
||||||
|
|
||||||
def test_abspath(self):
|
|
||||||
self.assertIn("foo", posixpath.abspath("foo"))
|
|
||||||
self.assertRaises(TypeError, posixpath.abspath)
|
|
||||||
|
|
||||||
def test_abspath_with_ascii_cwd(self):
|
|
||||||
test_genericpath._issue3426(self, u'cwd', posixpath.abspath)
|
|
||||||
|
|
||||||
def test_abspath_with_nonascii_cwd(self):
|
|
||||||
test_genericpath._issue3426(self, u'\xe7w\xf0', posixpath.abspath)
|
|
||||||
|
|
||||||
def test_realpath(self):
|
|
||||||
self.assertIn("foo", realpath("foo"))
|
|
||||||
self.assertRaises(TypeError, posixpath.realpath)
|
|
||||||
|
|
||||||
if hasattr(os, "symlink"):
|
if hasattr(os, "symlink"):
|
||||||
def test_realpath_basic(self):
|
def test_realpath_basic(self):
|
||||||
# Basic operation.
|
# Basic operation.
|
||||||
|
@ -510,8 +325,15 @@ class PosixPathTest(unittest.TestCase):
|
||||||
finally:
|
finally:
|
||||||
os.getcwd = real_getcwd
|
os.getcwd = real_getcwd
|
||||||
|
|
||||||
|
|
||||||
|
class PosixCommonTest(test_genericpath.CommonTest):
|
||||||
|
path = posixpath
|
||||||
|
attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(PosixPathTest)
|
test_support.run_unittest(PosixPathTest, PosixCommonTest)
|
||||||
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue