Debian Codegraph @

testsuite/delete_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/delete.test.
3 #
4 # Exercises three independent delete-handling behaviours:
5 # 1. --del dry-run output matches a real copy's output (sans the trivial
6 # "created directory" / "sent" / "total size" lines).
7 # 2. --del --remove-source-files leaves the source empty (only dirs) and
8 # the destination matching what a plain copy would have produced.
9 # 3. per-directory filter file with "P" (protect) keeps a file alive across
10 # --delete-excluded; "-" (exclude) does NOT.
11
12 import os
13 import shutil
14 import subprocess
15
16 from rsyncfns import (
17 CHKDIR, FROMDIR, TMPDIR, TODIR,
18 checkit, hands_setup, makepath, rsync_argv, test_fail,
19 )
20
21
22 hands_setup()
23 makepath(CHKDIR, TODIR / 'extradir', TODIR / 'emptydir' / 'subdir')
24
25 (TODIR / 'remove1').write_text("extra\n")
26 (TODIR / 'remove2').write_text("extra\n")
27 (TODIR / 'extradir' / 'remove3').write_text("extra\n")
28 (TODIR / 'emptydir' / 'subdir' / 'remove4').write_text("extra\n")
29
30
31 def _run_capture(*args):
32 proc = subprocess.run(rsync_argv(*args), capture_output=True, text=True)
33 return proc
34
35
36 def _strip_chatter(text: str) -> str:
37 """Remove the lines the shell test stripped via grep -E -v."""
38 keep = []
39 for line in text.splitlines():
40 if (line.startswith('created directory ')
41 or line.startswith('sent ')
42 or line.startswith('total size ')):
43 continue
44 keep.append(line)
45 return '\n'.join(keep) + ('\n' if text.endswith('\n') else '')
46
47
48 # Two chkdirs: copy/ has what a normal copy looks like, empty/ has just
49 # directories (used as a remove-source-files comparator).
50 copy_proc = _run_capture('-av', f'{FROMDIR}/', f'{CHKDIR}/copy/')
51 copy_out = _strip_chatter(copy_proc.stdout + copy_proc.stderr)
52 (TMPDIR / 'copy.out').write_text(copy_out)
53 print(copy_proc.stdout)
54
55 # --del dry-run output (status may be 0 or 23 from delete behaviour; ignore
56 # return code as the shell test does).
57 copy2_proc = _run_capture('-avn', '--del', f'{FROMDIR}/', f'{CHKDIR}/copy2/')
58 copy2_out = _strip_chatter(copy2_proc.stdout + copy2_proc.stderr)
59 (TMPDIR / 'copy2.out').write_text(copy2_out)
60 print(copy2_proc.stdout)
61
62 if copy_out != copy2_out:
63 diff = subprocess.run(
64 ['diff', '-u', str(TMPDIR / 'copy.out'), str(TMPDIR / 'copy2.out')],
65 capture_output=True, text=True,
66 )
67 sys_stdout = diff.stdout
68 print(sys_stdout)
69 test_fail("--del dry-run output diverged from a plain copy's output")
70
71 # Build chk/empty as a directories-only mirror of fromdir.
72 proc = subprocess.run(
73 rsync_argv('-av', '-f', 'exclude,! */', f'{FROMDIR}/', f'{CHKDIR}/empty/'),
74 )
75 if proc.returncode != 0:
76 test_fail("setup of chk/empty failed")
77
78 # Main: --del + --remove-source-files leaves dirs only in fromdir, and
79 # destination matches a normal copy.
80 checkit(['-avv', '--del', '--remove-source-files', f'{FROMDIR}/', f'{TODIR}/'],
81 CHKDIR / 'copy', TODIR)
82
83 diff = subprocess.run(['diff', '-r', '-u', str(CHKDIR / 'empty'), str(FROMDIR)])
84 if diff.returncode != 0:
85 test_fail("--remove-source-files did not leave fromdir as just directories")
86
87
88 # Per-directory filter file: "P" protects, "-" excludes.
89 (TODIR / 'filters').write_text("P foo\n- bar\n")
90 for name in ('foo', 'bar', 'baz'):
91 (TODIR / name).touch()
92
93 proc = subprocess.run(
94 rsync_argv('-r', '--exclude=baz', '--filter=: filters', '--delete-excluded',
95 f'{FROMDIR}/', f'{TODIR}/'),
96 )
97 if proc.returncode != 0:
98 test_fail(f"filter-file run exited {proc.returncode}")
99
100 if not (TODIR / 'foo').is_file():
101 test_fail(f"rsync should NOT have deleted {TODIR / 'foo'}")
102 if (TODIR / 'bar').is_file():
103 test_fail(f"rsync SHOULD have deleted {TODIR / 'bar'}")
104 if (TODIR / 'baz').is_file():
105 test_fail(f"rsync SHOULD have deleted {TODIR / 'baz'}")