| 1 |
import collections |
| 2 |
import contextlib |
| 3 |
import io |
| 4 |
import os |
| 5 |
import sys |
| 6 |
import errno |
| 7 |
import ntpath |
| 8 |
import pathlib |
| 9 |
import pickle |
| 10 |
import posixpath |
| 11 |
import socket |
| 12 |
import stat |
| 13 |
import tempfile |
| 14 |
import unittest |
| 15 |
from unittest import mock |
| 16 |
from urllib.request import pathname2url |
| 17 |
|
| 18 |
from test.support import import_helper |
| 19 |
from test.support import cpython_only |
| 20 |
from test.support import is_emscripten, is_wasi, is_wasm32 |
| 21 |
from test.support import infinite_recursion |
| 22 |
from test.support import os_helper |
| 23 |
from test.support.os_helper import TESTFN, FS_NONASCII, FakePath |
| 24 |
try: |
| 25 |
import fcntl |
| 26 |
except ImportError: |
| 27 |
fcntl = None |
| 28 |
try: |
| 29 |
import grp, pwd |
| 30 |
except ImportError: |
| 31 |
grp = pwd = None |
| 32 |
try: |
| 33 |
import posix |
| 34 |
except ImportError: |
| 35 |
posix = None |
| 36 |
|
| 37 |
|
| 38 |
root_in_posix = False |
| 39 |
if hasattr(os, 'geteuid'): |
| 40 |
root_in_posix = (os.geteuid() == 0) |
| 41 |
|
| 42 |
|
| 43 |
def patch_replace(old_test): |
| 44 |
def new_replace(self, target): |
| 45 |
raise OSError(errno.EXDEV, "Cross-device link", self, target) |
| 46 |
|
| 47 |
def new_test(self): |
| 48 |
old_replace = self.cls.replace |
| 49 |
self.cls.replace = new_replace |
| 50 |
try: |
| 51 |
old_test(self) |
| 52 |
finally: |
| 53 |
self.cls.replace = old_replace |
| 54 |
return new_test |
| 55 |
|
| 56 |
|
| 57 |
_tests_needing_posix = set() |
| 58 |
_tests_needing_windows = set() |
| 59 |
_tests_needing_symlinks = set() |
| 60 |
|
| 61 |
def needs_posix(fn): |
| 62 |
"""Decorator that marks a test as requiring a POSIX-flavoured path class.""" |
| 63 |
_tests_needing_posix.add(fn.__name__) |
| 64 |
return fn |
| 65 |
|
| 66 |
def needs_windows(fn): |
| 67 |
"""Decorator that marks a test as requiring a Windows-flavoured path class.""" |
| 68 |
_tests_needing_windows.add(fn.__name__) |
| 69 |
return fn |
| 70 |
|
| 71 |
def needs_symlinks(fn): |
| 72 |
"""Decorator that marks a test as requiring a path class that supports symlinks.""" |
| 73 |
_tests_needing_symlinks.add(fn.__name__) |
| 74 |
return fn |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
class UnsupportedOperationTest(unittest.TestCase): |
| 79 |
def test_is_notimplemented(self): |
| 80 |
self.assertIsSubclass(pathlib.UnsupportedOperation, NotImplementedError) |
| 81 |
self.assertIsInstance(pathlib.UnsupportedOperation(), NotImplementedError) |
| 82 |
|
| 83 |
|
| 84 |
class LazyImportTest(unittest.TestCase): |
| 85 |
@cpython_only |
| 86 |
def test_lazy_import(self): |
| 87 |
import_helper.ensure_lazy_imports("pathlib", {"shutil"}) |
| 88 |
|
| 89 |
|
| 90 |
# |
| 91 |
# Tests for the pure classes. |
| 92 |
# |
| 93 |
|
| 94 |
class PurePathTest(unittest.TestCase): |
| 95 |
cls = pathlib.PurePath |
| 96 |
|
| 97 |
# Make sure any symbolic links in the base test path are resolved. |
| 98 |
base = os.path.realpath(TESTFN) |
| 99 |
|
| 100 |
# Keys are canonical paths, values are list of tuples of arguments |
| 101 |
# supposed to produce equal paths. |
| 102 |
equivalences = { |
| 103 |
'a/b': [ |
| 104 |
('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'), |
| 105 |
('a/b/',), ('a//b',), ('a//b//',), |
| 106 |
# Empty components get removed. |
| 107 |
('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''), |
| 108 |
], |
| 109 |
'/b/c/d': [ |
| 110 |
('a', '/b/c', 'd'), ('/a', '/b/c', 'd'), |
| 111 |
# Empty components get removed. |
| 112 |
('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'), |
| 113 |
], |
| 114 |
} |
| 115 |
|
| 116 |
def setUp(self): |
| 117 |
name = self.id().split('.')[-1] |
| 118 |
if name in _tests_needing_posix and self.cls.parser is not posixpath: |
| 119 |
self.skipTest('requires POSIX-flavoured path class') |
| 120 |
if name in _tests_needing_windows and self.cls.parser is posixpath: |
| 121 |
self.skipTest('requires Windows-flavoured path class') |
| 122 |
p = self.cls('a') |
| 123 |
self.parser = p.parser |
| 124 |
self.sep = self.parser.sep |
| 125 |
self.altsep = self.parser.altsep |
| 126 |
|
| 127 |
def _check_str_subclass(self, *args): |
| 128 |
# Issue #21127: it should be possible to construct a PurePath object |
| 129 |
# from a str subclass instance, and it then gets converted to |
| 130 |
# a pure str object. |
| 131 |
class StrSubclass(str): |
| 132 |
pass |
| 133 |
P = self.cls |
| 134 |
p = P(*(StrSubclass(x) for x in args)) |
| 135 |
self.assertEqual(p, P(*args)) |
| 136 |
for part in p.parts: |
| 137 |
self.assertIs(type(part), str) |
| 138 |
|
| 139 |
def test_str_subclass_common(self): |
| 140 |
self._check_str_subclass('') |
| 141 |
self._check_str_subclass('.') |
| 142 |
self._check_str_subclass('a') |
| 143 |
self._check_str_subclass('a/b.txt') |
| 144 |
self._check_str_subclass('/a/b.txt') |
| 145 |
|
| 146 |
@needs_windows |
| 147 |
def test_str_subclass_windows(self): |
| 148 |
self._check_str_subclass('.\\a:b') |
| 149 |
self._check_str_subclass('c:') |
| 150 |
self._check_str_subclass('c:a') |
| 151 |
self._check_str_subclass('c:a\\b.txt') |
| 152 |
self._check_str_subclass('c:\\') |
| 153 |
self._check_str_subclass('c:\\a') |
| 154 |
self._check_str_subclass('c:\\a\\b.txt') |
| 155 |
self._check_str_subclass('\\\\some\\share') |
| 156 |
self._check_str_subclass('\\\\some\\share\\a') |
| 157 |
self._check_str_subclass('\\\\some\\share\\a\\b.txt') |
| 158 |
|
| 159 |
def _check_str(self, expected, args): |
| 160 |
p = self.cls(*args) |
| 161 |
self.assertEqual(str(p), expected.replace('/', self.sep)) |
| 162 |
|
| 163 |
def test_str_common(self): |
| 164 |
# Canonicalized paths roundtrip. |
| 165 |
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 166 |
self._check_str(pathstr, (pathstr,)) |
| 167 |
# Other tests for str() are in test_equivalences(). |
| 168 |
|
| 169 |
@needs_windows |
| 170 |
def test_str_windows(self): |
| 171 |
p = self.cls('a/b/c') |
| 172 |
self.assertEqual(str(p), 'a\\b\\c') |
| 173 |
p = self.cls('c:/a/b/c') |
| 174 |
self.assertEqual(str(p), 'c:\\a\\b\\c') |
| 175 |
p = self.cls('//a/b') |
| 176 |
self.assertEqual(str(p), '\\\\a\\b\\') |
| 177 |
p = self.cls('//a/b/c') |
| 178 |
self.assertEqual(str(p), '\\\\a\\b\\c') |
| 179 |
p = self.cls('//a/b/c/d') |
| 180 |
self.assertEqual(str(p), '\\\\a\\b\\c\\d') |
| 181 |
|
| 182 |
def test_concrete_class(self): |
| 183 |
if self.cls is pathlib.PurePath: |
| 184 |
expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath |
| 185 |
else: |
| 186 |
expected = self.cls |
| 187 |
p = self.cls('a') |
| 188 |
self.assertIs(type(p), expected) |
| 189 |
|
| 190 |
def test_concrete_parser(self): |
| 191 |
if self.cls is pathlib.PurePosixPath: |
| 192 |
expected = posixpath |
| 193 |
elif self.cls is pathlib.PureWindowsPath: |
| 194 |
expected = ntpath |
| 195 |
else: |
| 196 |
expected = os.path |
| 197 |
p = self.cls('a') |
| 198 |
self.assertIs(p.parser, expected) |
| 199 |
|
| 200 |
def test_different_parsers_unequal(self): |
| 201 |
p = self.cls('a') |
| 202 |
if p.parser is posixpath: |
| 203 |
q = pathlib.PureWindowsPath('a') |
| 204 |
else: |
| 205 |
q = pathlib.PurePosixPath('a') |
| 206 |
self.assertNotEqual(p, q) |
| 207 |
|
| 208 |
def test_different_parsers_unordered(self): |
| 209 |
p = self.cls('a') |
| 210 |
if p.parser is posixpath: |
| 211 |
q = pathlib.PureWindowsPath('a') |
| 212 |
else: |
| 213 |
q = pathlib.PurePosixPath('a') |
| 214 |
with self.assertRaises(TypeError): |
| 215 |
p < q |
| 216 |
with self.assertRaises(TypeError): |
| 217 |
p <= q |
| 218 |
with self.assertRaises(TypeError): |
| 219 |
p > q |
| 220 |
with self.assertRaises(TypeError): |
| 221 |
p >= q |
| 222 |
|
| 223 |
def test_constructor_nested(self): |
| 224 |
P = self.cls |
| 225 |
P(FakePath("a/b/c")) |
| 226 |
self.assertEqual(P(P('a')), P('a')) |
| 227 |
self.assertEqual(P(P('a'), 'b'), P('a/b')) |
| 228 |
self.assertEqual(P(P('a'), P('b')), P('a/b')) |
| 229 |
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c"))) |
| 230 |
self.assertEqual(P(P('./a:b')), P('./a:b')) |
| 231 |
|
| 232 |
@needs_windows |
| 233 |
def test_constructor_nested_foreign_flavour(self): |
| 234 |
# See GH-125069. |
| 235 |
p1 = pathlib.PurePosixPath('b/c:\\d') |
| 236 |
p2 = pathlib.PurePosixPath('b/', 'c:\\d') |
| 237 |
self.assertEqual(p1, p2) |
| 238 |
self.assertEqual(self.cls(p1), self.cls('b/c:/d')) |
| 239 |
self.assertEqual(self.cls(p2), self.cls('b/c:/d')) |
| 240 |
|
| 241 |
def _check_parse_path(self, raw_path, *expected): |
| 242 |
sep = self.parser.sep |
| 243 |
actual = self.cls._parse_path(raw_path.replace('/', sep)) |
| 244 |
self.assertEqual(actual, expected) |
| 245 |
if altsep := self.parser.altsep: |
| 246 |
actual = self.cls._parse_path(raw_path.replace('/', altsep)) |
| 247 |
self.assertEqual(actual, expected) |
| 248 |
|
| 249 |
def test_parse_path_common(self): |
| 250 |
check = self._check_parse_path |
| 251 |
sep = self.parser.sep |
| 252 |
check('', '', '', []) |
| 253 |
check('a', '', '', ['a']) |
| 254 |
check('a/', '', '', ['a']) |
| 255 |
check('a/b', '', '', ['a', 'b']) |
| 256 |
check('a/b/', '', '', ['a', 'b']) |
| 257 |
check('a/b/c/d', '', '', ['a', 'b', 'c', 'd']) |
| 258 |
check('a/b//c/d', '', '', ['a', 'b', 'c', 'd']) |
| 259 |
check('a/b/c/d', '', '', ['a', 'b', 'c', 'd']) |
| 260 |
check('.', '', '', []) |
| 261 |
check('././b', '', '', ['b']) |
| 262 |
check('a/./b', '', '', ['a', 'b']) |
| 263 |
check('a/./.', '', '', ['a']) |
| 264 |
check('/a/b', '', sep, ['a', 'b']) |
| 265 |
|
| 266 |
def test_empty_path(self): |
| 267 |
# The empty path points to '.' |
| 268 |
p = self.cls('') |
| 269 |
self.assertEqual(str(p), '.') |
| 270 |
# Special case for the empty path. |
| 271 |
self._check_str('.', ('',)) |
| 272 |
|
| 273 |
def test_join_nested(self): |
| 274 |
P = self.cls |
| 275 |
p = P('a/b').joinpath(P('c')) |
| 276 |
self.assertEqual(p, P('a/b/c')) |
| 277 |
|
| 278 |
def test_div_nested(self): |
| 279 |
P = self.cls |
| 280 |
p = P('a/b') / P('c') |
| 281 |
self.assertEqual(p, P('a/b/c')) |
| 282 |
|
| 283 |
def test_pickling_common(self): |
| 284 |
P = self.cls |
| 285 |
for pathstr in ('a', 'a/', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c', 'a/b/c/'): |
| 286 |
with self.subTest(pathstr=pathstr): |
| 287 |
p = P(pathstr) |
| 288 |
for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
| 289 |
dumped = pickle.dumps(p, proto) |
| 290 |
pp = pickle.loads(dumped) |
| 291 |
self.assertIs(pp.__class__, p.__class__) |
| 292 |
self.assertEqual(pp, p) |
| 293 |
self.assertEqual(hash(pp), hash(p)) |
| 294 |
self.assertEqual(str(pp), str(p)) |
| 295 |
|
| 296 |
def test_unpicking_3_13(self): |
| 297 |
data = (b"\x80\x04\x95'\x00\x00\x00\x00\x00\x00\x00\x8c\x0e" |
| 298 |
b"pathlib._local\x94\x8c\rPurePosixPath\x94\x93\x94)R\x94.") |
| 299 |
p = pickle.loads(data) |
| 300 |
self.assertIsInstance(p, pathlib.PurePosixPath) |
| 301 |
|
| 302 |
def test_repr_common(self): |
| 303 |
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 304 |
with self.subTest(pathstr=pathstr): |
| 305 |
p = self.cls(pathstr) |
| 306 |
clsname = p.__class__.__name__ |
| 307 |
r = repr(p) |
| 308 |
# The repr() is in the form ClassName("forward-slashes path"). |
| 309 |
self.assertStartsWith(r, clsname + '(') |
| 310 |
self.assertEndsWith(r, ')') |
| 311 |
inner = r[len(clsname) + 1 : -1] |
| 312 |
self.assertEqual(eval(inner), p.as_posix()) |
| 313 |
|
| 314 |
def test_fspath_common(self): |
| 315 |
P = self.cls |
| 316 |
p = P('a/b') |
| 317 |
self._check_str(p.__fspath__(), ('a/b',)) |
| 318 |
self._check_str(os.fspath(p), ('a/b',)) |
| 319 |
|
| 320 |
def test_bytes(self): |
| 321 |
P = self.cls |
| 322 |
with self.assertRaises(TypeError): |
| 323 |
P(b'a') |
| 324 |
with self.assertRaises(TypeError): |
| 325 |
P(b'a', 'b') |
| 326 |
with self.assertRaises(TypeError): |
| 327 |
P('a', b'b') |
| 328 |
with self.assertRaises(TypeError): |
| 329 |
P('a').joinpath(b'b') |
| 330 |
with self.assertRaises(TypeError): |
| 331 |
P('a') / b'b' |
| 332 |
with self.assertRaises(TypeError): |
| 333 |
b'a' / P('b') |
| 334 |
with self.assertRaises(TypeError): |
| 335 |
P('a').match(b'b') |
| 336 |
with self.assertRaises(TypeError): |
| 337 |
P('a').relative_to(b'b') |
| 338 |
with self.assertRaises(TypeError): |
| 339 |
P('a').with_name(b'b') |
| 340 |
with self.assertRaises(TypeError): |
| 341 |
P('a').with_stem(b'b') |
| 342 |
with self.assertRaises(TypeError): |
| 343 |
P('a').with_suffix(b'b') |
| 344 |
|
| 345 |
def test_bytes_exc_message(self): |
| 346 |
P = self.cls |
| 347 |
message = (r"argument should be a str or an os\.PathLike object " |
| 348 |
r"where __fspath__ returns a str, not 'bytes'") |
| 349 |
with self.assertRaisesRegex(TypeError, message): |
| 350 |
P(b'a') |
| 351 |
with self.assertRaisesRegex(TypeError, message): |
| 352 |
P(b'a', 'b') |
| 353 |
with self.assertRaisesRegex(TypeError, message): |
| 354 |
P('a', b'b') |
| 355 |
|
| 356 |
def test_as_bytes_common(self): |
| 357 |
sep = os.fsencode(self.sep) |
| 358 |
P = self.cls |
| 359 |
self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b') |
| 360 |
|
| 361 |
def test_as_posix_common(self): |
| 362 |
P = self.cls |
| 363 |
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 364 |
self.assertEqual(P(pathstr).as_posix(), pathstr) |
| 365 |
# Other tests for as_posix() are in test_equivalences(). |
| 366 |
|
| 367 |
def test_eq_common(self): |
| 368 |
P = self.cls |
| 369 |
self.assertEqual(P('a/b'), P('a/b')) |
| 370 |
self.assertEqual(P('a/b'), P('a', 'b')) |
| 371 |
self.assertNotEqual(P('a/b'), P('a')) |
| 372 |
self.assertNotEqual(P('a/b'), P('/a/b')) |
| 373 |
self.assertNotEqual(P('a/b'), P()) |
| 374 |
self.assertNotEqual(P('/a/b'), P('/')) |
| 375 |
self.assertNotEqual(P(), P('/')) |
| 376 |
self.assertNotEqual(P(), "") |
| 377 |
self.assertNotEqual(P(), {}) |
| 378 |
self.assertNotEqual(P(), int) |
| 379 |
|
| 380 |
def test_equivalences(self, equivalences=None): |
| 381 |
if equivalences is None: |
| 382 |
equivalences = self.equivalences |
| 383 |
for k, tuples in equivalences.items(): |
| 384 |
canon = k.replace('/', self.sep) |
| 385 |
posix = k.replace(self.sep, '/') |
| 386 |
if canon != posix: |
| 387 |
tuples = tuples + [ |
| 388 |
tuple(part.replace('/', self.sep) for part in t) |
| 389 |
for t in tuples |
| 390 |
] |
| 391 |
tuples.append((posix, )) |
| 392 |
pcanon = self.cls(canon) |
| 393 |
for t in tuples: |
| 394 |
p = self.cls(*t) |
| 395 |
self.assertEqual(p, pcanon, "failed with args {}".format(t)) |
| 396 |
self.assertEqual(hash(p), hash(pcanon)) |
| 397 |
self.assertEqual(str(p), canon) |
| 398 |
self.assertEqual(p.as_posix(), posix) |
| 399 |
|
| 400 |
def test_ordering_common(self): |
| 401 |
# Ordering is tuple-alike. |
| 402 |
def assertLess(a, b): |
| 403 |
self.assertLess(a, b) |
| 404 |
self.assertGreater(b, a) |
| 405 |
P = self.cls |
| 406 |
a = P('a') |
| 407 |
b = P('a/b') |
| 408 |
c = P('abc') |
| 409 |
d = P('b') |
| 410 |
assertLess(a, b) |
| 411 |
assertLess(a, c) |
| 412 |
assertLess(a, d) |
| 413 |
assertLess(b, c) |
| 414 |
assertLess(c, d) |
| 415 |
P = self.cls |
| 416 |
a = P('/a') |
| 417 |
b = P('/a/b') |
| 418 |
c = P('/abc') |
| 419 |
d = P('/b') |
| 420 |
assertLess(a, b) |
| 421 |
assertLess(a, c) |
| 422 |
assertLess(a, d) |
| 423 |
assertLess(b, c) |
| 424 |
assertLess(c, d) |
| 425 |
with self.assertRaises(TypeError): |
| 426 |
P() < {} |
| 427 |
|
| 428 |
def make_uri(self, path): |
| 429 |
if isinstance(path, pathlib.Path): |
| 430 |
return path.as_uri() |
| 431 |
with self.assertWarns(DeprecationWarning): |
| 432 |
return path.as_uri() |
| 433 |
|
| 434 |
def test_as_uri_common(self): |
| 435 |
P = self.cls |
| 436 |
with self.assertRaises(ValueError): |
| 437 |
self.make_uri(P('a')) |
| 438 |
with self.assertRaises(ValueError): |
| 439 |
self.make_uri(P()) |
| 440 |
|
| 441 |
def test_repr_roundtrips(self): |
| 442 |
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 443 |
with self.subTest(pathstr=pathstr): |
| 444 |
p = self.cls(pathstr) |
| 445 |
r = repr(p) |
| 446 |
# The repr() roundtrips. |
| 447 |
q = eval(r, pathlib.__dict__) |
| 448 |
self.assertIs(q.__class__, p.__class__) |
| 449 |
self.assertEqual(q, p) |
| 450 |
self.assertEqual(repr(q), r) |
| 451 |
|
| 452 |
def test_drive_common(self): |
| 453 |
P = self.cls |
| 454 |
self.assertEqual(P('a/b').drive, '') |
| 455 |
self.assertEqual(P('/a/b').drive, '') |
| 456 |
self.assertEqual(P('').drive, '') |
| 457 |
|
| 458 |
@needs_windows |
| 459 |
def test_drive_windows(self): |
| 460 |
P = self.cls |
| 461 |
self.assertEqual(P('c:').drive, 'c:') |
| 462 |
self.assertEqual(P('c:a/b').drive, 'c:') |
| 463 |
self.assertEqual(P('c:/').drive, 'c:') |
| 464 |
self.assertEqual(P('c:/a/b/').drive, 'c:') |
| 465 |
self.assertEqual(P('//a/b').drive, '\\\\a\\b') |
| 466 |
self.assertEqual(P('//a/b/').drive, '\\\\a\\b') |
| 467 |
self.assertEqual(P('//a/b/c/d').drive, '\\\\a\\b') |
| 468 |
self.assertEqual(P('./c:a').drive, '') |
| 469 |
|
| 470 |
|
| 471 |
def test_root_common(self): |
| 472 |
P = self.cls |
| 473 |
sep = self.sep |
| 474 |
self.assertEqual(P('').root, '') |
| 475 |
self.assertEqual(P('a/b').root, '') |
| 476 |
self.assertEqual(P('/').root, sep) |
| 477 |
self.assertEqual(P('/a/b').root, sep) |
| 478 |
|
| 479 |
@needs_posix |
| 480 |
def test_root_posix(self): |
| 481 |
P = self.cls |
| 482 |
self.assertEqual(P('/a/b').root, '/') |
| 483 |
# POSIX special case for two leading slashes. |
| 484 |
self.assertEqual(P('//a/b').root, '//') |
| 485 |
|
| 486 |
@needs_windows |
| 487 |
def test_root_windows(self): |
| 488 |
P = self.cls |
| 489 |
self.assertEqual(P('c:').root, '') |
| 490 |
self.assertEqual(P('c:a/b').root, '') |
| 491 |
self.assertEqual(P('c:/').root, '\\') |
| 492 |
self.assertEqual(P('c:/a/b/').root, '\\') |
| 493 |
self.assertEqual(P('//a/b').root, '\\') |
| 494 |
self.assertEqual(P('//a/b/').root, '\\') |
| 495 |
self.assertEqual(P('//a/b/c/d').root, '\\') |
| 496 |
|
| 497 |
def test_name_empty(self): |
| 498 |
P = self.cls |
| 499 |
self.assertEqual(P('').name, '') |
| 500 |
self.assertEqual(P('.').name, '') |
| 501 |
self.assertEqual(P('/a/b/.').name, 'b') |
| 502 |
|
| 503 |
def test_stem_empty(self): |
| 504 |
P = self.cls |
| 505 |
self.assertEqual(P('').stem, '') |
| 506 |
self.assertEqual(P('.').stem, '') |
| 507 |
|
| 508 |
@needs_windows |
| 509 |
def test_with_name_windows(self): |
| 510 |
P = self.cls |
| 511 |
self.assertRaises(ValueError, P(r'c:').with_name, 'd.xml') |
| 512 |
self.assertRaises(ValueError, P(r'c:\\').with_name, 'd.xml') |
| 513 |
self.assertRaises(ValueError, P(r'\\My\Share').with_name, 'd.xml') |
| 514 |
# NTFS alternate data streams |
| 515 |
self.assertEqual(str(P('a').with_name('d:')), '.\\d:') |
| 516 |
self.assertEqual(str(P('a').with_name('d:e')), '.\\d:e') |
| 517 |
self.assertEqual(P(r'c:a\b').with_name('d:'), P(r'c:a\d:')) |
| 518 |
self.assertEqual(P(r'c:a\b').with_name('d:e'), P(r'c:a\d:e')) |
| 519 |
|
| 520 |
def test_with_name_empty(self): |
| 521 |
P = self.cls |
| 522 |
self.assertRaises(ValueError, P('').with_name, 'd.xml') |
| 523 |
self.assertRaises(ValueError, P('.').with_name, 'd.xml') |
| 524 |
self.assertRaises(ValueError, P('/').with_name, 'd.xml') |
| 525 |
self.assertRaises(ValueError, P('a/b').with_name, '') |
| 526 |
self.assertRaises(ValueError, P('a/b').with_name, '.') |
| 527 |
|
| 528 |
@needs_windows |
| 529 |
def test_with_stem_windows(self): |
| 530 |
P = self.cls |
| 531 |
self.assertRaises(ValueError, P('c:').with_stem, 'd') |
| 532 |
self.assertRaises(ValueError, P('c:/').with_stem, 'd') |
| 533 |
self.assertRaises(ValueError, P('//My/Share').with_stem, 'd') |
| 534 |
# NTFS alternate data streams |
| 535 |
self.assertEqual(str(P('a').with_stem('d:')), '.\\d:') |
| 536 |
self.assertEqual(str(P('a').with_stem('d:e')), '.\\d:e') |
| 537 |
self.assertEqual(P('c:a/b').with_stem('d:'), P('c:a/d:')) |
| 538 |
self.assertEqual(P('c:a/b').with_stem('d:e'), P('c:a/d:e')) |
| 539 |
|
| 540 |
def test_with_stem_empty(self): |
| 541 |
P = self.cls |
| 542 |
self.assertRaises(ValueError, P('').with_stem, 'd') |
| 543 |
self.assertRaises(ValueError, P('.').with_stem, 'd') |
| 544 |
self.assertRaises(ValueError, P('/').with_stem, 'd') |
| 545 |
self.assertRaises(ValueError, P('a/b').with_stem, '') |
| 546 |
self.assertRaises(ValueError, P('a/b').with_stem, '.') |
| 547 |
|
| 548 |
def test_is_reserved_deprecated(self): |
| 549 |
P = self.cls |
| 550 |
p = P('a/b') |
| 551 |
with self.assertWarns(DeprecationWarning): |
| 552 |
p.is_reserved() |
| 553 |
|
| 554 |
def test_full_match_case_sensitive(self): |
| 555 |
P = self.cls |
| 556 |
self.assertFalse(P('A.py').full_match('a.PY', case_sensitive=True)) |
| 557 |
self.assertTrue(P('A.py').full_match('a.PY', case_sensitive=False)) |
| 558 |
self.assertFalse(P('c:/a/B.Py').full_match('C:/A/*.pY', case_sensitive=True)) |
| 559 |
self.assertTrue(P('/a/b/c.py').full_match('/A/*/*.Py', case_sensitive=False)) |
| 560 |
|
| 561 |
def test_match_empty(self): |
| 562 |
P = self.cls |
| 563 |
self.assertRaises(ValueError, P('a').match, '') |
| 564 |
self.assertRaises(ValueError, P('a').match, '.') |
| 565 |
|
| 566 |
def test_match_common(self): |
| 567 |
P = self.cls |
| 568 |
# Simple relative pattern. |
| 569 |
self.assertTrue(P('b.py').match('b.py')) |
| 570 |
self.assertTrue(P('a/b.py').match('b.py')) |
| 571 |
self.assertTrue(P('/a/b.py').match('b.py')) |
| 572 |
self.assertFalse(P('a.py').match('b.py')) |
| 573 |
self.assertFalse(P('b/py').match('b.py')) |
| 574 |
self.assertFalse(P('/a.py').match('b.py')) |
| 575 |
self.assertFalse(P('b.py/c').match('b.py')) |
| 576 |
# Wildcard relative pattern. |
| 577 |
self.assertTrue(P('b.py').match('*.py')) |
| 578 |
self.assertTrue(P('a/b.py').match('*.py')) |
| 579 |
self.assertTrue(P('/a/b.py').match('*.py')) |
| 580 |
self.assertFalse(P('b.pyc').match('*.py')) |
| 581 |
self.assertFalse(P('b./py').match('*.py')) |
| 582 |
self.assertFalse(P('b.py/c').match('*.py')) |
| 583 |
# Multi-part relative pattern. |
| 584 |
self.assertTrue(P('ab/c.py').match('a*/*.py')) |
| 585 |
self.assertTrue(P('/d/ab/c.py').match('a*/*.py')) |
| 586 |
self.assertFalse(P('a.py').match('a*/*.py')) |
| 587 |
self.assertFalse(P('/dab/c.py').match('a*/*.py')) |
| 588 |
self.assertFalse(P('ab/c.py/d').match('a*/*.py')) |
| 589 |
# Absolute pattern. |
| 590 |
self.assertTrue(P('/b.py').match('/*.py')) |
| 591 |
self.assertFalse(P('b.py').match('/*.py')) |
| 592 |
self.assertFalse(P('a/b.py').match('/*.py')) |
| 593 |
self.assertFalse(P('/a/b.py').match('/*.py')) |
| 594 |
# Multi-part absolute pattern. |
| 595 |
self.assertTrue(P('/a/b.py').match('/a/*.py')) |
| 596 |
self.assertFalse(P('/ab.py').match('/a/*.py')) |
| 597 |
self.assertFalse(P('/a/b/c.py').match('/a/*.py')) |
| 598 |
# Multi-part glob-style pattern. |
| 599 |
self.assertFalse(P('/a/b/c.py').match('/**/*.py')) |
| 600 |
self.assertTrue(P('/a/b/c.py').match('/a/**/*.py')) |
| 601 |
# Case-sensitive flag |
| 602 |
self.assertFalse(P('A.py').match('a.PY', case_sensitive=True)) |
| 603 |
self.assertTrue(P('A.py').match('a.PY', case_sensitive=False)) |
| 604 |
self.assertFalse(P('c:/a/B.Py').match('C:/A/*.pY', case_sensitive=True)) |
| 605 |
self.assertTrue(P('/a/b/c.py').match('/A/*/*.Py', case_sensitive=False)) |
| 606 |
# Matching against empty path |
| 607 |
self.assertFalse(P('').match('*')) |
| 608 |
self.assertFalse(P('').match('**')) |
| 609 |
self.assertFalse(P('').match('**/*')) |
| 610 |
|
| 611 |
@needs_posix |
| 612 |
def test_match_posix(self): |
| 613 |
P = self.cls |
| 614 |
self.assertFalse(P('A.py').match('a.PY')) |
| 615 |
|
| 616 |
@needs_windows |
| 617 |
def test_match_windows(self): |
| 618 |
P = self.cls |
| 619 |
# Absolute patterns. |
| 620 |
self.assertTrue(P('c:/b.py').match('*:/*.py')) |
| 621 |
self.assertTrue(P('c:/b.py').match('c:/*.py')) |
| 622 |
self.assertFalse(P('d:/b.py').match('c:/*.py')) # wrong drive |
| 623 |
self.assertFalse(P('b.py').match('/*.py')) |
| 624 |
self.assertFalse(P('b.py').match('c:*.py')) |
| 625 |
self.assertFalse(P('b.py').match('c:/*.py')) |
| 626 |
self.assertFalse(P('c:b.py').match('/*.py')) |
| 627 |
self.assertFalse(P('c:b.py').match('c:/*.py')) |
| 628 |
self.assertFalse(P('/b.py').match('c:*.py')) |
| 629 |
self.assertFalse(P('/b.py').match('c:/*.py')) |
| 630 |
# UNC patterns. |
| 631 |
self.assertTrue(P('//some/share/a.py').match('//*/*/*.py')) |
| 632 |
self.assertTrue(P('//some/share/a.py').match('//some/share/*.py')) |
| 633 |
self.assertFalse(P('//other/share/a.py').match('//some/share/*.py')) |
| 634 |
self.assertFalse(P('//some/share/a/b.py').match('//some/share/*.py')) |
| 635 |
# Case-insensitivity. |
| 636 |
self.assertTrue(P('B.py').match('b.PY')) |
| 637 |
self.assertTrue(P('c:/a/B.Py').match('C:/A/*.pY')) |
| 638 |
self.assertTrue(P('//Some/Share/B.Py').match('//somE/sharE/*.pY')) |
| 639 |
# Path anchor doesn't match pattern anchor |
| 640 |
self.assertFalse(P('c:/b.py').match('/*.py')) # 'c:/' vs '/' |
| 641 |
self.assertFalse(P('c:/b.py').match('c:*.py')) # 'c:/' vs 'c:' |
| 642 |
self.assertFalse(P('//some/share/a.py').match('/*.py')) # '//some/share/' vs '/' |
| 643 |
|
| 644 |
@needs_posix |
| 645 |
def test_parse_path_posix(self): |
| 646 |
check = self._check_parse_path |
| 647 |
# Collapsing of excess leading slashes, except for the double-slash |
| 648 |
# special case. |
| 649 |
check('//a/b', '', '//', ['a', 'b']) |
| 650 |
check('///a/b', '', '/', ['a', 'b']) |
| 651 |
check('////a/b', '', '/', ['a', 'b']) |
| 652 |
# Paths which look like NT paths aren't treated specially. |
| 653 |
check('c:a', '', '', ['c:a',]) |
| 654 |
check('c:\\a', '', '', ['c:\\a',]) |
| 655 |
check('\\a', '', '', ['\\a',]) |
| 656 |
|
| 657 |
@needs_posix |
| 658 |
def test_eq_posix(self): |
| 659 |
P = self.cls |
| 660 |
self.assertNotEqual(P('a/b'), P('A/b')) |
| 661 |
self.assertEqual(P('/a'), P('///a')) |
| 662 |
self.assertNotEqual(P('/a'), P('//a')) |
| 663 |
|
| 664 |
@needs_posix |
| 665 |
def test_as_uri_posix(self): |
| 666 |
P = self.cls |
| 667 |
self.assertEqual(self.make_uri(P('/')), 'file:///') |
| 668 |
self.assertEqual(self.make_uri(P('/a/b.c')), 'file:///a/b.c') |
| 669 |
self.assertEqual(self.make_uri(P('/a/b%#c')), 'file:///a/b%25%23c') |
| 670 |
|
| 671 |
@needs_posix |
| 672 |
def test_as_uri_non_ascii(self): |
| 673 |
from urllib.parse import quote_from_bytes |
| 674 |
P = self.cls |
| 675 |
try: |
| 676 |
os.fsencode('\xe9') |
| 677 |
except UnicodeEncodeError: |
| 678 |
self.skipTest("\\xe9 cannot be encoded to the filesystem encoding") |
| 679 |
self.assertEqual(self.make_uri(P('/a/b\xe9')), |
| 680 |
'file:///a/b' + quote_from_bytes(os.fsencode('\xe9'))) |
| 681 |
|
| 682 |
@needs_posix |
| 683 |
def test_parse_windows_path(self): |
| 684 |
P = self.cls |
| 685 |
p = P('c:', 'a', 'b') |
| 686 |
pp = P(pathlib.PureWindowsPath('c:\\a\\b')) |
| 687 |
self.assertEqual(p, pp) |
| 688 |
|
| 689 |
windows_equivalences = { |
| 690 |
'./a:b': [ ('./a:b',) ], |
| 691 |
'c:a': [ ('c:', 'a'), ('c:', 'a/'), ('.', 'c:', 'a') ], |
| 692 |
'c:/a': [ |
| 693 |
('c:/', 'a'), ('c:', '/', 'a'), ('c:', '/a'), |
| 694 |
('/z', 'c:/', 'a'), ('//x/y', 'c:/', 'a'), |
| 695 |
], |
| 696 |
'//a/b/': [ ('//a/b',) ], |
| 697 |
'//a/b/c': [ |
| 698 |
('//a/b', 'c'), ('//a/b/', 'c'), |
| 699 |
], |
| 700 |
} |
| 701 |
|
| 702 |
@needs_windows |
| 703 |
def test_equivalences_windows(self): |
| 704 |
self.test_equivalences(self.windows_equivalences) |
| 705 |
|
| 706 |
@needs_windows |
| 707 |
def test_parse_path_windows(self): |
| 708 |
check = self._check_parse_path |
| 709 |
# First part is anchored. |
| 710 |
check('c:', 'c:', '', []) |
| 711 |
check('c:/', 'c:', '\\', []) |
| 712 |
check('/', '', '\\', []) |
| 713 |
check('c:a', 'c:', '', ['a']) |
| 714 |
check('c:/a', 'c:', '\\', ['a']) |
| 715 |
check('/a', '', '\\', ['a']) |
| 716 |
# UNC paths. |
| 717 |
check('//', '\\\\', '', []) |
| 718 |
check('//a', '\\\\a', '', []) |
| 719 |
check('//a/', '\\\\a\\', '', []) |
| 720 |
check('//a/b', '\\\\a\\b', '\\', []) |
| 721 |
check('//a/b/', '\\\\a\\b', '\\', []) |
| 722 |
check('//a/b/c', '\\\\a\\b', '\\', ['c']) |
| 723 |
# Collapsing and stripping excess slashes. |
| 724 |
check('Z://b//c/d/', 'Z:', '\\', ['b', 'c', 'd']) |
| 725 |
# UNC paths. |
| 726 |
check('//b/c//d', '\\\\b\\c', '\\', ['d']) |
| 727 |
# Extended paths. |
| 728 |
check('//./c:', '\\\\.\\c:', '', []) |
| 729 |
check('//?/c:/', '\\\\?\\c:', '\\', []) |
| 730 |
check('//?/c:/a', '\\\\?\\c:', '\\', ['a']) |
| 731 |
# Extended UNC paths (format is "\\?\UNC\server\share"). |
| 732 |
check('//?', '\\\\?', '', []) |
| 733 |
check('//?/', '\\\\?\\', '', []) |
| 734 |
check('//?/UNC', '\\\\?\\UNC', '', []) |
| 735 |
check('//?/UNC/', '\\\\?\\UNC\\', '', []) |
| 736 |
check('//?/UNC/b', '\\\\?\\UNC\\b', '', []) |
| 737 |
check('//?/UNC/b/', '\\\\?\\UNC\\b\\', '', []) |
| 738 |
check('//?/UNC/b/c', '\\\\?\\UNC\\b\\c', '\\', []) |
| 739 |
check('//?/UNC/b/c/', '\\\\?\\UNC\\b\\c', '\\', []) |
| 740 |
check('//?/UNC/b/c/d', '\\\\?\\UNC\\b\\c', '\\', ['d']) |
| 741 |
# UNC device paths |
| 742 |
check('//./BootPartition/', '\\\\.\\BootPartition', '\\', []) |
| 743 |
check('//?/BootPartition/', '\\\\?\\BootPartition', '\\', []) |
| 744 |
check('//./PhysicalDrive0', '\\\\.\\PhysicalDrive0', '', []) |
| 745 |
check('//?/Volume{}/', '\\\\?\\Volume{}', '\\', []) |
| 746 |
check('//./nul', '\\\\.\\nul', '', []) |
| 747 |
# Paths to files with NTFS alternate data streams |
| 748 |
check('./c:s', '', '', ['c:s']) |
| 749 |
check('cc:s', '', '', ['cc:s']) |
| 750 |
check('C:c:s', 'C:', '', ['c:s']) |
| 751 |
check('C:/c:s', 'C:', '\\', ['c:s']) |
| 752 |
check('D:a/c:b', 'D:', '', ['a', 'c:b']) |
| 753 |
check('D:/a/c:b', 'D:', '\\', ['a', 'c:b']) |
| 754 |
|
| 755 |
@needs_windows |
| 756 |
def test_eq_windows(self): |
| 757 |
P = self.cls |
| 758 |
self.assertEqual(P('c:a/b'), P('c:a/b')) |
| 759 |
self.assertEqual(P('c:a/b'), P('c:', 'a', 'b')) |
| 760 |
self.assertNotEqual(P('c:a/b'), P('d:a/b')) |
| 761 |
self.assertNotEqual(P('c:a/b'), P('c:/a/b')) |
| 762 |
self.assertNotEqual(P('/a/b'), P('c:/a/b')) |
| 763 |
# Case-insensitivity. |
| 764 |
self.assertEqual(P('a/B'), P('A/b')) |
| 765 |
self.assertEqual(P('C:a/B'), P('c:A/b')) |
| 766 |
self.assertEqual(P('//Some/SHARE/a/B'), P('//somE/share/A/b')) |
| 767 |
self.assertEqual(P('\u0130'), P('i\u0307')) |
| 768 |
|
| 769 |
@needs_windows |
| 770 |
def test_as_uri_windows(self): |
| 771 |
P = self.cls |
| 772 |
with self.assertRaises(ValueError): |
| 773 |
self.make_uri(P('/a/b')) |
| 774 |
with self.assertRaises(ValueError): |
| 775 |
self.make_uri(P('c:a/b')) |
| 776 |
self.assertEqual(self.make_uri(P('c:/')), 'file:///c:/') |
| 777 |
self.assertEqual(self.make_uri(P('c:/a/b.c')), 'file:///c:/a/b.c') |
| 778 |
self.assertEqual(self.make_uri(P('c:/a/b%#c')), 'file:///c:/a/b%25%23c') |
| 779 |
self.assertEqual(self.make_uri(P('//some/share/')), 'file://some/share/') |
| 780 |
self.assertEqual(self.make_uri(P('//some/share/a/b.c')), |
| 781 |
'file://some/share/a/b.c') |
| 782 |
|
| 783 |
from urllib.parse import quote_from_bytes |
| 784 |
QUOTED_FS_NONASCII = quote_from_bytes(os.fsencode(FS_NONASCII)) |
| 785 |
self.assertEqual(self.make_uri(P('c:/a/b' + FS_NONASCII)), |
| 786 |
'file:///c:/a/b' + QUOTED_FS_NONASCII) |
| 787 |
self.assertEqual(self.make_uri(P('//some/share/a/b%#c' + FS_NONASCII)), |
| 788 |
'file://some/share/a/b%25%23c' + QUOTED_FS_NONASCII) |
| 789 |
|
| 790 |
@needs_windows |
| 791 |
def test_ordering_windows(self): |
| 792 |
# Case-insensitivity. |
| 793 |
def assertOrderedEqual(a, b): |
| 794 |
self.assertLessEqual(a, b) |
| 795 |
self.assertGreaterEqual(b, a) |
| 796 |
P = self.cls |
| 797 |
p = P('c:A/b') |
| 798 |
q = P('C:a/B') |
| 799 |
assertOrderedEqual(p, q) |
| 800 |
self.assertFalse(p < q) |
| 801 |
self.assertFalse(p > q) |
| 802 |
p = P('//some/Share/A/b') |
| 803 |
q = P('//Some/SHARE/a/B') |
| 804 |
assertOrderedEqual(p, q) |
| 805 |
self.assertFalse(p < q) |
| 806 |
self.assertFalse(p > q) |
| 807 |
|
| 808 |
@needs_posix |
| 809 |
def test_is_absolute_posix(self): |
| 810 |
P = self.cls |
| 811 |
self.assertFalse(P('').is_absolute()) |
| 812 |
self.assertFalse(P('a').is_absolute()) |
| 813 |
self.assertFalse(P('a/b/').is_absolute()) |
| 814 |
self.assertTrue(P('/').is_absolute()) |
| 815 |
self.assertTrue(P('/a').is_absolute()) |
| 816 |
self.assertTrue(P('/a/b/').is_absolute()) |
| 817 |
self.assertTrue(P('//a').is_absolute()) |
| 818 |
self.assertTrue(P('//a/b').is_absolute()) |
| 819 |
|
| 820 |
@needs_windows |
| 821 |
def test_is_absolute_windows(self): |
| 822 |
P = self.cls |
| 823 |
# Under NT, only paths with both a drive and a root are absolute. |
| 824 |
self.assertFalse(P().is_absolute()) |
| 825 |
self.assertFalse(P('a').is_absolute()) |
| 826 |
self.assertFalse(P('a/b/').is_absolute()) |
| 827 |
self.assertFalse(P('/').is_absolute()) |
| 828 |
self.assertFalse(P('/a').is_absolute()) |
| 829 |
self.assertFalse(P('/a/b/').is_absolute()) |
| 830 |
self.assertFalse(P('c:').is_absolute()) |
| 831 |
self.assertFalse(P('c:a').is_absolute()) |
| 832 |
self.assertFalse(P('c:a/b/').is_absolute()) |
| 833 |
self.assertTrue(P('c:/').is_absolute()) |
| 834 |
self.assertTrue(P('c:/a').is_absolute()) |
| 835 |
self.assertTrue(P('c:/a/b/').is_absolute()) |
| 836 |
# UNC paths are absolute by definition. |
| 837 |
self.assertTrue(P('//').is_absolute()) |
| 838 |
self.assertTrue(P('//a').is_absolute()) |
| 839 |
self.assertTrue(P('//a/b').is_absolute()) |
| 840 |
self.assertTrue(P('//a/b/').is_absolute()) |
| 841 |
self.assertTrue(P('//a/b/c').is_absolute()) |
| 842 |
self.assertTrue(P('//a/b/c/d').is_absolute()) |
| 843 |
self.assertTrue(P('//?/UNC/').is_absolute()) |
| 844 |
self.assertTrue(P('//?/UNC/spam').is_absolute()) |
| 845 |
|
| 846 |
def test_relative_to_common(self): |
| 847 |
P = self.cls |
| 848 |
p = P('a/b') |
| 849 |
self.assertRaises(TypeError, p.relative_to) |
| 850 |
self.assertRaises(TypeError, p.relative_to, b'a') |
| 851 |
self.assertEqual(p.relative_to(P('')), P('a/b')) |
| 852 |
self.assertEqual(p.relative_to(''), P('a/b')) |
| 853 |
self.assertEqual(p.relative_to(P('a')), P('b')) |
| 854 |
self.assertEqual(p.relative_to('a'), P('b')) |
| 855 |
self.assertEqual(p.relative_to('a/'), P('b')) |
| 856 |
self.assertEqual(p.relative_to(P('a/b')), P('')) |
| 857 |
self.assertEqual(p.relative_to('a/b'), P('')) |
| 858 |
self.assertEqual(p.relative_to(P(''), walk_up=True), P('a/b')) |
| 859 |
self.assertEqual(p.relative_to('', walk_up=True), P('a/b')) |
| 860 |
self.assertEqual(p.relative_to(P('a'), walk_up=True), P('b')) |
| 861 |
self.assertEqual(p.relative_to('a', walk_up=True), P('b')) |
| 862 |
self.assertEqual(p.relative_to('a/', walk_up=True), P('b')) |
| 863 |
self.assertEqual(p.relative_to(P('a/b'), walk_up=True), P('')) |
| 864 |
self.assertEqual(p.relative_to('a/b', walk_up=True), P('')) |
| 865 |
self.assertEqual(p.relative_to(P('a/c'), walk_up=True), P('../b')) |
| 866 |
self.assertEqual(p.relative_to('a/c', walk_up=True), P('../b')) |
| 867 |
self.assertEqual(p.relative_to(P('a/b/c'), walk_up=True), P('..')) |
| 868 |
self.assertEqual(p.relative_to('a/b/c', walk_up=True), P('..')) |
| 869 |
self.assertEqual(p.relative_to(P('c'), walk_up=True), P('../a/b')) |
| 870 |
self.assertEqual(p.relative_to('c', walk_up=True), P('../a/b')) |
| 871 |
# Unrelated paths. |
| 872 |
self.assertRaises(ValueError, p.relative_to, P('c')) |
| 873 |
self.assertRaises(ValueError, p.relative_to, P('a/b/c')) |
| 874 |
self.assertRaises(ValueError, p.relative_to, P('a/c')) |
| 875 |
self.assertRaises(ValueError, p.relative_to, P('/a')) |
| 876 |
self.assertRaises(ValueError, p.relative_to, P("../a")) |
| 877 |
self.assertRaises(ValueError, p.relative_to, P("a/..")) |
| 878 |
self.assertRaises(ValueError, p.relative_to, P("/a/..")) |
| 879 |
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True) |
| 880 |
self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True) |
| 881 |
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True) |
| 882 |
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True) |
| 883 |
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True) |
| 884 |
p = P('/a/b') |
| 885 |
self.assertEqual(p.relative_to(P('/')), P('a/b')) |
| 886 |
self.assertEqual(p.relative_to('/'), P('a/b')) |
| 887 |
self.assertEqual(p.relative_to(P('/a')), P('b')) |
| 888 |
self.assertEqual(p.relative_to('/a'), P('b')) |
| 889 |
self.assertEqual(p.relative_to('/a/'), P('b')) |
| 890 |
self.assertEqual(p.relative_to(P('/a/b')), P('')) |
| 891 |
self.assertEqual(p.relative_to('/a/b'), P('')) |
| 892 |
self.assertEqual(p.relative_to(P('/'), walk_up=True), P('a/b')) |
| 893 |
self.assertEqual(p.relative_to('/', walk_up=True), P('a/b')) |
| 894 |
self.assertEqual(p.relative_to(P('/a'), walk_up=True), P('b')) |
| 895 |
self.assertEqual(p.relative_to('/a', walk_up=True), P('b')) |
| 896 |
self.assertEqual(p.relative_to('/a/', walk_up=True), P('b')) |
| 897 |
self.assertEqual(p.relative_to(P('/a/b'), walk_up=True), P('')) |
| 898 |
self.assertEqual(p.relative_to('/a/b', walk_up=True), P('')) |
| 899 |
self.assertEqual(p.relative_to(P('/a/c'), walk_up=True), P('../b')) |
| 900 |
self.assertEqual(p.relative_to('/a/c', walk_up=True), P('../b')) |
| 901 |
self.assertEqual(p.relative_to(P('/a/b/c'), walk_up=True), P('..')) |
| 902 |
self.assertEqual(p.relative_to('/a/b/c', walk_up=True), P('..')) |
| 903 |
self.assertEqual(p.relative_to(P('/c'), walk_up=True), P('../a/b')) |
| 904 |
self.assertEqual(p.relative_to('/c', walk_up=True), P('../a/b')) |
| 905 |
# Unrelated paths. |
| 906 |
self.assertRaises(ValueError, p.relative_to, P('/c')) |
| 907 |
self.assertRaises(ValueError, p.relative_to, P('/a/b/c')) |
| 908 |
self.assertRaises(ValueError, p.relative_to, P('/a/c')) |
| 909 |
self.assertRaises(ValueError, p.relative_to, P('')) |
| 910 |
self.assertRaises(ValueError, p.relative_to, '') |
| 911 |
self.assertRaises(ValueError, p.relative_to, P('a')) |
| 912 |
self.assertRaises(ValueError, p.relative_to, P("../a")) |
| 913 |
self.assertRaises(ValueError, p.relative_to, P("a/..")) |
| 914 |
self.assertRaises(ValueError, p.relative_to, P("/a/..")) |
| 915 |
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True) |
| 916 |
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True) |
| 917 |
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True) |
| 918 |
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True) |
| 919 |
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True) |
| 920 |
|
| 921 |
@needs_windows |
| 922 |
def test_relative_to_windows(self): |
| 923 |
P = self.cls |
| 924 |
p = P('C:Foo/Bar') |
| 925 |
self.assertEqual(p.relative_to(P('c:')), P('Foo/Bar')) |
| 926 |
self.assertEqual(p.relative_to('c:'), P('Foo/Bar')) |
| 927 |
self.assertEqual(p.relative_to(P('c:foO')), P('Bar')) |
| 928 |
self.assertEqual(p.relative_to('c:foO'), P('Bar')) |
| 929 |
self.assertEqual(p.relative_to('c:foO/'), P('Bar')) |
| 930 |
self.assertEqual(p.relative_to(P('c:foO/baR')), P()) |
| 931 |
self.assertEqual(p.relative_to('c:foO/baR'), P()) |
| 932 |
self.assertEqual(p.relative_to(P('c:'), walk_up=True), P('Foo/Bar')) |
| 933 |
self.assertEqual(p.relative_to('c:', walk_up=True), P('Foo/Bar')) |
| 934 |
self.assertEqual(p.relative_to(P('c:foO'), walk_up=True), P('Bar')) |
| 935 |
self.assertEqual(p.relative_to('c:foO', walk_up=True), P('Bar')) |
| 936 |
self.assertEqual(p.relative_to('c:foO/', walk_up=True), P('Bar')) |
| 937 |
self.assertEqual(p.relative_to(P('c:foO/baR'), walk_up=True), P()) |
| 938 |
self.assertEqual(p.relative_to('c:foO/baR', walk_up=True), P()) |
| 939 |
self.assertEqual(p.relative_to(P('C:Foo/Bar/Baz'), walk_up=True), P('..')) |
| 940 |
self.assertEqual(p.relative_to(P('C:Foo/Baz'), walk_up=True), P('../Bar')) |
| 941 |
self.assertEqual(p.relative_to(P('C:Baz/Bar'), walk_up=True), P('../../Foo/Bar')) |
| 942 |
# Unrelated paths. |
| 943 |
self.assertRaises(ValueError, p.relative_to, P()) |
| 944 |
self.assertRaises(ValueError, p.relative_to, '') |
| 945 |
self.assertRaises(ValueError, p.relative_to, P('d:')) |
| 946 |
self.assertRaises(ValueError, p.relative_to, P('/')) |
| 947 |
self.assertRaises(ValueError, p.relative_to, P('Foo')) |
| 948 |
self.assertRaises(ValueError, p.relative_to, P('/Foo')) |
| 949 |
self.assertRaises(ValueError, p.relative_to, P('C:/Foo')) |
| 950 |
self.assertRaises(ValueError, p.relative_to, P('C:Foo/Bar/Baz')) |
| 951 |
self.assertRaises(ValueError, p.relative_to, P('C:Foo/Baz')) |
| 952 |
self.assertRaises(ValueError, p.relative_to, P(), walk_up=True) |
| 953 |
self.assertRaises(ValueError, p.relative_to, '', walk_up=True) |
| 954 |
self.assertRaises(ValueError, p.relative_to, P('d:'), walk_up=True) |
| 955 |
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True) |
| 956 |
self.assertRaises(ValueError, p.relative_to, P('Foo'), walk_up=True) |
| 957 |
self.assertRaises(ValueError, p.relative_to, P('/Foo'), walk_up=True) |
| 958 |
self.assertRaises(ValueError, p.relative_to, P('C:/Foo'), walk_up=True) |
| 959 |
p = P('C:/Foo/Bar') |
| 960 |
self.assertEqual(p.relative_to(P('c:/')), P('Foo/Bar')) |
| 961 |
self.assertEqual(p.relative_to('c:/'), P('Foo/Bar')) |
| 962 |
self.assertEqual(p.relative_to(P('c:/foO')), P('Bar')) |
| 963 |
self.assertEqual(p.relative_to('c:/foO'), P('Bar')) |
| 964 |
self.assertEqual(p.relative_to('c:/foO/'), P('Bar')) |
| 965 |
self.assertEqual(p.relative_to(P('c:/foO/baR')), P()) |
| 966 |
self.assertEqual(p.relative_to('c:/foO/baR'), P()) |
| 967 |
self.assertEqual(p.relative_to(P('c:/'), walk_up=True), P('Foo/Bar')) |
| 968 |
self.assertEqual(p.relative_to('c:/', walk_up=True), P('Foo/Bar')) |
| 969 |
self.assertEqual(p.relative_to(P('c:/foO'), walk_up=True), P('Bar')) |
| 970 |
self.assertEqual(p.relative_to('c:/foO', walk_up=True), P('Bar')) |
| 971 |
self.assertEqual(p.relative_to('c:/foO/', walk_up=True), P('Bar')) |
| 972 |
self.assertEqual(p.relative_to(P('c:/foO/baR'), walk_up=True), P()) |
| 973 |
self.assertEqual(p.relative_to('c:/foO/baR', walk_up=True), P()) |
| 974 |
self.assertEqual(p.relative_to('C:/Baz', walk_up=True), P('../Foo/Bar')) |
| 975 |
self.assertEqual(p.relative_to('C:/Foo/Bar/Baz', walk_up=True), P('..')) |
| 976 |
self.assertEqual(p.relative_to('C:/Foo/Baz', walk_up=True), P('../Bar')) |
| 977 |
# Unrelated paths. |
| 978 |
self.assertRaises(ValueError, p.relative_to, 'c:') |
| 979 |
self.assertRaises(ValueError, p.relative_to, P('c:')) |
| 980 |
self.assertRaises(ValueError, p.relative_to, P('C:/Baz')) |
| 981 |
self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Bar/Baz')) |
| 982 |
self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Baz')) |
| 983 |
self.assertRaises(ValueError, p.relative_to, P('C:Foo')) |
| 984 |
self.assertRaises(ValueError, p.relative_to, P('d:')) |
| 985 |
self.assertRaises(ValueError, p.relative_to, P('d:/')) |
| 986 |
self.assertRaises(ValueError, p.relative_to, P('/')) |
| 987 |
self.assertRaises(ValueError, p.relative_to, P('/Foo')) |
| 988 |
self.assertRaises(ValueError, p.relative_to, P('//C/Foo')) |
| 989 |
self.assertRaises(ValueError, p.relative_to, 'c:', walk_up=True) |
| 990 |
self.assertRaises(ValueError, p.relative_to, P('c:'), walk_up=True) |
| 991 |
self.assertRaises(ValueError, p.relative_to, P('C:Foo'), walk_up=True) |
| 992 |
self.assertRaises(ValueError, p.relative_to, P('d:'), walk_up=True) |
| 993 |
self.assertRaises(ValueError, p.relative_to, P('d:/'), walk_up=True) |
| 994 |
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True) |
| 995 |
self.assertRaises(ValueError, p.relative_to, P('/Foo'), walk_up=True) |
| 996 |
self.assertRaises(ValueError, p.relative_to, P('//C/Foo'), walk_up=True) |
| 997 |
# UNC paths. |
| 998 |
p = P('//Server/Share/Foo/Bar') |
| 999 |
self.assertEqual(p.relative_to(P('//sErver/sHare')), P('Foo/Bar')) |
| 1000 |
self.assertEqual(p.relative_to('//sErver/sHare'), P('Foo/Bar')) |
| 1001 |
self.assertEqual(p.relative_to('//sErver/sHare/'), P('Foo/Bar')) |
| 1002 |
self.assertEqual(p.relative_to(P('//sErver/sHare/Foo')), P('Bar')) |
| 1003 |
self.assertEqual(p.relative_to('//sErver/sHare/Foo'), P('Bar')) |
| 1004 |
self.assertEqual(p.relative_to('//sErver/sHare/Foo/'), P('Bar')) |
| 1005 |
self.assertEqual(p.relative_to(P('//sErver/sHare/Foo/Bar')), P()) |
| 1006 |
self.assertEqual(p.relative_to('//sErver/sHare/Foo/Bar'), P()) |
| 1007 |
self.assertEqual(p.relative_to(P('//sErver/sHare'), walk_up=True), P('Foo/Bar')) |
| 1008 |
self.assertEqual(p.relative_to('//sErver/sHare', walk_up=True), P('Foo/Bar')) |
| 1009 |
self.assertEqual(p.relative_to('//sErver/sHare/', walk_up=True), P('Foo/Bar')) |
| 1010 |
self.assertEqual(p.relative_to(P('//sErver/sHare/Foo'), walk_up=True), P('Bar')) |
| 1011 |
self.assertEqual(p.relative_to('//sErver/sHare/Foo', walk_up=True), P('Bar')) |
| 1012 |
self.assertEqual(p.relative_to('//sErver/sHare/Foo/', walk_up=True), P('Bar')) |
| 1013 |
self.assertEqual(p.relative_to(P('//sErver/sHare/Foo/Bar'), walk_up=True), P()) |
| 1014 |
self.assertEqual(p.relative_to('//sErver/sHare/Foo/Bar', walk_up=True), P()) |
| 1015 |
self.assertEqual(p.relative_to(P('//sErver/sHare/bar'), walk_up=True), P('../Foo/Bar')) |
| 1016 |
self.assertEqual(p.relative_to('//sErver/sHare/bar', walk_up=True), P('../Foo/Bar')) |
| 1017 |
# Unrelated paths. |
| 1018 |
self.assertRaises(ValueError, p.relative_to, P('/Server/Share/Foo')) |
| 1019 |
self.assertRaises(ValueError, p.relative_to, P('c:/Server/Share/Foo')) |
| 1020 |
self.assertRaises(ValueError, p.relative_to, P('//z/Share/Foo')) |
| 1021 |
self.assertRaises(ValueError, p.relative_to, P('//Server/z/Foo')) |
| 1022 |
self.assertRaises(ValueError, p.relative_to, P('/Server/Share/Foo'), walk_up=True) |
| 1023 |
self.assertRaises(ValueError, p.relative_to, P('c:/Server/Share/Foo'), walk_up=True) |
| 1024 |
self.assertRaises(ValueError, p.relative_to, P('//z/Share/Foo'), walk_up=True) |
| 1025 |
self.assertRaises(ValueError, p.relative_to, P('//Server/z/Foo'), walk_up=True) |
| 1026 |
|
| 1027 |
def test_is_relative_to_common(self): |
| 1028 |
P = self.cls |
| 1029 |
p = P('a/b') |
| 1030 |
self.assertRaises(TypeError, p.is_relative_to) |
| 1031 |
self.assertRaises(TypeError, p.is_relative_to, b'a') |
| 1032 |
self.assertTrue(p.is_relative_to(P(''))) |
| 1033 |
self.assertTrue(p.is_relative_to('')) |
| 1034 |
self.assertTrue(p.is_relative_to(P('a'))) |
| 1035 |
self.assertTrue(p.is_relative_to('a/')) |
| 1036 |
self.assertTrue(p.is_relative_to(P('a/b'))) |
| 1037 |
self.assertTrue(p.is_relative_to('a/b')) |
| 1038 |
# Unrelated paths. |
| 1039 |
self.assertFalse(p.is_relative_to(P('c'))) |
| 1040 |
self.assertFalse(p.is_relative_to(P('a/b/c'))) |
| 1041 |
self.assertFalse(p.is_relative_to(P('a/c'))) |
| 1042 |
self.assertFalse(p.is_relative_to(P('/a'))) |
| 1043 |
p = P('/a/b') |
| 1044 |
self.assertTrue(p.is_relative_to(P('/'))) |
| 1045 |
self.assertTrue(p.is_relative_to('/')) |
| 1046 |
self.assertTrue(p.is_relative_to(P('/a'))) |
| 1047 |
self.assertTrue(p.is_relative_to('/a')) |
| 1048 |
self.assertTrue(p.is_relative_to('/a/')) |
| 1049 |
self.assertTrue(p.is_relative_to(P('/a/b'))) |
| 1050 |
self.assertTrue(p.is_relative_to('/a/b')) |
| 1051 |
# Unrelated paths. |
| 1052 |
self.assertFalse(p.is_relative_to(P('/c'))) |
| 1053 |
self.assertFalse(p.is_relative_to(P('/a/b/c'))) |
| 1054 |
self.assertFalse(p.is_relative_to(P('/a/c'))) |
| 1055 |
self.assertFalse(p.is_relative_to(P(''))) |
| 1056 |
self.assertFalse(p.is_relative_to('')) |
| 1057 |
self.assertFalse(p.is_relative_to(P('a'))) |
| 1058 |
|
| 1059 |
@needs_windows |
| 1060 |
def test_is_relative_to_windows(self): |
| 1061 |
P = self.cls |
| 1062 |
p = P('C:Foo/Bar') |
| 1063 |
self.assertTrue(p.is_relative_to(P('c:'))) |
| 1064 |
self.assertTrue(p.is_relative_to('c:')) |
| 1065 |
self.assertTrue(p.is_relative_to(P('c:foO'))) |
| 1066 |
self.assertTrue(p.is_relative_to('c:foO')) |
| 1067 |
self.assertTrue(p.is_relative_to('c:foO/')) |
| 1068 |
self.assertTrue(p.is_relative_to(P('c:foO/baR'))) |
| 1069 |
self.assertTrue(p.is_relative_to('c:foO/baR')) |
| 1070 |
# Unrelated paths. |
| 1071 |
self.assertFalse(p.is_relative_to(P())) |
| 1072 |
self.assertFalse(p.is_relative_to('')) |
| 1073 |
self.assertFalse(p.is_relative_to(P('d:'))) |
| 1074 |
self.assertFalse(p.is_relative_to(P('/'))) |
| 1075 |
self.assertFalse(p.is_relative_to(P('Foo'))) |
| 1076 |
self.assertFalse(p.is_relative_to(P('/Foo'))) |
| 1077 |
self.assertFalse(p.is_relative_to(P('C:/Foo'))) |
| 1078 |
self.assertFalse(p.is_relative_to(P('C:Foo/Bar/Baz'))) |
| 1079 |
self.assertFalse(p.is_relative_to(P('C:Foo/Baz'))) |
| 1080 |
p = P('C:/Foo/Bar') |
| 1081 |
self.assertTrue(p.is_relative_to(P('c:/'))) |
| 1082 |
self.assertTrue(p.is_relative_to(P('c:/foO'))) |
| 1083 |
self.assertTrue(p.is_relative_to('c:/foO/')) |
| 1084 |
self.assertTrue(p.is_relative_to(P('c:/foO/baR'))) |
| 1085 |
self.assertTrue(p.is_relative_to('c:/foO/baR')) |
| 1086 |
# Unrelated paths. |
| 1087 |
self.assertFalse(p.is_relative_to('c:')) |
| 1088 |
self.assertFalse(p.is_relative_to(P('C:/Baz'))) |
| 1089 |
self.assertFalse(p.is_relative_to(P('C:/Foo/Bar/Baz'))) |
| 1090 |
self.assertFalse(p.is_relative_to(P('C:/Foo/Baz'))) |
| 1091 |
self.assertFalse(p.is_relative_to(P('C:Foo'))) |
| 1092 |
self.assertFalse(p.is_relative_to(P('d:'))) |
| 1093 |
self.assertFalse(p.is_relative_to(P('d:/'))) |
| 1094 |
self.assertFalse(p.is_relative_to(P('/'))) |
| 1095 |
self.assertFalse(p.is_relative_to(P('/Foo'))) |
| 1096 |
self.assertFalse(p.is_relative_to(P('//C/Foo'))) |
| 1097 |
# UNC paths. |
| 1098 |
p = P('//Server/Share/Foo/Bar') |
| 1099 |
self.assertTrue(p.is_relative_to(P('//sErver/sHare'))) |
| 1100 |
self.assertTrue(p.is_relative_to('//sErver/sHare')) |
| 1101 |
self.assertTrue(p.is_relative_to('//sErver/sHare/')) |
| 1102 |
self.assertTrue(p.is_relative_to(P('//sErver/sHare/Foo'))) |
| 1103 |
self.assertTrue(p.is_relative_to('//sErver/sHare/Foo')) |
| 1104 |
self.assertTrue(p.is_relative_to('//sErver/sHare/Foo/')) |
| 1105 |
self.assertTrue(p.is_relative_to(P('//sErver/sHare/Foo/Bar'))) |
| 1106 |
self.assertTrue(p.is_relative_to('//sErver/sHare/Foo/Bar')) |
| 1107 |
# Unrelated paths. |
| 1108 |
self.assertFalse(p.is_relative_to(P('/Server/Share/Foo'))) |
| 1109 |
self.assertFalse(p.is_relative_to(P('c:/Server/Share/Foo'))) |
| 1110 |
self.assertFalse(p.is_relative_to(P('//z/Share/Foo'))) |
| 1111 |
self.assertFalse(p.is_relative_to(P('//Server/z/Foo'))) |
| 1112 |
|
| 1113 |
|
| 1114 |
class PurePosixPathTest(PurePathTest): |
| 1115 |
cls = pathlib.PurePosixPath |
| 1116 |
|
| 1117 |
|
| 1118 |
class PureWindowsPathTest(PurePathTest): |
| 1119 |
cls = pathlib.PureWindowsPath |
| 1120 |
|
| 1121 |
|
| 1122 |
class PurePathSubclassTest(PurePathTest): |
| 1123 |
class cls(pathlib.PurePath): |
| 1124 |
pass |
| 1125 |
|
| 1126 |
# repr() roundtripping is not supported in custom subclass. |
| 1127 |
test_repr_roundtrips = None |
| 1128 |
|
| 1129 |
|
| 1130 |
# |
| 1131 |
# Tests for the concrete classes. |
| 1132 |
# |
| 1133 |
|
| 1134 |
class PathTest(PurePathTest): |
| 1135 |
"""Tests for the FS-accessing functionalities of the Path classes.""" |
| 1136 |
cls = pathlib.Path |
| 1137 |
can_symlink = os_helper.can_symlink() |
| 1138 |
|
| 1139 |
def setUp(self): |
| 1140 |
name = self.id().split('.')[-1] |
| 1141 |
if name in _tests_needing_symlinks and not self.can_symlink: |
| 1142 |
self.skipTest('requires symlinks') |
| 1143 |
super().setUp() |
| 1144 |
os.mkdir(self.base) |
| 1145 |
os.mkdir(os.path.join(self.base, 'dirA')) |
| 1146 |
os.mkdir(os.path.join(self.base, 'dirB')) |
| 1147 |
os.mkdir(os.path.join(self.base, 'dirC')) |
| 1148 |
os.mkdir(os.path.join(self.base, 'dirC', 'dirD')) |
| 1149 |
os.mkdir(os.path.join(self.base, 'dirE')) |
| 1150 |
with open(os.path.join(self.base, 'fileA'), 'wb') as f: |
| 1151 |
f.write(b"this is file A\n") |
| 1152 |
with open(os.path.join(self.base, 'dirB', 'fileB'), 'wb') as f: |
| 1153 |
f.write(b"this is file B\n") |
| 1154 |
with open(os.path.join(self.base, 'dirC', 'fileC'), 'wb') as f: |
| 1155 |
f.write(b"this is file C\n") |
| 1156 |
with open(os.path.join(self.base, 'dirC', 'novel.txt'), 'wb') as f: |
| 1157 |
f.write(b"this is a novel\n") |
| 1158 |
with open(os.path.join(self.base, 'dirC', 'dirD', 'fileD'), 'wb') as f: |
| 1159 |
f.write(b"this is file D\n") |
| 1160 |
os.chmod(os.path.join(self.base, 'dirE'), 0) |
| 1161 |
if self.can_symlink: |
| 1162 |
# Relative symlinks. |
| 1163 |
os.symlink('fileA', os.path.join(self.base, 'linkA')) |
| 1164 |
os.symlink('non-existing', os.path.join(self.base, 'brokenLink')) |
| 1165 |
os.symlink('dirB', |
| 1166 |
os.path.join(self.base, 'linkB'), |
| 1167 |
target_is_directory=True) |
| 1168 |
os.symlink(os.path.join('..', 'dirB'), |
| 1169 |
os.path.join(self.base, 'dirA', 'linkC'), |
| 1170 |
target_is_directory=True) |
| 1171 |
# This one goes upwards, creating a loop. |
| 1172 |
os.symlink(os.path.join('..', 'dirB'), |
| 1173 |
os.path.join(self.base, 'dirB', 'linkD'), |
| 1174 |
target_is_directory=True) |
| 1175 |
# Broken symlink (pointing to itself). |
| 1176 |
os.symlink('brokenLinkLoop', os.path.join(self.base, 'brokenLinkLoop')) |
| 1177 |
|
| 1178 |
def tearDown(self): |
| 1179 |
os.chmod(os.path.join(self.base, 'dirE'), 0o777) |
| 1180 |
os_helper.rmtree(self.base) |
| 1181 |
|
| 1182 |
def assertFileNotFound(self, func, *args, **kwargs): |
| 1183 |
with self.assertRaises(FileNotFoundError) as cm: |
| 1184 |
func(*args, **kwargs) |
| 1185 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 1186 |
|
| 1187 |
def assertEqualNormCase(self, path_a, path_b): |
| 1188 |
normcase = self.parser.normcase |
| 1189 |
self.assertEqual(normcase(path_a), normcase(path_b)) |
| 1190 |
|
| 1191 |
def tempdir(self): |
| 1192 |
d = os_helper._longpath(tempfile.mkdtemp(suffix='-dirD', |
| 1193 |
dir=os.getcwd())) |
| 1194 |
self.addCleanup(os_helper.rmtree, d) |
| 1195 |
return d |
| 1196 |
|
| 1197 |
def test_matches_writablepath_docstrings(self): |
| 1198 |
path_names = {name for name in dir(pathlib.types._WritablePath) if name[0] != '_'} |
| 1199 |
for attr_name in path_names: |
| 1200 |
if attr_name == 'parser': |
| 1201 |
# On Windows, Path.parser is ntpath, but WritablePath.parser is |
| 1202 |
# posixpath, and so their docstrings differ. |
| 1203 |
continue |
| 1204 |
our_attr = getattr(self.cls, attr_name) |
| 1205 |
path_attr = getattr(pathlib.types._WritablePath, attr_name) |
| 1206 |
self.assertEqual(our_attr.__doc__, path_attr.__doc__) |
| 1207 |
|
| 1208 |
def test_concrete_class(self): |
| 1209 |
if self.cls is pathlib.Path: |
| 1210 |
expected = pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath |
| 1211 |
else: |
| 1212 |
expected = self.cls |
| 1213 |
p = self.cls('a') |
| 1214 |
self.assertIs(type(p), expected) |
| 1215 |
|
| 1216 |
def test_unsupported_parser(self): |
| 1217 |
if self.cls.parser is os.path: |
| 1218 |
self.skipTest("path parser is supported") |
| 1219 |
else: |
| 1220 |
self.assertRaises(pathlib.UnsupportedOperation, self.cls) |
| 1221 |
|
| 1222 |
def _test_cwd(self, p): |
| 1223 |
q = self.cls(os.getcwd()) |
| 1224 |
self.assertEqual(p, q) |
| 1225 |
self.assertEqualNormCase(str(p), str(q)) |
| 1226 |
self.assertIs(type(p), type(q)) |
| 1227 |
self.assertTrue(p.is_absolute()) |
| 1228 |
|
| 1229 |
def test_cwd(self): |
| 1230 |
p = self.cls.cwd() |
| 1231 |
self._test_cwd(p) |
| 1232 |
|
| 1233 |
def test_absolute_common(self): |
| 1234 |
P = self.cls |
| 1235 |
|
| 1236 |
with mock.patch("os.getcwd") as getcwd: |
| 1237 |
getcwd.return_value = self.base |
| 1238 |
|
| 1239 |
# Simple relative paths. |
| 1240 |
self.assertEqual(str(P().absolute()), self.base) |
| 1241 |
self.assertEqual(str(P('.').absolute()), self.base) |
| 1242 |
self.assertEqual(str(P('a').absolute()), os.path.join(self.base, 'a')) |
| 1243 |
self.assertEqual(str(P('a', 'b', 'c').absolute()), os.path.join(self.base, 'a', 'b', 'c')) |
| 1244 |
|
| 1245 |
# Symlinks should not be resolved. |
| 1246 |
self.assertEqual(str(P('linkB', 'fileB').absolute()), os.path.join(self.base, 'linkB', 'fileB')) |
| 1247 |
self.assertEqual(str(P('brokenLink').absolute()), os.path.join(self.base, 'brokenLink')) |
| 1248 |
self.assertEqual(str(P('brokenLinkLoop').absolute()), os.path.join(self.base, 'brokenLinkLoop')) |
| 1249 |
|
| 1250 |
# '..' entries should be preserved and not normalised. |
| 1251 |
self.assertEqual(str(P('..').absolute()), os.path.join(self.base, '..')) |
| 1252 |
self.assertEqual(str(P('a', '..').absolute()), os.path.join(self.base, 'a', '..')) |
| 1253 |
self.assertEqual(str(P('..', 'b').absolute()), os.path.join(self.base, '..', 'b')) |
| 1254 |
|
| 1255 |
def _test_home(self, p): |
| 1256 |
q = self.cls(os.path.expanduser('~')) |
| 1257 |
self.assertEqual(p, q) |
| 1258 |
self.assertEqualNormCase(str(p), str(q)) |
| 1259 |
self.assertIs(type(p), type(q)) |
| 1260 |
self.assertTrue(p.is_absolute()) |
| 1261 |
|
| 1262 |
@unittest.skipIf( |
| 1263 |
pwd is None, reason="Test requires pwd module to get homedir." |
| 1264 |
) |
| 1265 |
def test_home(self): |
| 1266 |
with os_helper.EnvironmentVarGuard() as env: |
| 1267 |
self._test_home(self.cls.home()) |
| 1268 |
|
| 1269 |
env.clear() |
| 1270 |
env['USERPROFILE'] = os.path.join(self.base, 'userprofile') |
| 1271 |
self._test_home(self.cls.home()) |
| 1272 |
|
| 1273 |
# bpo-38883: ignore `HOME` when set on windows |
| 1274 |
env['HOME'] = os.path.join(self.base, 'home') |
| 1275 |
self._test_home(self.cls.home()) |
| 1276 |
|
| 1277 |
@unittest.skipIf(is_wasi, "WASI has no user accounts.") |
| 1278 |
def test_expanduser_common(self): |
| 1279 |
P = self.cls |
| 1280 |
p = P('~') |
| 1281 |
self.assertEqual(p.expanduser(), P(os.path.expanduser('~'))) |
| 1282 |
p = P('foo') |
| 1283 |
self.assertEqual(p.expanduser(), p) |
| 1284 |
p = P('/~') |
| 1285 |
self.assertEqual(p.expanduser(), p) |
| 1286 |
p = P('../~') |
| 1287 |
self.assertEqual(p.expanduser(), p) |
| 1288 |
p = P(P('').absolute().anchor) / '~' |
| 1289 |
self.assertEqual(p.expanduser(), p) |
| 1290 |
p = P('~/a:b') |
| 1291 |
self.assertEqual(p.expanduser(), P(os.path.expanduser('~'), './a:b')) |
| 1292 |
|
| 1293 |
def test_with_segments(self): |
| 1294 |
class P(self.cls): |
| 1295 |
def __init__(self, *pathsegments, session_id): |
| 1296 |
super().__init__(*pathsegments) |
| 1297 |
self.session_id = session_id |
| 1298 |
|
| 1299 |
def with_segments(self, *pathsegments): |
| 1300 |
return type(self)(*pathsegments, session_id=self.session_id) |
| 1301 |
p = P(self.base, session_id=42) |
| 1302 |
self.assertEqual(42, p.absolute().session_id) |
| 1303 |
self.assertEqual(42, p.resolve().session_id) |
| 1304 |
if not is_wasi: # WASI has no user accounts. |
| 1305 |
self.assertEqual(42, p.with_segments('~').expanduser().session_id) |
| 1306 |
self.assertEqual(42, (p / 'fileA').rename(p / 'fileB').session_id) |
| 1307 |
self.assertEqual(42, (p / 'fileB').replace(p / 'fileA').session_id) |
| 1308 |
if self.can_symlink: |
| 1309 |
self.assertEqual(42, (p / 'linkA').readlink().session_id) |
| 1310 |
for path in p.iterdir(): |
| 1311 |
self.assertEqual(42, path.session_id) |
| 1312 |
for path in p.glob('*'): |
| 1313 |
self.assertEqual(42, path.session_id) |
| 1314 |
for path in p.rglob('*'): |
| 1315 |
self.assertEqual(42, path.session_id) |
| 1316 |
for dirpath, dirnames, filenames in p.walk(): |
| 1317 |
self.assertEqual(42, dirpath.session_id) |
| 1318 |
|
| 1319 |
def test_open_common(self): |
| 1320 |
p = self.cls(self.base) |
| 1321 |
with (p / 'fileA').open('r') as f: |
| 1322 |
self.assertIsInstance(f, io.TextIOBase) |
| 1323 |
self.assertEqual(f.read(), "this is file A\n") |
| 1324 |
with (p / 'fileA').open('rb') as f: |
| 1325 |
self.assertIsInstance(f, io.BufferedIOBase) |
| 1326 |
self.assertEqual(f.read().strip(), b"this is file A") |
| 1327 |
|
| 1328 |
def test_open_unbuffered(self): |
| 1329 |
p = self.cls(self.base) |
| 1330 |
with (p / 'fileA').open('rb', buffering=0) as f: |
| 1331 |
self.assertIsInstance(f, io.RawIOBase) |
| 1332 |
self.assertEqual(f.read().strip(), b"this is file A") |
| 1333 |
|
| 1334 |
def test_copy_file_preserve_metadata(self): |
| 1335 |
base = self.cls(self.base) |
| 1336 |
source = base / 'fileA' |
| 1337 |
if hasattr(os, 'chmod'): |
| 1338 |
os.chmod(source, stat.S_IRWXU | stat.S_IRWXO) |
| 1339 |
if hasattr(os, 'chflags') and hasattr(stat, 'UF_NODUMP'): |
| 1340 |
os.chflags(source, stat.UF_NODUMP) |
| 1341 |
source_st = source.stat() |
| 1342 |
target = base / 'copyA' |
| 1343 |
source.copy(target, preserve_metadata=True) |
| 1344 |
self.assertTrue(target.exists()) |
| 1345 |
self.assertEqual(source.read_text(), target.read_text()) |
| 1346 |
target_st = target.stat() |
| 1347 |
self.assertLessEqual(source_st.st_atime, target_st.st_atime) |
| 1348 |
self.assertLessEqual(source_st.st_mtime, target_st.st_mtime) |
| 1349 |
self.assertEqual(source_st.st_mode, target_st.st_mode) |
| 1350 |
if hasattr(source_st, 'st_flags'): |
| 1351 |
self.assertEqual(source_st.st_flags, target_st.st_flags) |
| 1352 |
|
| 1353 |
@needs_symlinks |
| 1354 |
def test_copy_file_to_existing_symlink(self): |
| 1355 |
base = self.cls(self.base) |
| 1356 |
source = base / 'dirB' / 'fileB' |
| 1357 |
target = base / 'linkA' |
| 1358 |
real_target = base / 'fileA' |
| 1359 |
result = source.copy(target) |
| 1360 |
self.assertEqual(result, target) |
| 1361 |
self.assertTrue(target.exists()) |
| 1362 |
self.assertTrue(target.is_symlink()) |
| 1363 |
self.assertTrue(real_target.exists()) |
| 1364 |
self.assertFalse(real_target.is_symlink()) |
| 1365 |
self.assertEqual(source.read_text(), real_target.read_text()) |
| 1366 |
|
| 1367 |
@needs_symlinks |
| 1368 |
def test_copy_file_to_existing_symlink_follow_symlinks_false(self): |
| 1369 |
base = self.cls(self.base) |
| 1370 |
source = base / 'dirB' / 'fileB' |
| 1371 |
target = base / 'linkA' |
| 1372 |
real_target = base / 'fileA' |
| 1373 |
result = source.copy(target, follow_symlinks=False) |
| 1374 |
self.assertEqual(result, target) |
| 1375 |
self.assertTrue(target.exists()) |
| 1376 |
self.assertTrue(target.is_symlink()) |
| 1377 |
self.assertTrue(real_target.exists()) |
| 1378 |
self.assertFalse(real_target.is_symlink()) |
| 1379 |
self.assertEqual(source.read_text(), real_target.read_text()) |
| 1380 |
|
| 1381 |
@os_helper.skip_unless_xattr |
| 1382 |
def test_copy_file_preserve_metadata_xattrs(self): |
| 1383 |
base = self.cls(self.base) |
| 1384 |
source = base / 'fileA' |
| 1385 |
os.setxattr(source, b'user.foo', b'42') |
| 1386 |
target = base / 'copyA' |
| 1387 |
source.copy(target, preserve_metadata=True) |
| 1388 |
self.assertEqual(os.getxattr(target, b'user.foo'), b'42') |
| 1389 |
|
| 1390 |
@needs_symlinks |
| 1391 |
def test_copy_symlink_follow_symlinks_true(self): |
| 1392 |
base = self.cls(self.base) |
| 1393 |
source = base / 'linkA' |
| 1394 |
target = base / 'copyA' |
| 1395 |
result = source.copy(target) |
| 1396 |
self.assertEqual(result, target) |
| 1397 |
self.assertTrue(target.exists()) |
| 1398 |
self.assertFalse(target.is_symlink()) |
| 1399 |
self.assertEqual(source.read_text(), target.read_text()) |
| 1400 |
|
| 1401 |
@needs_symlinks |
| 1402 |
def test_copy_symlink_follow_symlinks_false(self): |
| 1403 |
base = self.cls(self.base) |
| 1404 |
source = base / 'linkA' |
| 1405 |
target = base / 'copyA' |
| 1406 |
result = source.copy(target, follow_symlinks=False) |
| 1407 |
self.assertEqual(result, target) |
| 1408 |
self.assertTrue(target.exists()) |
| 1409 |
self.assertTrue(target.is_symlink()) |
| 1410 |
self.assertEqual(source.readlink(), target.readlink()) |
| 1411 |
|
| 1412 |
@needs_symlinks |
| 1413 |
def test_copy_symlink_to_itself(self): |
| 1414 |
base = self.cls(self.base) |
| 1415 |
source = base / 'linkA' |
| 1416 |
self.assertRaises(OSError, source.copy, source) |
| 1417 |
|
| 1418 |
@needs_symlinks |
| 1419 |
def test_copy_symlink_to_existing_symlink(self): |
| 1420 |
base = self.cls(self.base) |
| 1421 |
source = base / 'copySource' |
| 1422 |
target = base / 'copyTarget' |
| 1423 |
source.symlink_to(base / 'fileA') |
| 1424 |
target.symlink_to(base / 'dirC') |
| 1425 |
self.assertRaises(OSError, source.copy, target) |
| 1426 |
self.assertRaises(OSError, source.copy, target, follow_symlinks=False) |
| 1427 |
|
| 1428 |
@needs_symlinks |
| 1429 |
def test_copy_symlink_to_existing_directory_symlink(self): |
| 1430 |
base = self.cls(self.base) |
| 1431 |
source = base / 'copySource' |
| 1432 |
target = base / 'copyTarget' |
| 1433 |
source.symlink_to(base / 'fileA') |
| 1434 |
target.symlink_to(base / 'dirC') |
| 1435 |
self.assertRaises(OSError, source.copy, target) |
| 1436 |
self.assertRaises(OSError, source.copy, target, follow_symlinks=False) |
| 1437 |
|
| 1438 |
@needs_symlinks |
| 1439 |
def test_copy_directory_symlink_follow_symlinks_false(self): |
| 1440 |
base = self.cls(self.base) |
| 1441 |
source = base / 'linkB' |
| 1442 |
target = base / 'copyA' |
| 1443 |
result = source.copy(target, follow_symlinks=False) |
| 1444 |
self.assertEqual(result, target) |
| 1445 |
self.assertTrue(target.exists()) |
| 1446 |
self.assertTrue(target.is_symlink()) |
| 1447 |
self.assertEqual(source.readlink(), target.readlink()) |
| 1448 |
|
| 1449 |
@needs_symlinks |
| 1450 |
def test_copy_directory_symlink_to_itself(self): |
| 1451 |
base = self.cls(self.base) |
| 1452 |
source = base / 'linkB' |
| 1453 |
self.assertRaises(OSError, source.copy, source) |
| 1454 |
self.assertRaises(OSError, source.copy, source, follow_symlinks=False) |
| 1455 |
|
| 1456 |
@needs_symlinks |
| 1457 |
def test_copy_directory_symlink_into_itself(self): |
| 1458 |
base = self.cls(self.base) |
| 1459 |
source = base / 'linkB' |
| 1460 |
target = base / 'linkB' / 'copyB' |
| 1461 |
self.assertRaises(OSError, source.copy, target) |
| 1462 |
self.assertRaises(OSError, source.copy, target, follow_symlinks=False) |
| 1463 |
self.assertFalse(target.exists()) |
| 1464 |
|
| 1465 |
@needs_symlinks |
| 1466 |
def test_copy_directory_symlink_to_existing_symlink(self): |
| 1467 |
base = self.cls(self.base) |
| 1468 |
source = base / 'copySource' |
| 1469 |
target = base / 'copyTarget' |
| 1470 |
source.symlink_to(base / 'dirC') |
| 1471 |
target.symlink_to(base / 'fileA') |
| 1472 |
self.assertRaises(FileExistsError, source.copy, target) |
| 1473 |
self.assertRaises(FileExistsError, source.copy, target, follow_symlinks=False) |
| 1474 |
|
| 1475 |
@needs_symlinks |
| 1476 |
def test_copy_directory_symlink_to_existing_directory_symlink(self): |
| 1477 |
base = self.cls(self.base) |
| 1478 |
source = base / 'copySource' |
| 1479 |
target = base / 'copyTarget' |
| 1480 |
source.symlink_to(base / 'dirC' / 'dirD') |
| 1481 |
target.symlink_to(base / 'dirC') |
| 1482 |
self.assertRaises(FileExistsError, source.copy, target) |
| 1483 |
self.assertRaises(FileExistsError, source.copy, target, follow_symlinks=False) |
| 1484 |
|
| 1485 |
@needs_symlinks |
| 1486 |
def test_copy_dangling_symlink(self): |
| 1487 |
base = self.cls(self.base) |
| 1488 |
source = base / 'source' |
| 1489 |
target = base / 'target' |
| 1490 |
|
| 1491 |
source.mkdir() |
| 1492 |
source.joinpath('link').symlink_to('nonexistent') |
| 1493 |
|
| 1494 |
self.assertRaises(FileNotFoundError, source.copy, target) |
| 1495 |
|
| 1496 |
target2 = base / 'target2' |
| 1497 |
result = source.copy(target2, follow_symlinks=False) |
| 1498 |
self.assertEqual(result, target2) |
| 1499 |
self.assertTrue(target2.joinpath('link').is_symlink()) |
| 1500 |
self.assertEqual(target2.joinpath('link').readlink(), self.cls('nonexistent')) |
| 1501 |
|
| 1502 |
@needs_symlinks |
| 1503 |
def test_copy_link_preserve_metadata(self): |
| 1504 |
base = self.cls(self.base) |
| 1505 |
source = base / 'linkA' |
| 1506 |
if hasattr(os, 'lchmod'): |
| 1507 |
os.lchmod(source, stat.S_IRWXU | stat.S_IRWXO) |
| 1508 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 1509 |
os.lchflags(source, stat.UF_NODUMP) |
| 1510 |
source_st = source.lstat() |
| 1511 |
target = base / 'copyA' |
| 1512 |
source.copy(target, follow_symlinks=False, preserve_metadata=True) |
| 1513 |
self.assertTrue(target.exists()) |
| 1514 |
self.assertTrue(target.is_symlink()) |
| 1515 |
self.assertEqual(source.readlink(), target.readlink()) |
| 1516 |
target_st = target.lstat() |
| 1517 |
self.assertLessEqual(source_st.st_atime, target_st.st_atime) |
| 1518 |
self.assertLessEqual(source_st.st_mtime, target_st.st_mtime) |
| 1519 |
self.assertEqual(source_st.st_mode, target_st.st_mode) |
| 1520 |
if hasattr(source_st, 'st_flags'): |
| 1521 |
self.assertEqual(source_st.st_flags, target_st.st_flags) |
| 1522 |
|
| 1523 |
def test_copy_error_handling(self): |
| 1524 |
def make_raiser(err): |
| 1525 |
def raiser(*args, **kwargs): |
| 1526 |
raise OSError(err, os.strerror(err)) |
| 1527 |
return raiser |
| 1528 |
|
| 1529 |
base = self.cls(self.base) |
| 1530 |
source = base / 'fileA' |
| 1531 |
target = base / 'copyA' |
| 1532 |
|
| 1533 |
# Raise non-fatal OSError from all available fast copy functions. |
| 1534 |
with contextlib.ExitStack() as ctx: |
| 1535 |
if fcntl and hasattr(fcntl, 'FICLONE'): |
| 1536 |
ctx.enter_context(mock.patch('fcntl.ioctl', make_raiser(errno.EXDEV))) |
| 1537 |
if posix and hasattr(posix, '_fcopyfile'): |
| 1538 |
ctx.enter_context(mock.patch('posix._fcopyfile', make_raiser(errno.ENOTSUP))) |
| 1539 |
if hasattr(os, 'copy_file_range'): |
| 1540 |
ctx.enter_context(mock.patch('os.copy_file_range', make_raiser(errno.EXDEV))) |
| 1541 |
if hasattr(os, 'sendfile'): |
| 1542 |
ctx.enter_context(mock.patch('os.sendfile', make_raiser(errno.ENOTSOCK))) |
| 1543 |
|
| 1544 |
source.copy(target) |
| 1545 |
self.assertTrue(target.exists()) |
| 1546 |
self.assertEqual(source.read_text(), target.read_text()) |
| 1547 |
|
| 1548 |
# Raise fatal OSError from first available fast copy function. |
| 1549 |
if fcntl and hasattr(fcntl, 'FICLONE'): |
| 1550 |
patchpoint = 'fcntl.ioctl' |
| 1551 |
elif posix and hasattr(posix, '_fcopyfile'): |
| 1552 |
patchpoint = 'posix._fcopyfile' |
| 1553 |
elif hasattr(os, 'copy_file_range'): |
| 1554 |
patchpoint = 'os.copy_file_range' |
| 1555 |
elif hasattr(os, 'sendfile'): |
| 1556 |
patchpoint = 'os.sendfile' |
| 1557 |
else: |
| 1558 |
return |
| 1559 |
with mock.patch(patchpoint, make_raiser(errno.ENOENT)): |
| 1560 |
self.assertRaises(FileNotFoundError, source.copy, target) |
| 1561 |
|
| 1562 |
@unittest.skipIf(sys.platform == "win32" or sys.platform == "wasi", "directories are always readable on Windows and WASI") |
| 1563 |
@unittest.skipIf(root_in_posix, "test fails with root privilege") |
| 1564 |
def test_copy_dir_no_read_permission(self): |
| 1565 |
base = self.cls(self.base) |
| 1566 |
source = base / 'dirE' |
| 1567 |
target = base / 'copyE' |
| 1568 |
self.assertRaises(PermissionError, source.copy, target) |
| 1569 |
self.assertFalse(target.exists()) |
| 1570 |
|
| 1571 |
def test_copy_dir_preserve_metadata(self): |
| 1572 |
base = self.cls(self.base) |
| 1573 |
source = base / 'dirC' |
| 1574 |
if hasattr(os, 'chmod'): |
| 1575 |
os.chmod(source / 'dirD', stat.S_IRWXU | stat.S_IRWXO) |
| 1576 |
if hasattr(os, 'chflags') and hasattr(stat, 'UF_NODUMP'): |
| 1577 |
os.chflags(source / 'fileC', stat.UF_NODUMP) |
| 1578 |
target = base / 'copyA' |
| 1579 |
|
| 1580 |
subpaths = ['.', 'fileC', 'dirD', 'dirD/fileD'] |
| 1581 |
source_sts = [source.joinpath(subpath).stat() for subpath in subpaths] |
| 1582 |
source.copy(target, preserve_metadata=True) |
| 1583 |
target_sts = [target.joinpath(subpath).stat() for subpath in subpaths] |
| 1584 |
|
| 1585 |
for source_st, target_st in zip(source_sts, target_sts): |
| 1586 |
self.assertLessEqual(source_st.st_atime, target_st.st_atime) |
| 1587 |
self.assertLessEqual(source_st.st_mtime, target_st.st_mtime) |
| 1588 |
self.assertEqual(source_st.st_mode, target_st.st_mode) |
| 1589 |
if hasattr(source_st, 'st_flags'): |
| 1590 |
self.assertEqual(source_st.st_flags, target_st.st_flags) |
| 1591 |
|
| 1592 |
@os_helper.skip_unless_xattr |
| 1593 |
def test_copy_dir_preserve_metadata_xattrs(self): |
| 1594 |
base = self.cls(self.base) |
| 1595 |
source = base / 'dirC' |
| 1596 |
source_file = source.joinpath('dirD', 'fileD') |
| 1597 |
os.setxattr(source_file, b'user.foo', b'42') |
| 1598 |
target = base / 'copyA' |
| 1599 |
source.copy(target, preserve_metadata=True) |
| 1600 |
target_file = target.joinpath('dirD', 'fileD') |
| 1601 |
self.assertEqual(os.getxattr(target_file, b'user.foo'), b'42') |
| 1602 |
|
| 1603 |
@needs_symlinks |
| 1604 |
def test_move_file_symlink(self): |
| 1605 |
base = self.cls(self.base) |
| 1606 |
source = base / 'linkA' |
| 1607 |
source_readlink = source.readlink() |
| 1608 |
target = base / 'linkA_moved' |
| 1609 |
result = source.move(target) |
| 1610 |
self.assertEqual(result, target) |
| 1611 |
self.assertFalse(source.exists()) |
| 1612 |
self.assertTrue(target.is_symlink()) |
| 1613 |
self.assertEqual(source_readlink, target.readlink()) |
| 1614 |
|
| 1615 |
@needs_symlinks |
| 1616 |
def test_move_file_symlink_to_itself(self): |
| 1617 |
base = self.cls(self.base) |
| 1618 |
source = base / 'linkA' |
| 1619 |
self.assertRaises(OSError, source.move, source) |
| 1620 |
|
| 1621 |
@needs_symlinks |
| 1622 |
def test_move_dir_symlink(self): |
| 1623 |
base = self.cls(self.base) |
| 1624 |
source = base / 'linkB' |
| 1625 |
source_readlink = source.readlink() |
| 1626 |
target = base / 'linkB_moved' |
| 1627 |
result = source.move(target) |
| 1628 |
self.assertEqual(result, target) |
| 1629 |
self.assertFalse(source.exists()) |
| 1630 |
self.assertTrue(target.is_symlink()) |
| 1631 |
self.assertEqual(source_readlink, target.readlink()) |
| 1632 |
|
| 1633 |
@needs_symlinks |
| 1634 |
def test_move_dir_symlink_to_itself(self): |
| 1635 |
base = self.cls(self.base) |
| 1636 |
source = base / 'linkB' |
| 1637 |
self.assertRaises(OSError, source.move, source) |
| 1638 |
|
| 1639 |
@needs_symlinks |
| 1640 |
def test_move_dangling_symlink(self): |
| 1641 |
base = self.cls(self.base) |
| 1642 |
source = base / 'brokenLink' |
| 1643 |
source_readlink = source.readlink() |
| 1644 |
target = base / 'brokenLink_moved' |
| 1645 |
result = source.move(target) |
| 1646 |
self.assertEqual(result, target) |
| 1647 |
self.assertFalse(source.exists()) |
| 1648 |
self.assertTrue(target.is_symlink()) |
| 1649 |
self.assertEqual(source_readlink, target.readlink()) |
| 1650 |
|
| 1651 |
def test_move_file(self): |
| 1652 |
base = self.cls(self.base) |
| 1653 |
source = base / 'fileA' |
| 1654 |
source_text = source.read_text() |
| 1655 |
target = base / 'fileA_moved' |
| 1656 |
result = source.move(target) |
| 1657 |
self.assertEqual(result, target) |
| 1658 |
self.assertFalse(source.exists()) |
| 1659 |
self.assertTrue(target.exists()) |
| 1660 |
self.assertEqual(source_text, target.read_text()) |
| 1661 |
|
| 1662 |
@patch_replace |
| 1663 |
def test_move_file_other_fs(self): |
| 1664 |
self.test_move_file() |
| 1665 |
|
| 1666 |
def test_move_file_to_file(self): |
| 1667 |
base = self.cls(self.base) |
| 1668 |
source = base / 'fileA' |
| 1669 |
source_text = source.read_text() |
| 1670 |
target = base / 'dirB' / 'fileB' |
| 1671 |
result = source.move(target) |
| 1672 |
self.assertEqual(result, target) |
| 1673 |
self.assertFalse(source.exists()) |
| 1674 |
self.assertTrue(target.exists()) |
| 1675 |
self.assertEqual(source_text, target.read_text()) |
| 1676 |
|
| 1677 |
@patch_replace |
| 1678 |
def test_move_file_to_file_other_fs(self): |
| 1679 |
self.test_move_file_to_file() |
| 1680 |
|
| 1681 |
def test_move_file_to_dir(self): |
| 1682 |
base = self.cls(self.base) |
| 1683 |
source = base / 'fileA' |
| 1684 |
target = base / 'dirB' |
| 1685 |
self.assertRaises(OSError, source.move, target) |
| 1686 |
|
| 1687 |
@patch_replace |
| 1688 |
def test_move_file_to_dir_other_fs(self): |
| 1689 |
self.test_move_file_to_dir() |
| 1690 |
|
| 1691 |
def test_move_file_to_itself(self): |
| 1692 |
base = self.cls(self.base) |
| 1693 |
source = base / 'fileA' |
| 1694 |
self.assertRaises(OSError, source.move, source) |
| 1695 |
|
| 1696 |
def test_move_dir(self): |
| 1697 |
base = self.cls(self.base) |
| 1698 |
source = base / 'dirC' |
| 1699 |
target = base / 'dirC_moved' |
| 1700 |
result = source.move(target) |
| 1701 |
self.assertEqual(result, target) |
| 1702 |
self.assertFalse(source.exists()) |
| 1703 |
self.assertTrue(target.is_dir()) |
| 1704 |
self.assertTrue(target.joinpath('dirD').is_dir()) |
| 1705 |
self.assertTrue(target.joinpath('dirD', 'fileD').is_file()) |
| 1706 |
self.assertEqual(target.joinpath('dirD', 'fileD').read_text(), |
| 1707 |
"this is file D\n") |
| 1708 |
self.assertTrue(target.joinpath('fileC').is_file()) |
| 1709 |
self.assertTrue(target.joinpath('fileC').read_text(), |
| 1710 |
"this is file C\n") |
| 1711 |
|
| 1712 |
@patch_replace |
| 1713 |
def test_move_dir_other_fs(self): |
| 1714 |
self.test_move_dir() |
| 1715 |
|
| 1716 |
def test_move_dir_to_dir(self): |
| 1717 |
base = self.cls(self.base) |
| 1718 |
source = base / 'dirC' |
| 1719 |
target = base / 'dirB' |
| 1720 |
self.assertRaises(OSError, source.move, target) |
| 1721 |
self.assertTrue(source.exists()) |
| 1722 |
self.assertTrue(target.exists()) |
| 1723 |
|
| 1724 |
@patch_replace |
| 1725 |
def test_move_dir_to_dir_other_fs(self): |
| 1726 |
self.test_move_dir_to_dir() |
| 1727 |
|
| 1728 |
def test_move_dir_to_itself(self): |
| 1729 |
base = self.cls(self.base) |
| 1730 |
source = base / 'dirC' |
| 1731 |
self.assertRaises(OSError, source.move, source) |
| 1732 |
self.assertTrue(source.exists()) |
| 1733 |
|
| 1734 |
def test_move_dir_into_itself(self): |
| 1735 |
base = self.cls(self.base) |
| 1736 |
source = base / 'dirC' |
| 1737 |
target = base / 'dirC' / 'bar' |
| 1738 |
self.assertRaises(OSError, source.move, target) |
| 1739 |
self.assertTrue(source.exists()) |
| 1740 |
self.assertFalse(target.exists()) |
| 1741 |
|
| 1742 |
@patch_replace |
| 1743 |
def test_move_dir_into_itself_other_fs(self): |
| 1744 |
self.test_move_dir_into_itself() |
| 1745 |
|
| 1746 |
@patch_replace |
| 1747 |
@needs_symlinks |
| 1748 |
def test_move_file_symlink_other_fs(self): |
| 1749 |
self.test_move_file_symlink() |
| 1750 |
|
| 1751 |
@patch_replace |
| 1752 |
@needs_symlinks |
| 1753 |
def test_move_file_symlink_to_itself_other_fs(self): |
| 1754 |
self.test_move_file_symlink_to_itself() |
| 1755 |
|
| 1756 |
@patch_replace |
| 1757 |
@needs_symlinks |
| 1758 |
def test_move_dir_symlink_other_fs(self): |
| 1759 |
self.test_move_dir_symlink() |
| 1760 |
|
| 1761 |
@patch_replace |
| 1762 |
@needs_symlinks |
| 1763 |
def test_move_dir_symlink_to_itself_other_fs(self): |
| 1764 |
self.test_move_dir_symlink_to_itself() |
| 1765 |
|
| 1766 |
@patch_replace |
| 1767 |
@needs_symlinks |
| 1768 |
def test_move_dangling_symlink_other_fs(self): |
| 1769 |
self.test_move_dangling_symlink() |
| 1770 |
|
| 1771 |
def test_move_into(self): |
| 1772 |
base = self.cls(self.base) |
| 1773 |
source = base / 'fileA' |
| 1774 |
source_text = source.read_text() |
| 1775 |
target_dir = base / 'dirA' |
| 1776 |
result = source.move_into(target_dir) |
| 1777 |
self.assertEqual(result, target_dir / 'fileA') |
| 1778 |
self.assertFalse(source.exists()) |
| 1779 |
self.assertTrue(result.exists()) |
| 1780 |
self.assertEqual(source_text, result.read_text()) |
| 1781 |
|
| 1782 |
@patch_replace |
| 1783 |
def test_move_into_other_os(self): |
| 1784 |
self.test_move_into() |
| 1785 |
|
| 1786 |
def test_move_into_empty_name(self): |
| 1787 |
source = self.cls('') |
| 1788 |
target_dir = self.base |
| 1789 |
self.assertRaises(ValueError, source.move_into, target_dir) |
| 1790 |
|
| 1791 |
@patch_replace |
| 1792 |
def test_move_into_empty_name_other_os(self): |
| 1793 |
self.test_move_into_empty_name() |
| 1794 |
|
| 1795 |
@needs_symlinks |
| 1796 |
def test_complex_symlinks_absolute(self): |
| 1797 |
self._check_complex_symlinks(self.base) |
| 1798 |
|
| 1799 |
@needs_symlinks |
| 1800 |
def test_complex_symlinks_relative(self): |
| 1801 |
self._check_complex_symlinks('.') |
| 1802 |
|
| 1803 |
@needs_symlinks |
| 1804 |
def test_complex_symlinks_relative_dot_dot(self): |
| 1805 |
self._check_complex_symlinks(self.parser.join('dirA', '..')) |
| 1806 |
|
| 1807 |
def _check_complex_symlinks(self, link0_target): |
| 1808 |
# Test solving a non-looping chain of symlinks (issue #19887). |
| 1809 |
parser = self.parser |
| 1810 |
P = self.cls(self.base) |
| 1811 |
P.joinpath('link1').symlink_to(parser.join('link0', 'link0'), target_is_directory=True) |
| 1812 |
P.joinpath('link2').symlink_to(parser.join('link1', 'link1'), target_is_directory=True) |
| 1813 |
P.joinpath('link3').symlink_to(parser.join('link2', 'link2'), target_is_directory=True) |
| 1814 |
P.joinpath('link0').symlink_to(link0_target, target_is_directory=True) |
| 1815 |
|
| 1816 |
# Resolve absolute paths. |
| 1817 |
p = (P / 'link0').resolve() |
| 1818 |
self.assertEqual(p, P) |
| 1819 |
self.assertEqualNormCase(str(p), self.base) |
| 1820 |
p = (P / 'link1').resolve() |
| 1821 |
self.assertEqual(p, P) |
| 1822 |
self.assertEqualNormCase(str(p), self.base) |
| 1823 |
p = (P / 'link2').resolve() |
| 1824 |
self.assertEqual(p, P) |
| 1825 |
self.assertEqualNormCase(str(p), self.base) |
| 1826 |
p = (P / 'link3').resolve() |
| 1827 |
self.assertEqual(p, P) |
| 1828 |
self.assertEqualNormCase(str(p), self.base) |
| 1829 |
|
| 1830 |
# Resolve relative paths. |
| 1831 |
old_path = os.getcwd() |
| 1832 |
os.chdir(self.base) |
| 1833 |
try: |
| 1834 |
p = self.cls('link0').resolve() |
| 1835 |
self.assertEqual(p, P) |
| 1836 |
self.assertEqualNormCase(str(p), self.base) |
| 1837 |
p = self.cls('link1').resolve() |
| 1838 |
self.assertEqual(p, P) |
| 1839 |
self.assertEqualNormCase(str(p), self.base) |
| 1840 |
p = self.cls('link2').resolve() |
| 1841 |
self.assertEqual(p, P) |
| 1842 |
self.assertEqualNormCase(str(p), self.base) |
| 1843 |
p = self.cls('link3').resolve() |
| 1844 |
self.assertEqual(p, P) |
| 1845 |
self.assertEqualNormCase(str(p), self.base) |
| 1846 |
finally: |
| 1847 |
os.chdir(old_path) |
| 1848 |
|
| 1849 |
def _check_resolve(self, p, expected, strict=True): |
| 1850 |
q = p.resolve(strict) |
| 1851 |
self.assertEqual(q, expected) |
| 1852 |
|
| 1853 |
# This can be used to check both relative and absolute resolutions. |
| 1854 |
_check_resolve_relative = _check_resolve_absolute = _check_resolve |
| 1855 |
|
| 1856 |
@needs_symlinks |
| 1857 |
def test_resolve_common(self): |
| 1858 |
P = self.cls |
| 1859 |
p = P(self.base, 'foo') |
| 1860 |
with self.assertRaises(OSError) as cm: |
| 1861 |
p.resolve(strict=True) |
| 1862 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 1863 |
# Non-strict |
| 1864 |
parser = self.parser |
| 1865 |
self.assertEqualNormCase(str(p.resolve(strict=False)), |
| 1866 |
parser.join(self.base, 'foo')) |
| 1867 |
p = P(self.base, 'foo', 'in', 'spam') |
| 1868 |
self.assertEqualNormCase(str(p.resolve(strict=False)), |
| 1869 |
parser.join(self.base, 'foo', 'in', 'spam')) |
| 1870 |
p = P(self.base, '..', 'foo', 'in', 'spam') |
| 1871 |
self.assertEqualNormCase(str(p.resolve(strict=False)), |
| 1872 |
parser.join(parser.dirname(self.base), 'foo', 'in', 'spam')) |
| 1873 |
# These are all relative symlinks. |
| 1874 |
p = P(self.base, 'dirB', 'fileB') |
| 1875 |
self._check_resolve_relative(p, p) |
| 1876 |
p = P(self.base, 'linkA') |
| 1877 |
self._check_resolve_relative(p, P(self.base, 'fileA')) |
| 1878 |
p = P(self.base, 'dirA', 'linkC', 'fileB') |
| 1879 |
self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB')) |
| 1880 |
p = P(self.base, 'dirB', 'linkD', 'fileB') |
| 1881 |
self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB')) |
| 1882 |
# Non-strict |
| 1883 |
p = P(self.base, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam') |
| 1884 |
self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB', 'foo', 'in', |
| 1885 |
'spam'), False) |
| 1886 |
p = P(self.base, 'dirA', 'linkC', '..', 'foo', 'in', 'spam') |
| 1887 |
if self.cls.parser is not posixpath: |
| 1888 |
# In Windows, if linkY points to dirB, 'dirA\linkY\..' |
| 1889 |
# resolves to 'dirA' without resolving linkY first. |
| 1890 |
self._check_resolve_relative(p, P(self.base, 'dirA', 'foo', 'in', |
| 1891 |
'spam'), False) |
| 1892 |
else: |
| 1893 |
# In Posix, if linkY points to dirB, 'dirA/linkY/..' |
| 1894 |
# resolves to 'dirB/..' first before resolving to parent of dirB. |
| 1895 |
self._check_resolve_relative(p, P(self.base, 'foo', 'in', 'spam'), False) |
| 1896 |
# Now create absolute symlinks. |
| 1897 |
d = self.tempdir() |
| 1898 |
P(self.base, 'dirA', 'linkX').symlink_to(d) |
| 1899 |
P(self.base, str(d), 'linkY').symlink_to(self.parser.join(self.base, 'dirB')) |
| 1900 |
p = P(self.base, 'dirA', 'linkX', 'linkY', 'fileB') |
| 1901 |
self._check_resolve_absolute(p, P(self.base, 'dirB', 'fileB')) |
| 1902 |
# Non-strict |
| 1903 |
p = P(self.base, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam') |
| 1904 |
self._check_resolve_relative(p, P(self.base, 'dirB', 'foo', 'in', 'spam'), |
| 1905 |
False) |
| 1906 |
p = P(self.base, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam') |
| 1907 |
if self.cls.parser is not posixpath: |
| 1908 |
# In Windows, if linkY points to dirB, 'dirA\linkY\..' |
| 1909 |
# resolves to 'dirA' without resolving linkY first. |
| 1910 |
self._check_resolve_relative(p, P(d, 'foo', 'in', 'spam'), False) |
| 1911 |
else: |
| 1912 |
# In Posix, if linkY points to dirB, 'dirA/linkY/..' |
| 1913 |
# resolves to 'dirB/..' first before resolving to parent of dirB. |
| 1914 |
self._check_resolve_relative(p, P(self.base, 'foo', 'in', 'spam'), False) |
| 1915 |
|
| 1916 |
@needs_symlinks |
| 1917 |
def test_resolve_dot(self): |
| 1918 |
# See http://web.archive.org/web/20200623062557/https://bitbucket.org/pitrou/pathlib/issues/9/ |
| 1919 |
parser = self.parser |
| 1920 |
p = self.cls(self.base) |
| 1921 |
p.joinpath('0').symlink_to('.', target_is_directory=True) |
| 1922 |
p.joinpath('1').symlink_to(parser.join('0', '0'), target_is_directory=True) |
| 1923 |
p.joinpath('2').symlink_to(parser.join('1', '1'), target_is_directory=True) |
| 1924 |
q = p / '2' |
| 1925 |
self.assertEqual(q.resolve(strict=True), p) |
| 1926 |
r = q / '3' / '4' |
| 1927 |
self.assertRaises(FileNotFoundError, r.resolve, strict=True) |
| 1928 |
# Non-strict |
| 1929 |
self.assertEqual(r.resolve(strict=False), p / '3' / '4') |
| 1930 |
|
| 1931 |
def _check_symlink_loop(self, *args): |
| 1932 |
path = self.cls(*args) |
| 1933 |
with self.assertRaises(OSError) as cm: |
| 1934 |
path.resolve(strict=True) |
| 1935 |
self.assertEqual(cm.exception.errno, errno.ELOOP) |
| 1936 |
|
| 1937 |
@needs_posix |
| 1938 |
@needs_symlinks |
| 1939 |
def test_resolve_loop(self): |
| 1940 |
# Loops with relative symlinks. |
| 1941 |
self.cls(self.base, 'linkX').symlink_to('linkX/inside') |
| 1942 |
self._check_symlink_loop(self.base, 'linkX') |
| 1943 |
self.cls(self.base, 'linkY').symlink_to('linkY') |
| 1944 |
self._check_symlink_loop(self.base, 'linkY') |
| 1945 |
self.cls(self.base, 'linkZ').symlink_to('linkZ/../linkZ') |
| 1946 |
self._check_symlink_loop(self.base, 'linkZ') |
| 1947 |
# Non-strict |
| 1948 |
p = self.cls(self.base, 'linkZ', 'foo') |
| 1949 |
self.assertEqual(p.resolve(strict=False), p) |
| 1950 |
# Loops with absolute symlinks. |
| 1951 |
self.cls(self.base, 'linkU').symlink_to(self.parser.join(self.base, 'linkU/inside')) |
| 1952 |
self._check_symlink_loop(self.base, 'linkU') |
| 1953 |
self.cls(self.base, 'linkV').symlink_to(self.parser.join(self.base, 'linkV')) |
| 1954 |
self._check_symlink_loop(self.base, 'linkV') |
| 1955 |
self.cls(self.base, 'linkW').symlink_to(self.parser.join(self.base, 'linkW/../linkW')) |
| 1956 |
self._check_symlink_loop(self.base, 'linkW') |
| 1957 |
# Non-strict |
| 1958 |
q = self.cls(self.base, 'linkW', 'foo') |
| 1959 |
self.assertEqual(q.resolve(strict=False), q) |
| 1960 |
|
| 1961 |
def test_resolve_nonexist_relative_issue38671(self): |
| 1962 |
p = self.cls('non', 'exist') |
| 1963 |
|
| 1964 |
old_cwd = os.getcwd() |
| 1965 |
os.chdir(self.base) |
| 1966 |
try: |
| 1967 |
self.assertEqual(p.resolve(), self.cls(self.base, p)) |
| 1968 |
finally: |
| 1969 |
os.chdir(old_cwd) |
| 1970 |
|
| 1971 |
@needs_symlinks |
| 1972 |
def test_readlink(self): |
| 1973 |
P = self.cls(self.base) |
| 1974 |
self.assertEqual((P / 'linkA').readlink(), self.cls('fileA')) |
| 1975 |
self.assertEqual((P / 'brokenLink').readlink(), |
| 1976 |
self.cls('non-existing')) |
| 1977 |
self.assertEqual((P / 'linkB').readlink(), self.cls('dirB')) |
| 1978 |
self.assertEqual((P / 'linkB' / 'linkD').readlink(), self.cls('../dirB')) |
| 1979 |
with self.assertRaises(OSError): |
| 1980 |
(P / 'fileA').readlink() |
| 1981 |
|
| 1982 |
@unittest.skipIf(hasattr(os, "readlink"), "os.readlink() is present") |
| 1983 |
def test_readlink_unsupported(self): |
| 1984 |
P = self.cls(self.base) |
| 1985 |
p = P / 'fileA' |
| 1986 |
with self.assertRaises(pathlib.UnsupportedOperation): |
| 1987 |
q.readlink(p) |
| 1988 |
|
| 1989 |
@os_helper.skip_unless_working_chmod |
| 1990 |
def test_chmod(self): |
| 1991 |
p = self.cls(self.base) / 'fileA' |
| 1992 |
mode = p.stat().st_mode |
| 1993 |
# Clear writable bit. |
| 1994 |
new_mode = mode & ~0o222 |
| 1995 |
p.chmod(new_mode) |
| 1996 |
self.assertEqual(p.stat().st_mode, new_mode) |
| 1997 |
# Set writable bit. |
| 1998 |
new_mode = mode | 0o222 |
| 1999 |
p.chmod(new_mode) |
| 2000 |
self.assertEqual(p.stat().st_mode, new_mode) |
| 2001 |
|
| 2002 |
# On Windows, os.chmod does not follow symlinks (issue #15411) |
| 2003 |
@needs_posix |
| 2004 |
@os_helper.skip_unless_working_chmod |
| 2005 |
def test_chmod_follow_symlinks_true(self): |
| 2006 |
p = self.cls(self.base) / 'linkA' |
| 2007 |
q = p.resolve() |
| 2008 |
mode = q.stat().st_mode |
| 2009 |
# Clear writable bit. |
| 2010 |
new_mode = mode & ~0o222 |
| 2011 |
p.chmod(new_mode, follow_symlinks=True) |
| 2012 |
self.assertEqual(q.stat().st_mode, new_mode) |
| 2013 |
# Set writable bit |
| 2014 |
new_mode = mode | 0o222 |
| 2015 |
p.chmod(new_mode, follow_symlinks=True) |
| 2016 |
self.assertEqual(q.stat().st_mode, new_mode) |
| 2017 |
|
| 2018 |
# XXX also need a test for lchmod. |
| 2019 |
|
| 2020 |
def _get_pw_name_or_skip_test(self, uid): |
| 2021 |
try: |
| 2022 |
return pwd.getpwuid(uid).pw_name |
| 2023 |
except KeyError: |
| 2024 |
self.skipTest( |
| 2025 |
"user %d doesn't have an entry in the system database" % uid) |
| 2026 |
|
| 2027 |
@unittest.skipUnless(pwd, "the pwd module is needed for this test") |
| 2028 |
def test_owner(self): |
| 2029 |
p = self.cls(self.base) / 'fileA' |
| 2030 |
expected_uid = p.stat().st_uid |
| 2031 |
expected_name = self._get_pw_name_or_skip_test(expected_uid) |
| 2032 |
|
| 2033 |
self.assertEqual(expected_name, p.owner()) |
| 2034 |
|
| 2035 |
@unittest.skipUnless(pwd, "the pwd module is needed for this test") |
| 2036 |
@unittest.skipUnless(root_in_posix, "test needs root privilege") |
| 2037 |
def test_owner_no_follow_symlinks(self): |
| 2038 |
all_users = [u.pw_uid for u in pwd.getpwall()] |
| 2039 |
if len(all_users) < 2: |
| 2040 |
self.skipTest("test needs more than one user") |
| 2041 |
|
| 2042 |
target = self.cls(self.base) / 'fileA' |
| 2043 |
link = self.cls(self.base) / 'linkA' |
| 2044 |
|
| 2045 |
uid_1, uid_2 = all_users[:2] |
| 2046 |
os.chown(target, uid_1, -1) |
| 2047 |
os.chown(link, uid_2, -1, follow_symlinks=False) |
| 2048 |
|
| 2049 |
expected_uid = link.stat(follow_symlinks=False).st_uid |
| 2050 |
expected_name = self._get_pw_name_or_skip_test(expected_uid) |
| 2051 |
|
| 2052 |
self.assertEqual(expected_uid, uid_2) |
| 2053 |
self.assertEqual(expected_name, link.owner(follow_symlinks=False)) |
| 2054 |
|
| 2055 |
def _get_gr_name_or_skip_test(self, gid): |
| 2056 |
try: |
| 2057 |
return grp.getgrgid(gid).gr_name |
| 2058 |
except KeyError: |
| 2059 |
self.skipTest( |
| 2060 |
"group %d doesn't have an entry in the system database" % gid) |
| 2061 |
|
| 2062 |
@unittest.skipUnless(grp, "the grp module is needed for this test") |
| 2063 |
def test_group(self): |
| 2064 |
p = self.cls(self.base) / 'fileA' |
| 2065 |
expected_gid = p.stat().st_gid |
| 2066 |
expected_name = self._get_gr_name_or_skip_test(expected_gid) |
| 2067 |
|
| 2068 |
self.assertEqual(expected_name, p.group()) |
| 2069 |
|
| 2070 |
@unittest.skipUnless(grp, "the grp module is needed for this test") |
| 2071 |
@unittest.skipUnless(root_in_posix, "test needs root privilege") |
| 2072 |
def test_group_no_follow_symlinks(self): |
| 2073 |
all_groups = [g.gr_gid for g in grp.getgrall()] |
| 2074 |
if len(all_groups) < 2: |
| 2075 |
self.skipTest("test needs more than one group") |
| 2076 |
|
| 2077 |
target = self.cls(self.base) / 'fileA' |
| 2078 |
link = self.cls(self.base) / 'linkA' |
| 2079 |
|
| 2080 |
gid_1, gid_2 = all_groups[:2] |
| 2081 |
os.chown(target, -1, gid_1) |
| 2082 |
os.chown(link, -1, gid_2, follow_symlinks=False) |
| 2083 |
|
| 2084 |
expected_gid = link.stat(follow_symlinks=False).st_gid |
| 2085 |
expected_name = self._get_gr_name_or_skip_test(expected_gid) |
| 2086 |
|
| 2087 |
self.assertEqual(expected_gid, gid_2) |
| 2088 |
self.assertEqual(expected_name, link.group(follow_symlinks=False)) |
| 2089 |
|
| 2090 |
def test_unlink(self): |
| 2091 |
p = self.cls(self.base) / 'fileA' |
| 2092 |
p.unlink() |
| 2093 |
self.assertFileNotFound(p.stat) |
| 2094 |
self.assertFileNotFound(p.unlink) |
| 2095 |
|
| 2096 |
def test_unlink_missing_ok(self): |
| 2097 |
p = self.cls(self.base) / 'fileAAA' |
| 2098 |
self.assertFileNotFound(p.unlink) |
| 2099 |
p.unlink(missing_ok=True) |
| 2100 |
|
| 2101 |
def test_rmdir(self): |
| 2102 |
p = self.cls(self.base) / 'dirA' |
| 2103 |
for q in p.iterdir(): |
| 2104 |
q.unlink() |
| 2105 |
p.rmdir() |
| 2106 |
self.assertFileNotFound(p.stat) |
| 2107 |
self.assertFileNotFound(p.unlink) |
| 2108 |
|
| 2109 |
def test_delete_file(self): |
| 2110 |
p = self.cls(self.base) / 'fileA' |
| 2111 |
p._delete() |
| 2112 |
self.assertFalse(p.exists()) |
| 2113 |
self.assertFileNotFound(p._delete) |
| 2114 |
|
| 2115 |
def test_delete_dir(self): |
| 2116 |
base = self.cls(self.base) |
| 2117 |
base.joinpath('dirA')._delete() |
| 2118 |
self.assertFalse(base.joinpath('dirA').exists()) |
| 2119 |
self.assertFalse(base.joinpath('dirA', 'linkC').exists( |
| 2120 |
follow_symlinks=False)) |
| 2121 |
base.joinpath('dirB')._delete() |
| 2122 |
self.assertFalse(base.joinpath('dirB').exists()) |
| 2123 |
self.assertFalse(base.joinpath('dirB', 'fileB').exists()) |
| 2124 |
self.assertFalse(base.joinpath('dirB', 'linkD').exists( |
| 2125 |
follow_symlinks=False)) |
| 2126 |
base.joinpath('dirC')._delete() |
| 2127 |
self.assertFalse(base.joinpath('dirC').exists()) |
| 2128 |
self.assertFalse(base.joinpath('dirC', 'dirD').exists()) |
| 2129 |
self.assertFalse(base.joinpath('dirC', 'dirD', 'fileD').exists()) |
| 2130 |
self.assertFalse(base.joinpath('dirC', 'fileC').exists()) |
| 2131 |
self.assertFalse(base.joinpath('dirC', 'novel.txt').exists()) |
| 2132 |
|
| 2133 |
def test_delete_missing(self): |
| 2134 |
tmp = self.cls(self.base, 'delete') |
| 2135 |
tmp.mkdir() |
| 2136 |
# filename is guaranteed not to exist |
| 2137 |
filename = tmp / 'foo' |
| 2138 |
self.assertRaises(FileNotFoundError, filename._delete) |
| 2139 |
|
| 2140 |
@needs_symlinks |
| 2141 |
def test_delete_symlink(self): |
| 2142 |
tmp = self.cls(self.base, 'delete') |
| 2143 |
tmp.mkdir() |
| 2144 |
dir_ = tmp / 'dir' |
| 2145 |
dir_.mkdir() |
| 2146 |
link = tmp / 'link' |
| 2147 |
link.symlink_to(dir_) |
| 2148 |
link._delete() |
| 2149 |
self.assertTrue(dir_.exists()) |
| 2150 |
self.assertFalse(link.exists(follow_symlinks=False)) |
| 2151 |
|
| 2152 |
@needs_symlinks |
| 2153 |
def test_delete_inner_symlink(self): |
| 2154 |
tmp = self.cls(self.base, 'delete') |
| 2155 |
tmp.mkdir() |
| 2156 |
dir1 = tmp / 'dir1' |
| 2157 |
dir2 = dir1 / 'dir2' |
| 2158 |
dir3 = tmp / 'dir3' |
| 2159 |
for d in dir1, dir2, dir3: |
| 2160 |
d.mkdir() |
| 2161 |
file1 = tmp / 'file1' |
| 2162 |
file1.write_text('foo') |
| 2163 |
link1 = dir1 / 'link1' |
| 2164 |
link1.symlink_to(dir2) |
| 2165 |
link2 = dir1 / 'link2' |
| 2166 |
link2.symlink_to(dir3) |
| 2167 |
link3 = dir1 / 'link3' |
| 2168 |
link3.symlink_to(file1) |
| 2169 |
# make sure symlinks are removed but not followed |
| 2170 |
dir1._delete() |
| 2171 |
self.assertFalse(dir1.exists()) |
| 2172 |
self.assertTrue(dir3.exists()) |
| 2173 |
self.assertTrue(file1.exists()) |
| 2174 |
|
| 2175 |
@unittest.skipIf(sys.platform[:6] == 'cygwin', |
| 2176 |
"This test can't be run on Cygwin (issue #1071513).") |
| 2177 |
@os_helper.skip_if_dac_override |
| 2178 |
@os_helper.skip_unless_working_chmod |
| 2179 |
def test_delete_unwritable(self): |
| 2180 |
tmp = self.cls(self.base, 'delete') |
| 2181 |
tmp.mkdir() |
| 2182 |
child_file_path = tmp / 'a' |
| 2183 |
child_dir_path = tmp / 'b' |
| 2184 |
child_file_path.write_text("") |
| 2185 |
child_dir_path.mkdir() |
| 2186 |
old_dir_mode = tmp.stat().st_mode |
| 2187 |
old_child_file_mode = child_file_path.stat().st_mode |
| 2188 |
old_child_dir_mode = child_dir_path.stat().st_mode |
| 2189 |
# Make unwritable. |
| 2190 |
new_mode = stat.S_IREAD | stat.S_IEXEC |
| 2191 |
try: |
| 2192 |
child_file_path.chmod(new_mode) |
| 2193 |
child_dir_path.chmod(new_mode) |
| 2194 |
tmp.chmod(new_mode) |
| 2195 |
|
| 2196 |
self.assertRaises(PermissionError, tmp._delete) |
| 2197 |
finally: |
| 2198 |
tmp.chmod(old_dir_mode) |
| 2199 |
child_file_path.chmod(old_child_file_mode) |
| 2200 |
child_dir_path.chmod(old_child_dir_mode) |
| 2201 |
|
| 2202 |
@needs_windows |
| 2203 |
def test_delete_inner_junction(self): |
| 2204 |
import _winapi |
| 2205 |
tmp = self.cls(self.base, 'delete') |
| 2206 |
tmp.mkdir() |
| 2207 |
dir1 = tmp / 'dir1' |
| 2208 |
dir2 = dir1 / 'dir2' |
| 2209 |
dir3 = tmp / 'dir3' |
| 2210 |
for d in dir1, dir2, dir3: |
| 2211 |
d.mkdir() |
| 2212 |
file1 = tmp / 'file1' |
| 2213 |
file1.write_text('foo') |
| 2214 |
link1 = dir1 / 'link1' |
| 2215 |
_winapi.CreateJunction(str(dir2), str(link1)) |
| 2216 |
link2 = dir1 / 'link2' |
| 2217 |
_winapi.CreateJunction(str(dir3), str(link2)) |
| 2218 |
link3 = dir1 / 'link3' |
| 2219 |
_winapi.CreateJunction(str(file1), str(link3)) |
| 2220 |
# make sure junctions are removed but not followed |
| 2221 |
dir1._delete() |
| 2222 |
self.assertFalse(dir1.exists()) |
| 2223 |
self.assertTrue(dir3.exists()) |
| 2224 |
self.assertTrue(file1.exists()) |
| 2225 |
|
| 2226 |
@needs_windows |
| 2227 |
def test_delete_outer_junction(self): |
| 2228 |
import _winapi |
| 2229 |
tmp = self.cls(self.base, 'delete') |
| 2230 |
tmp.mkdir() |
| 2231 |
src = tmp / 'cheese' |
| 2232 |
dst = tmp / 'shop' |
| 2233 |
src.mkdir() |
| 2234 |
spam = src / 'spam' |
| 2235 |
spam.write_text('') |
| 2236 |
_winapi.CreateJunction(str(src), str(dst)) |
| 2237 |
dst._delete() |
| 2238 |
self.assertFalse(dst.exists()) |
| 2239 |
self.assertTrue(spam.exists()) |
| 2240 |
self.assertTrue(src.exists()) |
| 2241 |
|
| 2242 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 2243 |
@unittest.skipIf(sys.platform == "vxworks", |
| 2244 |
"fifo requires special path on VxWorks") |
| 2245 |
def test_delete_on_named_pipe(self): |
| 2246 |
p = self.cls(self.base, 'pipe') |
| 2247 |
os.mkfifo(p) |
| 2248 |
p._delete() |
| 2249 |
self.assertFalse(p.exists()) |
| 2250 |
|
| 2251 |
p = self.cls(self.base, 'dir') |
| 2252 |
p.mkdir() |
| 2253 |
os.mkfifo(p / 'mypipe') |
| 2254 |
p._delete() |
| 2255 |
self.assertFalse(p.exists()) |
| 2256 |
|
| 2257 |
def test_delete_does_not_choke_on_failing_lstat(self): |
| 2258 |
try: |
| 2259 |
orig_lstat = os.lstat |
| 2260 |
tmp = self.cls(self.base, 'delete') |
| 2261 |
|
| 2262 |
def raiser(fn, *args, **kwargs): |
| 2263 |
if fn != tmp: |
| 2264 |
raise OSError() |
| 2265 |
else: |
| 2266 |
return orig_lstat(fn) |
| 2267 |
|
| 2268 |
os.lstat = raiser |
| 2269 |
|
| 2270 |
tmp.mkdir() |
| 2271 |
foo = tmp / 'foo' |
| 2272 |
foo.write_text('') |
| 2273 |
tmp._delete() |
| 2274 |
finally: |
| 2275 |
os.lstat = orig_lstat |
| 2276 |
|
| 2277 |
@os_helper.skip_unless_hardlink |
| 2278 |
def test_hardlink_to(self): |
| 2279 |
P = self.cls(self.base) |
| 2280 |
target = P / 'fileA' |
| 2281 |
size = target.stat().st_size |
| 2282 |
# linking to another path. |
| 2283 |
link = P / 'dirA' / 'fileAA' |
| 2284 |
link.hardlink_to(target) |
| 2285 |
self.assertEqual(link.stat().st_size, size) |
| 2286 |
self.assertTrue(os.path.samefile(target, link)) |
| 2287 |
self.assertTrue(target.exists()) |
| 2288 |
# Linking to a str of a relative path. |
| 2289 |
link2 = P / 'dirA' / 'fileAAA' |
| 2290 |
target2 = self.parser.join(TESTFN, 'fileA') |
| 2291 |
link2.hardlink_to(target2) |
| 2292 |
self.assertEqual(os.stat(target2).st_size, size) |
| 2293 |
self.assertTrue(link2.exists()) |
| 2294 |
|
| 2295 |
@unittest.skipIf(hasattr(os, "link"), "os.link() is present") |
| 2296 |
def test_hardlink_to_unsupported(self): |
| 2297 |
P = self.cls(self.base) |
| 2298 |
p = P / 'fileA' |
| 2299 |
# linking to another path. |
| 2300 |
q = P / 'dirA' / 'fileAA' |
| 2301 |
with self.assertRaises(pathlib.UnsupportedOperation): |
| 2302 |
q.hardlink_to(p) |
| 2303 |
|
| 2304 |
def test_rename(self): |
| 2305 |
P = self.cls(self.base) |
| 2306 |
p = P / 'fileA' |
| 2307 |
size = p.stat().st_size |
| 2308 |
# Renaming to another path. |
| 2309 |
q = P / 'dirA' / 'fileAA' |
| 2310 |
renamed_p = p.rename(q) |
| 2311 |
self.assertEqual(renamed_p, q) |
| 2312 |
self.assertEqual(q.stat().st_size, size) |
| 2313 |
self.assertFileNotFound(p.stat) |
| 2314 |
# Renaming to a str of a relative path. |
| 2315 |
r = self.parser.join(TESTFN, 'fileAAA') |
| 2316 |
renamed_q = q.rename(r) |
| 2317 |
self.assertEqual(renamed_q, self.cls(r)) |
| 2318 |
self.assertEqual(os.stat(r).st_size, size) |
| 2319 |
self.assertFileNotFound(q.stat) |
| 2320 |
|
| 2321 |
def test_replace(self): |
| 2322 |
P = self.cls(self.base) |
| 2323 |
p = P / 'fileA' |
| 2324 |
size = p.stat().st_size |
| 2325 |
# Replacing a non-existing path. |
| 2326 |
q = P / 'dirA' / 'fileAA' |
| 2327 |
replaced_p = p.replace(q) |
| 2328 |
self.assertEqual(replaced_p, q) |
| 2329 |
self.assertEqual(q.stat().st_size, size) |
| 2330 |
self.assertFileNotFound(p.stat) |
| 2331 |
# Replacing another (existing) path. |
| 2332 |
r = self.parser.join(TESTFN, 'dirB', 'fileB') |
| 2333 |
replaced_q = q.replace(r) |
| 2334 |
self.assertEqual(replaced_q, self.cls(r)) |
| 2335 |
self.assertEqual(os.stat(r).st_size, size) |
| 2336 |
self.assertFileNotFound(q.stat) |
| 2337 |
|
| 2338 |
def test_touch_common(self): |
| 2339 |
P = self.cls(self.base) |
| 2340 |
p = P / 'newfileA' |
| 2341 |
self.assertFalse(p.exists()) |
| 2342 |
p.touch() |
| 2343 |
self.assertTrue(p.exists()) |
| 2344 |
st = p.stat() |
| 2345 |
old_mtime = st.st_mtime |
| 2346 |
old_mtime_ns = st.st_mtime_ns |
| 2347 |
# Rewind the mtime sufficiently far in the past to work around |
| 2348 |
# filesystem-specific timestamp granularity. |
| 2349 |
os.utime(str(p), (old_mtime - 10, old_mtime - 10)) |
| 2350 |
# The file mtime should be refreshed by calling touch() again. |
| 2351 |
p.touch() |
| 2352 |
st = p.stat() |
| 2353 |
self.assertGreaterEqual(st.st_mtime_ns, old_mtime_ns) |
| 2354 |
self.assertGreaterEqual(st.st_mtime, old_mtime) |
| 2355 |
# Now with exist_ok=False. |
| 2356 |
p = P / 'newfileB' |
| 2357 |
self.assertFalse(p.exists()) |
| 2358 |
p.touch(mode=0o700, exist_ok=False) |
| 2359 |
self.assertTrue(p.exists()) |
| 2360 |
self.assertRaises(OSError, p.touch, exist_ok=False) |
| 2361 |
|
| 2362 |
def test_touch_nochange(self): |
| 2363 |
P = self.cls(self.base) |
| 2364 |
p = P / 'fileA' |
| 2365 |
p.touch() |
| 2366 |
with p.open('rb') as f: |
| 2367 |
self.assertEqual(f.read().strip(), b"this is file A") |
| 2368 |
|
| 2369 |
def test_mkdir(self): |
| 2370 |
P = self.cls(self.base) |
| 2371 |
p = P / 'newdirA' |
| 2372 |
self.assertFalse(p.exists()) |
| 2373 |
p.mkdir() |
| 2374 |
self.assertTrue(p.exists()) |
| 2375 |
self.assertTrue(p.is_dir()) |
| 2376 |
with self.assertRaises(OSError) as cm: |
| 2377 |
p.mkdir() |
| 2378 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2379 |
|
| 2380 |
def test_mkdir_parents(self): |
| 2381 |
# Creating a chain of directories. |
| 2382 |
p = self.cls(self.base, 'newdirB', 'newdirC') |
| 2383 |
self.assertFalse(p.exists()) |
| 2384 |
with self.assertRaises(OSError) as cm: |
| 2385 |
p.mkdir() |
| 2386 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 2387 |
p.mkdir(parents=True) |
| 2388 |
self.assertTrue(p.exists()) |
| 2389 |
self.assertTrue(p.is_dir()) |
| 2390 |
with self.assertRaises(OSError) as cm: |
| 2391 |
p.mkdir(parents=True) |
| 2392 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2393 |
# Test `mode` arg. |
| 2394 |
mode = stat.S_IMODE(p.stat().st_mode) # Default mode. |
| 2395 |
p = self.cls(self.base, 'newdirD', 'newdirE') |
| 2396 |
p.mkdir(0o555, parents=True) |
| 2397 |
self.assertTrue(p.exists()) |
| 2398 |
self.assertTrue(p.is_dir()) |
| 2399 |
if os.name != 'nt': |
| 2400 |
# The directory's permissions follow the mode argument. |
| 2401 |
self.assertEqual(stat.S_IMODE(p.stat().st_mode), 0o7555 & mode) |
| 2402 |
# The parent's permissions follow the default process settings. |
| 2403 |
self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode) |
| 2404 |
|
| 2405 |
def test_mkdir_exist_ok(self): |
| 2406 |
p = self.cls(self.base, 'dirB') |
| 2407 |
st_ctime_first = p.stat().st_ctime |
| 2408 |
self.assertTrue(p.exists()) |
| 2409 |
self.assertTrue(p.is_dir()) |
| 2410 |
with self.assertRaises(FileExistsError) as cm: |
| 2411 |
p.mkdir() |
| 2412 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2413 |
p.mkdir(exist_ok=True) |
| 2414 |
self.assertTrue(p.exists()) |
| 2415 |
self.assertEqual(p.stat().st_ctime, st_ctime_first) |
| 2416 |
|
| 2417 |
def test_mkdir_exist_ok_with_parent(self): |
| 2418 |
p = self.cls(self.base, 'dirC') |
| 2419 |
self.assertTrue(p.exists()) |
| 2420 |
with self.assertRaises(FileExistsError) as cm: |
| 2421 |
p.mkdir() |
| 2422 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2423 |
p = p / 'newdirC' |
| 2424 |
p.mkdir(parents=True) |
| 2425 |
st_ctime_first = p.stat().st_ctime |
| 2426 |
self.assertTrue(p.exists()) |
| 2427 |
with self.assertRaises(FileExistsError) as cm: |
| 2428 |
p.mkdir(parents=True) |
| 2429 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2430 |
p.mkdir(parents=True, exist_ok=True) |
| 2431 |
self.assertTrue(p.exists()) |
| 2432 |
self.assertEqual(p.stat().st_ctime, st_ctime_first) |
| 2433 |
|
| 2434 |
def test_mkdir_exist_ok_root(self): |
| 2435 |
# Issue #25803: A drive root could raise PermissionError on Windows. |
| 2436 |
self.cls('/').resolve().mkdir(exist_ok=True) |
| 2437 |
self.cls('/').resolve().mkdir(parents=True, exist_ok=True) |
| 2438 |
|
| 2439 |
@needs_windows # XXX: not sure how to test this on POSIX. |
| 2440 |
def test_mkdir_with_unknown_drive(self): |
| 2441 |
for d in 'ZYXWVUTSRQPONMLKJIHGFEDCBA': |
| 2442 |
p = self.cls(d + ':\\') |
| 2443 |
if not p.is_dir(): |
| 2444 |
break |
| 2445 |
else: |
| 2446 |
self.skipTest("cannot find a drive that doesn't exist") |
| 2447 |
with self.assertRaises(OSError): |
| 2448 |
(p / 'child' / 'path').mkdir(parents=True) |
| 2449 |
|
| 2450 |
def test_mkdir_with_child_file(self): |
| 2451 |
p = self.cls(self.base, 'dirB', 'fileB') |
| 2452 |
self.assertTrue(p.exists()) |
| 2453 |
# An exception is raised when the last path component is an existing |
| 2454 |
# regular file, regardless of whether exist_ok is true or not. |
| 2455 |
with self.assertRaises(FileExistsError) as cm: |
| 2456 |
p.mkdir(parents=True) |
| 2457 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2458 |
with self.assertRaises(FileExistsError) as cm: |
| 2459 |
p.mkdir(parents=True, exist_ok=True) |
| 2460 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2461 |
|
| 2462 |
def test_mkdir_no_parents_file(self): |
| 2463 |
p = self.cls(self.base, 'fileA') |
| 2464 |
self.assertTrue(p.exists()) |
| 2465 |
# An exception is raised when the last path component is an existing |
| 2466 |
# regular file, regardless of whether exist_ok is true or not. |
| 2467 |
with self.assertRaises(FileExistsError) as cm: |
| 2468 |
p.mkdir() |
| 2469 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2470 |
with self.assertRaises(FileExistsError) as cm: |
| 2471 |
p.mkdir(exist_ok=True) |
| 2472 |
self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2473 |
|
| 2474 |
def test_mkdir_concurrent_parent_creation(self): |
| 2475 |
for pattern_num in range(32): |
| 2476 |
p = self.cls(self.base, 'dirCPC%d' % pattern_num) |
| 2477 |
self.assertFalse(p.exists()) |
| 2478 |
|
| 2479 |
real_mkdir = os.mkdir |
| 2480 |
def my_mkdir(path, mode=0o777): |
| 2481 |
path = str(path) |
| 2482 |
# Emulate another process that would create the directory |
| 2483 |
# just before we try to create it ourselves. We do it |
| 2484 |
# in all possible pattern combinations, assuming that this |
| 2485 |
# function is called at most 5 times (dirCPC/dir1/dir2, |
| 2486 |
# dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2). |
| 2487 |
if pattern.pop(): |
| 2488 |
real_mkdir(path, mode) # From another process. |
| 2489 |
concurrently_created.add(path) |
| 2490 |
real_mkdir(path, mode) # Our real call. |
| 2491 |
|
| 2492 |
pattern = [bool(pattern_num & (1 << n)) for n in range(5)] |
| 2493 |
concurrently_created = set() |
| 2494 |
p12 = p / 'dir1' / 'dir2' |
| 2495 |
try: |
| 2496 |
with mock.patch("os.mkdir", my_mkdir): |
| 2497 |
p12.mkdir(parents=True, exist_ok=False) |
| 2498 |
except FileExistsError: |
| 2499 |
self.assertIn(str(p12), concurrently_created) |
| 2500 |
else: |
| 2501 |
self.assertNotIn(str(p12), concurrently_created) |
| 2502 |
self.assertTrue(p.exists()) |
| 2503 |
|
| 2504 |
@needs_symlinks |
| 2505 |
def test_symlink_to(self): |
| 2506 |
P = self.cls(self.base) |
| 2507 |
target = P / 'fileA' |
| 2508 |
# Symlinking a path target. |
| 2509 |
link = P / 'dirA' / 'linkAA' |
| 2510 |
link.symlink_to(target) |
| 2511 |
self.assertEqual(link.stat(), target.stat()) |
| 2512 |
self.assertNotEqual(link.lstat(), target.stat()) |
| 2513 |
# Symlinking a str target. |
| 2514 |
link = P / 'dirA' / 'linkAAA' |
| 2515 |
link.symlink_to(str(target)) |
| 2516 |
self.assertEqual(link.stat(), target.stat()) |
| 2517 |
self.assertNotEqual(link.lstat(), target.stat()) |
| 2518 |
self.assertFalse(link.is_dir()) |
| 2519 |
# Symlinking to a directory. |
| 2520 |
target = P / 'dirB' |
| 2521 |
link = P / 'dirA' / 'linkAAAA' |
| 2522 |
link.symlink_to(target, target_is_directory=True) |
| 2523 |
self.assertEqual(link.stat(), target.stat()) |
| 2524 |
self.assertNotEqual(link.lstat(), target.stat()) |
| 2525 |
self.assertTrue(link.is_dir()) |
| 2526 |
self.assertTrue(list(link.iterdir())) |
| 2527 |
|
| 2528 |
@unittest.skipIf(hasattr(os, "symlink"), "os.symlink() is present") |
| 2529 |
def test_symlink_to_unsupported(self): |
| 2530 |
P = self.cls(self.base) |
| 2531 |
p = P / 'fileA' |
| 2532 |
# linking to another path. |
| 2533 |
q = P / 'dirA' / 'fileAA' |
| 2534 |
with self.assertRaises(pathlib.UnsupportedOperation): |
| 2535 |
q.symlink_to(p) |
| 2536 |
|
| 2537 |
def test_info_exists_caching(self): |
| 2538 |
p = self.cls(self.base) |
| 2539 |
q = p / 'myfile' |
| 2540 |
self.assertFalse(q.info.exists()) |
| 2541 |
self.assertFalse(q.info.exists(follow_symlinks=False)) |
| 2542 |
q.write_text('hullo') |
| 2543 |
self.assertFalse(q.info.exists()) |
| 2544 |
self.assertFalse(q.info.exists(follow_symlinks=False)) |
| 2545 |
|
| 2546 |
def test_info_is_dir_caching(self): |
| 2547 |
p = self.cls(self.base) |
| 2548 |
q = p / 'mydir' |
| 2549 |
self.assertFalse(q.info.is_dir()) |
| 2550 |
self.assertFalse(q.info.is_dir(follow_symlinks=False)) |
| 2551 |
q.mkdir() |
| 2552 |
self.assertFalse(q.info.is_dir()) |
| 2553 |
self.assertFalse(q.info.is_dir(follow_symlinks=False)) |
| 2554 |
|
| 2555 |
def test_info_is_file_caching(self): |
| 2556 |
p = self.cls(self.base) |
| 2557 |
q = p / 'myfile' |
| 2558 |
self.assertFalse(q.info.is_file()) |
| 2559 |
self.assertFalse(q.info.is_file(follow_symlinks=False)) |
| 2560 |
q.write_text('hullo') |
| 2561 |
self.assertFalse(q.info.is_file()) |
| 2562 |
self.assertFalse(q.info.is_file(follow_symlinks=False)) |
| 2563 |
|
| 2564 |
@needs_symlinks |
| 2565 |
def test_info_is_symlink_caching(self): |
| 2566 |
p = self.cls(self.base) |
| 2567 |
q = p / 'mylink' |
| 2568 |
self.assertFalse(q.info.is_symlink()) |
| 2569 |
q.symlink_to('blah') |
| 2570 |
self.assertFalse(q.info.is_symlink()) |
| 2571 |
|
| 2572 |
q = p / 'mylink' # same path, new instance. |
| 2573 |
self.assertTrue(q.info.is_symlink()) |
| 2574 |
q.unlink() |
| 2575 |
self.assertTrue(q.info.is_symlink()) |
| 2576 |
|
| 2577 |
def test_stat(self): |
| 2578 |
statA = self.cls(self.base).joinpath('fileA').stat() |
| 2579 |
statB = self.cls(self.base).joinpath('dirB', 'fileB').stat() |
| 2580 |
statC = self.cls(self.base).joinpath('dirC').stat() |
| 2581 |
# st_mode: files are the same, directory differs. |
| 2582 |
self.assertIsInstance(statA.st_mode, int) |
| 2583 |
self.assertEqual(statA.st_mode, statB.st_mode) |
| 2584 |
self.assertNotEqual(statA.st_mode, statC.st_mode) |
| 2585 |
self.assertNotEqual(statB.st_mode, statC.st_mode) |
| 2586 |
# st_ino: all different, |
| 2587 |
self.assertIsInstance(statA.st_ino, int) |
| 2588 |
self.assertNotEqual(statA.st_ino, statB.st_ino) |
| 2589 |
self.assertNotEqual(statA.st_ino, statC.st_ino) |
| 2590 |
self.assertNotEqual(statB.st_ino, statC.st_ino) |
| 2591 |
# st_dev: all the same. |
| 2592 |
self.assertIsInstance(statA.st_dev, int) |
| 2593 |
self.assertEqual(statA.st_dev, statB.st_dev) |
| 2594 |
self.assertEqual(statA.st_dev, statC.st_dev) |
| 2595 |
# other attributes not used by pathlib. |
| 2596 |
|
| 2597 |
def test_stat_no_follow_symlinks_nosymlink(self): |
| 2598 |
p = self.cls(self.base) / 'fileA' |
| 2599 |
st = p.stat() |
| 2600 |
self.assertEqual(st, p.stat(follow_symlinks=False)) |
| 2601 |
|
| 2602 |
@needs_symlinks |
| 2603 |
def test_stat_no_follow_symlinks(self): |
| 2604 |
p = self.cls(self.base) / 'linkA' |
| 2605 |
st = p.stat() |
| 2606 |
self.assertNotEqual(st, p.stat(follow_symlinks=False)) |
| 2607 |
|
| 2608 |
@needs_symlinks |
| 2609 |
def test_lstat(self): |
| 2610 |
p = self.cls(self.base)/ 'linkA' |
| 2611 |
st = p.stat() |
| 2612 |
self.assertNotEqual(st, p.lstat()) |
| 2613 |
|
| 2614 |
def test_lstat_nosymlink(self): |
| 2615 |
p = self.cls(self.base) / 'fileA' |
| 2616 |
st = p.stat() |
| 2617 |
self.assertEqual(st, p.lstat()) |
| 2618 |
|
| 2619 |
def test_exists(self): |
| 2620 |
P = self.cls |
| 2621 |
p = P(self.base) |
| 2622 |
self.assertIs(True, p.exists()) |
| 2623 |
self.assertIs(True, (p / 'dirA').exists()) |
| 2624 |
self.assertIs(True, (p / 'fileA').exists()) |
| 2625 |
self.assertIs(False, (p / 'fileA' / 'bah').exists()) |
| 2626 |
if self.can_symlink: |
| 2627 |
self.assertIs(True, (p / 'linkA').exists()) |
| 2628 |
self.assertIs(True, (p / 'linkB').exists()) |
| 2629 |
self.assertIs(True, (p / 'linkB' / 'fileB').exists()) |
| 2630 |
self.assertIs(False, (p / 'linkA' / 'bah').exists()) |
| 2631 |
self.assertIs(False, (p / 'brokenLink').exists()) |
| 2632 |
self.assertIs(True, (p / 'brokenLink').exists(follow_symlinks=False)) |
| 2633 |
self.assertIs(False, (p / 'foo').exists()) |
| 2634 |
self.assertIs(False, P('/xyzzy').exists()) |
| 2635 |
self.assertIs(False, P(self.base + '\udfff').exists()) |
| 2636 |
self.assertIs(False, P(self.base + '\x00').exists()) |
| 2637 |
|
| 2638 |
def test_is_dir(self): |
| 2639 |
P = self.cls(self.base) |
| 2640 |
self.assertTrue((P / 'dirA').is_dir()) |
| 2641 |
self.assertFalse((P / 'fileA').is_dir()) |
| 2642 |
self.assertFalse((P / 'non-existing').is_dir()) |
| 2643 |
self.assertFalse((P / 'fileA' / 'bah').is_dir()) |
| 2644 |
if self.can_symlink: |
| 2645 |
self.assertFalse((P / 'linkA').is_dir()) |
| 2646 |
self.assertTrue((P / 'linkB').is_dir()) |
| 2647 |
self.assertFalse((P/ 'brokenLink').is_dir()) |
| 2648 |
self.assertFalse((P / 'dirA\udfff').is_dir()) |
| 2649 |
self.assertFalse((P / 'dirA\x00').is_dir()) |
| 2650 |
|
| 2651 |
def test_is_dir_no_follow_symlinks(self): |
| 2652 |
P = self.cls(self.base) |
| 2653 |
self.assertTrue((P / 'dirA').is_dir(follow_symlinks=False)) |
| 2654 |
self.assertFalse((P / 'fileA').is_dir(follow_symlinks=False)) |
| 2655 |
self.assertFalse((P / 'non-existing').is_dir(follow_symlinks=False)) |
| 2656 |
self.assertFalse((P / 'fileA' / 'bah').is_dir(follow_symlinks=False)) |
| 2657 |
if self.can_symlink: |
| 2658 |
self.assertFalse((P / 'linkA').is_dir(follow_symlinks=False)) |
| 2659 |
self.assertFalse((P / 'linkB').is_dir(follow_symlinks=False)) |
| 2660 |
self.assertFalse((P/ 'brokenLink').is_dir(follow_symlinks=False)) |
| 2661 |
self.assertFalse((P / 'dirA\udfff').is_dir(follow_symlinks=False)) |
| 2662 |
self.assertFalse((P / 'dirA\x00').is_dir(follow_symlinks=False)) |
| 2663 |
|
| 2664 |
def test_is_file(self): |
| 2665 |
P = self.cls(self.base) |
| 2666 |
self.assertTrue((P / 'fileA').is_file()) |
| 2667 |
self.assertFalse((P / 'dirA').is_file()) |
| 2668 |
self.assertFalse((P / 'non-existing').is_file()) |
| 2669 |
self.assertFalse((P / 'fileA' / 'bah').is_file()) |
| 2670 |
if self.can_symlink: |
| 2671 |
self.assertTrue((P / 'linkA').is_file()) |
| 2672 |
self.assertFalse((P / 'linkB').is_file()) |
| 2673 |
self.assertFalse((P/ 'brokenLink').is_file()) |
| 2674 |
self.assertFalse((P / 'fileA\udfff').is_file()) |
| 2675 |
self.assertFalse((P / 'fileA\x00').is_file()) |
| 2676 |
|
| 2677 |
def test_is_file_no_follow_symlinks(self): |
| 2678 |
P = self.cls(self.base) |
| 2679 |
self.assertTrue((P / 'fileA').is_file(follow_symlinks=False)) |
| 2680 |
self.assertFalse((P / 'dirA').is_file(follow_symlinks=False)) |
| 2681 |
self.assertFalse((P / 'non-existing').is_file(follow_symlinks=False)) |
| 2682 |
self.assertFalse((P / 'fileA' / 'bah').is_file(follow_symlinks=False)) |
| 2683 |
if self.can_symlink: |
| 2684 |
self.assertFalse((P / 'linkA').is_file(follow_symlinks=False)) |
| 2685 |
self.assertFalse((P / 'linkB').is_file(follow_symlinks=False)) |
| 2686 |
self.assertFalse((P/ 'brokenLink').is_file(follow_symlinks=False)) |
| 2687 |
self.assertFalse((P / 'fileA\udfff').is_file(follow_symlinks=False)) |
| 2688 |
self.assertFalse((P / 'fileA\x00').is_file(follow_symlinks=False)) |
| 2689 |
|
| 2690 |
def test_is_symlink(self): |
| 2691 |
P = self.cls(self.base) |
| 2692 |
self.assertFalse((P / 'fileA').is_symlink()) |
| 2693 |
self.assertFalse((P / 'dirA').is_symlink()) |
| 2694 |
self.assertFalse((P / 'non-existing').is_symlink()) |
| 2695 |
self.assertFalse((P / 'fileA' / 'bah').is_symlink()) |
| 2696 |
if self.can_symlink: |
| 2697 |
self.assertTrue((P / 'linkA').is_symlink()) |
| 2698 |
self.assertTrue((P / 'linkB').is_symlink()) |
| 2699 |
self.assertTrue((P/ 'brokenLink').is_symlink()) |
| 2700 |
self.assertIs((P / 'fileA\udfff').is_file(), False) |
| 2701 |
self.assertIs((P / 'fileA\x00').is_file(), False) |
| 2702 |
if self.can_symlink: |
| 2703 |
self.assertIs((P / 'linkA\udfff').is_file(), False) |
| 2704 |
self.assertIs((P / 'linkA\x00').is_file(), False) |
| 2705 |
|
| 2706 |
def test_is_junction_false(self): |
| 2707 |
P = self.cls(self.base) |
| 2708 |
self.assertFalse((P / 'fileA').is_junction()) |
| 2709 |
self.assertFalse((P / 'dirA').is_junction()) |
| 2710 |
self.assertFalse((P / 'non-existing').is_junction()) |
| 2711 |
self.assertFalse((P / 'fileA' / 'bah').is_junction()) |
| 2712 |
self.assertFalse((P / 'fileA\udfff').is_junction()) |
| 2713 |
self.assertFalse((P / 'fileA\x00').is_junction()) |
| 2714 |
|
| 2715 |
def test_is_junction_true(self): |
| 2716 |
P = self.cls(self.base) |
| 2717 |
|
| 2718 |
with mock.patch.object(P.parser, 'isjunction'): |
| 2719 |
self.assertEqual(P.is_junction(), P.parser.isjunction.return_value) |
| 2720 |
P.parser.isjunction.assert_called_once_with(P) |
| 2721 |
|
| 2722 |
def test_is_fifo_false(self): |
| 2723 |
P = self.cls(self.base) |
| 2724 |
self.assertFalse((P / 'fileA').is_fifo()) |
| 2725 |
self.assertFalse((P / 'dirA').is_fifo()) |
| 2726 |
self.assertFalse((P / 'non-existing').is_fifo()) |
| 2727 |
self.assertFalse((P / 'fileA' / 'bah').is_fifo()) |
| 2728 |
self.assertIs((P / 'fileA\udfff').is_fifo(), False) |
| 2729 |
self.assertIs((P / 'fileA\x00').is_fifo(), False) |
| 2730 |
|
| 2731 |
@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required") |
| 2732 |
@unittest.skipIf(sys.platform == "vxworks", |
| 2733 |
"fifo requires special path on VxWorks") |
| 2734 |
def test_is_fifo_true(self): |
| 2735 |
P = self.cls(self.base, 'myfifo') |
| 2736 |
try: |
| 2737 |
os.mkfifo(str(P)) |
| 2738 |
except PermissionError as e: |
| 2739 |
self.skipTest('os.mkfifo(): %s' % e) |
| 2740 |
self.assertTrue(P.is_fifo()) |
| 2741 |
self.assertFalse(P.is_socket()) |
| 2742 |
self.assertFalse(P.is_file()) |
| 2743 |
self.assertIs(self.cls(self.base, 'myfifo\udfff').is_fifo(), False) |
| 2744 |
self.assertIs(self.cls(self.base, 'myfifo\x00').is_fifo(), False) |
| 2745 |
|
| 2746 |
def test_is_socket_false(self): |
| 2747 |
P = self.cls(self.base) |
| 2748 |
self.assertFalse((P / 'fileA').is_socket()) |
| 2749 |
self.assertFalse((P / 'dirA').is_socket()) |
| 2750 |
self.assertFalse((P / 'non-existing').is_socket()) |
| 2751 |
self.assertFalse((P / 'fileA' / 'bah').is_socket()) |
| 2752 |
self.assertIs((P / 'fileA\udfff').is_socket(), False) |
| 2753 |
self.assertIs((P / 'fileA\x00').is_socket(), False) |
| 2754 |
|
| 2755 |
@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required") |
| 2756 |
@unittest.skipIf( |
| 2757 |
is_emscripten, "Unix sockets are not implemented on Emscripten." |
| 2758 |
) |
| 2759 |
@unittest.skipIf( |
| 2760 |
is_wasi, "Cannot create socket on WASI." |
| 2761 |
) |
| 2762 |
def test_is_socket_true(self): |
| 2763 |
P = self.cls(self.base, 'mysock') |
| 2764 |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 2765 |
self.addCleanup(sock.close) |
| 2766 |
try: |
| 2767 |
sock.bind(str(P)) |
| 2768 |
except OSError as e: |
| 2769 |
if (isinstance(e, PermissionError) or |
| 2770 |
"AF_UNIX path too long" in str(e)): |
| 2771 |
self.skipTest("cannot bind Unix socket: " + str(e)) |
| 2772 |
self.assertTrue(P.is_socket()) |
| 2773 |
self.assertFalse(P.is_fifo()) |
| 2774 |
self.assertFalse(P.is_file()) |
| 2775 |
self.assertIs(self.cls(self.base, 'mysock\udfff').is_socket(), False) |
| 2776 |
self.assertIs(self.cls(self.base, 'mysock\x00').is_socket(), False) |
| 2777 |
|
| 2778 |
def test_is_block_device_false(self): |
| 2779 |
P = self.cls(self.base) |
| 2780 |
self.assertFalse((P / 'fileA').is_block_device()) |
| 2781 |
self.assertFalse((P / 'dirA').is_block_device()) |
| 2782 |
self.assertFalse((P / 'non-existing').is_block_device()) |
| 2783 |
self.assertFalse((P / 'fileA' / 'bah').is_block_device()) |
| 2784 |
self.assertIs((P / 'fileA\udfff').is_block_device(), False) |
| 2785 |
self.assertIs((P / 'fileA\x00').is_block_device(), False) |
| 2786 |
|
| 2787 |
def test_is_char_device_false(self): |
| 2788 |
P = self.cls(self.base) |
| 2789 |
self.assertFalse((P / 'fileA').is_char_device()) |
| 2790 |
self.assertFalse((P / 'dirA').is_char_device()) |
| 2791 |
self.assertFalse((P / 'non-existing').is_char_device()) |
| 2792 |
self.assertFalse((P / 'fileA' / 'bah').is_char_device()) |
| 2793 |
self.assertIs((P / 'fileA\udfff').is_char_device(), False) |
| 2794 |
self.assertIs((P / 'fileA\x00').is_char_device(), False) |
| 2795 |
|
| 2796 |
def test_is_char_device_true(self): |
| 2797 |
# os.devnull should generally be a char device. |
| 2798 |
P = self.cls(os.devnull) |
| 2799 |
if not P.exists(): |
| 2800 |
self.skipTest("null device required") |
| 2801 |
self.assertTrue(P.is_char_device()) |
| 2802 |
self.assertFalse(P.is_block_device()) |
| 2803 |
self.assertFalse(P.is_file()) |
| 2804 |
self.assertIs(self.cls(f'{os.devnull}\udfff').is_char_device(), False) |
| 2805 |
self.assertIs(self.cls(f'{os.devnull}\x00').is_char_device(), False) |
| 2806 |
|
| 2807 |
def test_is_mount(self): |
| 2808 |
P = self.cls(self.base) |
| 2809 |
self.assertFalse((P / 'fileA').is_mount()) |
| 2810 |
self.assertFalse((P / 'dirA').is_mount()) |
| 2811 |
self.assertFalse((P / 'non-existing').is_mount()) |
| 2812 |
self.assertFalse((P / 'fileA' / 'bah').is_mount()) |
| 2813 |
if self.can_symlink: |
| 2814 |
self.assertFalse((P / 'linkA').is_mount()) |
| 2815 |
if os.name == 'nt': |
| 2816 |
R = self.cls('c:\\') |
| 2817 |
else: |
| 2818 |
R = self.cls('/') |
| 2819 |
self.assertTrue(R.is_mount()) |
| 2820 |
self.assertFalse((R / '\udfff').is_mount()) |
| 2821 |
|
| 2822 |
def test_samefile(self): |
| 2823 |
parser = self.parser |
| 2824 |
fileA_path = parser.join(self.base, 'fileA') |
| 2825 |
fileB_path = parser.join(self.base, 'dirB', 'fileB') |
| 2826 |
p = self.cls(fileA_path) |
| 2827 |
pp = self.cls(fileA_path) |
| 2828 |
q = self.cls(fileB_path) |
| 2829 |
self.assertTrue(p.samefile(fileA_path)) |
| 2830 |
self.assertTrue(p.samefile(pp)) |
| 2831 |
self.assertFalse(p.samefile(fileB_path)) |
| 2832 |
self.assertFalse(p.samefile(q)) |
| 2833 |
# Test the non-existent file case |
| 2834 |
non_existent = parser.join(self.base, 'foo') |
| 2835 |
r = self.cls(non_existent) |
| 2836 |
self.assertRaises(FileNotFoundError, p.samefile, r) |
| 2837 |
self.assertRaises(FileNotFoundError, p.samefile, non_existent) |
| 2838 |
self.assertRaises(FileNotFoundError, r.samefile, p) |
| 2839 |
self.assertRaises(FileNotFoundError, r.samefile, non_existent) |
| 2840 |
self.assertRaises(FileNotFoundError, r.samefile, r) |
| 2841 |
self.assertRaises(FileNotFoundError, r.samefile, non_existent) |
| 2842 |
|
| 2843 |
def test_passing_kwargs_errors(self): |
| 2844 |
with self.assertRaises(TypeError): |
| 2845 |
self.cls(foo="bar") |
| 2846 |
|
| 2847 |
@needs_symlinks |
| 2848 |
def test_iterdir_symlink(self): |
| 2849 |
# __iter__ on a symlink to a directory. |
| 2850 |
P = self.cls |
| 2851 |
p = P(self.base, 'linkB') |
| 2852 |
paths = set(p.iterdir()) |
| 2853 |
expected = { P(self.base, 'linkB', q) for q in ['fileB', 'linkD'] } |
| 2854 |
self.assertEqual(paths, expected) |
| 2855 |
|
| 2856 |
@needs_posix |
| 2857 |
def test_glob_posix(self): |
| 2858 |
P = self.cls |
| 2859 |
p = P(self.base) |
| 2860 |
q = p / "FILEa" |
| 2861 |
given = set(p.glob("FILEa")) |
| 2862 |
expect = {q} if q.info.exists() else set() |
| 2863 |
self.assertEqual(given, expect) |
| 2864 |
self.assertEqual(set(p.glob("FILEa*")), set()) |
| 2865 |
|
| 2866 |
@needs_windows |
| 2867 |
def test_glob_windows(self): |
| 2868 |
P = self.cls |
| 2869 |
p = P(self.base) |
| 2870 |
self.assertEqual(set(p.glob("FILEa")), { P(self.base, "fileA") }) |
| 2871 |
self.assertEqual(set(p.glob("*a\\")), { P(self.base, "dirA/") }) |
| 2872 |
self.assertEqual(set(p.glob("F*a")), { P(self.base, "fileA") }) |
| 2873 |
|
| 2874 |
def test_glob_empty_pattern(self): |
| 2875 |
p = self.cls('') |
| 2876 |
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): |
| 2877 |
list(p.glob('')) |
| 2878 |
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): |
| 2879 |
list(p.glob('.')) |
| 2880 |
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): |
| 2881 |
list(p.glob('./')) |
| 2882 |
|
| 2883 |
def test_glob_many_open_files(self): |
| 2884 |
depth = 30 |
| 2885 |
P = self.cls |
| 2886 |
p = base = P(self.base) / 'deep' |
| 2887 |
p.mkdir() |
| 2888 |
for _ in range(depth): |
| 2889 |
p /= 'd' |
| 2890 |
p.mkdir() |
| 2891 |
pattern = '/'.join(['*'] * depth) |
| 2892 |
iters = [base.glob(pattern) for j in range(100)] |
| 2893 |
for it in iters: |
| 2894 |
self.assertEqual(next(it), p) |
| 2895 |
iters = [base.rglob('d') for j in range(100)] |
| 2896 |
p = base |
| 2897 |
for i in range(depth): |
| 2898 |
p = p / 'd' |
| 2899 |
for it in iters: |
| 2900 |
self.assertEqual(next(it), p) |
| 2901 |
|
| 2902 |
def test_glob_above_recursion_limit(self): |
| 2903 |
recursion_limit = 50 |
| 2904 |
# directory_depth > recursion_limit |
| 2905 |
directory_depth = recursion_limit + 10 |
| 2906 |
base = self.cls(self.base, 'deep') |
| 2907 |
path = base.joinpath(*(['d'] * directory_depth)) |
| 2908 |
path.mkdir(parents=True) |
| 2909 |
|
| 2910 |
with infinite_recursion(recursion_limit): |
| 2911 |
list(base.glob('**/')) |
| 2912 |
|
| 2913 |
def test_glob_pathlike(self): |
| 2914 |
P = self.cls |
| 2915 |
p = P(self.base) |
| 2916 |
pattern = "dir*/file*" |
| 2917 |
expect = {p / "dirB/fileB", p / "dirC/fileC"} |
| 2918 |
self.assertEqual(expect, set(p.glob(P(pattern)))) |
| 2919 |
self.assertEqual(expect, set(p.glob(FakePath(pattern)))) |
| 2920 |
|
| 2921 |
def test_glob_case_sensitive(self): |
| 2922 |
P = self.cls |
| 2923 |
def _check(path, pattern, case_sensitive, expected): |
| 2924 |
actual = {str(q) for q in path.glob(pattern, case_sensitive=case_sensitive)} |
| 2925 |
expected = {str(P(self.base, q)) for q in expected} |
| 2926 |
self.assertEqual(actual, expected) |
| 2927 |
path = P(self.base) |
| 2928 |
_check(path, "DIRB/FILE*", True, []) |
| 2929 |
_check(path, "DIRB/FILE*", False, ["dirB/fileB"]) |
| 2930 |
_check(path, "dirb/file*", True, []) |
| 2931 |
_check(path, "dirb/file*", False, ["dirB/fileB"]) |
| 2932 |
|
| 2933 |
@needs_symlinks |
| 2934 |
def test_glob_dot(self): |
| 2935 |
P = self.cls |
| 2936 |
with os_helper.change_cwd(P(self.base, "dirC")): |
| 2937 |
self.assertEqual( |
| 2938 |
set(P('.').glob('*')), {P("fileC"), P("novel.txt"), P("dirD")}) |
| 2939 |
self.assertEqual( |
| 2940 |
set(P('.').glob('**')), {P("fileC"), P("novel.txt"), P("dirD"), P("dirD/fileD"), P(".")}) |
| 2941 |
self.assertEqual( |
| 2942 |
set(P('.').glob('**/*')), {P("fileC"), P("novel.txt"), P("dirD"), P("dirD/fileD")}) |
| 2943 |
self.assertEqual( |
| 2944 |
set(P('.').glob('**/*/*')), {P("dirD/fileD")}) |
| 2945 |
|
| 2946 |
# See https://github.com/WebAssembly/wasi-filesystem/issues/26 |
| 2947 |
@unittest.skipIf(is_wasi, "WASI resolution of '..' parts doesn't match POSIX") |
| 2948 |
def test_glob_dotdot(self): |
| 2949 |
# ".." is not special in globs. |
| 2950 |
P = self.cls |
| 2951 |
p = P(self.base) |
| 2952 |
self.assertEqual(set(p.glob("..")), { P(self.base, "..") }) |
| 2953 |
self.assertEqual(set(p.glob("../..")), { P(self.base, "..", "..") }) |
| 2954 |
self.assertEqual(set(p.glob("dirA/..")), { P(self.base, "dirA", "..") }) |
| 2955 |
self.assertEqual(set(p.glob("dirA/../file*")), { P(self.base, "dirA/../fileA") }) |
| 2956 |
self.assertEqual(set(p.glob("dirA/../file*/..")), set()) |
| 2957 |
self.assertEqual(set(p.glob("../xyzzy")), set()) |
| 2958 |
if self.cls.parser is posixpath: |
| 2959 |
self.assertEqual(set(p.glob("xyzzy/..")), set()) |
| 2960 |
else: |
| 2961 |
# ".." segments are normalized first on Windows, so this path is stat()able. |
| 2962 |
self.assertEqual(set(p.glob("xyzzy/..")), { P(self.base, "xyzzy", "..") }) |
| 2963 |
if sys.platform == "emscripten": |
| 2964 |
# Emscripten will return ELOOP if there are 49 or more ..'s. |
| 2965 |
# Can remove when https://github.com/emscripten-core/emscripten/pull/24591 is merged. |
| 2966 |
NDOTDOTS = 48 |
| 2967 |
else: |
| 2968 |
NDOTDOTS = 50 |
| 2969 |
self.assertEqual(set(p.glob("/".join([".."] * NDOTDOTS))), { P(self.base, *[".."] * NDOTDOTS)}) |
| 2970 |
|
| 2971 |
def test_glob_inaccessible(self): |
| 2972 |
P = self.cls |
| 2973 |
p = P(self.base, "mydir1", "mydir2") |
| 2974 |
p.mkdir(parents=True) |
| 2975 |
p.parent.chmod(0) |
| 2976 |
self.assertEqual(set(p.glob('*')), set()) |
| 2977 |
|
| 2978 |
def test_rglob_pathlike(self): |
| 2979 |
P = self.cls |
| 2980 |
p = P(self.base, "dirC") |
| 2981 |
pattern = "**/file*" |
| 2982 |
expect = {p / "fileC", p / "dirD/fileD"} |
| 2983 |
self.assertEqual(expect, set(p.rglob(P(pattern)))) |
| 2984 |
self.assertEqual(expect, set(p.rglob(FakePath(pattern)))) |
| 2985 |
|
| 2986 |
@needs_symlinks |
| 2987 |
def test_glob_recurse_symlinks_common(self): |
| 2988 |
def _check(path, glob, expected): |
| 2989 |
actual = {path for path in path.glob(glob, recurse_symlinks=True) |
| 2990 |
if path.parts.count("linkD") <= 1} # exclude symlink loop. |
| 2991 |
self.assertEqual(actual, { P(self.base, q) for q in expected }) |
| 2992 |
P = self.cls |
| 2993 |
p = P(self.base) |
| 2994 |
_check(p, "fileB", []) |
| 2995 |
_check(p, "dir*/file*", ["dirB/fileB", "dirC/fileC"]) |
| 2996 |
_check(p, "*A", ["dirA", "fileA", "linkA"]) |
| 2997 |
_check(p, "*B/*", ["dirB/fileB", "dirB/linkD", "linkB/fileB", "linkB/linkD"]) |
| 2998 |
_check(p, "*/fileB", ["dirB/fileB", "linkB/fileB"]) |
| 2999 |
_check(p, "*/", ["dirA/", "dirB/", "dirC/", "dirE/", "linkB/"]) |
| 3000 |
_check(p, "dir*/*/..", ["dirC/dirD/..", "dirA/linkC/..", "dirB/linkD/.."]) |
| 3001 |
_check(p, "dir*/**", [ |
| 3002 |
"dirA/", "dirA/linkC", "dirA/linkC/fileB", "dirA/linkC/linkD", "dirA/linkC/linkD/fileB", |
| 3003 |
"dirB/", "dirB/fileB", "dirB/linkD", "dirB/linkD/fileB", |
| 3004 |
"dirC/", "dirC/fileC", "dirC/dirD", "dirC/dirD/fileD", "dirC/novel.txt", |
| 3005 |
"dirE/"]) |
| 3006 |
_check(p, "dir*/**/", ["dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/", |
| 3007 |
"dirC/", "dirC/dirD/", "dirE/"]) |
| 3008 |
_check(p, "dir*/**/..", ["dirA/..", "dirA/linkC/..", "dirB/..", |
| 3009 |
"dirB/linkD/..", "dirA/linkC/linkD/..", |
| 3010 |
"dirC/..", "dirC/dirD/..", "dirE/.."]) |
| 3011 |
_check(p, "dir*/*/**", [ |
| 3012 |
"dirA/linkC/", "dirA/linkC/linkD", "dirA/linkC/fileB", "dirA/linkC/linkD/fileB", |
| 3013 |
"dirB/linkD/", "dirB/linkD/fileB", |
| 3014 |
"dirC/dirD/", "dirC/dirD/fileD"]) |
| 3015 |
_check(p, "dir*/*/**/", ["dirA/linkC/", "dirA/linkC/linkD/", "dirB/linkD/", "dirC/dirD/"]) |
| 3016 |
_check(p, "dir*/*/**/..", ["dirA/linkC/..", "dirA/linkC/linkD/..", |
| 3017 |
"dirB/linkD/..", "dirC/dirD/.."]) |
| 3018 |
_check(p, "dir*/**/fileC", ["dirC/fileC"]) |
| 3019 |
_check(p, "dir*/*/../dirD/**/", ["dirC/dirD/../dirD/"]) |
| 3020 |
_check(p, "*/dirD/**", ["dirC/dirD/", "dirC/dirD/fileD"]) |
| 3021 |
_check(p, "*/dirD/**/", ["dirC/dirD/"]) |
| 3022 |
|
| 3023 |
@needs_symlinks |
| 3024 |
def test_rglob_recurse_symlinks_common(self): |
| 3025 |
def _check(path, glob, expected): |
| 3026 |
actual = {path for path in path.rglob(glob, recurse_symlinks=True) |
| 3027 |
if path.parts.count("linkD") <= 1} # exclude symlink loop. |
| 3028 |
self.assertEqual(actual, { P(self.base, q) for q in expected }) |
| 3029 |
P = self.cls |
| 3030 |
p = P(self.base) |
| 3031 |
_check(p, "fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB", |
| 3032 |
"dirA/linkC/linkD/fileB", "dirB/linkD/fileB", "linkB/linkD/fileB"]) |
| 3033 |
_check(p, "*/fileA", []) |
| 3034 |
_check(p, "*/fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB", |
| 3035 |
"dirA/linkC/linkD/fileB", "dirB/linkD/fileB", "linkB/linkD/fileB"]) |
| 3036 |
_check(p, "file*", ["fileA", "dirA/linkC/fileB", "dirB/fileB", |
| 3037 |
"dirA/linkC/linkD/fileB", "dirB/linkD/fileB", "linkB/linkD/fileB", |
| 3038 |
"dirC/fileC", "dirC/dirD/fileD", "linkB/fileB"]) |
| 3039 |
_check(p, "*/", ["dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/", |
| 3040 |
"dirC/", "dirC/dirD/", "dirE/", "linkB/", "linkB/linkD/"]) |
| 3041 |
_check(p, "", ["", "dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/", |
| 3042 |
"dirC/", "dirE/", "dirC/dirD/", "linkB/", "linkB/linkD/"]) |
| 3043 |
|
| 3044 |
p = P(self.base, "dirC") |
| 3045 |
_check(p, "*", ["dirC/fileC", "dirC/novel.txt", |
| 3046 |
"dirC/dirD", "dirC/dirD/fileD"]) |
| 3047 |
_check(p, "file*", ["dirC/fileC", "dirC/dirD/fileD"]) |
| 3048 |
_check(p, "*/*", ["dirC/dirD/fileD"]) |
| 3049 |
_check(p, "*/", ["dirC/dirD/"]) |
| 3050 |
_check(p, "", ["dirC/", "dirC/dirD/"]) |
| 3051 |
# gh-91616, a re module regression |
| 3052 |
_check(p, "*.txt", ["dirC/novel.txt"]) |
| 3053 |
_check(p, "*.*", ["dirC/novel.txt"]) |
| 3054 |
|
| 3055 |
def test_rglob_recurse_symlinks_false(self): |
| 3056 |
def _check(path, glob, expected): |
| 3057 |
actual = set(path.rglob(glob, recurse_symlinks=False)) |
| 3058 |
self.assertEqual(actual, { P(self.base, q) for q in expected }) |
| 3059 |
P = self.cls |
| 3060 |
p = P(self.base) |
| 3061 |
it = p.rglob("fileA") |
| 3062 |
self.assertIsInstance(it, collections.abc.Iterator) |
| 3063 |
_check(p, "fileA", ["fileA"]) |
| 3064 |
_check(p, "fileB", ["dirB/fileB"]) |
| 3065 |
_check(p, "**/fileB", ["dirB/fileB"]) |
| 3066 |
_check(p, "*/fileA", []) |
| 3067 |
|
| 3068 |
if self.can_symlink: |
| 3069 |
_check(p, "*/fileB", ["dirB/fileB", "dirB/linkD/fileB", |
| 3070 |
"linkB/fileB", "dirA/linkC/fileB"]) |
| 3071 |
_check(p, "*/", [ |
| 3072 |
"dirA/", "dirA/linkC/", "dirB/", "dirB/linkD/", "dirC/", |
| 3073 |
"dirC/dirD/", "dirE/", "linkB/"]) |
| 3074 |
else: |
| 3075 |
_check(p, "*/fileB", ["dirB/fileB"]) |
| 3076 |
_check(p, "*/", ["dirA/", "dirB/", "dirC/", "dirC/dirD/", "dirE/"]) |
| 3077 |
|
| 3078 |
_check(p, "file*", ["fileA", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD"]) |
| 3079 |
_check(p, "", ["", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"]) |
| 3080 |
p = P(self.base, "dirC") |
| 3081 |
_check(p, "*", ["dirC/fileC", "dirC/novel.txt", |
| 3082 |
"dirC/dirD", "dirC/dirD/fileD"]) |
| 3083 |
_check(p, "file*", ["dirC/fileC", "dirC/dirD/fileD"]) |
| 3084 |
_check(p, "**/file*", ["dirC/fileC", "dirC/dirD/fileD"]) |
| 3085 |
_check(p, "dir*/**", ["dirC/dirD/", "dirC/dirD/fileD"]) |
| 3086 |
_check(p, "dir*/**/", ["dirC/dirD/"]) |
| 3087 |
_check(p, "*/*", ["dirC/dirD/fileD"]) |
| 3088 |
_check(p, "*/", ["dirC/dirD/"]) |
| 3089 |
_check(p, "", ["dirC/", "dirC/dirD/"]) |
| 3090 |
_check(p, "**", ["dirC/", "dirC/fileC", "dirC/dirD", "dirC/dirD/fileD", "dirC/novel.txt"]) |
| 3091 |
_check(p, "**/", ["dirC/", "dirC/dirD/"]) |
| 3092 |
# gh-91616, a re module regression |
| 3093 |
_check(p, "*.txt", ["dirC/novel.txt"]) |
| 3094 |
_check(p, "*.*", ["dirC/novel.txt"]) |
| 3095 |
|
| 3096 |
@needs_posix |
| 3097 |
def test_rglob_posix(self): |
| 3098 |
P = self.cls |
| 3099 |
p = P(self.base, "dirC") |
| 3100 |
q = p / "dirD" / "FILEd" |
| 3101 |
given = set(p.rglob("FILEd")) |
| 3102 |
expect = {q} if q.exists() else set() |
| 3103 |
self.assertEqual(given, expect) |
| 3104 |
self.assertEqual(set(p.rglob("FILEd*")), set()) |
| 3105 |
|
| 3106 |
@needs_windows |
| 3107 |
def test_rglob_windows(self): |
| 3108 |
P = self.cls |
| 3109 |
p = P(self.base, "dirC") |
| 3110 |
self.assertEqual(set(p.rglob("FILEd")), { P(self.base, "dirC/dirD/fileD") }) |
| 3111 |
self.assertEqual(set(p.rglob("*\\")), { P(self.base, "dirC/dirD/") }) |
| 3112 |
|
| 3113 |
@needs_symlinks |
| 3114 |
def test_rglob_symlink_loop(self): |
| 3115 |
# Don't get fooled by symlink loops (Issue #26012). |
| 3116 |
P = self.cls |
| 3117 |
p = P(self.base) |
| 3118 |
given = set(p.rglob('*', recurse_symlinks=False)) |
| 3119 |
expect = {'brokenLink', |
| 3120 |
'dirA', 'dirA/linkC', |
| 3121 |
'dirB', 'dirB/fileB', 'dirB/linkD', |
| 3122 |
'dirC', 'dirC/dirD', 'dirC/dirD/fileD', |
| 3123 |
'dirC/fileC', 'dirC/novel.txt', |
| 3124 |
'dirE', |
| 3125 |
'fileA', |
| 3126 |
'linkA', |
| 3127 |
'linkB', |
| 3128 |
'brokenLinkLoop', |
| 3129 |
} |
| 3130 |
self.assertEqual(given, {p / x for x in expect}) |
| 3131 |
|
| 3132 |
@needs_symlinks |
| 3133 |
def test_glob_permissions(self): |
| 3134 |
# See bpo-38894 |
| 3135 |
P = self.cls |
| 3136 |
base = P(self.base) / 'permissions' |
| 3137 |
base.mkdir() |
| 3138 |
|
| 3139 |
for i in range(100): |
| 3140 |
link = base / f"link{i}" |
| 3141 |
if i % 2: |
| 3142 |
link.symlink_to(P(self.base, "dirE", "nonexistent")) |
| 3143 |
else: |
| 3144 |
link.symlink_to(P(self.base, "dirC"), target_is_directory=True) |
| 3145 |
|
| 3146 |
self.assertEqual(len(set(base.glob("*"))), 100) |
| 3147 |
self.assertEqual(len(set(base.glob("*/"))), 50) |
| 3148 |
self.assertEqual(len(set(base.glob("*/fileC"))), 50) |
| 3149 |
self.assertEqual(len(set(base.glob("*/file*"))), 50) |
| 3150 |
|
| 3151 |
@needs_symlinks |
| 3152 |
def test_glob_long_symlink(self): |
| 3153 |
# See gh-87695 |
| 3154 |
base = self.cls(self.base) / 'long_symlink' |
| 3155 |
base.mkdir() |
| 3156 |
bad_link = base / 'bad_link' |
| 3157 |
bad_link.symlink_to("bad" * 200) |
| 3158 |
self.assertEqual(sorted(base.glob('**/*')), [bad_link]) |
| 3159 |
|
| 3160 |
@needs_posix |
| 3161 |
def test_absolute_posix(self): |
| 3162 |
P = self.cls |
| 3163 |
self.assertEqual(str(P('/').absolute()), '/') |
| 3164 |
self.assertEqual(str(P('/a').absolute()), '/a') |
| 3165 |
self.assertEqual(str(P('/a/b').absolute()), '/a/b') |
| 3166 |
|
| 3167 |
# '//'-prefixed absolute path (supported by POSIX). |
| 3168 |
self.assertEqual(str(P('//').absolute()), '//') |
| 3169 |
self.assertEqual(str(P('//a').absolute()), '//a') |
| 3170 |
self.assertEqual(str(P('//a/b').absolute()), '//a/b') |
| 3171 |
|
| 3172 |
@unittest.skipIf( |
| 3173 |
is_wasm32, |
| 3174 |
"umask is not implemented on Emscripten/WASI." |
| 3175 |
) |
| 3176 |
@needs_posix |
| 3177 |
def test_open_mode(self): |
| 3178 |
# Unmask all permissions except world-write, which may |
| 3179 |
# not be supported on some filesystems (see GH-85633.) |
| 3180 |
old_mask = os.umask(0o002) |
| 3181 |
self.addCleanup(os.umask, old_mask) |
| 3182 |
p = self.cls(self.base) |
| 3183 |
with (p / 'new_file').open('wb'): |
| 3184 |
pass |
| 3185 |
st = os.stat(self.parser.join(self.base, 'new_file')) |
| 3186 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o664) |
| 3187 |
os.umask(0o026) |
| 3188 |
with (p / 'other_new_file').open('wb'): |
| 3189 |
pass |
| 3190 |
st = os.stat(self.parser.join(self.base, 'other_new_file')) |
| 3191 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o640) |
| 3192 |
|
| 3193 |
@needs_posix |
| 3194 |
def test_resolve_root(self): |
| 3195 |
current_directory = os.getcwd() |
| 3196 |
try: |
| 3197 |
os.chdir('/') |
| 3198 |
p = self.cls('spam') |
| 3199 |
self.assertEqual(str(p.resolve()), '/spam') |
| 3200 |
finally: |
| 3201 |
os.chdir(current_directory) |
| 3202 |
|
| 3203 |
@unittest.skipIf( |
| 3204 |
is_wasm32, |
| 3205 |
"umask is not implemented on Emscripten/WASI." |
| 3206 |
) |
| 3207 |
@needs_posix |
| 3208 |
def test_touch_mode(self): |
| 3209 |
# Unmask all permissions except world-write, which may |
| 3210 |
# not be supported on some filesystems (see GH-85633.) |
| 3211 |
old_mask = os.umask(0o002) |
| 3212 |
self.addCleanup(os.umask, old_mask) |
| 3213 |
p = self.cls(self.base) |
| 3214 |
(p / 'new_file').touch() |
| 3215 |
st = os.stat(self.parser.join(self.base, 'new_file')) |
| 3216 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o664) |
| 3217 |
os.umask(0o026) |
| 3218 |
(p / 'other_new_file').touch() |
| 3219 |
st = os.stat(self.parser.join(self.base, 'other_new_file')) |
| 3220 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o640) |
| 3221 |
(p / 'masked_new_file').touch(mode=0o750) |
| 3222 |
st = os.stat(self.parser.join(self.base, 'masked_new_file')) |
| 3223 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o750) |
| 3224 |
|
| 3225 |
@unittest.skipUnless(hasattr(pwd, 'getpwall'), |
| 3226 |
'pwd module does not expose getpwall()') |
| 3227 |
@unittest.skipIf(sys.platform == "vxworks", |
| 3228 |
"no home directory on VxWorks") |
| 3229 |
@needs_posix |
| 3230 |
def test_expanduser_posix(self): |
| 3231 |
P = self.cls |
| 3232 |
import_helper.import_module('pwd') |
| 3233 |
import pwd |
| 3234 |
pwdent = pwd.getpwuid(os.getuid()) |
| 3235 |
username = pwdent.pw_name |
| 3236 |
userhome = pwdent.pw_dir.rstrip('/') or '/' |
| 3237 |
# Find arbitrary different user (if exists). |
| 3238 |
for pwdent in pwd.getpwall(): |
| 3239 |
othername = pwdent.pw_name |
| 3240 |
otherhome = pwdent.pw_dir.rstrip('/') |
| 3241 |
if othername != username and otherhome: |
| 3242 |
break |
| 3243 |
else: |
| 3244 |
othername = username |
| 3245 |
otherhome = userhome |
| 3246 |
|
| 3247 |
fakename = 'fakeuser' |
| 3248 |
# This user can theoretically exist on a test runner. Create unique name: |
| 3249 |
try: |
| 3250 |
while pwd.getpwnam(fakename): |
| 3251 |
fakename += '1' |
| 3252 |
except KeyError: |
| 3253 |
pass # Non-existent name found |
| 3254 |
|
| 3255 |
p1 = P('~/Documents') |
| 3256 |
p2 = P(f'~{username}/Documents') |
| 3257 |
p3 = P(f'~{othername}/Documents') |
| 3258 |
p4 = P(f'../~{username}/Documents') |
| 3259 |
p5 = P(f'/~{username}/Documents') |
| 3260 |
p6 = P('') |
| 3261 |
p7 = P(f'~{fakename}/Documents') |
| 3262 |
|
| 3263 |
with os_helper.EnvironmentVarGuard() as env: |
| 3264 |
env.unset('HOME') |
| 3265 |
|
| 3266 |
self.assertEqual(p1.expanduser(), P(userhome) / 'Documents') |
| 3267 |
self.assertEqual(p2.expanduser(), P(userhome) / 'Documents') |
| 3268 |
self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents') |
| 3269 |
self.assertEqual(p4.expanduser(), p4) |
| 3270 |
self.assertEqual(p5.expanduser(), p5) |
| 3271 |
self.assertEqual(p6.expanduser(), p6) |
| 3272 |
self.assertRaises(RuntimeError, p7.expanduser) |
| 3273 |
|
| 3274 |
env['HOME'] = '/tmp' |
| 3275 |
self.assertEqual(p1.expanduser(), P('/tmp/Documents')) |
| 3276 |
self.assertEqual(p2.expanduser(), P(userhome) / 'Documents') |
| 3277 |
self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents') |
| 3278 |
self.assertEqual(p4.expanduser(), p4) |
| 3279 |
self.assertEqual(p5.expanduser(), p5) |
| 3280 |
self.assertEqual(p6.expanduser(), p6) |
| 3281 |
self.assertRaises(RuntimeError, p7.expanduser) |
| 3282 |
|
| 3283 |
@unittest.skipIf(sys.platform != "darwin", |
| 3284 |
"Bad file descriptor in /dev/fd affects only macOS") |
| 3285 |
@needs_posix |
| 3286 |
def test_handling_bad_descriptor(self): |
| 3287 |
try: |
| 3288 |
file_descriptors = list(pathlib.Path('/dev/fd').rglob("*"))[3:] |
| 3289 |
if not file_descriptors: |
| 3290 |
self.skipTest("no file descriptors - issue was not reproduced") |
| 3291 |
# Checking all file descriptors because there is no guarantee |
| 3292 |
# which one will fail. |
| 3293 |
for f in file_descriptors: |
| 3294 |
f.exists() |
| 3295 |
f.is_dir() |
| 3296 |
f.is_file() |
| 3297 |
f.is_symlink() |
| 3298 |
f.is_block_device() |
| 3299 |
f.is_char_device() |
| 3300 |
f.is_fifo() |
| 3301 |
f.is_socket() |
| 3302 |
except OSError as e: |
| 3303 |
if e.errno == errno.EBADF: |
| 3304 |
self.fail("Bad file descriptor not handled.") |
| 3305 |
raise |
| 3306 |
|
| 3307 |
@needs_posix |
| 3308 |
def test_from_uri_posix(self): |
| 3309 |
P = self.cls |
| 3310 |
self.assertEqual(P.from_uri('file:/foo/bar'), P('/foo/bar')) |
| 3311 |
self.assertRaises(ValueError, P.from_uri, 'file://foo/bar') |
| 3312 |
self.assertEqual(P.from_uri('file:///foo/bar'), P('/foo/bar')) |
| 3313 |
self.assertEqual(P.from_uri('file:////foo/bar'), P('//foo/bar')) |
| 3314 |
self.assertEqual(P.from_uri('file://localhost/foo/bar'), P('/foo/bar')) |
| 3315 |
if not is_wasi: |
| 3316 |
self.assertEqual(P.from_uri(f'file://{socket.gethostname()}/foo/bar'), |
| 3317 |
P('/foo/bar')) |
| 3318 |
self.assertRaises(ValueError, P.from_uri, 'foo/bar') |
| 3319 |
self.assertRaises(ValueError, P.from_uri, '/foo/bar') |
| 3320 |
self.assertRaises(ValueError, P.from_uri, '//foo/bar') |
| 3321 |
self.assertRaises(ValueError, P.from_uri, 'file:foo/bar') |
| 3322 |
self.assertRaises(ValueError, P.from_uri, 'http://foo/bar') |
| 3323 |
|
| 3324 |
@needs_posix |
| 3325 |
def test_from_uri_pathname2url_posix(self): |
| 3326 |
P = self.cls |
| 3327 |
self.assertEqual(P.from_uri(pathname2url('/foo/bar', add_scheme=True)), P('/foo/bar')) |
| 3328 |
self.assertEqual(P.from_uri(pathname2url('//foo/bar', add_scheme=True)), P('//foo/bar')) |
| 3329 |
|
| 3330 |
@needs_windows |
| 3331 |
def test_absolute_windows(self): |
| 3332 |
P = self.cls |
| 3333 |
|
| 3334 |
# Simple absolute paths. |
| 3335 |
self.assertEqual(str(P('c:\\').absolute()), 'c:\\') |
| 3336 |
self.assertEqual(str(P('c:\\a').absolute()), 'c:\\a') |
| 3337 |
self.assertEqual(str(P('c:\\a\\b').absolute()), 'c:\\a\\b') |
| 3338 |
|
| 3339 |
# UNC absolute paths. |
| 3340 |
share = '\\\\server\\share\\' |
| 3341 |
self.assertEqual(str(P(share).absolute()), share) |
| 3342 |
self.assertEqual(str(P(share + 'a').absolute()), share + 'a') |
| 3343 |
self.assertEqual(str(P(share + 'a\\b').absolute()), share + 'a\\b') |
| 3344 |
|
| 3345 |
# UNC relative paths. |
| 3346 |
with mock.patch("os.getcwd") as getcwd: |
| 3347 |
getcwd.return_value = share |
| 3348 |
|
| 3349 |
self.assertEqual(str(P().absolute()), share) |
| 3350 |
self.assertEqual(str(P('.').absolute()), share) |
| 3351 |
self.assertEqual(str(P('a').absolute()), os.path.join(share, 'a')) |
| 3352 |
self.assertEqual(str(P('a', 'b', 'c').absolute()), |
| 3353 |
os.path.join(share, 'a', 'b', 'c')) |
| 3354 |
|
| 3355 |
drive = os.path.splitdrive(self.base)[0] |
| 3356 |
with os_helper.change_cwd(self.base): |
| 3357 |
# Relative path with root |
| 3358 |
self.assertEqual(str(P('\\').absolute()), drive + '\\') |
| 3359 |
self.assertEqual(str(P('\\foo').absolute()), drive + '\\foo') |
| 3360 |
|
| 3361 |
# Relative path on current drive |
| 3362 |
self.assertEqual(str(P(drive).absolute()), self.base) |
| 3363 |
self.assertEqual(str(P(drive + 'foo').absolute()), os.path.join(self.base, 'foo')) |
| 3364 |
|
| 3365 |
with os_helper.subst_drive(self.base) as other_drive: |
| 3366 |
# Set the working directory on the substitute drive |
| 3367 |
saved_cwd = os.getcwd() |
| 3368 |
other_cwd = f'{other_drive}\\dirA' |
| 3369 |
os.chdir(other_cwd) |
| 3370 |
os.chdir(saved_cwd) |
| 3371 |
|
| 3372 |
# Relative path on another drive |
| 3373 |
self.assertEqual(str(P(other_drive).absolute()), other_cwd) |
| 3374 |
self.assertEqual(str(P(other_drive + 'foo').absolute()), other_cwd + '\\foo') |
| 3375 |
|
| 3376 |
@needs_windows |
| 3377 |
def test_expanduser_windows(self): |
| 3378 |
P = self.cls |
| 3379 |
with os_helper.EnvironmentVarGuard() as env: |
| 3380 |
env.unset('HOME', 'USERPROFILE', 'HOMEPATH', 'HOMEDRIVE') |
| 3381 |
env['USERNAME'] = 'alice' |
| 3382 |
|
| 3383 |
# test that the path returns unchanged |
| 3384 |
p1 = P('~/My Documents') |
| 3385 |
p2 = P('~alice/My Documents') |
| 3386 |
p3 = P('~bob/My Documents') |
| 3387 |
p4 = P('/~/My Documents') |
| 3388 |
p5 = P('d:~/My Documents') |
| 3389 |
p6 = P('') |
| 3390 |
self.assertRaises(RuntimeError, p1.expanduser) |
| 3391 |
self.assertRaises(RuntimeError, p2.expanduser) |
| 3392 |
self.assertRaises(RuntimeError, p3.expanduser) |
| 3393 |
self.assertEqual(p4.expanduser(), p4) |
| 3394 |
self.assertEqual(p5.expanduser(), p5) |
| 3395 |
self.assertEqual(p6.expanduser(), p6) |
| 3396 |
|
| 3397 |
def check(): |
| 3398 |
env.pop('USERNAME', None) |
| 3399 |
self.assertEqual(p1.expanduser(), |
| 3400 |
P('C:/Users/alice/My Documents')) |
| 3401 |
self.assertRaises(RuntimeError, p2.expanduser) |
| 3402 |
env['USERNAME'] = 'alice' |
| 3403 |
self.assertEqual(p2.expanduser(), |
| 3404 |
P('C:/Users/alice/My Documents')) |
| 3405 |
self.assertEqual(p3.expanduser(), |
| 3406 |
P('C:/Users/bob/My Documents')) |
| 3407 |
self.assertEqual(p4.expanduser(), p4) |
| 3408 |
self.assertEqual(p5.expanduser(), p5) |
| 3409 |
self.assertEqual(p6.expanduser(), p6) |
| 3410 |
|
| 3411 |
env['HOMEPATH'] = 'C:\\Users\\alice' |
| 3412 |
check() |
| 3413 |
|
| 3414 |
env['HOMEDRIVE'] = 'C:\\' |
| 3415 |
env['HOMEPATH'] = 'Users\\alice' |
| 3416 |
check() |
| 3417 |
|
| 3418 |
env.unset('HOMEDRIVE', 'HOMEPATH') |
| 3419 |
env['USERPROFILE'] = 'C:\\Users\\alice' |
| 3420 |
check() |
| 3421 |
|
| 3422 |
# bpo-38883: ignore `HOME` when set on windows |
| 3423 |
env['HOME'] = 'C:\\Users\\eve' |
| 3424 |
check() |
| 3425 |
|
| 3426 |
@needs_windows |
| 3427 |
def test_from_uri_windows(self): |
| 3428 |
P = self.cls |
| 3429 |
# DOS drive paths |
| 3430 |
self.assertEqual(P.from_uri('file:c:/path/to/file'), P('c:/path/to/file')) |
| 3431 |
self.assertEqual(P.from_uri('file:c|/path/to/file'), P('c:/path/to/file')) |
| 3432 |
self.assertEqual(P.from_uri('file:/c|/path/to/file'), P('c:/path/to/file')) |
| 3433 |
self.assertEqual(P.from_uri('file:///c|/path/to/file'), P('c:/path/to/file')) |
| 3434 |
# UNC paths |
| 3435 |
self.assertEqual(P.from_uri('file://server/path/to/file'), P('//server/path/to/file')) |
| 3436 |
self.assertEqual(P.from_uri('file:////server/path/to/file'), P('//server/path/to/file')) |
| 3437 |
self.assertEqual(P.from_uri('file://///server/path/to/file'), P('//server/path/to/file')) |
| 3438 |
# Localhost paths |
| 3439 |
self.assertEqual(P.from_uri('file://localhost/c:/path/to/file'), P('c:/path/to/file')) |
| 3440 |
self.assertEqual(P.from_uri('file://localhost/c|/path/to/file'), P('c:/path/to/file')) |
| 3441 |
# Invalid paths |
| 3442 |
self.assertRaises(ValueError, P.from_uri, 'foo/bar') |
| 3443 |
self.assertRaises(ValueError, P.from_uri, 'c:/foo/bar') |
| 3444 |
self.assertRaises(ValueError, P.from_uri, '//foo/bar') |
| 3445 |
self.assertRaises(ValueError, P.from_uri, 'file:foo/bar') |
| 3446 |
self.assertRaises(ValueError, P.from_uri, 'http://foo/bar') |
| 3447 |
|
| 3448 |
@needs_windows |
| 3449 |
def test_from_uri_pathname2url_windows(self): |
| 3450 |
P = self.cls |
| 3451 |
self.assertEqual(P.from_uri('file:' + pathname2url(r'c:\path\to\file')), P('c:/path/to/file')) |
| 3452 |
self.assertEqual(P.from_uri('file:' + pathname2url(r'\\server\path\to\file')), P('//server/path/to/file')) |
| 3453 |
|
| 3454 |
@needs_windows |
| 3455 |
def test_owner_windows(self): |
| 3456 |
P = self.cls |
| 3457 |
with self.assertRaises(pathlib.UnsupportedOperation): |
| 3458 |
P('c:/').owner() |
| 3459 |
|
| 3460 |
@needs_windows |
| 3461 |
def test_group_windows(self): |
| 3462 |
P = self.cls |
| 3463 |
with self.assertRaises(pathlib.UnsupportedOperation): |
| 3464 |
P('c:/').group() |
| 3465 |
|
| 3466 |
|
| 3467 |
class PathWalkTest(unittest.TestCase): |
| 3468 |
cls = pathlib.Path |
| 3469 |
base = PathTest.base |
| 3470 |
can_symlink = PathTest.can_symlink |
| 3471 |
|
| 3472 |
def setUp(self): |
| 3473 |
name = self.id().split('.')[-1] |
| 3474 |
if name in _tests_needing_symlinks and not self.can_symlink: |
| 3475 |
self.skipTest('requires symlinks') |
| 3476 |
self.walk_path = self.cls(self.base, "TEST1") |
| 3477 |
self.sub1_path = self.walk_path / "SUB1" |
| 3478 |
self.sub11_path = self.sub1_path / "SUB11" |
| 3479 |
self.sub2_path = self.walk_path / "SUB2" |
| 3480 |
self.link_path = self.sub2_path / "link" |
| 3481 |
self.sub2_tree = (self.sub2_path, [], ["tmp3"]) |
| 3482 |
|
| 3483 |
# Build: |
| 3484 |
# TESTFN/ |
| 3485 |
# TEST1/ a file kid and two directory kids |
| 3486 |
# tmp1 |
| 3487 |
# SUB1/ a file kid and a directory kid |
| 3488 |
# tmp2 |
| 3489 |
# SUB11/ no kids |
| 3490 |
# SUB2/ a file kid and a dirsymlink kid |
| 3491 |
# tmp3 |
| 3492 |
# link/ a symlink to TEST2 |
| 3493 |
# broken_link |
| 3494 |
# broken_link2 |
| 3495 |
# TEST2/ |
| 3496 |
# tmp4 a lone file |
| 3497 |
t2_path = self.cls(self.base, "TEST2") |
| 3498 |
os.makedirs(self.sub11_path) |
| 3499 |
os.makedirs(self.sub2_path) |
| 3500 |
os.makedirs(t2_path) |
| 3501 |
|
| 3502 |
tmp1_path = self.walk_path / "tmp1" |
| 3503 |
tmp2_path = self.sub1_path / "tmp2" |
| 3504 |
tmp3_path = self.sub2_path / "tmp3" |
| 3505 |
tmp4_path = self.cls(self.base, "TEST2", "tmp4") |
| 3506 |
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
| 3507 |
with open(path, "w", encoding='utf-8') as f: |
| 3508 |
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n") |
| 3509 |
|
| 3510 |
if self.can_symlink: |
| 3511 |
broken_link_path = self.sub2_path / "broken_link" |
| 3512 |
broken_link2_path = self.sub2_path / "broken_link2" |
| 3513 |
os.symlink(t2_path, self.link_path, target_is_directory=True) |
| 3514 |
os.symlink('broken', broken_link_path) |
| 3515 |
os.symlink(os.path.join('tmp3', 'broken'), broken_link2_path) |
| 3516 |
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"]) |
| 3517 |
sub21_path= self.sub2_path / "SUB21" |
| 3518 |
tmp5_path = sub21_path / "tmp3" |
| 3519 |
broken_link3_path = self.sub2_path / "broken_link3" |
| 3520 |
|
| 3521 |
os.makedirs(sub21_path) |
| 3522 |
tmp5_path.write_text("I am tmp5, blame test_pathlib.") |
| 3523 |
if self.can_symlink: |
| 3524 |
os.symlink(tmp5_path, broken_link3_path) |
| 3525 |
self.sub2_tree[2].append('broken_link3') |
| 3526 |
self.sub2_tree[2].sort() |
| 3527 |
os.chmod(sub21_path, 0) |
| 3528 |
try: |
| 3529 |
os.listdir(sub21_path) |
| 3530 |
except PermissionError: |
| 3531 |
self.sub2_tree[1].append('SUB21') |
| 3532 |
else: |
| 3533 |
os.chmod(sub21_path, stat.S_IRWXU) |
| 3534 |
os.unlink(tmp5_path) |
| 3535 |
os.rmdir(sub21_path) |
| 3536 |
|
| 3537 |
def tearDown(self): |
| 3538 |
if 'SUB21' in self.sub2_tree[1]: |
| 3539 |
os.chmod(self.sub2_path / "SUB21", stat.S_IRWXU) |
| 3540 |
os_helper.rmtree(self.base) |
| 3541 |
|
| 3542 |
def test_walk_bad_dir(self): |
| 3543 |
errors = [] |
| 3544 |
walk_it = self.walk_path.walk(on_error=errors.append) |
| 3545 |
root, dirs, files = next(walk_it) |
| 3546 |
self.assertEqual(errors, []) |
| 3547 |
dir1 = 'SUB1' |
| 3548 |
path1 = root / dir1 |
| 3549 |
path1new = (root / dir1).with_suffix(".new") |
| 3550 |
path1.rename(path1new) |
| 3551 |
try: |
| 3552 |
roots = [r for r, _, _ in walk_it] |
| 3553 |
self.assertTrue(errors) |
| 3554 |
self.assertNotIn(path1, roots) |
| 3555 |
self.assertNotIn(path1new, roots) |
| 3556 |
for dir2 in dirs: |
| 3557 |
if dir2 != dir1: |
| 3558 |
self.assertIn(root / dir2, roots) |
| 3559 |
finally: |
| 3560 |
path1new.rename(path1) |
| 3561 |
|
| 3562 |
def test_walk_many_open_files(self): |
| 3563 |
depth = 30 |
| 3564 |
base = self.cls(self.base, 'deep') |
| 3565 |
path = self.cls(base, *(['d']*depth)) |
| 3566 |
path.mkdir(parents=True) |
| 3567 |
|
| 3568 |
iters = [base.walk(top_down=False) for _ in range(100)] |
| 3569 |
for i in range(depth + 1): |
| 3570 |
expected = (path, ['d'] if i else [], []) |
| 3571 |
for it in iters: |
| 3572 |
self.assertEqual(next(it), expected) |
| 3573 |
path = path.parent |
| 3574 |
|
| 3575 |
iters = [base.walk(top_down=True) for _ in range(100)] |
| 3576 |
path = base |
| 3577 |
for i in range(depth + 1): |
| 3578 |
expected = (path, ['d'] if i < depth else [], []) |
| 3579 |
for it in iters: |
| 3580 |
self.assertEqual(next(it), expected) |
| 3581 |
path = path / 'd' |
| 3582 |
|
| 3583 |
def test_walk_above_recursion_limit(self): |
| 3584 |
recursion_limit = 40 |
| 3585 |
# directory_depth > recursion_limit |
| 3586 |
directory_depth = recursion_limit + 10 |
| 3587 |
base = self.cls(self.base, 'deep') |
| 3588 |
path = base.joinpath(*(['d'] * directory_depth)) |
| 3589 |
path.mkdir(parents=True) |
| 3590 |
|
| 3591 |
with infinite_recursion(recursion_limit): |
| 3592 |
list(base.walk()) |
| 3593 |
list(base.walk(top_down=False)) |
| 3594 |
|
| 3595 |
@needs_symlinks |
| 3596 |
def test_walk_follow_symlinks(self): |
| 3597 |
walk_it = self.walk_path.walk(follow_symlinks=True) |
| 3598 |
for root, dirs, files in walk_it: |
| 3599 |
if root == self.link_path: |
| 3600 |
self.assertEqual(dirs, []) |
| 3601 |
self.assertEqual(files, ["tmp4"]) |
| 3602 |
break |
| 3603 |
else: |
| 3604 |
self.fail("Didn't follow symlink with follow_symlinks=True") |
| 3605 |
|
| 3606 |
@needs_symlinks |
| 3607 |
def test_walk_symlink_location(self): |
| 3608 |
# Tests whether symlinks end up in filenames or dirnames depending |
| 3609 |
# on the `follow_symlinks` argument. |
| 3610 |
walk_it = self.walk_path.walk(follow_symlinks=False) |
| 3611 |
for root, dirs, files in walk_it: |
| 3612 |
if root == self.sub2_path: |
| 3613 |
self.assertIn("link", files) |
| 3614 |
break |
| 3615 |
else: |
| 3616 |
self.fail("symlink not found") |
| 3617 |
|
| 3618 |
walk_it = self.walk_path.walk(follow_symlinks=True) |
| 3619 |
for root, dirs, files in walk_it: |
| 3620 |
if root == self.sub2_path: |
| 3621 |
self.assertIn("link", dirs) |
| 3622 |
break |
| 3623 |
else: |
| 3624 |
self.fail("symlink not found") |
| 3625 |
|
| 3626 |
|
| 3627 |
@unittest.skipIf(os.name == 'nt', 'test requires a POSIX-compatible system') |
| 3628 |
class PosixPathTest(PathTest, PurePosixPathTest): |
| 3629 |
cls = pathlib.PosixPath |
| 3630 |
|
| 3631 |
|
| 3632 |
@unittest.skipIf(os.name != 'nt', 'test requires a Windows-compatible system') |
| 3633 |
class WindowsPathTest(PathTest, PureWindowsPathTest): |
| 3634 |
cls = pathlib.WindowsPath |
| 3635 |
|
| 3636 |
|
| 3637 |
class PathSubclassTest(PathTest): |
| 3638 |
class cls(pathlib.Path): |
| 3639 |
pass |
| 3640 |
|
| 3641 |
# repr() roundtripping is not supported in custom subclass. |
| 3642 |
test_repr_roundtrips = None |
| 3643 |
|
| 3644 |
|
| 3645 |
class CompatiblePathTest(unittest.TestCase): |
| 3646 |
""" |
| 3647 |
Test that a type can be made compatible with PurePath |
| 3648 |
derivatives by implementing division operator overloads. |
| 3649 |
""" |
| 3650 |
|
| 3651 |
class CompatPath: |
| 3652 |
""" |
| 3653 |
Minimum viable class to test PurePath compatibility. |
| 3654 |
Simply uses the division operator to join a given |
| 3655 |
string and the string value of another object with |
| 3656 |
a forward slash. |
| 3657 |
""" |
| 3658 |
def __init__(self, string): |
| 3659 |
self.string = string |
| 3660 |
|
| 3661 |
def __truediv__(self, other): |
| 3662 |
return type(self)(f"{self.string}/{other}") |
| 3663 |
|
| 3664 |
def __rtruediv__(self, other): |
| 3665 |
return type(self)(f"{other}/{self.string}") |
| 3666 |
|
| 3667 |
def test_truediv(self): |
| 3668 |
result = pathlib.PurePath("test") / self.CompatPath("right") |
| 3669 |
self.assertIsInstance(result, self.CompatPath) |
| 3670 |
self.assertEqual(result.string, "test/right") |
| 3671 |
|
| 3672 |
with self.assertRaises(TypeError): |
| 3673 |
# Verify improper operations still raise a TypeError |
| 3674 |
pathlib.PurePath("test") / 10 |
| 3675 |
|
| 3676 |
def test_rtruediv(self): |
| 3677 |
result = self.CompatPath("left") / pathlib.PurePath("test") |
| 3678 |
self.assertIsInstance(result, self.CompatPath) |
| 3679 |
self.assertEqual(result.string, "left/test") |
| 3680 |
|
| 3681 |
with self.assertRaises(TypeError): |
| 3682 |
# Verify improper operations still raise a TypeError |
| 3683 |
10 / pathlib.PurePath("test") |
| 3684 |
|
| 3685 |
|
| 3686 |
if __name__ == "__main__": |
| 3687 |
unittest.main() |