/usr/lib/python2.7/test/test_genericpath.py is in libpython2.7-testsuite 2.7.6-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | """
Tests common to genericpath, macpath, ntpath and posixpath
"""
import unittest
from test import test_support
import os
import genericpath
import sys
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(
commonprefix([]),
""
)
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"
)
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"
)
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(test_support.TESTFN, "wb")
try:
f.write("foo")
f.close()
self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3)
finally:
if not f.closed:
f.close()
test_support.unlink(test_support.TESTFN)
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(
self.pathmodule.getctime(test_support.TESTFN),
self.pathmodule.getmtime(test_support.TESTFN)
)
finally:
if not f.closed:
f.close()
test_support.unlink(test_support.TESTFN)
def test_exists(self):
self.assertIs(self.pathmodule.exists(test_support.TESTFN), False)
f = open(test_support.TESTFN, "wb")
try:
f.write("foo")
f.close()
self.assertIs(self.pathmodule.exists(test_support.TESTFN), True)
if not self.pathmodule == genericpath:
self.assertIs(self.pathmodule.lexists(test_support.TESTFN),
True)
finally:
if not f.close():
f.close()
test_support.unlink(test_support.TESTFN)
def test_isdir(self):
self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
f = open(test_support.TESTFN, "wb")
try:
f.write("foo")
f.close()
self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
os.remove(test_support.TESTFN)
os.mkdir(test_support.TESTFN)
self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True)
os.rmdir(test_support.TESTFN)
finally:
if not f.close():
f.close()
test_support.unlink(test_support.TESTFN)
safe_rmdir(test_support.TESTFN)
def test_isfile(self):
self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
f = open(test_support.TESTFN, "wb")
try:
f.write("foo")
f.close()
self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True)
os.remove(test_support.TESTFN)
os.mkdir(test_support.TESTFN)
self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
os.rmdir(test_support.TESTFN)
finally:
if not f.close():
f.close()
test_support.unlink(test_support.TESTFN)
safe_rmdir(test_support.TESTFN)
# Following TestCase is not supposed to be run from test_genericpath.
# It is inherited by other test modules (macpath, ntpath, posixpath).
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))
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"))
def test_expandvars(self):
if self.pathmodule.__name__ == 'macpath':
self.skipTest('macpath.expandvars is a stub')
expandvars = self.pathmodule.expandvars
with test_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"), "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")
@unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII')
def test_expandvars_nonascii(self):
if self.pathmodule.__name__ == 'macpath':
self.skipTest('macpath.expandvars is a stub')
expandvars = self.pathmodule.expandvars
def check(value, expected):
self.assertEqual(expandvars(value), expected)
encoding = sys.getfilesystemencoding()
with test_support.EnvironmentVarGuard() as env:
env.clear()
unonascii = test_support.FS_NONASCII
snonascii = unonascii.encode(encoding)
env['spam'] = snonascii
env[snonascii] = 'ham' + snonascii
check(snonascii, snonascii)
check('$spam bar', '%s bar' % snonascii)
check('${spam}bar', '%sbar' % snonascii)
check('${%s}bar' % snonascii, 'ham%sbar' % snonascii)
check('$bar%s bar' % snonascii, '$bar%s bar' % snonascii)
check('$spam}bar', '%s}bar' % snonascii)
check(unonascii, unonascii)
check(u'$spam bar', u'%s bar' % unonascii)
check(u'${spam}bar', u'%sbar' % unonascii)
check(u'${%s}bar' % unonascii, u'ham%sbar' % unonascii)
check(u'$bar%s bar' % unonascii, u'$bar%s bar' % unonascii)
check(u'$spam}bar', u'%s}bar' % unonascii)
def test_abspath(self):
self.assertIn("foo", self.pathmodule.abspath("foo"))
# Abspath returns bytes when the arg is bytes
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
self.assertIsInstance(self.pathmodule.abspath(path), str)
def test_realpath(self):
self.assertIn("foo", self.pathmodule.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.pathmodule.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.
abspath = self.pathmodule.abspath
for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
self.assertIsInstance(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(abspath(path), unicode)
@unittest.skipIf(sys.platform == 'darwin',
"Mac OS X denies the creation of a directory with an invalid utf8 name")
def test_nonascii_abspath(self):
# Test non-ASCII, non-UTF8 bytes in the path.
with test_support.temp_cwd('\xe7w\xf0'):
self.test_abspath()
def test_main():
test_support.run_unittest(GenericTest)
if __name__=="__main__":
test_main()
|