Debian Codegraph @

testsuite/file-to-file-mkpath-dry-run_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for issue #880 (and the dry-run itemize regression that the
3 # first proposed fix, PR #952, would have introduced).
4 #
5 # (1) Copying file-to-file with --mkpath and --dry-run used to abort with
6 # "change_dir#3 ... failed", because make_path() only *reports* (does not
7 # create) directories in a dry run, so the later chdir found no parent.
8 #
9 # (2) The fix must stay scoped to the missing-parent case: a plain
10 # file-to-file --dry-run onto an *existing*, differing destination must
11 # still itemize the real change, not report the file as brand new (PR #952
12 # bumped dry_run unconditionally, which broke this).
13 #
14 # In both cases a "--dry-run -i" must produce the same itemized output as the
15 # real run. Based on the test from PR #952 by Stiliyan Tonev.
16
17 import os
18 import subprocess
19
20 from rsyncfns import SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail
21
22
23 def itemize(*args):
24 p = subprocess.run(rsync_argv('-ai', *args), capture_output=True, text=True)
25 return p.returncode, p.stdout + p.stderr
26
27
28 # (1) --mkpath file-to-file: the dry run must succeed and match the real run.
29 mk = SCRATCHDIR / 'mk'
30 rmtree(mk)
31 makepath(mk / 'from')
32 (mk / 'from' / 'src').write_text("payload\n")
33
34 drc, dry = itemize('--dry-run', '--mkpath',
35 str(mk / 'from' / 'src'), str(mk / 'dndir' / 'dst'))
36 rc, real = itemize('--mkpath', str(mk / 'from' / 'src'), str(mk / 'rdir' / 'dst'))
37 if drc != 0:
38 print(dry)
39 test_fail("--mkpath file-to-file --dry-run failed (#880)")
40 if not (mk / 'rdir' / 'dst').exists():
41 test_fail("--mkpath real run did not create the file")
42 if dry.replace('dndir', 'X') != real.replace('rdir', 'X'):
43 test_fail(f"--mkpath dry-run output differs from the real run:\n"
44 f" dry : {dry!r}\n real: {real!r}")
45
46 # (2) Plain file-to-file onto an existing, differing destination: the dry run
47 # must itemize the same change as the real run (a/dst and b/dst share the
48 # basename 'dst', so the itemized lines are directly comparable).
49 ex = SCRATCHDIR / 'ex'
50 rmtree(ex)
51 makepath(ex / 'a')
52 makepath(ex / 'b')
53 (ex / 'src').write_text("brand new content\n")
54 for d in ('a', 'b'):
55 (ex / d / 'dst').write_text("old\n")
56 os.utime(ex / d / 'dst', (0, 0)) # make size + mtime differ
57
58 _, dry2 = itemize('--dry-run', str(ex / 'src'), str(ex / 'a' / 'dst'))
59 _, real2 = itemize(str(ex / 'src'), str(ex / 'b' / 'dst'))
60 if dry2 != real2:
61 test_fail(f"file-to-file --dry-run misreports an existing destination:\n"
62 f" dry : {dry2!r}\n real: {real2!r}")