Debian Codegraph @

testsuite/output-options_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Breadth coverage of the output / reporting options.
3
4 These options control rsync's OUTPUT, not its path handling, so they are
5 checked for the documented output shape rather than at depth:
6 --version, --help, --itemize-changes (-i), --dry-run (-n), --stats,
7 --out-format, --list-only, --quiet (-q), --progress, -h, -8.
8
9 Every rsync run that is expected to succeed has its exit status checked (a
10 silent failure must not pass as "no output"), and the format-changing options
11 (-h, -8) assert the documented difference rather than merely "didn't break".
12 """
13
14 import os
15 import re
16 import subprocess
17
18 from rsyncfns import (
19 FROMDIR, TODIR,
20 assert_not_exists, make_data_file, make_tree, makepath, rmtree, rsync_argv,
21 test_fail, verify_dirs,
22 )
23
24 src = FROMDIR
25
26
27 def out(*args, want_rc=0, env=None, text=True):
28 """Run rsync capturing output. Unless want_rc is None, fail the test if the
29 exit status isn't want_rc -- so a broken transfer can't masquerade as the
30 expected (often empty) output."""
31 p = subprocess.run(rsync_argv(*args), capture_output=True, text=text, env=env)
32 if want_rc is not None and p.returncode != want_rc:
33 err = p.stderr if text else p.stderr.decode('latin-1', 'replace')
34 test_fail(f"rsync {' '.join(args)} exited {p.returncode}, "
35 f"expected {want_rc}:\n{err}")
36 return p
37
38
39 # --- --version / --help -----------------------------------------------------
40 p = out('--version')
41 if 'protocol version' not in p.stdout:
42 test_fail(f"--version output unexpected:\n{p.stdout}")
43 p = out('--help')
44 help_txt = p.stdout + p.stderr
45 if 'rsync' not in help_txt or 'Usage' not in help_txt:
46 test_fail("--help did not print usage")
47
48 rmtree(src)
49 rmtree(TODIR)
50 make_tree(src, depth=2)
51
52 # --- --itemize-changes: a new file shows the create itemization -------------
53 p = out('-ai', f'{src}/', f'{TODIR}/')
54 if '>f+++++++++' not in p.stdout:
55 test_fail(f"--itemize-changes missing create line:\n{p.stdout}")
56
57 # --- --dry-run lists but does not create ------------------------------------
58 rmtree(TODIR)
59 p = out('-ain', f'{src}/', f'{TODIR}/')
60 if '>f+++++++++' not in p.stdout:
61 test_fail("--dry-run itemize output missing")
62 assert_not_exists(TODIR / 'f0', label='--dry-run created a file')
63
64 # --- --stats prints the summary block ---------------------------------------
65 rmtree(TODIR)
66 p = out('-a', '--stats', f'{src}/', f'{TODIR}/')
67 if 'Number of files:' not in p.stdout or 'Total file size:' not in p.stdout:
68 test_fail(f"--stats output missing expected lines:\n{p.stdout}")
69
70 # --- --out-format=%n emits bare filenames -----------------------------------
71 rmtree(TODIR)
72 p = out('-a', '--out-format=%n', f'{src}/', f'{TODIR}/')
73 if 'f0' not in p.stdout:
74 test_fail(f"--out-format=%n did not emit filenames:\n{p.stdout}")
75
76 # --- --list-only lists the source without copying ---------------------------
77 # Pass a destination too: without --list-only this transfer would populate
78 # TODIR, so the assert_not_exists below actually proves the "without copying"
79 # property rather than being vacuously true for a destination-less command.
80 rmtree(TODIR)
81 p = out('--list-only', '-r', f'{src}/', f'{TODIR}/')
82 if 'f0' not in p.stdout:
83 test_fail(f"--list-only did not list files:\n{p.stdout}")
84 assert_not_exists(TODIR / 'f0', label='--list-only copied a file')
85
86 # --- --quiet suppresses normal stdout BUT still transfers -------------------
87 # Checking only for empty stdout would also pass if the transfer silently
88 # failed, so confirm the destination actually received the tree.
89 rmtree(TODIR)
90 p = out('-a', '-q', f'{src}/', f'{TODIR}/')
91 if p.stdout.strip() != '':
92 test_fail(f"--quiet produced stdout: {p.stdout!r}")
93 verify_dirs(src, TODIR, label='--quiet still transferred')
94
95 # --- --progress shows a percentage ------------------------------------------
96 rmtree(TODIR)
97 p = out('-a', '--progress', f'{src}/', f'{TODIR}/')
98 if '100%' not in p.stdout:
99 test_fail(f"--progress did not show a percentage:\n{p.stdout}")
100
101 # --- -h / --human-readable formats byte counts with a unit suffix -----------
102 # Without -h, --stats prints grouped digits ("50,000 bytes"); with -h it uses a
103 # K/M/G suffix ("50.00K"). Use a file big enough that the two forms differ.
104 rmtree(src)
105 rmtree(TODIR)
106 makepath(src)
107 make_data_file(src / 'big', 50_000)
108 plain = out('-a', '--stats', f'{src}/', f'{TODIR}/').stdout
109 rmtree(TODIR)
110 human = out('-a', '-h', '--stats', f'{src}/', f'{TODIR}/').stdout
111 suffix_re = r'Total file size: [\d.,]+[KMG]'
112 if not re.search(suffix_re, human):
113 test_fail(f"-h did not use a human-readable unit suffix:\n{human}")
114 if re.search(suffix_re, plain):
115 test_fail(f"--stats without -h unexpectedly used a unit suffix:\n{plain}")
116
117 # --- -8 / --8-bit-output leaves high-bit filename bytes unescaped ------------
118 # rsync escapes non-printable name bytes as \#NNN; -8 prints 8-bit bytes raw.
119 # This needs a filename containing a high-bit byte and a C locale (where such a
120 # byte is non-printable). Best-effort: skip silently where the filesystem can't
121 # store the raw byte (macOS/Cygwin may reject or normalise it).
122 rmtree(src)
123 rmtree(TODIR)
124 makepath(src)
125 weird = os.fsencode(str(src)) + b'/hi\xf9name' # 0xf9 -> octal 371
126 try:
127 with open(weird, 'wb') as f:
128 f.write(b"x\n")
129 weird_ok = True
130 except OSError:
131 weird_ok = False
132
133 if weird_ok:
134 cenv = dict(os.environ, LC_ALL='C')
135 makepath(TODIR)
136 noesc = out('-ai', f'{src}/', f'{TODIR}/', env=cenv, text=False)
137 if rb'\#371' in noesc.stdout: # FS preserved the raw byte as expected
138 rmtree(TODIR)
139 makepath(TODIR)
140 esc = out('-ai', '-8', f'{src}/', f'{TODIR}/', env=cenv, text=False)
141 if rb'\#371' in esc.stdout:
142 test_fail("-8 should leave the high-bit name byte unescaped, but "
143 f"the \\#371 escape was still present:\n{esc.stdout!r}")
144
145 print("output-options: version/help/-i/-n/--stats/--out-format/--list-only/"
146 "-q/--progress/-h/-8 verified")