Debian Codegraph @

testsuite/backup-deep_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Property-level coverage of --backup / --suffix / --backup-dir at depth.
3
4 backup_test.py is the ported regression test (2 levels, no custom suffix); this
5 companion checks the concrete outcome of each backup mode at >=3 levels and,
6 for --backup-dir, with the backup tree placed OUTSIDE the destination (a
7 sibling) so the old file is renamed across directories into a parallel deep
8 path -- the cross-directory operation the resolver restructure rewrites.
9
10 Asserts, at every level of the tree:
11 * --backup --suffix=S saves the overwritten file as <name>S beside the new
12 one (old content in the backup, new content in place);
13 * --backup --backup-dir=DIR relocates the old file to DIR/<relpath>,
14 preserving the deep structure, while the destination gets the new content;
15 * --backup-dir together with --delete routes a deletion into the backup
16 tree instead of losing it.
17 """
18
19 import os
20
21 from rsyncfns import (
22 FROMDIR, SCRATCHDIR, TODIR,
23 assert_not_exists, assert_same, make_tree, rmtree, run_rsync, test_fail,
24 walk_files,
25 )
26
27 src = FROMDIR
28 bak = SCRATCHDIR / 'backups' # sibling of from/ and to/ -- outside both trees
29
30
31 def seed():
32 """Fresh v1 source, a matching destination, and the old (v1) bytes; then
33 mutate the source to v2 so the next sync overwrites every file."""
34 rmtree(src)
35 rmtree(TODIR)
36 rmtree(bak)
37 make_tree(src, depth=3, data=True, data_size=4096)
38 rels = [p.relative_to(src) for p in walk_files(src)]
39 run_rsync('-a', f'{src}/', f'{TODIR}/')
40 old = {rel: (src / rel).read_bytes() for rel in rels}
41 for rel in rels: # v1 -> v2
42 with open(src / rel, 'ab') as f:
43 f.write(b'\nversion-2 tail\n')
44 return rels, old
45
46
47 # --- --backup --suffix=.old (same directory) --------------------------------
48 rels, old = seed()
49 run_rsync('-a', '-b', '--suffix=.old', '--no-whole-file',
50 f'{src}/', f'{TODIR}/')
51 for rel in rels:
52 assert_same(TODIR / rel, src / rel, label=f'suffix new {rel}')
53 backup = (TODIR / rel)
54 backup = backup.with_name(backup.name + '.old')
55 if not backup.is_file():
56 test_fail(f"--suffix backup missing for {rel}: {backup}")
57 if backup.read_bytes() != old[rel]:
58 test_fail(f"--suffix backup of {rel} does not hold the old content")
59
60 # --- --backup-dir at depth, outside the dest tree (cross-dir) ---------------
61 rels, old = seed()
62 run_rsync('-a', '-b', f'--backup-dir={bak}', '--no-whole-file',
63 f'{src}/', f'{TODIR}/')
64 for rel in rels:
65 assert_same(TODIR / rel, src / rel, label=f'backup-dir new {rel}')
66 saved = bak / rel
67 if not saved.is_file():
68 test_fail(f"--backup-dir did not preserve deep path for {rel}: {saved}")
69 if saved.read_bytes() != old[rel]:
70 test_fail(f"--backup-dir copy of {rel} does not hold the old content")
71
72 # --- --backup-dir captures a deletion under --delete ------------------------
73 rels, old = seed()
74 # Add a deep file to the destination that is absent from the source.
75 extra = os.path.join('d1', 'd2', 'd3', 'goner')
76 (TODIR / extra).write_bytes(b'about to be deleted\n')
77 run_rsync('-a', '--delete', '-b', f'--backup-dir={bak}', '--no-whole-file',
78 f'{src}/', f'{TODIR}/')
79 assert_not_exists(TODIR / extra, label='deleted file removed from dest')
80 saved = bak / extra
81 if not saved.is_file():
82 test_fail(f"--backup-dir did not capture the deletion of {extra}")
83 if saved.read_bytes() != b'about to be deleted\n':
84 test_fail("captured deletion has the wrong content")
85
86 print("backup-deep: suffix / backup-dir / delete-capture verified at depth")