1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
import ubelt as ub
DEBUG_PATH = 0
# ub.Path.home().name == 'joncrall'
def _demo_directory_structure():
import ubelt as ub
import uuid
level = 0
suffix = ub.hash_data(uuid.uuid4())[0:8]
dpath = ub.Path.appdir('ubelt', 'tests', 'test_path')
base = (dpath / suffix).delete().ensuredir()
(base / 'root' / 'dir_L0_X0_A').ensuredir()
if level > 2:
(base / 'root' / 'dir_L0_X0_A' / 'dir_L1_X0_B').ensuredir()
if level > 1:
(base / 'root' / 'dir_L0_X1_C').ensuredir()
(base / 'root' / 'inside_dir').ensuredir()
(base / 'root' / 'links').ensuredir()
(base / 'outside_dir').ensuredir()
(base / 'root' / 'file_L0_X0_a.txt').touch()
if level > 1:
(base / 'root' / 'dir_L0_X0_A' / 'file_L1_X0_b.txt').touch()
if level > 1:
(base / 'root' / 'dir_L0_X1_C' / 'file_L1_X0_c.txt').touch()
(base / 'root' / 'inside_dir' / 'inside_file.txt').touch()
(base / 'outside_dir' / 'outside_file.txt').touch()
# Create links inside and outside the root
to_abs_symlink = []
to_abs_symlink.append((base / 'root/inside_dir/inside_file.txt' , base / 'root/links/inside_flink.txt'))
to_abs_symlink.append((base / 'outside_dir/outside_file.txt' , base / 'root/links/outside_flink.txt'))
to_abs_symlink.append((base / 'outside_dir' , base / 'root/links/outside_dlink'))
to_abs_symlink.append((base / 'root/inside_dir' , base / 'root/links/inside_dlink'))
to_abs_symlink.append((base / 'root/links/cyclic' , (base / 'root/links/cyclic/n1/n2').ensuredir() / 'loop'))
to_rel_symlink = []
to_rel_symlink.append((base / 'root/inside_dir/inside_file.txt' , base / 'root/links/rel_inside_flink.txt'))
to_rel_symlink.append((base / 'outside_dir/outside_file.txt' , base / 'root/links/rel_outside_flink.txt'))
to_rel_symlink.append((base / 'outside_dir' , base / 'root/links/rel_outside_dlink'))
to_rel_symlink.append((base / 'root/inside_dir' , base / 'root/links/rel_inside_dlink'))
to_rel_symlink.append((base / 'root/links/rel_cyclic' , (base / 'root/links/rel_cyclic/n1/n2/').ensuredir() / 'rel_loop'))
try:
# TODO: the implementation of ubelt.symlink might be wrong when the
# link target is relative.
import os
for real, link in to_abs_symlink:
link.symlink_to(real)
# ub.symlink(real, link, verbose=1)
for real, link in to_rel_symlink:
rel_real = os.path.relpath(real, link.parent)
link.symlink_to(rel_real)
# ub.symlink(rel_real, link, verbose=1)
except Exception:
import pytest
pytest.skip('unable to symlink')
if 0:
import xdev
xdev.tree_repr(base)
return base
### MOVE TESTS
def test_move_dir_to_non_existing():
base = _demo_directory_structure()
root = base / 'root'
if ub.LINUX:
root2 = root.copy(root.augment(tail='2'))
root3 = root.copy(root.augment(tail='3'))
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
root.move(base / 'our_move')
if ub.LINUX:
ub.cmd(f'mv {root2} {base}/linux_move', verbose=2, check=1)
ub.cmd(f'mv -T {root3} {base}/linux_moveT', verbose=2, check=1)
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
if ub.LINUX:
# We behave like Linux mv here in both cases here
case1 = _comparable_walk(base / 'linux_move')
case2 = _comparable_walk(base / 'linux_moveT')
case3 = _comparable_walk(base / 'our_move')
assert case1 == case2 == case3
base.delete()
def test_move_to_nested_non_existing():
base = _demo_directory_structure()
root = base / 'root'
import platform
if ub.WIN32 and platform.python_implementation() == 'PyPy':
ub.util_path._patch_win32_stats_on_pypy()
if ub.LINUX:
root2 = root.copy(root.augment(tail='2'))
root3 = root.copy(root.augment(tail='3'))
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
# shutil move will make the parent directory if it doesn't exist.
root.move(base / 'our/move')
if ub.LINUX:
# Posix fails unless the parent exists
(base / 'linux').ensuredir()
ub.cmd(f'mv -v {root2} {base}/linux/move', verbose=2, check=1)
ub.cmd(f'mv -Tv {root3} {base}/linux/moveT', verbose=2, check=1)
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
if ub.LINUX:
# We behave like Linux mv here in both cases here
# up to the fact that we will always create the dir, whereas mv wont
case1 = _comparable_walk(base / 'linux/move')
case2 = _comparable_walk(base / 'linux/moveT')
case3 = _comparable_walk(base / 'our/move')
assert case1 == case2 == case3
base.delete()
def test_move_dir_to_existing_dir_noconflict():
base = _demo_directory_structure()
root = base / 'root'
(base / 'our_move').ensuredir()
if ub.LINUX:
root2 = root.copy(root.augment(tail='2'))
root3 = root.copy(root.augment(tail='3'))
(base / 'linux_move').ensuredir()
(base / 'linux_moveT').ensuredir()
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
import pytest
with pytest.raises(IOError):
# shutil.move behaves similar to linux with -T
# We are just going to disallow this case
root.move(base / 'our_move')
if ub.LINUX:
ub.cmd(f'mv {root2} {base}/linux_move', verbose=2, check=1)
ub.cmd(f'mv -T {root3} {base}/linux_moveT', verbose=2, check=1)
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
base.delete()
def test_move_dir_to_existing_dir_withconflict():
base = _demo_directory_structure()
root = base / 'root'
bluntobject = (root / 'will_they_wont_they.txt')
bluntobject.write_text('smash!')
if ub.LINUX:
root2 = root.copy(root.augment(tail='2'))
root3 = root.copy(root.augment(tail='3')) # NOQA
dst1 = (base / 'our_move').ensuredir()
dst2 = (base / 'linux_move').ensuredir()
dst3 = (base / 'linux_move_T').ensuredir()
toclobber1 = (dst1 / 'will_they_wont_they.txt')
toclobber1.write_text('I hope nobody clobbers me!')
disjoint1 = (dst1 / 'disjoint.txt')
disjoint1.write_text('I should be disjoint!')
toclobber2 = (dst2 / 'will_they_wont_they.txt')
toclobber2.write_text('I hope nobody clobbers me!')
disjoint2 = (dst2 / 'disjoint.txt')
disjoint2.write_text('I should be disjoint!')
toclobber3 = (dst3 / 'will_they_wont_they.txt')
toclobber3.write_text('I hope nobody clobbers me!')
disjoint3 = (dst3 / 'disjoint.txt')
disjoint3.write_text('I should be disjoint!')
if DEBUG_PATH:
import xdev
print('BEFORE MOVE')
xdev.tree_repr(base)
# This case is weird, dont let the user do it.
# they can use shutil if they want
import pytest
with pytest.raises(IOError):
root.move(dst1)
if 0:
if ub.LINUX:
ub.cmd(f'mv -v {root2} {dst2}', verbose=2, check=1)
# The mv command wont move a non-empty directory!
# Maybe we shouldn't either.
# ub.cmd(f'mv -T -u -f -v {root3} {dst3}', verbose=3, check=1)
if DEBUG_PATH:
import xdev
print('AFTER MOVE')
xdev.tree_repr(base)
got = toclobber1.read_text()
# THIS IS VERY SURPRISING, the file being moved is clobbered, but the file
# in the dst is safe!
assert got != 'smash!'
assert not bluntobject.exists()
if ub.LINUX:
got2 = toclobber3.read_text()
assert got2 == 'smash!'
assert toclobber1.exists()
assert bluntobject.exists()
assert disjoint1.exists()
assert disjoint1.read_text() == 'I should be disjoint!'
if ub.LINUX:
assert disjoint2.exists()
assert disjoint2.read_text() == 'I should be disjoint!'
assert disjoint3.exists()
assert disjoint3.read_text() == 'I should be disjoint!'
base.delete()
### Simple Copy Tests
def test_copy_basic():
dpath = ub.Path.appdir('ubelt', 'tests', 'test_path', 'test_copy_basic')
dpath.delete().ensuredir()
fpath = (dpath / 'file.txt')
fpath.write_text('foobar')
empty_dpath = (dpath / 'empty_dir').ensuredir()
full_dpath = (dpath / 'full_dir').ensuredir()
(full_dpath / 'nested_file.txt').touch()
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(dpath)
fpath.copy(fpath.augment(prefix='copied_'))
empty_dpath.copy(empty_dpath.augment(prefix='copied_'))
full_dpath.copy(full_dpath.augment(prefix='copied_'))
# Doing it again will fail
import pytest
with pytest.raises(IOError):
fpath.copy(fpath.augment(prefix='copied_'))
with pytest.raises(IOError):
empty_dpath.copy(empty_dpath.augment(prefix='copied_'))
with pytest.raises(IOError):
full_dpath.copy(full_dpath.augment(prefix='copied_'))
# But with overwrite=True it is ok
fpath.copy(fpath.augment(prefix='copied_'), overwrite=True)
empty_dpath.copy(empty_dpath.augment(prefix='copied_'), overwrite=True)
full_dpath.copy(full_dpath.augment(prefix='copied_'), overwrite=True)
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(dpath)
def test_copy_meta():
dpath = ub.Path.appdir('ubelt', 'tests', 'test_path', 'test_copy_basic')
dpath.delete().ensuredir()
fpath = (dpath / 'file.txt')
fpath.write_text('foobar')
empty_dpath = (dpath / 'empty_dir').ensuredir()
full_dpath = (dpath / 'full_dir').ensuredir()
(full_dpath / 'nested_file.txt').touch()
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(dpath)
for meta in ['stats', 'mode', None]:
prefix = 'copied_' + str(meta) + '_'
fpath.copy(fpath.augment(prefix=prefix), meta=meta)
empty_dpath.copy(empty_dpath.augment(prefix=prefix))
full_dpath.copy(full_dpath.augment(prefix=prefix))
# TODO: verify that the metadata really did copy as intended
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(dpath)
### Simple Move Tests
def test_move_basic():
dpath = ub.Path.appdir('ubelt', 'tests', 'test_path', 'test_move_basic')
dpath.delete().ensuredir()
fpath = (dpath / 'file.txt')
fpath.write_text('foobar')
empty_dpath = (dpath / 'empty_dir').ensuredir()
full_dpath = (dpath / 'full_dir').ensuredir()
(full_dpath / 'nested_file.txt').touch()
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(dpath)
fpath.move(fpath.augment(prefix='moved_'))
empty_dpath.move(empty_dpath.augment(prefix='moved_'))
full_dpath.move(full_dpath.augment(prefix='moved_'))
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(dpath)
def test_move_meta():
base_dpath = ub.Path.appdir('ubelt', 'tests', 'test_path', 'test_move_basic')
base_dpath.delete().ensuredir()
for meta in ['stats', 'mode', None]:
prefix = 'copied_' + str(meta) + '_'
dpath = (base_dpath / prefix).ensuredir()
fpath = (dpath / 'file.txt')
fpath.write_text('foobar')
empty_dpath = (dpath / 'empty_dir').ensuredir()
full_dpath = (dpath / 'full_dir').ensuredir()
(full_dpath / 'nested_file.txt').touch()
fpath.move(fpath.augment(prefix=prefix), meta=meta)
empty_dpath.move(empty_dpath.augment(prefix=prefix))
full_dpath.move(full_dpath.augment(prefix=prefix))
# TODO: test that the metadata really did move as intended
if DEBUG_PATH:
print('AFTER MOVE')
import xdev
xdev.tree_repr(base_dpath)
### COPY TESTS
def test_copy_dir_to_non_existing():
base = _demo_directory_structure()
root = base / 'root'
if DEBUG_PATH:
print('BEFORE COPY')
import xdev
xdev.tree_repr(base)
dst = root.copy(base / 'our_copy')
if ub.LINUX:
ub.cmd(f'cp -r {root} {base}/linux_copy', verbose=2)
print(f'dst={dst}')
if DEBUG_PATH:
print('AFTER COPY')
import xdev
xdev.tree_repr(base)
if ub.LINUX:
# Our copy should behave like the linux copy
case1 = _comparable_walk(base / 'our_copy')
case2 = _comparable_walk(base / 'linux_copy')
print('case1 = {}'.format(ub.urepr(case1, nl=1)))
print('case2 = {}'.format(ub.urepr(case2, nl=1)))
assert case1 == case2
base.delete()
def test_copy_to_nested_non_existing_with_different_symlink_flags():
base = _demo_directory_structure()
root = base / 'root'
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
root.copy(base / 'new_subdir' / 'new_root_FD0_FF1', follow_dir_symlinks=False, follow_file_symlinks=True)
root.copy(base / 'new_subdir' / 'new_root_FD0_FF0', follow_dir_symlinks=False, follow_file_symlinks=False)
(root / 'links' / 'cyclic').delete()
(root / 'links' / 'rel_cyclic').delete()
root.copy(base / 'new_subdir' / 'new_root_FD1_FF1', follow_dir_symlinks=True, follow_file_symlinks=True)
root.copy(base / 'new_subdir' / 'new_root_FD1_FF0', follow_dir_symlinks=True, follow_file_symlinks=False)
if DEBUG_PATH:
import xdev
xdev.tree_repr(base)
base.delete()
def test_copy_dir_to_existing_dir_noconflict():
base = _demo_directory_structure()
root = base / 'root'
(root / 'links' / 'cyclic').delete()
(root / 'links' / 'rel_cyclic').delete()
dst1 = (base / 'our_copy').ensuredir()
dst2 = (base / 'linux_copy').ensuredir()
dst3 = (base / 'linux_copyT').ensuredir()
if DEBUG_PATH:
import xdev
print('BEFORE MOVE')
xdev.tree_repr(base)
root.copy(dst1, overwrite=True)
if ub.LINUX:
# We behave like linux copy with T here.
ub.cmd(f'cp -r {root} {dst2}', verbose=2)
ub.cmd(f'cp -r -T {root} {dst3}', verbose=2)
if DEBUG_PATH:
import xdev
print('AFTER MOVE')
xdev.tree_repr(base)
if ub.LINUX:
# Our copy should behave like the linux copy
case1 = _comparable_walk(base / 'our_copy')
case2 = _comparable_walk(base / 'linux_copy')
case3 = _comparable_walk(base / 'linux_copyT')
assert case1 == case3
assert case1 != case2
base.delete()
def test_copy_dir_to_existing_dir_withconflict():
base = _demo_directory_structure()
root = base / 'root'
bluntobject = (root / 'will_they_wont_they.txt')
bluntobject.write_text('smash!')
dst1 = (base / 'our_copy').ensuredir()
dst2 = (base / 'linux_copy').ensuredir()
dst3 = (base / 'linux_copyT').ensuredir()
toclobber1 = (dst1 / 'will_they_wont_they.txt')
toclobber1.write_text('I hope nobody clobbers me!')
disjoint1 = (dst1 / 'disjoint.txt')
disjoint1.write_text('I should be disjoint!')
toclobber2 = (dst2 / 'will_they_wont_they.txt')
toclobber2.write_text('I hope nobody clobbers me!')
disjoint2 = (dst2 / 'disjoint.txt')
disjoint2.write_text('I should be disjoint!')
toclobber3 = (dst3 / 'will_they_wont_they.txt')
toclobber3.write_text('I hope nobody clobbers me!')
disjoint3 = (dst3 / 'disjoint.txt')
disjoint3.write_text('I should be disjoint!')
if DEBUG_PATH:
import xdev
print('BEFORE MOVE')
xdev.tree_repr(base)
root.copy(dst1, overwrite=True)
if ub.LINUX:
ub.cmd(f'cp -r {root} {dst2}', verbose=2, check=1)
ub.cmd(f'cp -r -T {root} {dst3}', verbose=2, check=1)
if DEBUG_PATH:
import xdev
print('AFTER MOVE')
xdev.tree_repr(base)
# This behavior makes more sense to me
got = toclobber1.read_text()
assert got == 'smash!'
if ub.LINUX:
got2 = toclobber3.read_text()
assert got2 == 'smash!'
assert toclobber1.exists()
assert bluntobject.exists()
assert disjoint1.exists()
assert disjoint1.read_text() == 'I should be disjoint!'
if ub.LINUX:
assert disjoint2.exists()
assert disjoint2.read_text() == 'I should be disjoint!'
assert disjoint3.exists()
assert disjoint3.read_text() == 'I should be disjoint!'
if ub.LINUX:
# Our copy should behave like the linux copy
case1 = _comparable_walk(base / 'our_copy')
case2 = _comparable_walk(base / 'linux_copy')
case3 = _comparable_walk(base / 'linux_copyT')
print('case1 = {}'.format(ub.urepr(case1, nl=1)))
print('case3 = {}'.format(ub.urepr(case3, nl=1)))
print('case2 = {}'.format(ub.urepr(case2, nl=1)))
assert case1 == case3
assert case1 != case2
base.delete()
def _comparable_walk(p):
return sorted([(tuple(sorted(f)), tuple(sorted(d))) for (r, f, d) in (p).walk()])
def test_walk_compat_312():
"""
Test that our version works the same as the 3.12 version
"""
import sys
if sys.version_info[0:2] < (3, 12):
import pytest
pytest.skip('only test on 3.12')
import ubelt as ub
import pathlib
ours = ub.Path.appdir('ubelt/tests/ls')
theirs = pathlib.Path(ours)
(ours / 'dir1').ensuredir()
(ours / 'dir2').ensuredir()
(ours / 'file1').touch()
(ours / 'file2').touch()
(ours / 'dir1/file3').touch()
(ours / 'dir2/file4').touch()
subdirs1 = list(ours.walk())
assert len(subdirs1) == 3
subdirs2 = list(theirs.walk())
assert subdirs1 == subdirs2
def test_walk_bad_kwargs():
import ubelt as ub
import pytest
self = ub.Path('foo')
with pytest.raises(TypeError):
list(self.walk(does_not_exist=True))
|