Debian Codegraph @

testsuite/backup_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/backup.test.
3 #
4 # Walk through --backup behaviour:
5 # * a plain backup leaves the old file at name~ alongside the new one,
6 # * --backup-dir relocates the old file into a parallel tree and also
7 # captures deletions when used with --delete-delay,
8 # * --backup --inplace --backup-dir handles delta-overwrites too.
9 # Each phase also confirms the destination ends up matching the source via
10 # the usual checkit listing+content diff.
11
12 import os
13 import shutil
14 import subprocess
15
16 from rsyncfns import (
17 CHKDIR, FROMDIR, OUTFILE, SRCDIR, TMPDIR, TODIR,
18 checkit, cp_touch, makepath, rsync_argv, test_fail, verify_dirs,
19 )
20
21
22 bakdir = TMPDIR / 'bak'
23
24 makepath(FROMDIR / 'deep', bakdir / 'dname')
25 name1 = FROMDIR / 'deep' / 'name1'
26 name2 = FROMDIR / 'deep' / 'name2'
27
28
29 def _cat_glob(pattern: str, dest):
30 """Concatenate every srcdir file matching `pattern` into `dest`.
31
32 Mirrors `cat "$srcdir"/[abc]*.[ch] > "$dest"`.
33 """
34 chunks = bytearray()
35 for f in sorted(SRCDIR.glob(pattern)):
36 chunks.extend(f.read_bytes())
37 dest.write_bytes(bytes(chunks))
38
39
40 _cat_glob('[gr]*.[ch]', name1)
41 _cat_glob('[et]*.[ch]', name2)
42
43 # Establish baseline destination and chk copies of the source.
44 checkit(['-ai', '--info=backup', f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR)
45 checkit(['-ai', '--info=backup', f'{FROMDIR}/', f'{CHKDIR}/'], FROMDIR, CHKDIR)
46
47 # Mutate the source files; delta-transfer will need to back up the old
48 # contents at $todir/$fn~ before overwriting in place.
49 _cat_glob('[fgpr]*.[ch]', name1)
50 _cat_glob('[etw]*.[ch]', name2)
51
52
53 def _run_and_capture(args, outfile):
54 proc = subprocess.run(rsync_argv(*args), capture_output=True, text=True)
55 outfile.write_text(proc.stdout)
56 print(proc.stdout, end='')
57 if proc.returncode != 0:
58 test_fail(f"rsync exited {proc.returncode}")
59 return proc
60
61
62 _run_and_capture(
63 ['-ai', '--info=backup', '--no-whole-file', '--backup',
64 f'{FROMDIR}/', f'{TODIR}/'],
65 OUTFILE,
66 )
67 text = OUTFILE.read_text()
68 for fn in ('deep/name1', 'deep/name2'):
69 if f"backed up {fn} to {fn}~" not in text:
70 test_fail(f"no backup message output for {fn}")
71 diff = subprocess.run(['diff', '-u', str(FROMDIR / fn), str(TODIR / fn)])
72 if diff.returncode != 0:
73 test_fail(f"copy of {fn} failed")
74 diff = subprocess.run(['diff', '-u', str(CHKDIR / fn), str(TODIR / f'{fn}~')])
75 if diff.returncode != 0:
76 test_fail(f"backup of {fn} to {fn}~ failed")
77 shutil.move(str(TODIR / f'{fn}~'), str(TODIR / fn))
78
79
80 # --backup-dir + --delete-delay: a deletion at the dest gets routed into
81 # the backup dir rather than being lost.
82 (TODIR / 'dname').write_text("deleted-file\n")
83 cp_touch(TODIR / 'dname', CHKDIR)
84
85 _run_and_capture(
86 ['-ai', '--info=backup', '--no-whole-file', '--delete-delay',
87 '--backup', f'--backup-dir={bakdir}',
88 f'{FROMDIR}/', f'{TODIR}/'],
89 OUTFILE,
90 )
91 # After the run, FROMDIR and TODIR should match (the backup ran into
92 # bakdir, not into chkdir, so chkdir must NOT be touched -- it still
93 # holds the pre-rsync contents that we'll compare against bakdir below).
94 verify_dirs(FROMDIR, TODIR, label="post --backup-dir run")
95
96 text = OUTFILE.read_text()
97 import re
98 for fn in ('deep/name1', 'deep/name2'):
99 if not re.search(rf"backed up {re.escape(fn)} to .*/{re.escape(fn)}$",
100 text, flags=re.MULTILINE):
101 test_fail(f"no backup message output for {fn}")
102 diff = subprocess.run(['diff', '-r', '-u', str(CHKDIR), str(bakdir)])
103 if diff.returncode != 0:
104 test_fail("backup dir contents are bogus")
105 (bakdir / 'dname').unlink()
106
107
108 # Re-establish chk and mutate source again for the --inplace pass.
109 checkit(['-ai', '--info=backup', '--del', f'{FROMDIR}/', f'{CHKDIR}/'],
110 FROMDIR, CHKDIR)
111 _cat_glob('[efgr]*.[ch]', name1)
112 _cat_glob('[ew]*.[ch]', name2)
113
114 _run_and_capture(
115 ['-ai', '--info=backup', '--inplace', '--no-whole-file',
116 '--backup', f'--backup-dir={bakdir}',
117 f'{FROMDIR}/', f'{TODIR}/'],
118 OUTFILE,
119 )
120 verify_dirs(FROMDIR, TODIR, label="post --inplace --backup-dir run")
121
122 text = OUTFILE.read_text()
123 for fn in ('deep/name1', 'deep/name2'):
124 if not re.search(rf"backed up {re.escape(fn)} to .*/{re.escape(fn)}$",
125 text, flags=re.MULTILINE):
126 test_fail(f"no backup message output for {fn}")
127 diff = subprocess.run(['diff', '-r', '-u', str(CHKDIR), str(bakdir)])
128 if diff.returncode != 0:
129 test_fail("backup dir contents are bogus")
130
131 # Final clean inplace sync to the bakdir so it ends up matching fromdir.
132 checkit(['-ai', '--info=backup', '--inplace', '--no-whole-file',
133 f'{FROMDIR}/', f'{bakdir}/'], FROMDIR, bakdir)