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