summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_genericpath.py
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2010-03-08 12:24:53 (GMT)
committerFlorent Xicluna <florent.xicluna@gmail.com>2010-03-08 12:24:53 (GMT)
commitc9c79784cf675819981e54b9c64a9ec114aff994 (patch)
tree00124eb960e0a9847c12708a9ba52c2a34fc069c /Lib/test/test_genericpath.py
parent961683ac0f37de933ed15ccc60a9eee1a2fbcefc (diff)
downloadcpython-c9c79784cf675819981e54b9c64a9ec114aff994.zip
cpython-c9c79784cf675819981e54b9c64a9ec114aff994.tar.gz
cpython-c9c79784cf675819981e54b9c64a9ec114aff994.tar.bz2
Merged revisions 78734-78735 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r78734 | florent.xicluna | 2010-03-06 19:07:18 +0100 (sam, 06 mar 2010) | 2 lines Create test_genericpath.CommonTest and reuse it to test other path modules. ........ r78735 | florent.xicluna | 2010-03-06 19:52:52 +0100 (sam, 06 mar 2010) | 2 lines Minor tweaking of previous r78734, and add a NEWS entry. ........
Diffstat (limited to 'Lib/test/test_genericpath.py')
-rw-r--r--Lib/test/test_genericpath.py284
1 files changed, 188 insertions, 96 deletions
diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py
index ec722f3..614f887 100644
--- a/Lib/test/test_genericpath.py
+++ b/Lib/test/test_genericpath.py
@@ -1,38 +1,87 @@
+"""
+Tests common to genericpath, macpath, ntpath and posixpath
+"""
+
import unittest
from test import support
import os
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
+ pathmodule = 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.pathmodule, attr)()
+ raise self.fail("{}.{}() did not raise a TypeError"
+ .format(self.pathmodule.__name__, attr))
def test_commonprefix(self):
+ commonprefix = self.pathmodule.commonprefix
self.assertEqual(
- genericpath.commonprefix([]),
+ commonprefix([]),
""
)
self.assertEqual(
- genericpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
+ commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
"/home/swen"
)
self.assertEqual(
- genericpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
+ commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
"/home/swen/"
)
self.assertEqual(
- genericpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
+ commonprefix(["/home/swen/spam", "/home/swen/spam"]),
"/home/swen/spam"
)
+ self.assertEqual(
+ commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
+ b"/home/swen"
+ )
+ self.assertEqual(
+ commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
+ b"/home/swen/"
+ )
+ self.assertEqual(
+ commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
+ b"/home/swen/spam"
+ )
+
+ testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
+ 'aXc', 'abd', 'ab', 'aX', 'abcX']
+ for s1 in testlist:
+ for s2 in testlist:
+ p = 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(support.TESTFN, "wb")
try:
f.write(b"foo")
f.close()
- self.assertEqual(genericpath.getsize(support.TESTFN), 3)
+ self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
finally:
if not f.closed:
f.close()
- os.remove(support.TESTFN)
+ support.unlink(support.TESTFN)
def test_time(self):
f = open(support.TESTFN, "wb")
@@ -48,134 +97,177 @@ class AllCommonTest(unittest.TestCase):
self.assertEqual(d, b"foobar")
self.assertLessEqual(
- genericpath.getctime(support.TESTFN),
- genericpath.getmtime(support.TESTFN)
+ self.pathmodule.getctime(support.TESTFN),
+ self.pathmodule.getmtime(support.TESTFN)
)
finally:
if not f.closed:
f.close()
- os.remove(support.TESTFN)
+ support.unlink(support.TESTFN)
def test_exists(self):
- self.assertIs(genericpath.exists(support.TESTFN), False)
+ self.assertIs(self.pathmodule.exists(support.TESTFN), False)
f = open(support.TESTFN, "wb")
try:
f.write(b"foo")
f.close()
- self.assertIs(genericpath.exists(support.TESTFN), True)
+ self.assertIs(self.pathmodule.exists(support.TESTFN), True)
+ if not self.pathmodule == genericpath:
+ self.assertIs(self.pathmodule.lexists(support.TESTFN),
+ True)
finally:
if not f.close():
f.close()
- try:
- os.remove(support.TESTFN)
- except os.error:
- pass
-
- self.assertRaises(TypeError, genericpath.exists)
+ support.unlink(support.TESTFN)
def test_isdir(self):
- self.assertIs(genericpath.isdir(support.TESTFN), False)
+ self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
f = open(support.TESTFN, "wb")
try:
f.write(b"foo")
f.close()
- self.assertIs(genericpath.isdir(support.TESTFN), False)
+ self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
os.remove(support.TESTFN)
os.mkdir(support.TESTFN)
- self.assertIs(genericpath.isdir(support.TESTFN), True)
+ self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
os.rmdir(support.TESTFN)
finally:
if not f.close():
f.close()
- try:
- os.remove(support.TESTFN)
- except os.error:
- pass
- try:
- os.rmdir(support.TESTFN)
- except os.error:
- pass
-
- self.assertRaises(TypeError, genericpath.isdir)
+ support.unlink(support.TESTFN)
+ safe_rmdir(support.TESTFN)
def test_isfile(self):
- self.assertIs(genericpath.isfile(support.TESTFN), False)
+ self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
f = open(support.TESTFN, "wb")
try:
f.write(b"foo")
f.close()
- self.assertIs(genericpath.isfile(support.TESTFN), True)
+ self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
os.remove(support.TESTFN)
os.mkdir(support.TESTFN)
- self.assertIs(genericpath.isfile(support.TESTFN), False)
+ self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
os.rmdir(support.TESTFN)
finally:
if not f.close():
f.close()
- try:
- os.remove(support.TESTFN)
- except os.error:
- pass
- try:
- os.rmdir(support.TESTFN)
- except os.error:
- pass
-
- self.assertRaises(TypeError, genericpath.isdir)
-
- def test_samefile(self):
- f = open(support.TESTFN + "1", "wb")
- try:
- f.write(b"foo")
- f.close()
- self.assertIs(
- genericpath.samefile(
- support.TESTFN + "1",
- 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(
- support.TESTFN + "1",
- support.TESTFN + "2"
- )
- self.assertIs(
- genericpath.samefile(
- support.TESTFN + "1",
- support.TESTFN + "2"
- ),
- True
- )
- os.remove(support.TESTFN + "2")
- f = open(support.TESTFN + "2", "wb")
- f.write(b"bar")
- f.close()
- self.assertIs(
- genericpath.samefile(
- support.TESTFN + "1",
- support.TESTFN + "2"
- ),
- False
- )
- finally:
- if not f.close():
- f.close()
- try:
- os.remove(support.TESTFN + "1")
- except os.error:
- pass
- try:
- os.remove(support.TESTFN + "2")
- except os.error:
- pass
-
- self.assertRaises(TypeError, genericpath.samefile)
+ support.unlink(support.TESTFN)
+ safe_rmdir(support.TESTFN)
+
+
+class CommonTest(GenericTest):
+ # The path module to be tested
+ pathmodule = None
+ 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"
+ p = self.pathmodule.normcase(p)
+ self.assertEqual(p, self.pathmodule.normcase(p))
+
+ p = b"FoO/./BaR"
+ p = self.pathmodule.normcase(p)
+ self.assertEqual(p, self.pathmodule.normcase(p))
+
+ def test_splitdrive(self):
+ # splitdrive for non-NT paths
+ 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"))
+
+ self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
+ self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
+ self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
+
+ def test_expandvars(self):
+ if self.pathmodule.__name__ == 'macpath':
+ self.skipTest('macpath.expandvars is a stub')
+ expandvars = self.pathmodule.expandvars
+ with support.EnvironmentVarGuard() as env:
+ env.clear()
+ env["foo"] = "bar"
+ env["{foo"] = "baz1"
+ env["{foo}"] = "baz2"
+ 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")
+
+ self.assertEqual(expandvars(b"foo"), b"foo")
+ self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
+ self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
+ self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
+ self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
+ self.assertEqual(expandvars(b"$?bar"), b"$?bar")
+ self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
+ self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
+ self.assertEqual(expandvars(b"${foo"), b"${foo")
+ self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
+ self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
+ self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
+
+ def test_abspath(self):
+ self.assertIn("foo", self.pathmodule.abspath("foo"))
+ self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
+
+ # Abspath returns bytes when the arg is bytes
+ for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
+ self.assertIsInstance(self.pathmodule.abspath(path), bytes)
+
+ def test_realpath(self):
+ self.assertIn("foo", self.pathmodule.realpath("foo"))
+ self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
+
+ def test_normpath_issue5827(self):
+ # Make sure normpath preserves unicode
+ for path in ('', '.', '/', '\\', '///foo/.//bar//'):
+ self.assertIsInstance(self.pathmodule.normpath(path), str)
+
+ def test_abspath_issue3426(self):
+ # Check that abspath returns unicode when the arg is unicode
+ # with both ASCII and non-ASCII cwds.
+ abspath = self.pathmodule.abspath
+ for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
+ self.assertIsInstance(abspath(path), str)
+
+ unicwd = '\xe7w\xf0'
+ try:
+ fsencoding = support.TESTFN_ENCODING or "ascii"
+ unicwd.encode(fsencoding)
+ except (AttributeError, UnicodeEncodeError):
+ # FS encoding is probably ASCII
+ pass
+ else:
+ with support.temp_cwd(unicwd):
+ for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
+ self.assertIsInstance(abspath(path), str)
+
+ # Test non-ASCII, non-UTF8 bytes in the path.
+ with support.temp_cwd('\xe7w\xf0'):
+ self.test_abspath()
+
def test_main():
- support.run_unittest(AllCommonTest)
+ support.run_unittest(GenericTest)
+
if __name__=="__main__":
test_main()