Debian Codegraph @

tests/by-util/test_fold.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 fullwidth refgh tefgh nefgh unflushed
7
8 use bytecount::count;
9 use unicode_width::UnicodeWidthChar;
10 use uutests::new_ucmd;
11 use uutests::util::TestScenario;
12 use uutests::util_name;
13
14 #[test]
15 fn test_invalid_arg() {
16 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
17 }
18
19 #[test]
20 fn test_default_80_column_wrap() {
21 new_ucmd!()
22 .arg("lorem_ipsum.txt")
23 .succeeds()
24 .stdout_is_fixture("lorem_ipsum_80_column.expected");
25 }
26
27 #[test]
28 fn test_40_column_hard_cutoff() {
29 new_ucmd!()
30 .args(&["-w", "40", "lorem_ipsum.txt"])
31 .succeeds()
32 .stdout_is_fixture("lorem_ipsum_40_column_hard.expected");
33 }
34
35 #[test]
36 fn test_40_column_word_boundary() {
37 new_ucmd!()
38 .args(&["-s", "-w", "40", "lorem_ipsum.txt"])
39 .succeeds()
40 .stdout_is_fixture("lorem_ipsum_40_column_word.expected");
41 }
42
43 #[test]
44 fn test_default_wrap_with_newlines() {
45 new_ucmd!()
46 .arg("lorem_ipsum_new_line.txt")
47 .succeeds()
48 .stdout_is_fixture("lorem_ipsum_new_line_80_column.expected");
49 }
50
51 #[test]
52 fn test_wide_characters_in_column_mode() {
53 new_ucmd!()
54 .args(&["-w", "5"])
55 .pipe_in("\u{B250}\u{B250}\u{B250}\n")
56 .succeeds()
57 .stdout_is("\u{B250}\u{B250}\n\u{B250}\n");
58 }
59
60 #[test]
61 fn test_wide_characters_with_characters_option() {
62 new_ucmd!()
63 .args(&["--characters", "-w", "5"])
64 .pipe_in("\u{B250}\u{B250}\u{B250}\n")
65 .succeeds()
66 .stdout_is("\u{B250}\u{B250}\u{B250}\n");
67 }
68
69 #[test]
70 fn test_wide_characters_with_characters_short_option() {
71 new_ucmd!()
72 .args(&["-c", "-w", "5"])
73 .pipe_in("\u{B250}\u{B250}\u{B250}\n")
74 .succeeds()
75 .stdout_is("\u{B250}\u{B250}\u{B250}\n");
76 }
77
78 #[test]
79 fn test_multiple_wide_characters_in_column_mode() {
80 let wide = '\u{FF1A}';
81 let mut input = wide.to_string().repeat(50);
82 input.push('\n');
83
84 let mut expected = String::new();
85 for i in 1..=50 {
86 expected.push(wide);
87 if i % 5 == 0 {
88 expected.push('\n');
89 }
90 }
91
92 new_ucmd!()
93 .args(&["-w", "10"])
94 .pipe_in(input)
95 .succeeds()
96 .stdout_is(expected);
97 }
98
99 #[test]
100 fn test_multiple_wide_characters_in_character_mode() {
101 let wide = '\u{FF1A}';
102 let mut input = wide.to_string().repeat(50);
103 input.push('\n');
104
105 let mut expected = String::new();
106 for i in 1..=50 {
107 expected.push(wide);
108 if i % 10 == 0 {
109 expected.push('\n');
110 }
111 }
112
113 new_ucmd!()
114 .args(&["--characters", "-w", "10"])
115 .pipe_in(input)
116 .succeeds()
117 .stdout_is(expected);
118 }
119
120 #[test]
121 fn test_unicode_on_reader_buffer_boundary_in_character_mode() {
122 let boundary = buf_reader_capacity().saturating_sub(1);
123 assert!(boundary > 0, "BufReader capacity must be greater than 1");
124
125 let mut input = "a".repeat(boundary);
126 input.push('\u{B250}');
127 input.push_str(&"a".repeat(100));
128 input.push('\n');
129
130 let expected_tail = tail_inclusive(&fold_characters_reference(&input, 80), 4);
131
132 let result = new_ucmd!().arg("--characters").pipe_in(input).succeeds();
133
134 let actual_tail = tail_inclusive(result.stdout_str(), 4);
135
136 assert_eq!(actual_tail, expected_tail);
137 }
138
139 #[test]
140 fn test_fold_preserves_invalid_utf8_sequences() {
141 let bad_input: &[u8] = b"\xC3|\xED\xBA\xAD|\x00|\x89|\xED\xA6\xBF\xED\xBF\xBF\n";
142
143 new_ucmd!()
144 .pipe_in(bad_input.to_vec())
145 .succeeds()
146 .stdout_is_bytes(bad_input);
147 }
148
149 #[test]
150 fn test_fold_preserves_incomplete_utf8_at_eof() {
151 let trailing_byte: &[u8] = b"\xC3";
152
153 new_ucmd!()
154 .pipe_in(trailing_byte.to_vec())
155 .succeeds()
156 .stdout_is_bytes(trailing_byte);
157 }
158
159 #[test]
160 fn test_zero_width_bytes_in_column_mode() {
161 let len = io_buf_size_times_two();
162 let input = vec![0u8; len];
163
164 new_ucmd!()
165 .pipe_in(input.clone())
166 .succeeds()
167 .stdout_is_bytes(input);
168 }
169
170 #[test]
171 fn test_zero_width_bytes_in_character_mode() {
172 let len = io_buf_size_times_two();
173 let input = vec![0u8; len];
174 let expected = fold_characters_reference_bytes(&input, 80);
175
176 new_ucmd!()
177 .args(&["--characters"])
178 .pipe_in(input)
179 .succeeds()
180 .stdout_is_bytes(expected);
181 }
182
183 #[test]
184 fn test_zero_width_spaces_in_column_mode() {
185 let len = io_buf_size_times_two();
186 let input = "\u{200B}".repeat(len);
187
188 new_ucmd!()
189 .pipe_in(input.clone())
190 .succeeds()
191 .stdout_is(&input);
192 }
193
194 #[test]
195 fn test_zero_width_spaces_in_character_mode() {
196 let len = io_buf_size_times_two();
197 let input = "\u{200B}".repeat(len);
198 let expected = fold_characters_reference(&input, 80);
199
200 new_ucmd!()
201 .args(&["--characters"])
202 .pipe_in(input)
203 .succeeds()
204 .stdout_is(&expected);
205 }
206
207 #[test]
208 fn test_zero_width_bytes_from_file() {
209 let len = io_buf_size_times_two();
210 let input = vec![0u8; len];
211 let expected = fold_characters_reference_bytes(&input, 80);
212
213 let ts = TestScenario::new(util_name!());
214 let path = "zeros.bin";
215 ts.fixtures.write_bytes(path, &input);
216
217 ts.ucmd().arg(path).succeeds().stdout_is_bytes(&input);
218
219 ts.ucmd()
220 .args(&["--characters", path])
221 .succeeds()
222 .stdout_is_bytes(expected);
223 }
224
225 #[test]
226 fn test_zero_width_spaces_from_file() {
227 let len = io_buf_size_times_two();
228 let input = "\u{200B}".repeat(len);
229 let expected = fold_characters_reference(&input, 80);
230
231 let ts = TestScenario::new(util_name!());
232 let path = "zero-width.txt";
233 ts.fixtures.write(path, &input);
234
235 ts.ucmd().arg(path).succeeds().stdout_is(&input);
236
237 ts.ucmd()
238 .args(&["--characters", path])
239 .succeeds()
240 .stdout_is(&expected);
241 }
242
243 #[test]
244 fn test_zero_width_data_line_counts() {
245 let len = io_buf_size_times_two();
246
247 let zero_bytes = vec![0u8; len];
248 let column_bytes = new_ucmd!().pipe_in(zero_bytes.clone()).succeeds();
249 assert_eq!(
250 newline_count(column_bytes.stdout()),
251 0,
252 "fold should not wrap zero-width bytes in column mode",
253 );
254
255 let characters_bytes = new_ucmd!()
256 .args(&["--characters"])
257 .pipe_in(zero_bytes)
258 .succeeds();
259 assert_eq!(
260 newline_count(characters_bytes.stdout()),
261 len / 80,
262 "fold --characters should wrap zero-width bytes every 80 bytes",
263 );
264
265 if UnicodeWidthChar::width('\u{200B}') != Some(0) {
266 eprintln!("skip zero width space checks because width != 0");
267 return;
268 }
269
270 let zero_width_spaces = "\u{200B}".repeat(len);
271 let column_spaces = new_ucmd!().pipe_in(zero_width_spaces.clone()).succeeds();
272 assert_eq!(
273 newline_count(column_spaces.stdout()),
274 0,
275 "fold should keep zero-width spaces on a single line in column mode",
276 );
277
278 let characters_spaces = new_ucmd!()
279 .args(&["--characters"])
280 .pipe_in(zero_width_spaces)
281 .succeeds();
282 assert_eq!(
283 newline_count(characters_spaces.stdout()),
284 len / 80,
285 "fold --characters should wrap zero-width spaces every 80 characters",
286 );
287 }
288
289 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
290 #[test]
291 fn test_fold_reports_no_space_left_on_dev_full() {
292 use std::fs::OpenOptions;
293 use std::process::Stdio;
294
295 for &byte in &[b'\n', b'\0', 0xC3u8] {
296 let dev_full = OpenOptions::new()
297 .write(true)
298 .open("/dev/full")
299 .expect("/dev/full must exist on supported targets");
300
301 new_ucmd!()
302 .pipe_in(vec![byte; 1024])
303 .set_stdout(Stdio::from(dev_full))
304 .fails()
305 .stderr_contains("No space left");
306 }
307 }
308
309 fn buf_reader_capacity() -> usize {
310 std::io::BufReader::new(&b""[..]).capacity()
311 }
312
313 fn io_buf_size_times_two() -> usize {
314 buf_reader_capacity()
315 .checked_mul(2)
316 .expect("BufReader capacity overflow")
317 }
318
319 fn fold_characters_reference(input: &str, width: usize) -> String {
320 let mut output = String::with_capacity(input.len());
321 let mut col_count = 0usize;
322
323 for ch in input.chars() {
324 if ch == '\n' {
325 output.push('\n');
326 col_count = 0;
327 continue;
328 }
329
330 if col_count >= width {
331 output.push('\n');
332 col_count = 0;
333 }
334
335 output.push(ch);
336 col_count += 1;
337 }
338
339 output
340 }
341
342 fn fold_characters_reference_bytes(input: &[u8], width: usize) -> Vec<u8> {
343 let mut output = Vec::with_capacity(input.len() + input.len() / width + 1);
344
345 for chunk in input.chunks(width) {
346 output.extend_from_slice(chunk);
347 if chunk.len() == width {
348 output.push(b'\n');
349 }
350 }
351
352 output
353 }
354
355 fn newline_count(bytes: &[u8]) -> usize {
356 count(bytes, b'\n')
357 }
358
359 fn tail_inclusive(text: &str, lines: usize) -> String {
360 if lines == 0 {
361 return String::new();
362 }
363
364 let segments: Vec<&str> = text.split_inclusive('\n').collect();
365 if segments.is_empty() {
366 return text.to_owned();
367 }
368
369 let start = segments.len().saturating_sub(lines);
370 segments[start..].concat()
371 }
372
373 #[test]
374 fn test_should_preserve_empty_line_without_final_newline() {
375 new_ucmd!()
376 .arg("-w2")
377 .pipe_in("12\n\n34")
378 .succeeds()
379 .stdout_is("12\n\n34");
380 }
381
382 #[test]
383 fn test_should_preserve_empty_line_and_final_newline() {
384 new_ucmd!()
385 .arg("-w2")
386 .pipe_in("12\n\n34\n")
387 .succeeds()
388 .stdout_is("12\n\n34\n");
389 }
390
391 #[test]
392 fn test_should_preserve_empty_lines() {
393 new_ucmd!().pipe_in("\n").succeeds().stdout_is("\n");
394
395 new_ucmd!()
396 .arg("-w1")
397 .pipe_in("0\n1\n\n2\n\n\n")
398 .succeeds()
399 .stdout_is("0\n1\n\n2\n\n\n");
400 }
401
402 #[test]
403 fn test_word_boundary_split_should_preserve_empty_lines() {
404 new_ucmd!()
405 .arg("-s")
406 .pipe_in("\n")
407 .succeeds()
408 .stdout_is("\n");
409
410 new_ucmd!()
411 .args(&["-w1", "-s"])
412 .pipe_in("0\n1\n\n2\n\n\n")
413 .succeeds()
414 .stdout_is("0\n1\n\n2\n\n\n");
415 }
416
417 #[test]
418 fn test_should_not_add_newline_when_line_less_than_fold() {
419 new_ucmd!().pipe_in("1234").succeeds().stdout_is("1234");
420 }
421
422 #[test]
423 fn test_should_not_add_newline_when_line_longer_than_fold() {
424 new_ucmd!()
425 .arg("-w2")
426 .pipe_in("1234")
427 .succeeds()
428 .stdout_is("12\n34");
429 }
430
431 #[test]
432 fn test_should_not_add_newline_when_line_equal_to_fold() {
433 new_ucmd!()
434 .arg("-w1")
435 .pipe_in(" ")
436 .succeeds()
437 .stdout_is(" ");
438 }
439
440 #[test]
441 fn test_should_preserve_final_newline_when_line_less_than_fold() {
442 new_ucmd!().pipe_in("1234\n").succeeds().stdout_is("1234\n");
443 }
444
445 #[test]
446 fn test_should_preserve_final_newline_when_line_longer_than_fold() {
447 new_ucmd!()
448 .arg("-w2")
449 .pipe_in("1234\n")
450 .succeeds()
451 .stdout_is("12\n34\n");
452 }
453
454 #[test]
455 fn test_should_preserve_final_newline_when_line_equal_to_fold() {
456 new_ucmd!()
457 .arg("-w2")
458 .pipe_in("1\n")
459 .succeeds()
460 .stdout_is("1\n");
461 }
462
463 #[test]
464 fn test_single_tab_should_not_add_extra_newline() {
465 new_ucmd!()
466 .arg("-w1")
467 .pipe_in("\t")
468 .succeeds()
469 .stdout_is("\t");
470 }
471
472 #[test]
473 fn test_initial_tab_counts_as_8_columns() {
474 new_ucmd!()
475 .arg("-w8")
476 .pipe_in("\t1")
477 .succeeds()
478 .stdout_is("\t\n1");
479 }
480
481 #[test]
482 fn test_tab_should_advance_to_next_tab_stop() {
483 // tab advances the column count to the next tab stop, i.e. the width
484 // of the tab varies based on the leading text
485 new_ucmd!()
486 .args(&["-w8", "tab_stops.input"])
487 .succeeds()
488 .stdout_is_fixture("tab_stops_w8.expected");
489 }
490
491 #[test]
492 fn test_all_tabs_should_advance_to_next_tab_stops() {
493 new_ucmd!()
494 .args(&["-w16", "tab_stops.input"])
495 .succeeds()
496 .stdout_is_fixture("tab_stops_w16.expected");
497 }
498
499 #[test]
500 fn test_fold_before_tab_with_narrow_width() {
501 new_ucmd!()
502 .arg("-w7")
503 .pipe_in("a\t1")
504 .succeeds()
505 .stdout_is("a\n\t\n1");
506 }
507
508 #[test]
509 fn test_fold_at_word_boundary() {
510 new_ucmd!()
511 .args(&["-w4", "-s"])
512 .pipe_in("one two")
513 .succeeds()
514 .stdout_is("one \ntwo");
515 }
516
517 #[test]
518 fn test_fold_at_leading_word_boundary() {
519 new_ucmd!()
520 .args(&["-w3", "-s"])
521 .pipe_in(" aaa")
522 .succeeds()
523 .stdout_is(" \naaa");
524 }
525
526 #[test]
527 fn test_fold_at_word_boundary_preserve_final_newline() {
528 new_ucmd!()
529 .args(&["-w4", "-s"])
530 .pipe_in("one two\n")
531 .succeeds()
532 .stdout_is("one \ntwo\n");
533 }
534
535 #[test]
536 fn test_fold_at_tab() {
537 new_ucmd!()
538 .arg("-w8")
539 .pipe_in("a\tbbb\n")
540 .succeeds()
541 .stdout_is("a\t\nbbb\n");
542 }
543
544 #[test]
545 fn test_fold_after_tab() {
546 new_ucmd!()
547 .arg("-w10")
548 .pipe_in("a\tbbb\n")
549 .succeeds()
550 .stdout_is("a\tbb\nb\n");
551 }
552
553 #[test]
554 fn test_fold_characters_tab_advances_to_next_tab_stop() {
555 new_ucmd!()
556 .args(&["-c", "-w", "4"])
557 .pipe_in("ab\tcd\n")
558 .succeeds()
559 .stdout_is("ab\n\t\ncd\n");
560 }
561
562 #[test]
563 fn test_fold_characters_tab_with_non_ascii() {
564 new_ucmd!()
565 .args(&["-c", "-w", "2"])
566 .pipe_in("\u{00E9}\tb\n")
567 .succeeds()
568 .stdout_is("\u{00E9}\n\t\nb\n");
569 }
570
571 #[test]
572 fn test_fold_at_tab_as_word_boundary() {
573 new_ucmd!()
574 .args(&["-w8", "-s"])
575 .pipe_in("a\tbbb\n")
576 .succeeds()
577 .stdout_is("a\t\nbbb\n");
578 }
579
580 #[test]
581 fn test_fold_after_tab_as_word_boundary() {
582 new_ucmd!()
583 .args(&["-w10", "-s"])
584 .pipe_in("a\tbbb\n")
585 .succeeds()
586 .stdout_is("a\t\nbbb\n");
587 }
588
589 #[test]
590 fn test_fold_at_word_boundary_only_whitespace() {
591 new_ucmd!()
592 .args(&["-w2", "-s"])
593 .pipe_in(" ")
594 .succeeds()
595 .stdout_is(" \n ");
596 }
597
598 #[test]
599 fn test_fold_at_word_boundary_only_whitespace_preserve_final_newline() {
600 new_ucmd!()
601 .args(&["-w2", "-s"])
602 .pipe_in(" \n")
603 .succeeds()
604 .stdout_is(" \n \n");
605 }
606
607 #[test]
608 fn test_backspace_should_be_preserved() {
609 new_ucmd!().pipe_in("\x08").succeeds().stdout_is("\x08");
610 }
611
612 #[test]
613 fn test_backspaced_char_should_be_preserved() {
614 new_ucmd!().pipe_in("x\x08").succeeds().stdout_is("x\x08");
615 }
616
617 #[test]
618 fn test_backspace_should_decrease_column_count() {
619 new_ucmd!()
620 .arg("-w2")
621 .pipe_in("1\x08345")
622 .succeeds()
623 .stdout_is("1\x0834\n5");
624 }
625
626 #[test]
627 fn test_backspace_should_not_decrease_column_count_past_zero() {
628 new_ucmd!()
629 .arg("-w2")
630 .pipe_in("1\x08\x083456")
631 .succeeds()
632 .stdout_is("1\x08\x0834\n56");
633 }
634
635 #[test]
636 fn test_backspace_is_not_word_boundary() {
637 new_ucmd!()
638 .args(&["-w10", "-s"])
639 .pipe_in("foobar\x086789abcdef")
640 .succeeds()
641 .stdout_is("foobar\x086789a\nbcdef"); // spell-checker:disable-line
642 }
643
644 #[test]
645 fn test_carriage_return_should_be_preserved() {
646 new_ucmd!().pipe_in("\r").succeeds().stdout_is("\r");
647 }
648
649 #[test]
650 fn test_carriage_return_overwritten_char_should_be_preserved() {
651 new_ucmd!().pipe_in("x\ry").succeeds().stdout_is("x\ry");
652 }
653
654 #[test]
655 fn test_carriage_return_should_reset_column_count() {
656 new_ucmd!()
657 .arg("-w6")
658 .pipe_in("12345\r123456789abcdef")
659 .succeeds()
660 .stdout_is("12345\r123456\n789abc\ndef");
661 }
662
663 #[test]
664 fn test_carriage_return_is_not_word_boundary() {
665 new_ucmd!()
666 .args(&["-w6", "-s"])
667 .pipe_in("fizz\rbuzz\rfizzbuzz") // spell-checker:disable-line
668 .succeeds()
669 .stdout_is("fizz\rbuzz\rfizzbu\nzz"); // spell-checker:disable-line
670 }
671
672 //
673 // bytewise tests
674
675 #[test]
676 fn test_bytewise_should_preserve_empty_line_without_final_newline() {
677 new_ucmd!()
678 .args(&["-w2", "-b"])
679 .pipe_in("123\n\n45")
680 .succeeds()
681 .stdout_is("12\n3\n\n45");
682 }
683
684 #[test]
685 fn test_bytewise_should_preserve_empty_line_and_final_newline() {
686 new_ucmd!()
687 .args(&["-w2", "-b"])
688 .pipe_in("12\n\n34\n")
689 .succeeds()
690 .stdout_is("12\n\n34\n");
691 }
692
693 #[test]
694 fn test_bytewise_should_preserve_empty_lines() {
695 new_ucmd!()
696 .arg("-b")
697 .pipe_in("\n")
698 .succeeds()
699 .stdout_is("\n");
700
701 new_ucmd!()
702 .args(&["-w1", "-b"])
703 .pipe_in("0\n1\n\n2\n\n\n")
704 .succeeds()
705 .stdout_is("0\n1\n\n2\n\n\n");
706 }
707
708 #[test]
709 fn test_bytewise_word_boundary_split_should_preserve_empty_lines() {
710 new_ucmd!()
711 .args(&["-s", "-b"])
712 .pipe_in("\n")
713 .succeeds()
714 .stdout_is("\n");
715
716 new_ucmd!()
717 .args(&["-w1", "-s", "-b"])
718 .pipe_in("0\n1\n\n2\n\n\n")
719 .succeeds()
720 .stdout_is("0\n1\n\n2\n\n\n");
721 }
722
723 #[test]
724 fn test_bytewise_should_not_add_newline_when_line_less_than_fold() {
725 new_ucmd!()
726 .arg("-b")
727 .pipe_in("1234")
728 .succeeds()
729 .stdout_is("1234");
730 }
731
732 #[test]
733 fn test_bytewise_should_not_add_newline_when_line_longer_than_fold() {
734 new_ucmd!()
735 .args(&["-w2", "-b"])
736 .pipe_in("1234")
737 .succeeds()
738 .stdout_is("12\n34");
739 }
740
741 #[test]
742 fn test_bytewise_should_not_add_newline_when_line_equal_to_fold() {
743 new_ucmd!()
744 .args(&["-w1", "-b"])
745 .pipe_in(" ")
746 .succeeds()
747 .stdout_is(" ");
748 }
749
750 #[test]
751 fn test_bytewise_should_preserve_final_newline_when_line_less_than_fold() {
752 new_ucmd!()
753 .arg("-b")
754 .pipe_in("1234\n")
755 .succeeds()
756 .stdout_is("1234\n");
757 }
758
759 #[test]
760 fn test_bytewise_should_preserve_final_newline_when_line_longer_than_fold() {
761 new_ucmd!()
762 .args(&["-w2", "-b"])
763 .pipe_in("1234\n")
764 .succeeds()
765 .stdout_is("12\n34\n");
766 }
767
768 #[test]
769 fn test_bytewise_should_preserve_final_newline_when_line_equal_to_fold() {
770 new_ucmd!()
771 .args(&["-w2", "-b"])
772 .pipe_in("1\n")
773 .succeeds()
774 .stdout_is("1\n");
775 }
776
777 #[test]
778 fn test_bytewise_single_tab_should_not_add_extra_newline() {
779 new_ucmd!()
780 .args(&["-w1", "-b"])
781 .pipe_in("\t")
782 .succeeds()
783 .stdout_is("\t");
784 }
785
786 #[test]
787 fn test_tab_counts_as_one_byte() {
788 new_ucmd!()
789 .args(&["-w2", "-b"])
790 .pipe_in("1\t2\n")
791 .succeeds()
792 .stdout_is("1\t\n2\n");
793 }
794
795 #[test]
796 fn test_bytewise_fold_before_tab_with_narrow_width() {
797 new_ucmd!()
798 .args(&["-w7", "-b"])
799 .pipe_in("a\t1")
800 .succeeds()
801 .stdout_is("a\t1");
802 }
803
804 #[test]
805 fn test_bytewise_fold_at_word_boundary_only_whitespace() {
806 new_ucmd!()
807 .args(&["-w2", "-s", "-b"])
808 .pipe_in(" ")
809 .succeeds()
810 .stdout_is(" \n ");
811 }
812
813 #[test]
814 fn test_bytewise_fold_at_word_boundary_only_whitespace_preserve_final_newline() {
815 new_ucmd!()
816 .args(&["-w2", "-s", "-b"])
817 .pipe_in(" \n")
818 .succeeds()
819 .stdout_is(" \n \n");
820 }
821
822 #[test]
823 fn test_bytewise_fold_line_of_exactly_width_is_not_folded() {
824 // A line that is exactly `width` bytes long already fits, so -s must not
825 // break it at its last blank.
826 new_ucmd!()
827 .args(&["-w7", "-s", "-b"])
828 .pipe_in("aaa bbb\nccc ddd\n")
829 .succeeds()
830 .stdout_is("aaa bbb\nccc ddd\n");
831 }
832
833 #[test]
834 fn test_bytewise_fold_remainder_of_exactly_width_is_not_folded() {
835 // Same, for what is left of a line after a width-driven fold.
836 new_ucmd!()
837 .args(&["-w7", "-s", "-b"])
838 .pipe_in("aaa bbb ccc\n")
839 .succeeds()
840 .stdout_is("aaa \nbbb ccc\n");
841 }
842
843 #[test]
844 fn test_bytewise_fold_line_longer_than_width_still_folds() {
845 new_ucmd!()
846 .args(&["-w7", "-s", "-b"])
847 .pipe_in("aaa bbbb\n")
848 .succeeds()
849 .stdout_is("aaa \nbbbb\n");
850 }
851
852 #[test]
853 fn test_bytewise_backspace_should_be_preserved() {
854 new_ucmd!()
855 .arg("-b")
856 .pipe_in("\x08")
857 .succeeds()
858 .stdout_is("\x08");
859 }
860
861 #[test]
862 fn test_bytewise_backspaced_char_should_be_preserved() {
863 new_ucmd!()
864 .arg("-b")
865 .pipe_in("x\x08")
866 .succeeds()
867 .stdout_is("x\x08");
868 }
869
870 #[test]
871 fn test_bytewise_backspace_should_not_decrease_column_count() {
872 new_ucmd!()
873 .args(&["-w2", "-b"])
874 .pipe_in("1\x08345")
875 .succeeds()
876 .stdout_is("1\x08\n34\n5");
877 }
878
879 #[test]
880 fn test_bytewise_backspace_is_not_word_boundary() {
881 new_ucmd!()
882 .args(&["-w10", "-s", "-b"])
883 .pipe_in("foobar\x0889abcdef")
884 .succeeds()
885 .stdout_is("foobar\x0889a\nbcdef"); // spell-checker:disable-line
886 }
887
888 #[test]
889 fn test_bytewise_carriage_return_should_be_preserved() {
890 new_ucmd!()
891 .arg("-b")
892 .pipe_in("\r")
893 .succeeds()
894 .stdout_is("\r");
895 }
896
897 #[test]
898 fn test_bytewise_carriage_return_overwritten_char_should_be_preserved() {
899 new_ucmd!()
900 .arg("-b")
901 .pipe_in("x\ry")
902 .succeeds()
903 .stdout_is("x\ry");
904 }
905
906 #[test]
907 fn test_bytewise_carriage_return_should_not_reset_column_count() {
908 new_ucmd!()
909 .args(&["-w6", "-b"])
910 .pipe_in("12345\r123456789abcdef")
911 .succeeds()
912 .stdout_is("12345\r\n123456\n789abc\ndef");
913 }
914
915 #[test]
916 fn test_bytewise_carriage_return_is_not_word_boundary() {
917 new_ucmd!()
918 .args(&["-w6", "-s", "-b"])
919 .pipe_in("fizz\rbuzz\rfizzbuzz") // spell-checker:disable-line
920 .succeeds()
921 .stdout_is("fizz\rb\nuzz\rfi\nzzbuzz"); // spell-checker:disable-line
922 }
923
924 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
925 #[cfg_attr(
926 wasi_runner,
927 ignore = "WASI: killing the wasmtime process discards the unflushed output buffer, so the streamed bytes never reach stdout"
928 )]
929 #[test]
930 fn test_bytewise_read_from_pseudo_device() {
931 let mut child = new_ucmd!().arg("-b").arg("/dev/zero").run_no_wait();
932
933 child.make_assertion_with_delay(100).is_alive();
934
935 child
936 .kill()
937 .make_assertion()
938 .with_all_output()
939 .stdout_contains_bytes(b"\x00\x0a")
940 .no_stderr();
941 }
942
943 /// A fold boundary that lands exactly on the read-buffer boundary must still
944 /// insert the fold newline. The streaming reader fills at most `BufReader`
945 /// capacity per call, so folding at `width == capacity` is the case where the
946 /// byte following the fold point is not yet buffered.
947 #[cfg(not(target_arch = "wasm32"))]
948 #[test]
949 fn test_bytewise_fold_at_read_buffer_boundary() {
950 let width = buf_reader_capacity();
951 let input = vec![b'a'; width * 2];
952
953 let mut expected = vec![b'a'; width];
954 expected.push(b'\n');
955 expected.extend(std::iter::repeat_n(b'a', width));
956
957 new_ucmd!()
958 .args(&["-b", &format!("-w{width}")])
959 .pipe_in(input)
960 .succeeds()
961 .stdout_is_bytes(expected);
962 }
963
964 #[test]
965 fn test_continue_after_missing_file() {
966 // A nonexistent operand must not abort the run: the surrounding files are
967 // still folded, the error is reported on stderr, and the exit status is 1.
968 let ts = TestScenario::new(util_name!());
969 ts.fixtures.write("first.txt", "hello\n");
970 ts.fixtures.write("third.txt", "world\n");
971
972 ts.ucmd()
973 .args(&["first.txt", "absent.txt", "third.txt"])
974 .fails_with_code(1)
975 .stdout_is("hello\nworld\n")
976 .stderr_is("fold: absent.txt: No such file or directory\n");
977 }
978
979 #[test]
980 fn test_obsolete_syntax() {
981 new_ucmd!()
982 .arg("-5")
983 .arg("-s")
984 .arg("space_separated_words.txt")
985 .succeeds()
986 .stdout_is("test1\n \ntest2\n \ntest3\n \ntest4\n \ntest5\n \ntest6\n ");
987 }
988 #[test]
989 fn test_byte_break_at_non_utf8_character() {
990 new_ucmd!()
991 .arg("-b")
992 .arg("-s")
993 .arg("-w")
994 .arg("40")
995 .arg("non_utf8.input")
996 .succeeds()
997 .stdout_is_fixture_bytes("non_utf8.expected");
998 }
999 #[test]
1000 fn test_tab_advances_at_non_utf8_character() {
1001 new_ucmd!()
1002 .arg("-w8")
1003 .arg("non_utf8_tab_stops.input")
1004 .succeeds()
1005 .stdout_is_fixture_bytes("non_utf8_tab_stops_w8.expected");
1006 }
1007 #[test]
1008 fn test_all_tab_advances_at_non_utf8_character() {
1009 new_ucmd!()
1010 .arg("-w16")
1011 .arg("non_utf8_tab_stops.input")
1012 .succeeds()
1013 .stdout_is_fixture_bytes("non_utf8_tab_stops_w16.expected");
1014 }
1015
1016 #[test]
1017 fn test_combining_characters_nfc() {
1018 // e acute NFC form (single character)
1019 let e_acute_nfc = "\u{00E9}"; // é as single character
1020 new_ucmd!()
1021 .arg("-w2")
1022 .pipe_in(format!("{e_acute_nfc}{e_acute_nfc}{e_acute_nfc}"))
1023 .succeeds()
1024 .stdout_is(format!("{e_acute_nfc}{e_acute_nfc}\n{e_acute_nfc}"));
1025 }
1026
1027 #[test]
1028 fn test_combining_characters_nfd() {
1029 // e acute NFD form (base + combining acute)
1030 let e_acute_nfd = "e\u{0301}"; // e + combining acute accent
1031 new_ucmd!()
1032 .arg("-w2")
1033 .pipe_in(format!("{e_acute_nfd}{e_acute_nfd}{e_acute_nfd}"))
1034 .succeeds()
1035 .stdout_is(format!("{e_acute_nfd}{e_acute_nfd}\n{e_acute_nfd}"));
1036 }
1037
1038 #[test]
1039 fn test_fullwidth_characters() {
1040 // e fullwidth (takes 2 columns)
1041 let e_fullwidth = "\u{FF45}"; // e
1042 new_ucmd!()
1043 .arg("-w2")
1044 .pipe_in(format!("{e_fullwidth}{e_fullwidth}"))
1045 .succeeds()
1046 .stdout_is(format!("{e_fullwidth}\n{e_fullwidth}"));
1047 }
1048
1049 #[test]
1050 fn test_character_mode_special_chars() {
1051 for (args, input, expected) in [
1052 // backspace decreases column
1053 (&["-c", "-w", "5"][..], "abcde\x08fg\n", "abcde\x08f\ng\n"),
1054 // carriage return resets column
1055 (&["-c", "-w", "5"], "abcd\refgh\n", "abcd\refgh\n"),
1056 // tab at start exceeds width
1057 (&["-c", "-w", "4"], "\tabc\n", "\t\nabc\n"),
1058 // multiple tabs
1059 (&["-c", "-w", "10"], "a\tb\tc\n", "a\tb\n\tc\n"),
1060 // basic folding
1061 (&["-c", "-w", "3"], "abcdef\n", "abc\ndef\n"),
1062 // preserves empty lines
1063 (&["-c", "-w", "5"], "abc\n\ndef\n", "abc\n\ndef\n"),
1064 // word boundary with -s
1065 (&["-c", "-s", "-w", "5"], "ab cd ef\n", "ab \ncd ef\n"),
1066 // tab as word boundary
1067 (&["-c", "-s", "-w", "10"], "abcd\tefgh\n", "abcd\t\nefgh\n"),
1068 // wide chars count as 1 in -c mode
1069 (
1070 &["-c", "-w", "3"],
1071 "\u{FF1A}\u{FF1A}\u{FF1A}\u{FF1A}\n",
1072 "\u{FF1A}\u{FF1A}\u{FF1A}\n\u{FF1A}\n",
1073 ),
1074 ] {
1075 new_ucmd!()
1076 .args(args)
1077 .pipe_in(input)
1078 .succeeds()
1079 .stdout_is(expected);
1080 }
1081 }
1082
1083 #[test]
1084 fn test_width_zero() {
1085 new_ucmd!()
1086 .arg("-w")
1087 .arg("0")
1088 .fails_with_code(1)
1089 .stderr_is("fold: invalid number of columns: '0': Result too large\n");
1090 }
1091
1092 #[test]
1093 fn test_width_invalid() {
1094 for width in ["xyz", "12x", "12.5", "1 2"] {
1095 new_ucmd!()
1096 .arg("-w")
1097 .arg(width)
1098 .fails_with_code(1)
1099 .stderr_is(format!("fold: invalid number of columns: '{width}'\n"));
1100 }
1101 }
1102
1103 #[test]
1104 fn test_width_overflow() {
1105 new_ucmd!()
1106 .arg("-w")
1107 .arg("999999999999999999999")
1108 .fails_with_code(1)
1109 .stderr_is(
1110 "fold: invalid number of columns: '999999999999999999999': Value too large to be stored in data type\n",
1111 );
1112 }