Debian Codegraph @

testsuite/mkpath_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/mkpath.test.
3 #
4 # Test the rsync --mkpath option: it should create any missing intermediate
5 # destination directories rather than erroring out.
6
7 import filecmp
8 import os
9 import shutil
10 from pathlib import Path
11
12 from rsyncfns import (
13 FROMDIR, SRCDIR, TMPDIR, TODIR,
14 makepath, rmtree, run_rsync, test_fail,
15 )
16
17
18 makepath(FROMDIR, TODIR)
19 shutil.copy2(SRCDIR / 'rsync.h', FROMDIR / 'text')
20 shutil.copy2(SRCDIR / 'configure.ac', FROMDIR / 'extra')
21
22 # All paths in the rsync invocations below are interpreted relative to
23 # TMPDIR, matching the original shell test which did `cd "$tmpdir"`.
24 os.chdir(TMPDIR)
25
26 deep_dir = Path('to/foo/bar/baz/down/deep')
27
28
29 def assert_file(path: Path, label: str, src: str = 'from/text') -> None:
30 if not path.is_file():
31 test_fail(f"{label}: {path} not found")
32 if not filecmp.cmp(path, src, shallow=False):
33 test_fail(f"{label}: {path} content differs from {src}")
34
35
36 # Negative control: without --mkpath, a missing intermediate path must fail and
37 # create nothing -- otherwise the --mkpath successes below prove nothing.
38 rmtree('to/foo')
39 proc = run_rsync('-ai', 'from/text', str(deep_dir / 'new'), check=False)
40 if proc.returncode == 0 or (deep_dir / 'new').exists():
41 test_fail("a transfer WITHOUT --mkpath created the missing intermediate path")
42
43 # Create several levels of dest dir (file destination — final component
44 # is the new filename).
45 run_rsync('-aiv', '--mkpath', 'from/text', str(deep_dir / 'new'))
46 assert_file(deep_dir / 'new', "'new' file in deep dir")
47 rmtree('to/foo')
48
49 # Trailing slash on the dest means it's a directory; the file keeps its name.
50 run_rsync('-aiv', '--mkpath', 'from/text', str(deep_dir) + '/')
51 assert_file(deep_dir / 'text', "'text' file in deep dir (trailing-slash dest)")
52 (deep_dir / 'text').unlink()
53
54 # An existing destination directory should also work.
55 (deep_dir / 'new').mkdir(parents=True, exist_ok=True)
56 run_rsync('-aiv', '--mkpath', 'from/text', str(deep_dir / 'new'))
57 assert_file(deep_dir / 'new' / 'text', "'text' file in pre-existing deep/new dir")
58
59 # ... and an existing path when an alternate dest filename is specified.
60 run_rsync('-aiv', '--mkpath', 'from/text', str(deep_dir / 'new' / 'text2'))
61 assert_file(deep_dir / 'new' / 'text2', "'text2' renamed file in pre-existing deep/new dir")
62 rmtree('to/foo')
63
64 # Multiple source args (whole directory) — bare dest name.
65 run_rsync('-aiv', '--mkpath', 'from/', str(deep_dir))
66 assert_file(deep_dir / 'extra', "'extra' file in deep dir (multi-source, no trailing slash)",
67 src='from/extra')
68 rmtree('to/foo')
69
70 # Multiple source args (whole directory) — dest with trailing slash.
71 run_rsync('-aiv', '--mkpath', 'from/', str(deep_dir) + '/')
72 assert_file(deep_dir / 'text', "'text' file in deep dir (multi-source, trailing slash)")
73
74 # No intermediate path at all — dest is just a file in the current dir.
75 run_rsync('-aiv', '--mkpath', 'from/text', 'to_text')
76 assert_file(Path('to_text'), "'to_text' file in current dir")