Debian Codegraph @

testsuite/links_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage of symlink-handling options -l / -L / -k at depth.
3
4 -l (--links) copy a symlink as a symlink (target preserved);
5 -L (--copy-links) replace a symlink with the file/dir it points to;
6 -k (--copy-dirlinks) treat a symlink-to-directory as the real directory.
7
8 (-K --keep-dirlinks -- the receiver-side follow of an in-tree directory
9 symlink, issue #715 -- is covered by symlink-dirlink-basis_test.py and needs
10 the secure resolver; here we cover the portable source-side options, all on a
11 symlink that lives several directory levels deep.)
12 """
13
14 import os
15
16 from rsyncfns import (
17 FROMDIR, TODIR,
18 assert_is_symlink, assert_same, make_tree, rmtree, run_rsync, test_fail,
19 )
20
21 src = FROMDIR
22 deepdir = os.path.join('d1', 'd2', 'd3')
23
24
25 def seed():
26 rmtree(src)
27 rmtree(TODIR)
28 make_tree(src, depth=3)
29 os.symlink('f3', src / deepdir / 'sl') # file symlink, deep
30 (src / deepdir / 'realdir').mkdir()
31 (src / deepdir / 'realdir' / 'inside').write_text("inside\n")
32 os.symlink('realdir', src / deepdir / 'dirlink') # dir symlink, deep
33
34
35 # --- -l: symlinks preserved as symlinks at depth ----------------------------
36 seed()
37 run_rsync('-rl', f'{src}/', f'{TODIR}/')
38 assert_is_symlink(TODIR / deepdir / 'sl', target='f3', label='-l file symlink')
39 assert_is_symlink(TODIR / deepdir / 'dirlink', target='realdir',
40 label='-l dir symlink')
41
42 # --- -L: symlinks dereferenced into their referents at depth -----------------
43 seed()
44 run_rsync('-rL', f'{src}/', f'{TODIR}/')
45 if os.path.islink(TODIR / deepdir / 'sl'):
46 test_fail("-L left a file symlink at depth instead of dereferencing it")
47 assert_same(TODIR / deepdir / 'sl', src / deepdir / 'f3', label='-L deref file')
48 if os.path.islink(TODIR / deepdir / 'dirlink'):
49 test_fail("-L left a dir symlink at depth instead of dereferencing it")
50 assert_same(TODIR / deepdir / 'dirlink' / 'inside',
51 src / deepdir / 'realdir' / 'inside', label='-L deref dir')
52
53 # --- -k: only the dir-symlink is followed; the file symlink stays a symlink --
54 seed()
55 run_rsync('-rlk', f'{src}/', f'{TODIR}/')
56 if os.path.islink(TODIR / deepdir / 'dirlink'):
57 test_fail("-k left the dir symlink as a symlink")
58 if not (TODIR / deepdir / 'dirlink').is_dir():
59 test_fail("-k did not turn the dir symlink into a real directory")
60 assert_same(TODIR / deepdir / 'dirlink' / 'inside',
61 src / deepdir / 'realdir' / 'inside', label='-k dir contents')
62 assert_is_symlink(TODIR / deepdir / 'sl', target='f3',
63 label='-k keeps the file symlink')
64
65 print("links: -l preserves, -L dereferences, -k follows dir-symlinks (at depth)")