Debian Codegraph @

testsuite/duplicates_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/duplicates.test.
3 #
4 # The same source directory can be listed many times on the command line
5 # (e.g. through shell globbing). clean_flist() is supposed to dedupe so
6 # each file/link is copied exactly once even with ten identical sources.
7
8 import os
9 import subprocess
10
11 from rsyncfns import (
12 FROMDIR, TODIR,
13 rsync_argv, rsync_ls_lR, test_fail,
14 )
15
16
17 # Build a single regular file plus a symlink to it.
18 FROMDIR.mkdir(parents=True, exist_ok=True)
19 name1 = FROMDIR / 'name1'
20 name2 = FROMDIR / 'name2'
21 name1.write_text("This is the file\n")
22 try:
23 os.symlink(str(name1), name2)
24 except OSError as e:
25 test_fail(f"can't create symlink: {e}")
26
27 # Drive rsync with the same source ten times. Capture the verbose output to
28 # inspect for duplicate-copy behaviour AND for the dir-listing comparison
29 # that the shell test's checkit was doing alongside.
30 sources = [f'{FROMDIR}/'] * 10
31 proc = subprocess.run(
32 rsync_argv('-avv', *sources, f'{TODIR}/'),
33 capture_output=True, text=True,
34 )
35 print(proc.stdout)
36 if proc.returncode != 0:
37 test_fail(f"rsync exited {proc.returncode}\n{proc.stderr}")
38
39 name1_count = sum(1 for ln in proc.stdout.splitlines() if ln == 'name1')
40 if name1_count != 1:
41 test_fail(f"name1 was not copied exactly once (got {name1_count})")
42
43 name2_count = sum(1 for ln in proc.stdout.splitlines() if ln.startswith('name2 -> '))
44 if name2_count != 1:
45 test_fail(f"name2 was not copied exactly once (got {name2_count})")
46
47 # Cross-check that the destination matches the source.
48 if rsync_ls_lR(FROMDIR) != rsync_ls_lR(TODIR):
49 test_fail("destination listing differs from source after deduplication")