| 1 |
""" |
| 2 |
Tests common to genericpath, ntpath and posixpath |
| 3 |
""" |
| 4 |
|
| 5 |
import genericpath |
| 6 |
import os |
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
import warnings |
| 10 |
from test import support |
| 11 |
from test.support import is_emscripten |
| 12 |
from test.support import os_helper |
| 13 |
from test.support import warnings_helper |
| 14 |
from test.support.script_helper import assert_python_ok |
| 15 |
from test.support.os_helper import FakePath |
| 16 |
|
| 17 |
|
| 18 |
def create_file(filename, data=b'foo'): |
| 19 |
with open(filename, 'xb', 0) as fp: |
| 20 |
fp.write(data) |
| 21 |
|
| 22 |
|
| 23 |
class GenericTest: |
| 24 |
common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime', |
| 25 |
'getmtime', 'exists', 'isdir', 'isfile'] |
| 26 |
attributes = [] |
| 27 |
|
| 28 |
def test_no_argument(self): |
| 29 |
for attr in self.common_attributes + self.attributes: |
| 30 |
with self.assertRaises(TypeError): |
| 31 |
getattr(self.pathmodule, attr)() |
| 32 |
raise self.fail("{}.{}() did not raise a TypeError" |
| 33 |
.format(self.pathmodule.__name__, attr)) |
| 34 |
|
| 35 |
def test_commonprefix(self): |
| 36 |
commonprefix = self.pathmodule.commonprefix |
| 37 |
self.assertEqual( |
| 38 |
commonprefix([]), |
| 39 |
"" |
| 40 |
) |
| 41 |
self.assertEqual( |
| 42 |
commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
| 43 |
"/home/swen" |
| 44 |
) |
| 45 |
self.assertEqual( |
| 46 |
commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
| 47 |
"/home/swen/" |
| 48 |
) |
| 49 |
self.assertEqual( |
| 50 |
commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
| 51 |
"/home/swen/spam" |
| 52 |
) |
| 53 |
self.assertEqual( |
| 54 |
commonprefix(["home:swenson:spam", "home:swen:spam"]), |
| 55 |
"home:swen" |
| 56 |
) |
| 57 |
self.assertEqual( |
| 58 |
commonprefix([":home:swen:spam", ":home:swen:eggs"]), |
| 59 |
":home:swen:" |
| 60 |
) |
| 61 |
self.assertEqual( |
| 62 |
commonprefix([":home:swen:spam", ":home:swen:spam"]), |
| 63 |
":home:swen:spam" |
| 64 |
) |
| 65 |
|
| 66 |
self.assertEqual( |
| 67 |
commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]), |
| 68 |
b"/home/swen" |
| 69 |
) |
| 70 |
self.assertEqual( |
| 71 |
commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]), |
| 72 |
b"/home/swen/" |
| 73 |
) |
| 74 |
self.assertEqual( |
| 75 |
commonprefix([b"/home/swen/spam", b"/home/swen/spam"]), |
| 76 |
b"/home/swen/spam" |
| 77 |
) |
| 78 |
self.assertEqual( |
| 79 |
commonprefix([b"home:swenson:spam", b"home:swen:spam"]), |
| 80 |
b"home:swen" |
| 81 |
) |
| 82 |
self.assertEqual( |
| 83 |
commonprefix([b":home:swen:spam", b":home:swen:eggs"]), |
| 84 |
b":home:swen:" |
| 85 |
) |
| 86 |
self.assertEqual( |
| 87 |
commonprefix([b":home:swen:spam", b":home:swen:spam"]), |
| 88 |
b":home:swen:spam" |
| 89 |
) |
| 90 |
|
| 91 |
testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', |
| 92 |
'aXc', 'abd', 'ab', 'aX', 'abcX'] |
| 93 |
for s1 in testlist: |
| 94 |
for s2 in testlist: |
| 95 |
p = commonprefix([s1, s2]) |
| 96 |
self.assertTrue(s1.startswith(p)) |
| 97 |
self.assertTrue(s2.startswith(p)) |
| 98 |
if s1 != s2: |
| 99 |
n = len(p) |
| 100 |
self.assertNotEqual(s1[n:n+1], s2[n:n+1]) |
| 101 |
|
| 102 |
def test_getsize(self): |
| 103 |
filename = os_helper.TESTFN |
| 104 |
self.addCleanup(os_helper.unlink, filename) |
| 105 |
|
| 106 |
create_file(filename, b'Hello') |
| 107 |
self.assertEqual(self.pathmodule.getsize(filename), 5) |
| 108 |
os.remove(filename) |
| 109 |
|
| 110 |
create_file(filename, b'Hello World!') |
| 111 |
self.assertEqual(self.pathmodule.getsize(filename), 12) |
| 112 |
|
| 113 |
def test_filetime(self): |
| 114 |
filename = os_helper.TESTFN |
| 115 |
self.addCleanup(os_helper.unlink, filename) |
| 116 |
|
| 117 |
create_file(filename, b'foo') |
| 118 |
|
| 119 |
with open(filename, "ab", 0) as f: |
| 120 |
f.write(b"bar") |
| 121 |
|
| 122 |
with open(filename, "rb", 0) as f: |
| 123 |
data = f.read() |
| 124 |
self.assertEqual(data, b"foobar") |
| 125 |
|
| 126 |
self.assertLessEqual( |
| 127 |
self.pathmodule.getctime(filename), |
| 128 |
self.pathmodule.getmtime(filename) |
| 129 |
) |
| 130 |
|
| 131 |
def test_exists(self): |
| 132 |
filename = os_helper.TESTFN |
| 133 |
bfilename = os.fsencode(filename) |
| 134 |
self.addCleanup(os_helper.unlink, filename) |
| 135 |
|
| 136 |
self.assertIs(self.pathmodule.exists(filename), False) |
| 137 |
self.assertIs(self.pathmodule.exists(bfilename), False) |
| 138 |
|
| 139 |
create_file(filename) |
| 140 |
|
| 141 |
self.assertIs(self.pathmodule.exists(filename), True) |
| 142 |
self.assertIs(self.pathmodule.exists(bfilename), True) |
| 143 |
|
| 144 |
self.assertIs(self.pathmodule.exists(filename + '\udfff'), False) |
| 145 |
self.assertIs(self.pathmodule.exists(bfilename + b'\xff'), False) |
| 146 |
self.assertIs(self.pathmodule.exists(filename + '\x00'), False) |
| 147 |
self.assertIs(self.pathmodule.exists(bfilename + b'\x00'), False) |
| 148 |
|
| 149 |
if self.pathmodule is not genericpath: |
| 150 |
self.assertIs(self.pathmodule.lexists(filename), True) |
| 151 |
self.assertIs(self.pathmodule.lexists(bfilename), True) |
| 152 |
|
| 153 |
self.assertIs(self.pathmodule.lexists(filename + '\udfff'), False) |
| 154 |
self.assertIs(self.pathmodule.lexists(bfilename + b'\xff'), False) |
| 155 |
self.assertIs(self.pathmodule.lexists(filename + '\x00'), False) |
| 156 |
self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False) |
| 157 |
|
| 158 |
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
| 159 |
@unittest.skipIf(is_emscripten, "Emscripten pipe fds have no stat") |
| 160 |
def test_exists_fd(self): |
| 161 |
r, w = os.pipe() |
| 162 |
try: |
| 163 |
self.assertTrue(self.pathmodule.exists(r)) |
| 164 |
finally: |
| 165 |
os.close(r) |
| 166 |
os.close(w) |
| 167 |
self.assertFalse(self.pathmodule.exists(r)) |
| 168 |
|
| 169 |
def test_isdir(self): |
| 170 |
filename = os_helper.TESTFN |
| 171 |
bfilename = os.fsencode(filename) |
| 172 |
self.assertIs(self.pathmodule.isdir(filename), False) |
| 173 |
self.assertIs(self.pathmodule.isdir(bfilename), False) |
| 174 |
|
| 175 |
self.assertIs(self.pathmodule.isdir(filename + '\udfff'), False) |
| 176 |
self.assertIs(self.pathmodule.isdir(bfilename + b'\xff'), False) |
| 177 |
self.assertIs(self.pathmodule.isdir(filename + '\x00'), False) |
| 178 |
self.assertIs(self.pathmodule.isdir(bfilename + b'\x00'), False) |
| 179 |
|
| 180 |
try: |
| 181 |
create_file(filename) |
| 182 |
self.assertIs(self.pathmodule.isdir(filename), False) |
| 183 |
self.assertIs(self.pathmodule.isdir(bfilename), False) |
| 184 |
finally: |
| 185 |
os_helper.unlink(filename) |
| 186 |
|
| 187 |
try: |
| 188 |
os.mkdir(filename) |
| 189 |
self.assertIs(self.pathmodule.isdir(filename), True) |
| 190 |
self.assertIs(self.pathmodule.isdir(bfilename), True) |
| 191 |
finally: |
| 192 |
os_helper.rmdir(filename) |
| 193 |
|
| 194 |
def test_isfile(self): |
| 195 |
filename = os_helper.TESTFN |
| 196 |
bfilename = os.fsencode(filename) |
| 197 |
self.assertIs(self.pathmodule.isfile(filename), False) |
| 198 |
self.assertIs(self.pathmodule.isfile(bfilename), False) |
| 199 |
|
| 200 |
self.assertIs(self.pathmodule.isfile(filename + '\udfff'), False) |
| 201 |
self.assertIs(self.pathmodule.isfile(bfilename + b'\xff'), False) |
| 202 |
self.assertIs(self.pathmodule.isfile(filename + '\x00'), False) |
| 203 |
self.assertIs(self.pathmodule.isfile(bfilename + b'\x00'), False) |
| 204 |
|
| 205 |
try: |
| 206 |
create_file(filename) |
| 207 |
self.assertIs(self.pathmodule.isfile(filename), True) |
| 208 |
self.assertIs(self.pathmodule.isfile(bfilename), True) |
| 209 |
finally: |
| 210 |
os_helper.unlink(filename) |
| 211 |
|
| 212 |
try: |
| 213 |
os.mkdir(filename) |
| 214 |
self.assertIs(self.pathmodule.isfile(filename), False) |
| 215 |
self.assertIs(self.pathmodule.isfile(bfilename), False) |
| 216 |
finally: |
| 217 |
os_helper.rmdir(filename) |
| 218 |
|
| 219 |
def test_samefile(self): |
| 220 |
file1 = os_helper.TESTFN |
| 221 |
file2 = os_helper.TESTFN + "2" |
| 222 |
self.addCleanup(os_helper.unlink, file1) |
| 223 |
self.addCleanup(os_helper.unlink, file2) |
| 224 |
|
| 225 |
create_file(file1) |
| 226 |
self.assertTrue(self.pathmodule.samefile(file1, file1)) |
| 227 |
|
| 228 |
create_file(file2) |
| 229 |
self.assertFalse(self.pathmodule.samefile(file1, file2)) |
| 230 |
|
| 231 |
self.assertRaises(TypeError, self.pathmodule.samefile) |
| 232 |
|
| 233 |
def _test_samefile_on_link_func(self, func): |
| 234 |
test_fn1 = os_helper.TESTFN |
| 235 |
test_fn2 = os_helper.TESTFN + "2" |
| 236 |
self.addCleanup(os_helper.unlink, test_fn1) |
| 237 |
self.addCleanup(os_helper.unlink, test_fn2) |
| 238 |
|
| 239 |
create_file(test_fn1) |
| 240 |
|
| 241 |
func(test_fn1, test_fn2) |
| 242 |
self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2)) |
| 243 |
os.remove(test_fn2) |
| 244 |
|
| 245 |
create_file(test_fn2) |
| 246 |
self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2)) |
| 247 |
|
| 248 |
@os_helper.skip_unless_symlink |
| 249 |
def test_samefile_on_symlink(self): |
| 250 |
self._test_samefile_on_link_func(os.symlink) |
| 251 |
|
| 252 |
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') |
| 253 |
def test_samefile_on_link(self): |
| 254 |
try: |
| 255 |
self._test_samefile_on_link_func(os.link) |
| 256 |
except PermissionError as e: |
| 257 |
self.skipTest('os.link(): %s' % e) |
| 258 |
|
| 259 |
def test_samestat(self): |
| 260 |
test_fn1 = os_helper.TESTFN |
| 261 |
test_fn2 = os_helper.TESTFN + "2" |
| 262 |
self.addCleanup(os_helper.unlink, test_fn1) |
| 263 |
self.addCleanup(os_helper.unlink, test_fn2) |
| 264 |
|
| 265 |
create_file(test_fn1) |
| 266 |
stat1 = os.stat(test_fn1) |
| 267 |
self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1))) |
| 268 |
|
| 269 |
create_file(test_fn2) |
| 270 |
stat2 = os.stat(test_fn2) |
| 271 |
self.assertFalse(self.pathmodule.samestat(stat1, stat2)) |
| 272 |
|
| 273 |
self.assertRaises(TypeError, self.pathmodule.samestat) |
| 274 |
|
| 275 |
def _test_samestat_on_link_func(self, func): |
| 276 |
test_fn1 = os_helper.TESTFN + "1" |
| 277 |
test_fn2 = os_helper.TESTFN + "2" |
| 278 |
self.addCleanup(os_helper.unlink, test_fn1) |
| 279 |
self.addCleanup(os_helper.unlink, test_fn2) |
| 280 |
|
| 281 |
create_file(test_fn1) |
| 282 |
func(test_fn1, test_fn2) |
| 283 |
self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1), |
| 284 |
os.stat(test_fn2))) |
| 285 |
os.remove(test_fn2) |
| 286 |
|
| 287 |
create_file(test_fn2) |
| 288 |
self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1), |
| 289 |
os.stat(test_fn2))) |
| 290 |
|
| 291 |
@os_helper.skip_unless_symlink |
| 292 |
def test_samestat_on_symlink(self): |
| 293 |
self._test_samestat_on_link_func(os.symlink) |
| 294 |
|
| 295 |
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') |
| 296 |
def test_samestat_on_link(self): |
| 297 |
try: |
| 298 |
self._test_samestat_on_link_func(os.link) |
| 299 |
except PermissionError as e: |
| 300 |
self.skipTest('os.link(): %s' % e) |
| 301 |
|
| 302 |
def test_sameopenfile(self): |
| 303 |
filename = os_helper.TESTFN |
| 304 |
self.addCleanup(os_helper.unlink, filename) |
| 305 |
create_file(filename) |
| 306 |
|
| 307 |
with open(filename, "rb", 0) as fp1: |
| 308 |
fd1 = fp1.fileno() |
| 309 |
with open(filename, "rb", 0) as fp2: |
| 310 |
fd2 = fp2.fileno() |
| 311 |
self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2)) |
| 312 |
|
| 313 |
|
| 314 |
class TestGenericTest(GenericTest, unittest.TestCase): |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
pathmodule = genericpath |
| 319 |
|
| 320 |
def test_invalid_paths(self): |
| 321 |
for attr in GenericTest.common_attributes: |
| 322 |
|
| 323 |
if attr == 'commonprefix': |
| 324 |
continue |
| 325 |
func = getattr(self.pathmodule, attr) |
| 326 |
with self.subTest(attr=attr): |
| 327 |
if attr in ('exists', 'isdir', 'isfile'): |
| 328 |
func('/tmp\udfffabcds') |
| 329 |
func(b'/tmp\xffabcds') |
| 330 |
func('/tmp\x00abcds') |
| 331 |
func(b'/tmp\x00abcds') |
| 332 |
else: |
| 333 |
with self.assertRaises((OSError, UnicodeEncodeError)): |
| 334 |
func('/tmp\udfffabcds') |
| 335 |
with self.assertRaises((OSError, UnicodeDecodeError)): |
| 336 |
func(b'/tmp\xffabcds') |
| 337 |
with self.assertRaisesRegex(ValueError, 'embedded null'): |
| 338 |
func('/tmp\x00abcds') |
| 339 |
with self.assertRaisesRegex(ValueError, 'embedded null'): |
| 340 |
func(b'/tmp\x00abcds') |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
class CommonTest(GenericTest): |
| 346 |
common_attributes = GenericTest.common_attributes + [ |
| 347 |
|
| 348 |
'curdir', 'pardir', 'extsep', 'sep', |
| 349 |
'pathsep', 'defpath', 'altsep', 'devnull', |
| 350 |
|
| 351 |
'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', |
| 352 |
'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', |
| 353 |
'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', |
| 354 |
] |
| 355 |
|
| 356 |
def test_normcase(self): |
| 357 |
normcase = self.pathmodule.normcase |
| 358 |
|
| 359 |
for p in ["FoO/./BaR", b"FoO/./BaR"]: |
| 360 |
p = normcase(p) |
| 361 |
self.assertEqual(p, normcase(p)) |
| 362 |
|
| 363 |
self.assertEqual(normcase(''), '') |
| 364 |
self.assertEqual(normcase(b''), b'') |
| 365 |
|
| 366 |
|
| 367 |
for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}): |
| 368 |
self.assertRaises(TypeError, normcase, path) |
| 369 |
|
| 370 |
def test_splitdrive(self): |
| 371 |
|
| 372 |
splitdrive = self.pathmodule.splitdrive |
| 373 |
self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar")) |
| 374 |
self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar")) |
| 375 |
self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar")) |
| 376 |
|
| 377 |
self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar")) |
| 378 |
self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar")) |
| 379 |
self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar")) |
| 380 |
|
| 381 |
def test_expandvars(self): |
| 382 |
expandvars = self.pathmodule.expandvars |
| 383 |
with os_helper.EnvironmentVarGuard() as env: |
| 384 |
env.clear() |
| 385 |
env["foo"] = "bar" |
| 386 |
env["{foo"] = "baz1" |
| 387 |
env["{foo}"] = "baz2" |
| 388 |
self.assertEqual(expandvars("foo"), "foo") |
| 389 |
self.assertEqual(expandvars("$foo bar"), "bar bar") |
| 390 |
self.assertEqual(expandvars("${foo}bar"), "barbar") |
| 391 |
self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar") |
| 392 |
self.assertEqual(expandvars("$bar bar"), "$bar bar") |
| 393 |
self.assertEqual(expandvars("$?bar"), "$?bar") |
| 394 |
self.assertEqual(expandvars("$foo}bar"), "bar}bar") |
| 395 |
self.assertEqual(expandvars("${foo"), "${foo") |
| 396 |
self.assertEqual(expandvars("${{foo}}"), "baz1}") |
| 397 |
self.assertEqual(expandvars("$foo$foo"), "barbar") |
| 398 |
self.assertEqual(expandvars("$bar$bar"), "$bar$bar") |
| 399 |
|
| 400 |
self.assertEqual(expandvars(b"foo"), b"foo") |
| 401 |
self.assertEqual(expandvars(b"$foo bar"), b"bar bar") |
| 402 |
self.assertEqual(expandvars(b"${foo}bar"), b"barbar") |
| 403 |
self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar") |
| 404 |
self.assertEqual(expandvars(b"$bar bar"), b"$bar bar") |
| 405 |
self.assertEqual(expandvars(b"$?bar"), b"$?bar") |
| 406 |
self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar") |
| 407 |
self.assertEqual(expandvars(b"${foo"), b"${foo") |
| 408 |
self.assertEqual(expandvars(b"${{foo}}"), b"baz1}") |
| 409 |
self.assertEqual(expandvars(b"$foo$foo"), b"barbar") |
| 410 |
self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar") |
| 411 |
|
| 412 |
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII') |
| 413 |
def test_expandvars_nonascii(self): |
| 414 |
expandvars = self.pathmodule.expandvars |
| 415 |
def check(value, expected): |
| 416 |
self.assertEqual(expandvars(value), expected) |
| 417 |
with os_helper.EnvironmentVarGuard() as env: |
| 418 |
env.clear() |
| 419 |
nonascii = os_helper.FS_NONASCII |
| 420 |
env['spam'] = nonascii |
| 421 |
env[nonascii] = 'ham' + nonascii |
| 422 |
check(nonascii, nonascii) |
| 423 |
check('$spam bar', '%s bar' % nonascii) |
| 424 |
check('${spam}bar', '%sbar' % nonascii) |
| 425 |
check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) |
| 426 |
check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii) |
| 427 |
check('$spam}bar', '%s}bar' % nonascii) |
| 428 |
|
| 429 |
check(os.fsencode(nonascii), os.fsencode(nonascii)) |
| 430 |
check(b'$spam bar', os.fsencode('%s bar' % nonascii)) |
| 431 |
check(b'${spam}bar', os.fsencode('%sbar' % nonascii)) |
| 432 |
check(os.fsencode('${%s}bar' % nonascii), |
| 433 |
os.fsencode('ham%sbar' % nonascii)) |
| 434 |
check(os.fsencode('$bar%s bar' % nonascii), |
| 435 |
os.fsencode('$bar%s bar' % nonascii)) |
| 436 |
check(b'$spam}bar', os.fsencode('%s}bar' % nonascii)) |
| 437 |
|
| 438 |
@support.requires_resource('cpu') |
| 439 |
def test_expandvars_large(self): |
| 440 |
expandvars = self.pathmodule.expandvars |
| 441 |
with os_helper.EnvironmentVarGuard() as env: |
| 442 |
env.clear() |
| 443 |
env["A"] = "B" |
| 444 |
n = 100_000 |
| 445 |
self.assertEqual(expandvars('$A'*n), 'B'*n) |
| 446 |
self.assertEqual(expandvars('${A}'*n), 'B'*n) |
| 447 |
self.assertEqual(expandvars('$A!'*n), 'B!'*n) |
| 448 |
self.assertEqual(expandvars('${A}A'*n), 'BA'*n) |
| 449 |
self.assertEqual(expandvars('${'*10*n), '${'*10*n) |
| 450 |
|
| 451 |
def test_abspath(self): |
| 452 |
self.assertIn("foo", self.pathmodule.abspath("foo")) |
| 453 |
with warnings.catch_warnings(): |
| 454 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 455 |
self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) |
| 456 |
|
| 457 |
|
| 458 |
undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2' |
| 459 |
|
| 460 |
|
| 461 |
with warnings.catch_warnings(): |
| 462 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 463 |
for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'): |
| 464 |
self.assertIsInstance(self.pathmodule.abspath(path), bytes) |
| 465 |
|
| 466 |
def test_realpath(self): |
| 467 |
self.assertIn("foo", self.pathmodule.realpath("foo")) |
| 468 |
with warnings.catch_warnings(): |
| 469 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 470 |
self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) |
| 471 |
|
| 472 |
def test_normpath_issue5827(self): |
| 473 |
|
| 474 |
for path in ('', '.', '/', '\\', '///foo/.//bar//'): |
| 475 |
self.assertIsInstance(self.pathmodule.normpath(path), str) |
| 476 |
|
| 477 |
def test_normpath_issue106242(self): |
| 478 |
for path in ('\x00', 'foo\x00bar', '\x00\x00', '\x00foo', 'foo\x00'): |
| 479 |
self.assertEqual(self.pathmodule.normpath(path), path) |
| 480 |
|
| 481 |
def test_abspath_issue3426(self): |
| 482 |
|
| 483 |
|
| 484 |
abspath = self.pathmodule.abspath |
| 485 |
for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 486 |
self.assertIsInstance(abspath(path), str) |
| 487 |
|
| 488 |
unicwd = '\xe7w\xf0' |
| 489 |
try: |
| 490 |
os.fsencode(unicwd) |
| 491 |
except (AttributeError, UnicodeEncodeError): |
| 492 |
|
| 493 |
pass |
| 494 |
else: |
| 495 |
with os_helper.temp_cwd(unicwd): |
| 496 |
for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 497 |
self.assertIsInstance(abspath(path), str) |
| 498 |
|
| 499 |
def test_nonascii_abspath(self): |
| 500 |
if (os_helper.TESTFN_UNDECODABLE |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
and sys.platform not in ('win32', 'darwin', 'emscripten', 'wasi')): |
| 506 |
name = os_helper.TESTFN_UNDECODABLE |
| 507 |
elif os_helper.TESTFN_NONASCII: |
| 508 |
name = os_helper.TESTFN_NONASCII |
| 509 |
else: |
| 510 |
self.skipTest("need os_helper.TESTFN_NONASCII") |
| 511 |
|
| 512 |
with warnings.catch_warnings(): |
| 513 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 514 |
with os_helper.temp_cwd(name): |
| 515 |
self.test_abspath() |
| 516 |
|
| 517 |
def test_join_errors(self): |
| 518 |
|
| 519 |
with warnings_helper.check_warnings(('', BytesWarning), quiet=True): |
| 520 |
errmsg = "Can't mix strings and bytes in path components" |
| 521 |
with self.assertRaisesRegex(TypeError, errmsg): |
| 522 |
self.pathmodule.join(b'bytes', 'str') |
| 523 |
with self.assertRaisesRegex(TypeError, errmsg): |
| 524 |
self.pathmodule.join('str', b'bytes') |
| 525 |
|
| 526 |
with self.assertRaisesRegex(TypeError, 'int'): |
| 527 |
self.pathmodule.join(42, 'str') |
| 528 |
with self.assertRaisesRegex(TypeError, 'int'): |
| 529 |
self.pathmodule.join('str', 42) |
| 530 |
with self.assertRaisesRegex(TypeError, 'int'): |
| 531 |
self.pathmodule.join(42) |
| 532 |
with self.assertRaisesRegex(TypeError, 'list'): |
| 533 |
self.pathmodule.join([]) |
| 534 |
with self.assertRaisesRegex(TypeError, 'bytearray'): |
| 535 |
self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar')) |
| 536 |
|
| 537 |
def test_relpath_errors(self): |
| 538 |
|
| 539 |
with warnings_helper.check_warnings( |
| 540 |
('', (BytesWarning, DeprecationWarning)), quiet=True): |
| 541 |
errmsg = "Can't mix strings and bytes in path components" |
| 542 |
with self.assertRaisesRegex(TypeError, errmsg): |
| 543 |
self.pathmodule.relpath(b'bytes', 'str') |
| 544 |
with self.assertRaisesRegex(TypeError, errmsg): |
| 545 |
self.pathmodule.relpath('str', b'bytes') |
| 546 |
with self.assertRaisesRegex(TypeError, 'int'): |
| 547 |
self.pathmodule.relpath(42, 'str') |
| 548 |
with self.assertRaisesRegex(TypeError, 'int'): |
| 549 |
self.pathmodule.relpath('str', 42) |
| 550 |
with self.assertRaisesRegex(TypeError, 'bytearray'): |
| 551 |
self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) |
| 552 |
|
| 553 |
def test_import(self): |
| 554 |
assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__) |
| 555 |
|
| 556 |
|
| 557 |
class PathLikeTests(unittest.TestCase): |
| 558 |
|
| 559 |
def setUp(self): |
| 560 |
self.file_name = os_helper.TESTFN |
| 561 |
self.file_path = FakePath(os_helper.TESTFN) |
| 562 |
self.addCleanup(os_helper.unlink, self.file_name) |
| 563 |
create_file(self.file_name, b"test_genericpath.PathLikeTests") |
| 564 |
|
| 565 |
def assertPathEqual(self, func): |
| 566 |
self.assertEqual(func(self.file_path), func(self.file_name)) |
| 567 |
|
| 568 |
def test_path_exists(self): |
| 569 |
self.assertPathEqual(os.path.exists) |
| 570 |
|
| 571 |
def test_path_isfile(self): |
| 572 |
self.assertPathEqual(os.path.isfile) |
| 573 |
|
| 574 |
def test_path_isdir(self): |
| 575 |
self.assertPathEqual(os.path.isdir) |
| 576 |
|
| 577 |
def test_path_commonprefix(self): |
| 578 |
self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]), |
| 579 |
self.file_name) |
| 580 |
|
| 581 |
def test_path_getsize(self): |
| 582 |
self.assertPathEqual(os.path.getsize) |
| 583 |
|
| 584 |
def test_path_getmtime(self): |
| 585 |
self.assertPathEqual(os.path.getatime) |
| 586 |
|
| 587 |
def test_path_getctime(self): |
| 588 |
self.assertPathEqual(os.path.getctime) |
| 589 |
|
| 590 |
def test_path_samefile(self): |
| 591 |
self.assertTrue(os.path.samefile(self.file_path, self.file_name)) |
| 592 |
|
| 593 |
|
| 594 |
if __name__ == "__main__": |
| 595 |
unittest.main() |