Debian Codegraph @

testsuite/relative_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/relative.test.
3 #
4 # Exercise rsync --relative (-R) behaviour: paths anchored at a "./" cut
5 # point should reproduce that subtree at the destination. We pile on
6 # hard-link preservation, --del / --del-on-extras and the side-by-side
7 # combination of an -R-anchored arg with an absolute "extra" path.
8
9 import os
10 import subprocess
11 import time
12
13 from rsyncfns import (
14 CHKDIR, FROMDIR, OUTFILE, TMPDIR, TODIR,
15 checkit, hands_setup, makepath, rsync_argv,
16 run_rsync, test_fail,
17 )
18
19
20 deepstr = 'down/3/deep'
21 deepdir = FROMDIR / deepstr
22 extradir = TMPDIR / 'extra'
23
24 makepath(deepdir, extradir / deepstr, CHKDIR)
25
26 # Generate the rich source tree underneath the deep nested dir, not under
27 # fromdir directly. hands_setup reads FROMDIR from the module, so override
28 # briefly via the rsyncfns module attribute.
29 import rsyncfns
30 real_fromdir = rsyncfns.FROMDIR
31 try:
32 rsyncfns.FROMDIR = deepdir
33 hands_setup()
34 finally:
35 rsyncfns.FROMDIR = real_fromdir
36
37 extrafile = extradir / deepstr / 'extra.added.value'
38 extrafile.write_text("wowza\n")
39 # rsync's -R uses "./" as the anchor cut point: anything after it is the
40 # subtree path to recreate at the destination. Preserve the literal "./"
41 # in the string we pass to rsync, separately from the Path we use for
42 # filesystem operations.
43 extrafile_for_rsync = f"{extradir}/./{deepstr}/extra.added.value"
44
45 # Seed extradir with just the directory skeleton of fromdir.
46 run_rsync('-av', '--existing', '--include=*/', '--exclude=*',
47 f'{FROMDIR}/', f'{extradir}/')
48
49 os.chdir(FROMDIR)
50
51 # chkdir: same shape as a --include=/down/ --exclude=/* sync of fromdir.
52 run_rsync('-ai', '--include=/down/', '--exclude=/*',
53 f'{FROMDIR}/', f'{CHKDIR}/')
54
55 time.sleep(1)
56
57 print("Test basic relative:")
58 checkit(['-avR', f'./{deepstr}', str(TODIR)], CHKDIR, TODIR)
59
60 # Add a hard link inside the source and the chk dir; mirror it on both
61 # sides so the --delete pass below doesn't see it as new on either tree.
62 os.link(deepdir / 'filelist', deepdir / 'dir' / 'filelist')
63 os.link(CHKDIR / deepstr / 'filelist', CHKDIR / deepstr / 'dir' / 'filelist')
64 # Re-touch both dirs so the inner-dir time matches.
65 src_t = (deepdir / 'dir').stat().st_mtime
66 os.utime(deepdir / 'dir', (src_t, src_t))
67 os.utime(CHKDIR / deepstr / 'dir', (src_t, src_t))
68
69 print("Test hard links:")
70 checkit(['-avHR', f'./{deepstr}/', str(TODIR)], CHKDIR, TODIR)
71
72 # Drop some stray files at the dest then re-sync with --del to confirm
73 # they're removed.
74 import shutil
75 shutil.copy(deepdir / 'text', TODIR / deepstr / 'ThisShouldGo')
76 shutil.copy(deepdir / 'text', TODIR / deepstr / 'dir' / 'ThisShouldGoToo')
77
78 print("Test deletion:")
79 checkit(['-avHR', '--del', f'./{deepstr}/', str(TODIR)], CHKDIR, TODIR)
80
81 print("Test non-deletion:")
82 # Same as the previous pass but capture output to grep for spurious
83 # 'deleting ' lines.
84 proc = subprocess.run(
85 rsync_argv('-aiHR', '--del', f'./{deepstr}/', str(TODIR)),
86 capture_output=True, text=True,
87 )
88 OUTFILE.write_text(proc.stdout)
89 print(proc.stdout, end='')
90 if proc.returncode != 0:
91 test_fail(f"non-deletion run exited {proc.returncode}")
92 if 'deleting ' in proc.stdout:
93 test_fail("Erroneous deletions occurred!")
94
95 # Relative with merging.
96 run_rsync('-ai', str(extradir / 'down'), f'{CHKDIR}/')
97
98 print("Test merge:")
99 checkit(['-aiR', deepstr, extrafile_for_rsync, str(TODIR)], CHKDIR, TODIR)
100
101 print("Test merge with --del:")
102 proc = subprocess.run(
103 rsync_argv('-aiR', '--del', deepstr, extrafile_for_rsync, str(TODIR)),
104 capture_output=True, text=True,
105 )
106 OUTFILE.write_text(proc.stdout)
107 print(proc.stdout, end='')
108 if proc.returncode != 0:
109 test_fail(f"merge --del run exited {proc.returncode}")
110 if 'deleting ' in proc.stdout:
111 test_fail("Erroneous deletions occurred! (2)")