Debian Codegraph @

testsuite/compare_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage of the comparison/skip options at depth: -c, -I, --size-only,
3 --modify-window.
4
5 These decide WHETHER a file is transferred. Each is checked on a file several
6 levels deep using a "stealth change" (same size, same mtime, different content)
7 that the default quick check deliberately skips.
8 """
9
10 import os
11
12 from rsyncfns import (
13 FROMDIR, TODIR,
14 assert_same, make_tree, rmtree, run_rsync, test_fail,
15 )
16
17 src = FROMDIR
18 deep = os.path.join('d1', 'd2', 'd3', 'f3')
19
20
21 def seed():
22 rmtree(src)
23 rmtree(TODIR)
24 make_tree(src, depth=3, data=True, data_size=4096)
25 run_rsync('-a', f'{src}/', f'{TODIR}/') # dest == src
26
27
28 def stealth_change():
29 """Change the deep source file's content but restore the destination's
30 size+mtime, so the quick check sees them as equal."""
31 st = os.stat(TODIR / deep)
32 data = bytearray((src / deep).read_bytes())
33 data[0] ^= 0xFF # same length, new content
34 (src / deep).write_bytes(bytes(data))
35 os.utime(src / deep, (st.st_atime, st.st_mtime))
36
37
38 # --- the default quick check skips a stealth change; -c and -I catch it ------
39 seed()
40 stealth_change()
41 run_rsync('-a', f'{src}/', f'{TODIR}/')
42 if (TODIR / deep).read_bytes() == (src / deep).read_bytes():
43 test_fail("default quick check unexpectedly transferred a same-size, "
44 "same-mtime change (test setup is wrong)")
45
46 run_rsync('-a', '-c', f'{src}/', f'{TODIR}/')
47 assert_same(TODIR / deep, src / deep, label='-c caught stealth change')
48
49 seed()
50 stealth_change()
51 run_rsync('-a', '-I', f'{src}/', f'{TODIR}/')
52 assert_same(TODIR / deep, src / deep, label='-I caught stealth change')
53
54 # --- --size-only skips a same-size change even when the mtime differs --------
55 def samesize_newmtime():
56 data = bytearray((src / deep).read_bytes())
57 data[0] ^= 0xFF # same size, new content
58 (src / deep).write_bytes(bytes(data))
59 st = os.stat(src / deep)
60 os.utime(src / deep, (st.st_atime, st.st_mtime + 100)) # mtime differs
61
62
63 seed()
64 samesize_newmtime()
65 run_rsync('-a', '--size-only', f'{src}/', f'{TODIR}/')
66 if (TODIR / deep).read_bytes() == (src / deep).read_bytes():
67 test_fail("--size-only transferred a same-size file (should have skipped)")
68
69 # Contrast on a fresh tree: the default DOES transfer it (mtime differs).
70 # (Re-seed because --size-only above updated the dest mtime to match.)
71 seed()
72 samesize_newmtime()
73 run_rsync('-a', f'{src}/', f'{TODIR}/')
74 assert_same(TODIR / deep, src / deep, label='default caught mtime change')
75
76 # --- --modify-window absorbs a small mtime difference -----------------------
77 # Both runs are dry-run (-ain): a real run would update the dest mtime to match
78 # the source, leaving the --modify-window run nothing to absorb (vacuous).
79 seed()
80 st = os.stat(TODIR / deep)
81 os.utime(src / deep, (st.st_atime, st.st_mtime + 1)) # 1s newer, same content
82 p = run_rsync('-ain', f'{src}/', f'{TODIR}/', capture_output=True)
83 if 'f3' not in p.stdout:
84 test_fail("a 1s mtime change was not itemized without --modify-window")
85 p = run_rsync('-ain', '--modify-window=2', f'{src}/', f'{TODIR}/',
86 capture_output=True)
87 if 'f3' in p.stdout:
88 test_fail("--modify-window=2 did not absorb a 1s mtime difference")
89
90 print("compare: -c / -I / --size-only / --modify-window verified at depth")