| 1 |
|
| 2 |
"""Coverage of -u (--update) and --force at depth. |
| 3 |
|
| 4 |
-u skips any destination file that is newer than the source. --force lets rsync |
| 5 |
delete a non-empty destination directory when it must be replaced by a |
| 6 |
non-directory. Both decide a per-entry action on a name whose parent chain the |
| 7 |
resolver restructure rewrites, so check them several levels deep. |
| 8 |
""" |
| 9 |
|
| 10 |
import os |
| 11 |
|
| 12 |
from rsyncfns import ( |
| 13 |
FROMDIR, TODIR, |
| 14 |
assert_same, make_tree, makepath, rmtree, run_rsync, test_fail, |
| 15 |
) |
| 16 |
|
| 17 |
src = FROMDIR |
| 18 |
deep = os.path.join('d1', 'd2', 'd3', 'f3') |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
rmtree(src) |
| 23 |
rmtree(TODIR) |
| 24 |
make_tree(src, depth=3) |
| 25 |
run_rsync('-a', f'{src}/', f'{TODIR}/') |
| 26 |
|
| 27 |
|
| 28 |
(src / deep).write_text("new source content\n") |
| 29 |
keep = "destination is newer - keep me\n" |
| 30 |
(TODIR / deep).write_text(keep) |
| 31 |
st = os.stat(src / deep) |
| 32 |
os.utime(TODIR / deep, (st.st_atime, st.st_mtime + 100)) |
| 33 |
|
| 34 |
run_rsync('-a', '-u', f'{src}/', f'{TODIR}/') |
| 35 |
if (TODIR / deep).read_text() != keep: |
| 36 |
test_fail("-u overwrote a destination file that was newer than the source") |
| 37 |
|
| 38 |
|
| 39 |
os.utime(TODIR / deep, (st.st_atime, st.st_mtime - 100)) |
| 40 |
run_rsync('-a', '-u', f'{src}/', f'{TODIR}/') |
| 41 |
assert_same(TODIR / deep, src / deep, label='-u updated an older dest file') |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
rmtree(src) |
| 46 |
rmtree(TODIR) |
| 47 |
makepath(src, TODIR) |
| 48 |
(src / 'foo').write_text("regular source file\n") |
| 49 |
os.symlink('/should/not/exist', TODIR / 'foo') |
| 50 |
st = os.stat(src / 'foo') |
| 51 |
os.utime(TODIR / 'foo', (st.st_atime, st.st_mtime + 100), |
| 52 |
follow_symlinks=False) |
| 53 |
run_rsync('-a', '-u', f'{src}/', f'{TODIR}/') |
| 54 |
if os.path.islink(TODIR / 'foo'): |
| 55 |
test_fail("-u skipped a source file over a newer destination symlink") |
| 56 |
assert_same(TODIR / 'foo', src / 'foo', |
| 57 |
label='-u replaced a newer dest symlink with a regular file') |
| 58 |
|
| 59 |
|
| 60 |
rmtree(src) |
| 61 |
rmtree(TODIR) |
| 62 |
makepath(src / 'd1' / 'd2' / 'd3') |
| 63 |
(src / deep).write_text("now a regular file\n") |
| 64 |
makepath(TODIR / 'd1' / 'd2' / 'd3' / 'f3') |
| 65 |
(TODIR / 'd1' / 'd2' / 'd3' / 'f3' / 'occupant').write_text("blocker\n") |
| 66 |
|
| 67 |
|
| 68 |
proc = run_rsync('-a', f'{src}/', f'{TODIR}/', check=False) |
| 69 |
if proc.returncode == 0 and (TODIR / deep).is_file(): |
| 70 |
test_fail("a non-empty directory was replaced by a file without --force") |
| 71 |
|
| 72 |
|
| 73 |
run_rsync('-a', '--force', f'{src}/', f'{TODIR}/') |
| 74 |
if not (TODIR / deep).is_file(): |
| 75 |
test_fail("--force did not replace the directory with the file at depth") |
| 76 |
assert_same(TODIR / deep, src / deep, label='--force replacement content') |
| 77 |
|
| 78 |
print("update: -u keeps newer dest / updates older; --force replaces a dir at depth") |