Debian Codegraph @

tests/by-util/test_fmt.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore plass samp FFFD
7 #[cfg(target_os = "linux")]
8 use std::os::unix::ffi::OsStringExt;
9 use uutests::new_ucmd;
10
11 #[test]
12 fn test_invalid_arg() {
13 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
14 }
15
16 #[test]
17 fn test_invalid_input() {
18 new_ucmd!().arg(".").fails_with_code(1);
19 }
20
21 #[test]
22 fn test_fmt() {
23 new_ucmd!()
24 .arg("one-word-per-line.txt")
25 .succeeds()
26 .stdout_is("this is a file with one word per line\n");
27 }
28
29 #[test]
30 fn test_fmt_quick() {
31 for param in ["-q", "--quick", "-qq"] {
32 new_ucmd!()
33 .args(&["one-word-per-line.txt", param])
34 .succeeds()
35 .stdout_is("this is a file with one word per line\n");
36 }
37 }
38
39 #[test]
40 fn test_fmt_width() {
41 for param in ["-w", "--width"] {
42 new_ucmd!()
43 .args(&["one-word-per-line.txt", param, "10"])
44 .succeeds()
45 .stdout_is("this is a\nfile with\none word\nper line\n");
46 }
47 new_ucmd!()
48 .args(&["one-word-per-line.txt", "-w50", "--width", "10"])
49 .succeeds()
50 .stdout_is("this is a\nfile with\none word\nper line\n");
51 }
52
53 #[test]
54 fn test_fmt_width_max_display_width() {
55 let input = "aa bb cc dd ee";
56 new_ucmd!()
57 .args(&["-w", "8"])
58 .pipe_in(input)
59 .succeeds()
60 .stdout_is("aa bb cc\ndd ee\n");
61 new_ucmd!()
62 .args(&["-w", "7"])
63 .pipe_in(input)
64 .succeeds()
65 .stdout_is("aa\nbb cc\ndd ee\n");
66 }
67
68 /// Regression for https://github.com/uutils/coreutils/issues/10095
69 ///
70 /// GNU `fmt` measures `-w` in UTF-8 bytes (not Unicode display columns).
71 /// These cases match GNU coreutils 9.11.
72 #[test]
73 fn test_fmt_width_multibyte_gnu_compatible() {
74 new_ucmd!()
75 .args(&["-w", "10"])
76 .pipe_in("漢 字 test 日 本 語")
77 .succeeds()
78 .stdout_is("漢 字\ntest 日\n本 語\n");
79
80 new_ucmd!()
81 .args(&["-w", "15"])
82 .pipe_in("漢字 test 日本語")
83 .succeeds()
84 .stdout_is("漢字 test\n日本語\n");
85 }
86
87 #[test]
88 fn test_fmt_width_invalid() {
89 new_ucmd!()
90 .args(&["one-word-per-line.txt", "-w", "apple"])
91 .fails_with_code(1)
92 .no_stdout()
93 .stderr_is("fmt: invalid width: 'apple'\n");
94 // an invalid width can be successfully overwritten later:
95 new_ucmd!()
96 .args(&["one-word-per-line.txt", "-w", "apple", "-w10"])
97 .succeeds()
98 .stdout_is("this is a\nfile with\none word\nper line\n");
99 }
100
101 #[test]
102 fn test_fmt_positional_width() {
103 new_ucmd!()
104 .args(&["-10", "one-word-per-line.txt"])
105 .succeeds()
106 .stdout_is("this is a\nfile with\none word\nper line\n");
107 }
108
109 #[test]
110 fn test_small_width() {
111 for width in ["0", "1", "2", "3"] {
112 for param in ["-w", "--width"] {
113 new_ucmd!()
114 .args(&[param, width, "one-word-per-line.txt"])
115 .succeeds()
116 .stdout_is("this\nis\na\nfile\nwith\none\nword\nper\nline\n");
117 }
118 }
119 }
120
121 #[test]
122 fn test_fmt_width_too_big() {
123 for param in ["-w", "--width"] {
124 new_ucmd!()
125 .args(&["one-word-per-line.txt", param, "2501"])
126 .fails_with_code(1)
127 .stderr_is("fmt: invalid width: '2501': Numerical result out of range\n");
128 }
129 // However, as a temporary value it is okay:
130 new_ucmd!()
131 .args(&["one-word-per-line.txt", "-w2501", "--width", "10"])
132 .succeeds()
133 .stdout_is("this is a\nfile with\none word\nper line\n");
134 }
135
136 #[test]
137 fn test_fmt_invalid_width() {
138 for param in ["-w", "--width"] {
139 new_ucmd!()
140 .args(&["one-word-per-line.txt", param, "invalid"])
141 .fails_with_code(1)
142 .stderr_contains("invalid width: 'invalid'");
143 }
144 }
145
146 #[test]
147 fn test_fmt_positional_width_not_first() {
148 new_ucmd!()
149 .args(&["one-word-per-line.txt", "-10"])
150 .fails_with_code(1)
151 .stderr_contains("fmt: invalid option -- 1; -WIDTH is recognized only when it is the first\noption; use -w N instead");
152 }
153
154 #[test]
155 fn test_fmt_width_not_valid_number() {
156 new_ucmd!()
157 .args(&["-25x", "one-word-per-line.txt"])
158 .fails_with_code(1)
159 .stderr_contains("fmt: invalid width: '25x'");
160 }
161
162 #[ignore = "our 'goal' algorithm is very different from GNU; fix this!"]
163 #[test]
164 fn test_fmt_goal() {
165 for param in ["-g", "--goal"] {
166 new_ucmd!()
167 .args(&["one-word-per-line.txt", param, "7"])
168 .succeeds()
169 .stdout_is("this is a\nfile with one\nword per line\n");
170 }
171 new_ucmd!()
172 .args(&["one-word-per-line.txt", "-g40", "-g7"])
173 .succeeds()
174 .stdout_is("this is a\nfile with one\nword per line\n");
175 }
176
177 #[test]
178 fn test_fmt_goal_too_big() {
179 for param in ["-g", "--goal"] {
180 new_ucmd!()
181 .args(&["one-word-per-line.txt", "--width=75", param, "76"])
182 .fails_with_code(1)
183 .stderr_is("fmt: GOAL cannot be greater than WIDTH.\n");
184 }
185 }
186
187 #[test]
188 fn test_fmt_goal_bigger_than_default_width_of_75() {
189 for param in ["-g", "--goal"] {
190 new_ucmd!()
191 .args(&["one-word-per-line.txt", param, "76"])
192 .fails_with_code(1)
193 .stderr_is("fmt: GOAL cannot be greater than WIDTH.\n");
194 }
195 }
196
197 #[ignore = "our 'goal' algorithm is very different from GNU; fix this!"]
198 #[test]
199 fn test_fmt_too_big_goal_sometimes_okay() {
200 new_ucmd!()
201 .args(&["one-word-per-line.txt", "--width=75", "-g76", "-g10"])
202 .succeeds()
203 .stdout_is("this is a\nfile with one\nword per line\n");
204 new_ucmd!()
205 .args(&["one-word-per-line.txt", "-g76", "-g10"])
206 .succeeds()
207 .stdout_is("this is a\nfile with one\nword per line\n");
208 }
209
210 #[test]
211 fn test_fmt_goal_too_small_to_check_negative_minlength() {
212 for param in ["-g", "--goal"] {
213 new_ucmd!()
214 .args(&["one-word-per-line.txt", "--width=75", param, "10"])
215 .succeeds()
216 .stdout_is("this is a file with one word per line\n");
217 }
218 }
219
220 #[test]
221 fn test_fmt_non_existent_file() {
222 new_ucmd!()
223 .args(&["non-existing"])
224 .fails_with_code(1)
225 .stderr_is("fmt: cannot open 'non-existing' for reading: No such file or directory\n");
226 }
227
228 #[test]
229 fn test_fmt_invalid_goal() {
230 for param in ["-g", "--goal"] {
231 new_ucmd!()
232 .args(&["one-word-per-line.txt", param, "invalid"])
233 .fails_with_code(1)
234 // GNU complains about "invalid width", which is confusing.
235 // We intentionally deviate from GNU, and show a more helpful message:
236 .stderr_contains("invalid goal: 'invalid'");
237 }
238 }
239
240 #[test]
241 fn test_fmt_invalid_goal_override() {
242 new_ucmd!()
243 .args(&["one-word-per-line.txt", "-g", "apple", "-g", "74"])
244 .succeeds()
245 .stdout_is("this is a file with one word per line\n");
246 }
247
248 #[test]
249 fn test_fmt_invalid_goal_width_priority() {
250 new_ucmd!()
251 .args(&["one-word-per-line.txt", "-g", "apple", "-w", "banana"])
252 .fails_with_code(1)
253 .no_stdout()
254 .stderr_is("fmt: invalid width: 'banana'\n");
255 new_ucmd!()
256 .args(&["one-word-per-line.txt", "-w", "banana", "-g", "apple"])
257 .fails_with_code(1)
258 .no_stdout()
259 .stderr_is("fmt: invalid width: 'banana'\n");
260 }
261
262 #[test]
263 fn test_fmt_set_goal_not_contain_width() {
264 for param in ["-g", "--goal"] {
265 new_ucmd!()
266 .args(&["one-word-per-line.txt", param, "74"])
267 .succeeds()
268 .stdout_is("this is a file with one word per line\n");
269 }
270 }
271
272 #[test]
273 fn split_does_not_reflow() {
274 for arg in ["-s", "-ss", "--split-only"] {
275 new_ucmd!()
276 .arg("one-word-per-line.txt")
277 .arg(arg)
278 .succeeds()
279 .stdout_is_fixture("one-word-per-line.txt");
280 }
281 }
282
283 #[test]
284 fn prefix_minus() {
285 for prefix_args in [
286 vec!["-p-"],
287 vec!["-p", "-"],
288 vec!["--prefix=-"],
289 vec!["--prefix", "-"],
290 vec!["--pref=-"],
291 vec!["--pref", "-"],
292 // Test self-overriding:
293 vec!["--prefix==", "--prefix=-"],
294 ] {
295 new_ucmd!()
296 .args(&prefix_args)
297 .arg("prefixed-one-word-per-line.txt")
298 .succeeds()
299 .stdout_is_fixture("prefixed-one-word-per-line_p-.txt");
300 }
301 }
302
303 #[test]
304 fn prefix_equal() {
305 for prefix_args in [
306 // FIXME: #6353 vec!["-p="],
307 vec!["-p", "="],
308 vec!["--prefix=="],
309 vec!["--prefix", "="],
310 vec!["--pref=="],
311 vec!["--pref", "="],
312 // Test self-overriding:
313 vec!["--prefix=-", "--prefix=="],
314 ] {
315 new_ucmd!()
316 .args(&prefix_args)
317 .arg("prefixed-one-word-per-line.txt")
318 .succeeds()
319 .stdout_is_fixture("prefixed-one-word-per-line_p=.txt");
320 }
321 }
322
323 #[test]
324 fn prefix_equal_skip_prefix_equal_two() {
325 for prefix_args in [
326 // FIXME: #6353 vec!["--prefix==", "-P=2"],
327 vec!["--prefix==", "-P", "=2"],
328 vec!["--prefix==", "--skip-prefix==2"],
329 vec!["--prefix==", "--skip-prefix", "=2"],
330 vec!["--prefix==", "--skip-pref==2"],
331 vec!["--prefix==", "--skip-pref", "=2"],
332 // Test self-overriding:
333 vec!["--prefix==", "--skip-pref", "asdf", "-P", "=2"],
334 ] {
335 new_ucmd!()
336 .args(&prefix_args)
337 .arg("prefixed-one-word-per-line.txt")
338 .succeeds()
339 .stdout_is_fixture("prefixed-one-word-per-line_p=_P=2.txt");
340 }
341 }
342
343 #[test]
344 fn prefix_ignores_leading_whitespace_without_exact_prefix() {
345 for prefix_args in [vec!["-p", "> "], vec!["--prefix", "> "]] {
346 new_ucmd!()
347 .args(&prefix_args)
348 .pipe_in(" > alpha\n > beta\n")
349 .succeeds()
350 .stdout_only(" > alpha beta\n");
351 }
352 }
353
354 #[test]
355 fn exact_prefix_requires_the_prefix_to_start_the_line() {
356 for prefix_args in [
357 vec!["-x", "-p", "> "],
358 vec!["--exact-prefix", "--prefix", "> "],
359 ] {
360 new_ucmd!()
361 .args(&prefix_args)
362 .pipe_in(" > alpha\n > beta\n")
363 .succeeds()
364 .stdout_only(" > alpha\n > beta\n");
365 }
366 }
367
368 #[test]
369 fn skip_prefix_ignores_leading_whitespace_without_exact_skip_prefix() {
370 for prefix_args in [vec!["-P", "#"], vec!["--skip-prefix", "#"]] {
371 new_ucmd!()
372 .args(&prefix_args)
373 .pipe_in(" # note\n # more\n")
374 .succeeds()
375 .stdout_only(" # note\n # more\n");
376 }
377 }
378
379 #[test]
380 fn exact_skip_prefix_requires_the_prefix_to_start_the_line() {
381 for prefix_args in [
382 vec!["-X", "-P", "#"],
383 vec!["--exact-skip-prefix", "--skip-prefix", "#"],
384 ] {
385 new_ucmd!()
386 .args(&prefix_args)
387 .pipe_in(" # note\n # more\n")
388 .succeeds()
389 .stdout_only(" # note # more\n");
390 }
391 }
392
393 #[test]
394 fn test_fmt_unicode_whitespace_handling() {
395 // Character classification fix: Test that Unicode whitespace characters like non-breaking space
396 // are NOT treated as whitespace by fmt, maintaining GNU fmt compatibility.
397 // GNU fmt only recognizes ASCII whitespace (space, tab, newline, etc.) and excludes
398 // Unicode whitespace characters to ensure consistent formatting behavior.
399 // This prevents regression of the character classification fix
400 let non_breaking_space = "\u{00A0}"; // U+00A0 NO-BREAK SPACE
401 let figure_space = "\u{2007}"; // U+2007 FIGURE SPACE
402 let narrow_no_break_space = "\u{202F}"; // U+202F NARROW NO-BREAK SPACE
403
404 // When fmt splits on width=1, these characters should NOT cause line breaks
405 // because they should not be considered whitespace
406 for (name, char) in [
407 ("non-breaking space", non_breaking_space),
408 ("figure space", figure_space),
409 ("narrow no-break space", narrow_no_break_space),
410 ("word joiner", "\u{2060}"),
411 ("cyrillic kha", "\u{0445}"),
412 ] {
413 let input = format!("={char}=");
414 let result = new_ucmd!()
415 .args(&["-s", "-w1"])
416 .pipe_in(input.as_bytes())
417 .succeeds();
418
419 // Should be 1 line since the Unicode char is not treated as whitespace
420 assert_eq!(
421 result.stdout_str().lines().count(),
422 1,
423 "Failed for {name}: Unicode character should not be treated as whitespace"
424 );
425 }
426 }
427
428 #[test]
429 fn test_fmt_knuth_plass_line_breaking() {
430 // Line breaking algorithm improvements: Test the enhanced Knuth-Plass optimal line breaking
431 // algorithm that better handles sentence boundaries, word positioning constraints,
432 // and produces more natural line breaks for complex text formatting.
433 // This prevents regression of the line breaking algorithm improvements
434 let input = "@command{fmt} prefers breaking lines at the end of a sentence, and tries to\n\
435 avoid line breaks after the first word of a sentence or before the last word\n\
436 of a sentence. A @dfn{sentence break} is defined as either the end of a\n\
437 paragraph or a word ending in any of @samp{.?!}, followed by two spaces or end\n\
438 of line, ignoring any intervening parentheses or quotes. Like @TeX{},\n\
439 @command{fmt} reads entire ''paragraphs'' before choosing line breaks; the\n\
440 algorithm is a variant of that given by\n\
441 Donald E. Knuth and Michael F. Plass\n\
442 in ''Breaking Paragraphs Into Lines'',\n\
443 @cite{Software---Practice & Experience}\n\
444 @b{11}, 11 (November 1981), 1119--1184.";
445
446 let expected = "@command{fmt} prefers breaking lines at the end of a sentence,\n\
447 and tries to avoid line breaks after the first word of a sentence\n\
448 or before the last word of a sentence. A @dfn{sentence break}\n\
449 is defined as either the end of a paragraph or a word ending\n\
450 in any of @samp{.?!}, followed by two spaces or end of line,\n\
451 ignoring any intervening parentheses or quotes. Like @TeX{},\n\
452 @command{fmt} reads entire ''paragraphs'' before choosing line\n\
453 breaks; the algorithm is a variant of that given by Donald\n\
454 E. Knuth and Michael F. Plass in ''Breaking Paragraphs Into\n\
455 Lines'', @cite{Software---Practice & Experience} @b{11}, 11\n\
456 (November 1981), 1119--1184.\n";
457
458 new_ucmd!()
459 .args(&["-g", "60", "-w", "72"])
460 .pipe_in(input)
461 .succeeds()
462 .stdout_is(expected);
463 }
464
465 #[test]
466 #[cfg(target_os = "linux")]
467 fn test_fmt_non_utf8_paths() {
468 use uutests::at_and_ucmd;
469
470 let (at, mut ucmd) = at_and_ucmd!();
471 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
472
473 std::fs::write(at.plus(&filename), b"hello world this is a test").unwrap();
474
475 ucmd.arg(&filename).succeeds();
476 }
477
478 #[test]
479 fn fmt_reflow_unicode() {
480 new_ucmd!()
481 .args(&["-w", "4"])
482 .pipe_in("漢字漢字 💐 日本語の文字\n")
483 .succeeds()
484 .stdout_is("漢字漢字\n💐\n日本語の文字\n");
485 }
486
487 #[test]
488 fn test_fmt_invalid_utf8() {
489 // Regression test for handling invalid UTF-8 input (e.g. ISO-8859-1)
490 // fmt should not drop lines with invalid UTF-8.
491 // \xA0 is non-breaking space in ISO-8859-1, but invalid in UTF-8.
492 // We expect GNU-compatible passthrough of the raw byte, not lossy replacement.
493 let input = b"=\xA0=";
494 new_ucmd!()
495 .args(&["-s", "-w1"])
496 .pipe_in(input)
497 .succeeds()
498 .stdout_is_bytes(b"=\xA0=\n");
499 }
500
501 #[test]
502 fn test_fmt_width_multiplication_overflow() {
503 new_ucmd!()
504 .args(&["-w", "267672676527678256"])
505 .fails_with_code(1)
506 .stderr_is("fmt: invalid width: '267672676527678256'\n");
507 }