Debian Codegraph @

tests/by-util/test_uniq.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 nabcd badoption schar
7
8 use uucore::posix::OBSOLETE;
9 use uutests::at_and_ucmd;
10 use uutests::new_ucmd;
11
12 static INPUT: &str = "sorted.txt";
13 static OUTPUT: &str = "sorted-output.txt";
14 static SKIP_CHARS: &str = "skip-chars.txt";
15 static SKIP_FIELDS: &str = "skip-fields.txt";
16 static SORTED_ZERO_TERMINATED: &str = "sorted-zero-terminated.txt";
17
18 #[test]
19 fn test_invalid_arg() {
20 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
21 }
22
23 #[test]
24 fn test_help_and_version_on_stdout() {
25 new_ucmd!()
26 .arg("--help")
27 .succeeds()
28 .no_stderr()
29 .stdout_contains("Usage");
30 new_ucmd!()
31 .arg("--version")
32 .succeeds()
33 .no_stderr()
34 .stdout_contains("uniq");
35 }
36
37 #[test]
38 fn test_stdin_default() {
39 new_ucmd!()
40 .pipe_in_fixture(INPUT)
41 .succeeds()
42 .stdout_is_fixture("sorted-simple.expected");
43 }
44
45 #[test]
46 fn test_single_default() {
47 new_ucmd!()
48 .arg(INPUT)
49 .succeeds()
50 .stdout_is_fixture("sorted-simple.expected");
51 }
52
53 #[test]
54 fn test_single_default_output() {
55 let (at, mut ucmd) = at_and_ucmd!();
56 let expected = at.read("sorted-simple.expected");
57 ucmd.args(&[INPUT, OUTPUT]).succeeds();
58 let found = at.read(OUTPUT);
59 assert_eq!(found, expected);
60 }
61
62 #[test]
63 fn test_stdin_counts() {
64 new_ucmd!()
65 .args(&["-c"])
66 .pipe_in_fixture(INPUT)
67 .succeeds()
68 .stdout_is_fixture("sorted-counts.expected");
69 }
70
71 #[test]
72 fn test_stdin_skip_1_char() {
73 new_ucmd!()
74 .args(&["-s1"])
75 .pipe_in_fixture(SKIP_CHARS)
76 .succeeds()
77 .stdout_is_fixture("skip-1-char.expected");
78 }
79
80 #[test]
81 fn test_stdin_skip_5_chars() {
82 new_ucmd!()
83 .args(&["-s5"])
84 .pipe_in_fixture(SKIP_CHARS)
85 .succeeds()
86 .stdout_is_fixture("skip-5-chars.expected");
87 }
88
89 #[test]
90 fn test_stdin_skip_and_check_2_chars() {
91 new_ucmd!()
92 .args(&["-s3", "-w2"])
93 .pipe_in_fixture(SKIP_CHARS)
94 .succeeds()
95 .stdout_is_fixture("skip-3-check-2-chars.expected");
96 }
97
98 #[test]
99 fn test_stdin_skip_2_fields() {
100 new_ucmd!()
101 .args(&["-f2"])
102 .pipe_in_fixture(SKIP_FIELDS)
103 .succeeds()
104 .stdout_is_fixture("skip-2-fields.expected");
105 }
106
107 #[test]
108 fn test_stdin_skip_2_fields_obsolete() {
109 new_ucmd!()
110 .args(&["-2"])
111 .pipe_in_fixture(SKIP_FIELDS)
112 .succeeds()
113 .stdout_is_fixture("skip-2-fields.expected");
114 }
115
116 #[test]
117 fn test_stdin_skip_21_fields() {
118 new_ucmd!()
119 .args(&["-f21"])
120 .pipe_in_fixture(SKIP_FIELDS)
121 .succeeds()
122 .stdout_is_fixture("skip-21-fields.expected");
123 }
124
125 #[test]
126 fn test_stdin_skip_21_fields_obsolete() {
127 new_ucmd!()
128 .args(&["-21"])
129 .pipe_in_fixture(SKIP_FIELDS)
130 .succeeds()
131 .stdout_is_fixture("skip-21-fields.expected");
132 }
133
134 #[test]
135 fn test_stdin_skip_invalid_fields_obsolete() {
136 new_ucmd!()
137 .args(&["-5q"])
138 .fails()
139 .stderr_contains("error: unexpected argument '-q' found\n");
140 }
141
142 #[test]
143 fn test_stdin_all_repeated() {
144 new_ucmd!()
145 .args(&["--all-repeated"])
146 .pipe_in_fixture(INPUT)
147 .succeeds()
148 .stdout_is_fixture("sorted-all-repeated.expected");
149 new_ucmd!()
150 .args(&["--all-repeated=none"])
151 .pipe_in_fixture(INPUT)
152 .succeeds()
153 .stdout_is_fixture("sorted-all-repeated.expected");
154 new_ucmd!()
155 .args(&["--all-repeated=non"])
156 .pipe_in_fixture(INPUT)
157 .succeeds()
158 .stdout_is_fixture("sorted-all-repeated.expected");
159 new_ucmd!()
160 .args(&["--all-repeated=n"])
161 .pipe_in_fixture(INPUT)
162 .succeeds()
163 .stdout_is_fixture("sorted-all-repeated.expected");
164 }
165
166 #[test]
167 fn test_repeated_all_repeated_uses_final_delimiter() {
168 const DUPLICATE_RUNS: &str = "copper\ncopper\nsilver\ngold\ngold\nplatinum\n";
169
170 new_ucmd!()
171 .args(&["--all-repeated=prepend", "-D"])
172 .pipe_in(DUPLICATE_RUNS)
173 .succeeds()
174 .stdout_is("copper\ncopper\ngold\ngold\n");
175
176 new_ucmd!()
177 .args(&["-D", "--all-repeated=separate"])
178 .pipe_in(DUPLICATE_RUNS)
179 .succeeds()
180 .stdout_is("copper\ncopper\n\ngold\ngold\n");
181 }
182
183 #[test]
184 fn test_all_repeated_followed_by_filename() {
185 let filename = "test.txt";
186 let (at, mut ucmd) = at_and_ucmd!();
187
188 at.write(filename, "a\na\n");
189
190 ucmd.args(&["--all-repeated", filename])
191 .succeeds()
192 .stdout_is("a\na\n");
193 }
194
195 #[test]
196 fn test_stdin_all_repeated_separate() {
197 new_ucmd!()
198 .args(&["--all-repeated=separate"])
199 .pipe_in_fixture(INPUT)
200 .succeeds()
201 .stdout_is_fixture("sorted-all-repeated-separate.expected");
202 new_ucmd!()
203 .args(&["--all-repeated=separat"]) // spell-checker:disable-line
204 .pipe_in_fixture(INPUT)
205 .succeeds()
206 .stdout_is_fixture("sorted-all-repeated-separate.expected");
207 new_ucmd!()
208 .args(&["--all-repeated=s"])
209 .pipe_in_fixture(INPUT)
210 .succeeds()
211 .stdout_is_fixture("sorted-all-repeated-separate.expected");
212 }
213
214 #[test]
215 fn test_stdin_all_repeated_prepend() {
216 new_ucmd!()
217 .args(&["--all-repeated=prepend"])
218 .pipe_in_fixture(INPUT)
219 .succeeds()
220 .stdout_is_fixture("sorted-all-repeated-prepend.expected");
221 new_ucmd!()
222 .args(&["--all-repeated=prepen"]) // spell-checker:disable-line
223 .pipe_in_fixture(INPUT)
224 .succeeds()
225 .stdout_is_fixture("sorted-all-repeated-prepend.expected");
226 new_ucmd!()
227 .args(&["--all-repeated=p"])
228 .pipe_in_fixture(INPUT)
229 .succeeds()
230 .stdout_is_fixture("sorted-all-repeated-prepend.expected");
231 }
232
233 #[test]
234 fn test_stdin_unique_only() {
235 new_ucmd!()
236 .args(&["-u"])
237 .pipe_in_fixture(INPUT)
238 .succeeds()
239 .stdout_is_fixture("sorted-unique-only.expected");
240 }
241
242 #[test]
243 fn test_stdin_repeated_only() {
244 new_ucmd!()
245 .args(&["-d"])
246 .pipe_in_fixture(INPUT)
247 .succeeds()
248 .stdout_is_fixture("sorted-repeated-only.expected");
249 }
250
251 #[test]
252 fn test_stdin_ignore_case() {
253 new_ucmd!()
254 .args(&["-i"])
255 .pipe_in_fixture(INPUT)
256 .succeeds()
257 .stdout_is_fixture("sorted-ignore-case.expected");
258 }
259
260 #[test]
261 fn test_stdin_zero_terminated() {
262 new_ucmd!()
263 .args(&["-z"])
264 .pipe_in_fixture(SORTED_ZERO_TERMINATED)
265 .succeeds()
266 .stdout_is_fixture("sorted-zero-terminated.expected");
267 }
268
269 #[test]
270 fn test_gnu_locale_fr_schar() {
271 new_ucmd!()
272 .args(&["-f1", "locale-fr-schar.txt"])
273 .env("LC_ALL", "C")
274 .succeeds()
275 .stdout_is_fixture_bytes("locale-fr-schar.txt");
276 }
277
278 #[test]
279 fn test_group() {
280 new_ucmd!()
281 .args(&["--group"])
282 .pipe_in_fixture(INPUT)
283 .succeeds()
284 .stdout_is_fixture("group.expected");
285 }
286
287 #[test]
288 fn test_group_followed_by_filename() {
289 let filename = "test.txt";
290 let (at, mut ucmd) = at_and_ucmd!();
291
292 at.write(filename, "a\na\n");
293
294 ucmd.args(&["--group", filename])
295 .succeeds()
296 .stdout_is("a\na\n");
297 }
298
299 #[test]
300 fn test_group_prepend() {
301 new_ucmd!()
302 .args(&["--group=prepend"])
303 .pipe_in_fixture(INPUT)
304 .succeeds()
305 .stdout_is_fixture("group-prepend.expected");
306 new_ucmd!()
307 .args(&["--group=p"])
308 .pipe_in_fixture(INPUT)
309 .succeeds()
310 .stdout_is_fixture("group-prepend.expected");
311 }
312
313 #[test]
314 fn test_group_append() {
315 new_ucmd!()
316 .args(&["--group=append"])
317 .pipe_in_fixture(INPUT)
318 .succeeds()
319 .stdout_is_fixture("group-append.expected");
320 new_ucmd!()
321 .args(&["--group=a"])
322 .pipe_in_fixture(INPUT)
323 .succeeds()
324 .stdout_is_fixture("group-append.expected");
325 }
326
327 #[test]
328 fn test_group_both() {
329 new_ucmd!()
330 .args(&["--group=both"])
331 .pipe_in_fixture(INPUT)
332 .succeeds()
333 .stdout_is_fixture("group-both.expected");
334 new_ucmd!()
335 .args(&["--group=bot"])
336 .pipe_in_fixture(INPUT)
337 .succeeds()
338 .stdout_is_fixture("group-both.expected");
339 new_ucmd!()
340 .args(&["--group=b"])
341 .pipe_in_fixture(INPUT)
342 .succeeds()
343 .stdout_is_fixture("group-both.expected");
344 }
345
346 #[test]
347 fn test_group_separate() {
348 new_ucmd!()
349 .args(&["--group=separate"])
350 .pipe_in_fixture(INPUT)
351 .succeeds()
352 .stdout_is_fixture("group.expected");
353 new_ucmd!()
354 .args(&["--group=s"])
355 .pipe_in_fixture(INPUT)
356 .succeeds()
357 .stdout_is_fixture("group.expected");
358 }
359
360 #[test]
361 fn test_case2() {
362 new_ucmd!().pipe_in("a\na\n").succeeds().stdout_is("a\n");
363 }
364
365 struct TestCase {
366 name: &'static str,
367 args: &'static [&'static str],
368 input: &'static str,
369 stdout: Option<&'static str>,
370 stderr: Option<&'static str>,
371 exit: Option<i32>,
372 }
373
374 #[test]
375 #[allow(clippy::too_many_lines)]
376 fn uniq_basic_dedup_cases() {
377 let cases = [
378 TestCase {
379 name: "tc_01",
380 args: &[],
381 input: "",
382 stdout: Some(""),
383 stderr: None,
384 exit: None,
385 },
386 TestCase {
387 name: "tc_02",
388 args: &[],
389 input: "x\nx\n",
390 stdout: Some("x\n"),
391 stderr: None,
392 exit: None,
393 },
394 TestCase {
395 name: "tc_03",
396 args: &[],
397 input: "x\nx",
398 stdout: Some("x\n"),
399 stderr: None,
400 exit: None,
401 },
402 TestCase {
403 name: "tc_04",
404 args: &[],
405 input: "p\nq",
406 stdout: Some("p\nq\n"),
407 stderr: None,
408 exit: None,
409 },
410 TestCase {
411 name: "tc_05",
412 args: &[],
413 input: "x\nx\ny",
414 stdout: Some("x\ny\n"),
415 stderr: None,
416 exit: None,
417 },
418 TestCase {
419 name: "tc_06",
420 args: &[],
421 input: "q\np\np\n",
422 stdout: Some("q\np\n"),
423 stderr: None,
424 exit: None,
425 },
426 TestCase {
427 name: "tc_07",
428 args: &[],
429 input: "r\ns\nt\n",
430 stdout: Some("r\ns\nt\n"),
431 stderr: None,
432 exit: None,
433 },
434 TestCase {
435 name: "2z",
436 args: &["-z"],
437 input: "x\nx\n",
438 stdout: Some("x\nx\n\0"),
439 stderr: None,
440 exit: None,
441 },
442 TestCase {
443 name: "3z",
444 args: &["-z"],
445 input: "a\na",
446 stdout: Some("a\na\0"),
447 stderr: None,
448 exit: None,
449 },
450 TestCase {
451 name: "4z",
452 args: &["-z"],
453 input: "a\nb",
454 stdout: Some("a\nb\0"),
455 stderr: None,
456 exit: None,
457 },
458 TestCase {
459 name: "5z",
460 args: &["-z"],
461 input: "a\na\nb",
462 stdout: Some("a\na\nb\0"),
463 stderr: None,
464 exit: None,
465 },
466 TestCase {
467 name: "10z",
468 args: &["-z", "-f1"],
469 input: "a\nb\n\0c\nb\n\0",
470 stdout: Some("a\nb\n\0"),
471 stderr: None,
472 exit: None,
473 },
474 TestCase {
475 name: "20z",
476 args: &["-dz"],
477 input: "a\na\n",
478 stdout: Some(""),
479 stderr: None,
480 exit: None,
481 },
482 TestCase {
483 name: "8",
484 args: &[],
485 input: "ö\nv\n",
486 stdout: Some("ö\nv\n"),
487 stderr: None,
488 exit: None,
489 },
490 TestCase {
491 name: "9",
492 args: &["-u"],
493 input: "a\na\n",
494 stdout: Some(""),
495 stderr: None,
496 exit: None,
497 },
498 TestCase {
499 name: "10",
500 args: &["-u"],
501 input: "a\nb\n",
502 stdout: Some("a\nb\n"),
503 stderr: None,
504 exit: None,
505 },
506 TestCase {
507 name: "11",
508 args: &["-u"],
509 input: "a\nb\na\n",
510 stdout: Some("a\nb\na\n"),
511 stderr: None,
512 exit: None,
513 },
514 TestCase {
515 name: "12",
516 args: &["-u"],
517 input: "a\na\n",
518 stdout: Some(""),
519 stderr: None,
520 exit: None,
521 },
522 TestCase {
523 name: "13",
524 args: &["-u"],
525 input: "a\na\n",
526 stdout: Some(""),
527 stderr: None,
528 exit: None,
529 },
530 TestCase {
531 name: "20",
532 args: &["-d"],
533 input: "a\na\n",
534 stdout: Some("a\n"),
535 stderr: None,
536 exit: None,
537 },
538 TestCase {
539 name: "21",
540 args: &["-d"],
541 input: "a\nb\n",
542 stdout: Some(""),
543 stderr: None,
544 exit: None,
545 },
546 TestCase {
547 name: "22",
548 args: &["-d"],
549 input: "a\nb\na\n",
550 stdout: Some(""),
551 stderr: None,
552 exit: None,
553 },
554 TestCase {
555 name: "23",
556 args: &["-d"],
557 input: "a\na\nb\n",
558 stdout: Some("a\n"),
559 stderr: None,
560 exit: None,
561 },
562 // Obsolete syntax for "-f 1"
563 TestCase {
564 name: "obs30",
565 args: &["-1"],
566 input: "a a\nb a\n",
567 stdout: Some("a a\n"),
568 stderr: None,
569 exit: None,
570 },
571 TestCase {
572 name: "31",
573 args: &["-f", "1"],
574 input: "a a\nb a\n",
575 stdout: Some("a a\n"),
576 stderr: None,
577 exit: None,
578 },
579 TestCase {
580 name: "32",
581 args: &["-f", "1"],
582 input: "a a\nb b\n",
583 stdout: Some("a a\nb b\n"),
584 stderr: None,
585 exit: None,
586 },
587 TestCase {
588 name: "33",
589 args: &["-f", "1"],
590 input: "a a a\nb a c\n",
591 stdout: Some("a a a\nb a c\n"),
592 stderr: None,
593 exit: None,
594 },
595 TestCase {
596 name: "34",
597 args: &["-f", "1"],
598 input: "b a\na a\n",
599 stdout: Some("b a\n"),
600 stderr: None,
601 exit: None,
602 },
603 TestCase {
604 name: "35",
605 args: &["-f", "2"],
606 input: "a a c\nb a c\n",
607 stdout: Some("a a c\n"),
608 stderr: None,
609 exit: None,
610 },
611 // Obsolete syntax for "-s 1"
612 TestCase {
613 name: "obs-plus40",
614 args: &["+1"],
615 input: "aaa\naaa\n",
616 stdout: Some("aaa\n"),
617 stderr: None,
618 exit: None,
619 },
620 TestCase {
621 name: "obs-plus41",
622 args: &["+1"],
623 input: "baa\naaa\n",
624 stdout: Some("baa\n"),
625 stderr: None,
626 exit: None,
627 },
628 TestCase {
629 name: "42",
630 args: &["-s", "1"],
631 input: "aaa\naaa\n",
632 stdout: Some("aaa\n"),
633 stderr: None,
634 exit: None,
635 },
636 TestCase {
637 name: "43",
638 args: &["-s", "2"],
639 input: "baa\naaa\n",
640 stdout: Some("baa\n"),
641 stderr: None,
642 exit: None,
643 },
644 // Obsolete syntax for "-s 1"
645 TestCase {
646 name: "obs-plus44",
647 args: &["+1", "--"],
648 input: "aaa\naaa\n",
649 stdout: Some("aaa\n"),
650 stderr: None,
651 exit: None,
652 },
653 TestCase {
654 name: "obs-plus45",
655 args: &["+1", "--"],
656 input: "baa\naaa\n",
657 stdout: Some("baa\n"),
658 stderr: None,
659 exit: None,
660 },
661 TestCase {
662 name: "50",
663 args: &["-f", "1", "-s", "1"],
664 input: "a aaa\nb ab\n",
665 stdout: Some("a aaa\nb ab\n"),
666 stderr: None,
667 exit: None,
668 },
669 TestCase {
670 name: "51",
671 args: &["-f", "1", "-s", "1"],
672 input: "a aaa\nb aaa\n",
673 stdout: Some("a aaa\n"),
674 stderr: None,
675 exit: None,
676 },
677 TestCase {
678 name: "52",
679 args: &["-s", "1", "-f", "1"],
680 input: "a aaa\nb ab\n",
681 stdout: Some("a aaa\nb ab\n"),
682 stderr: None,
683 exit: None,
684 },
685 TestCase {
686 name: "53",
687 args: &["-s", "1", "-f", "1"],
688 input: "a aaa\nb aaa\n",
689 stdout: Some("a aaa\n"),
690 stderr: None,
691 exit: None,
692 },
693 TestCase {
694 name: "54",
695 args: &["-s", "4"],
696 input: "abc\nabcd\n",
697 stdout: Some("abc\n"),
698 stderr: None,
699 exit: None,
700 },
701 TestCase {
702 name: "55",
703 args: &["-s", "0"],
704 input: "abc\nabcd\n",
705 stdout: Some("abc\nabcd\n"),
706 stderr: None,
707 exit: None,
708 },
709 TestCase {
710 name: "56",
711 args: &["-s", "0"],
712 input: "abc\n",
713 stdout: Some("abc\n"),
714 stderr: None,
715 exit: None,
716 },
717 TestCase {
718 name: "57",
719 args: &["-w", "0"],
720 input: "abc\nabcd\n",
721 stdout: Some("abc\n"),
722 stderr: None,
723 exit: None,
724 },
725 TestCase {
726 name: "60",
727 args: &["-w", "1"],
728 input: "a a\nb a\n",
729 stdout: Some("a a\nb a\n"),
730 stderr: None,
731 exit: None,
732 },
733 TestCase {
734 name: "61",
735 args: &["-w", "3"],
736 input: "a a\nb a\n",
737 stdout: Some("a a\nb a\n"),
738 stderr: None,
739 exit: None,
740 },
741 TestCase {
742 name: "62",
743 args: &["-w", "1", "-f", "1"],
744 input: "a a a\nb a c\n",
745 stdout: Some("a a a\n"),
746 stderr: None,
747 exit: None,
748 },
749 TestCase {
750 name: "63",
751 args: &["-f", "1", "-w", "1"],
752 input: "a a a\nb a c\n",
753 stdout: Some("a a a\n"),
754 stderr: None,
755 exit: None,
756 },
757 TestCase {
758 name: "64",
759 args: &["-f", "1", "-w", "4"],
760 input: "a a a\nb a c\n",
761 stdout: Some("a a a\nb a c\n"),
762 stderr: None,
763 exit: None,
764 },
765 TestCase {
766 name: "65",
767 args: &["-f", "1", "-w", "3"],
768 input: "a a a\nb a c\n",
769 stdout: Some("a a a\n"),
770 stderr: None,
771 exit: None,
772 },
773 TestCase {
774 name: "90",
775 args: &[],
776 input: "a\0a\na\n",
777 stdout: Some("a\0a\na\n"),
778 stderr: None,
779 exit: None,
780 },
781 TestCase {
782 name: "91",
783 args: &[],
784 input: "a\ta\na a\n",
785 stdout: Some("a\ta\na a\n"),
786 stderr: None,
787 exit: None,
788 },
789 TestCase {
790 name: "92",
791 args: &["-f", "1"],
792 input: "a\ta\na a\n",
793 stdout: Some("a\ta\na a\n"),
794 stderr: None,
795 exit: None,
796 },
797 TestCase {
798 name: "93",
799 args: &["-f", "2"],
800 input: "a\ta a\na a a\n",
801 stdout: Some("a\ta a\n"),
802 stderr: None,
803 exit: None,
804 },
805 TestCase {
806 name: "94",
807 args: &["-f", "1"],
808 input: "a\ta\na\ta\n",
809 stdout: Some("a\ta\n"),
810 stderr: None,
811 exit: None,
812 },
813 TestCase {
814 name: "101",
815 args: &["-c"],
816 input: "a\nb\n",
817 stdout: Some(" 1 a\n 1 b\n"),
818 stderr: None,
819 exit: None,
820 },
821 TestCase {
822 name: "102",
823 args: &["-c"],
824 input: "a\na\n",
825 stdout: Some(" 2 a\n"),
826 stderr: None,
827 exit: None,
828 },
829 TestCase {
830 name: "110",
831 args: &["-D"],
832 input: "a\na\n",
833 stdout: Some("a\na\n"),
834 stderr: None,
835 exit: None,
836 },
837 TestCase {
838 name: "111",
839 args: &["-D", "-w1"],
840 input: "a a\na b\n",
841 stdout: Some("a a\na b\n"),
842 stderr: None,
843 exit: None,
844 },
845 TestCase {
846 name: "112",
847 args: &["-D", "-c"],
848 input: "", // Note: Different from GNU test, but should not matter
849 stdout: Some(""),
850 stderr: Some(concat!(
851 "uniq: printing all duplicated lines and repeat counts is meaningless\n",
852 "Try 'uniq --help' for more information.\n"
853 )),
854 exit: Some(1),
855 },
856 TestCase {
857 name: "113",
858 args: &["--all-repeated=separate"],
859 input: "a\na\n",
860 stdout: Some("a\na\n"),
861 stderr: None,
862 exit: None,
863 },
864 TestCase {
865 name: "114",
866 args: &["--all-repeated=separate"],
867 input: "a\na\nb\nc\nc\n",
868 stdout: Some("a\na\n\nc\nc\n"),
869 stderr: None,
870 exit: None,
871 },
872 TestCase {
873 name: "115",
874 args: &["--all-repeated=separate"],
875 input: "a\na\nb\nb\nc\n",
876 stdout: Some("a\na\n\nb\nb\n"),
877 stderr: None,
878 exit: None,
879 },
880 TestCase {
881 name: "116",
882 args: &["--all-repeated=prepend"],
883 input: "a\na\n",
884 stdout: Some("\na\na\n"),
885 stderr: None,
886 exit: None,
887 },
888 TestCase {
889 name: "117",
890 args: &["--all-repeated=prepend"],
891 input: "a\na\nb\nc\nc\n",
892 stdout: Some("\na\na\n\nc\nc\n"),
893 stderr: None,
894 exit: None,
895 },
896 TestCase {
897 name: "118",
898 args: &["--all-repeated=prepend"],
899 input: "a\nb\n",
900 stdout: Some(""),
901 stderr: None,
902 exit: None,
903 },
904 TestCase {
905 name: "119",
906 args: &["--all-repeated=badoption"],
907 input: "", // Note: Different from GNU test, but should not matter
908 stdout: Some(""),
909 stderr: Some(concat!(
910 "error: invalid value 'badoption' for '--all-repeated[=<delimit-method>]'\n",
911 "\n",
912 " [possible values: none, prepend, separate]\n",
913 "\n",
914 "For more information, try '--help'.\n"
915 )),
916 exit: Some(1),
917 },
918 // \x08 is the backspace char
919 TestCase {
920 name: "120",
921 args: &["-d", "-u"],
922 input: "a\na\n\x08",
923 stdout: Some(""),
924 stderr: None,
925 exit: None,
926 },
927 // u128::MAX = 340282366920938463463374607431768211455
928 TestCase {
929 name: "121",
930 args: &["-d", "-u", "-w340282366920938463463374607431768211456"],
931 input: "a\na\n\x08",
932 stdout: Some(""),
933 stderr: None,
934 exit: None,
935 },
936 // Test 122 is the same as 121, just different big int overflow number
937 TestCase {
938 name: "123",
939 args: &["--zero-terminated"],
940 input: "a\na\nb",
941 stdout: Some("a\na\nb\0"),
942 stderr: None,
943 exit: None,
944 },
945 TestCase {
946 name: "124",
947 args: &["--zero-terminated"],
948 input: "a\0a\0b",
949 stdout: Some("a\0b\0"),
950 stderr: None,
951 exit: None,
952 },
953 TestCase {
954 name: "125",
955 args: &[],
956 input: "A\na\n",
957 stdout: Some("A\na\n"),
958 stderr: None,
959 exit: None,
960 },
961 TestCase {
962 name: "126",
963 args: &["-i"],
964 input: "A\na\n",
965 stdout: Some("A\n"),
966 stderr: None,
967 exit: None,
968 },
969 TestCase {
970 name: "127",
971 args: &["--ignore-case"],
972 input: "A\na\n",
973 stdout: Some("A\n"),
974 stderr: None,
975 exit: None,
976 },
977 TestCase {
978 name: "128",
979 args: &["--group=prepend"],
980 input: "a\na\nb\n",
981 stdout: Some("\na\na\n\nb\n"),
982 stderr: None,
983 exit: None,
984 },
985 TestCase {
986 name: "129",
987 args: &["--group=append"],
988 input: "a\na\nb\n",
989 stdout: Some("a\na\n\nb\n\n"),
990 stderr: None,
991 exit: None,
992 },
993 TestCase {
994 name: "130",
995 args: &["--group=separate"],
996 input: "a\na\nb\n",
997 stdout: Some("a\na\n\nb\n"),
998 stderr: None,
999 exit: None,
1000 },
1001 TestCase {
1002 name: "131",
1003 args: &["--group"],
1004 input: "a\na\nb\n",
1005 stdout: Some("a\na\n\nb\n"),
1006 stderr: None,
1007 exit: None,
1008 },
1009 TestCase {
1010 name: "132",
1011 args: &["--group=both"],
1012 input: "a\na\nb\n",
1013 stdout: Some("\na\na\n\nb\n\n"),
1014 stderr: None,
1015 exit: None,
1016 },
1017 TestCase {
1018 name: "133",
1019 args: &["--group=prepend"],
1020 input: "a\na\n",
1021 stdout: Some("\na\na\n"),
1022 stderr: None,
1023 exit: None,
1024 },
1025 TestCase {
1026 name: "134",
1027 args: &["--group=append"],
1028 input: "a\na\n",
1029 stdout: Some("a\na\n\n"),
1030 stderr: None,
1031 exit: None,
1032 },
1033 TestCase {
1034 name: "135",
1035 args: &["--group=separate"],
1036 input: "a\na\n",
1037 stdout: Some("a\na\n"),
1038 stderr: None,
1039 exit: None,
1040 },
1041 TestCase {
1042 name: "136",
1043 args: &["--group"],
1044 input: "a\na\n",
1045 stdout: Some("a\na\n"),
1046 stderr: None,
1047 exit: None,
1048 },
1049 TestCase {
1050 name: "137",
1051 args: &["--group=prepend"],
1052 input: "",
1053 stdout: Some(""),
1054 stderr: None,
1055 exit: None,
1056 },
1057 TestCase {
1058 name: "138",
1059 args: &["--group=append"],
1060 input: "",
1061 stdout: Some(""),
1062 stderr: None,
1063 exit: None,
1064 },
1065 TestCase {
1066 name: "139",
1067 args: &["--group=separate"],
1068 input: "",
1069 stdout: Some(""),
1070 stderr: None,
1071 exit: None,
1072 },
1073 TestCase {
1074 name: "140",
1075 args: &["--group=both"],
1076 input: "",
1077 stdout: Some(""),
1078 stderr: None,
1079 exit: None,
1080 },
1081 TestCase {
1082 name: "141",
1083 args: &["--group", "-c"],
1084 input: "",
1085 stdout: Some(""),
1086 stderr: Some(concat!(
1087 "error: the argument '--group[=<group-method>]' cannot be used with '--count'\n",
1088 "\n",
1089 "For more information, try '--help'.\n"
1090 )),
1091 exit: Some(1),
1092 },
1093 TestCase {
1094 name: "142",
1095 args: &["--group", "-d"],
1096 input: "",
1097 stdout: Some(""),
1098 stderr: Some(concat!(
1099 "error: the argument '--group[=<group-method>]' cannot be used with '--repeated'\n",
1100 "\n",
1101 "For more information, try '--help'.\n"
1102 )),
1103 exit: Some(1),
1104 },
1105 TestCase {
1106 name: "143",
1107 args: &["--group", "-u"],
1108 input: "",
1109 stdout: Some(""),
1110 stderr: Some(concat!(
1111 "error: the argument '--group[=<group-method>]' cannot be used with '--unique'\n",
1112 "\n",
1113 "For more information, try '--help'.\n"
1114 )),
1115 exit: Some(1),
1116 },
1117 TestCase {
1118 name: "144",
1119 args: &["--group", "-D"],
1120 input: "",
1121 stdout: Some(""),
1122 stderr: Some(concat!(
1123 "error: the argument '--group[=<group-method>]' cannot be used with '--all-repeated[=<delimit-method>]'\n",
1124 "\n",
1125 "For more information, try '--help'.\n"
1126 )),
1127 exit: Some(1),
1128 },
1129 TestCase {
1130 name: "145",
1131 args: &["--group=badoption"],
1132 input: "",
1133 stdout: Some(""),
1134 stderr: Some(concat!(
1135 "error: invalid value 'badoption' for '--group[=<group-method>]'\n",
1136 "\n",
1137 " [possible values: separate, prepend, append, both]\n",
1138 "\n",
1139 "For more information, try '--help'.\n"
1140 )),
1141 exit: Some(1),
1142 },
1143 ];
1144
1145 // run regular version of tests with regular file as input
1146 for case in cases {
1147 // prep input file
1148 let (at, mut ucmd) = at_and_ucmd!();
1149 at.write("input-file", case.input);
1150
1151 // first - run a version of tests with regular file as input
1152 eprintln!("Test {}", case.name);
1153 // set environment variable for obsolete skip char option tests
1154 if case.name.starts_with("obs-plus") {
1155 ucmd.env("_POSIX2_VERSION", OBSOLETE.to_string());
1156 }
1157 let result = ucmd.args(case.args).arg("input-file").run();
1158 if let Some(stdout) = case.stdout {
1159 result.stdout_is(stdout);
1160 }
1161 if let Some(stderr) = case.stderr {
1162 result.stderr_is(stderr);
1163 }
1164 if let Some(exit) = case.exit {
1165 result.code_is(exit);
1166 }
1167
1168 // then - ".stdin" version of tests with input piped in
1169 // NOTE: GNU has another variant for stdin redirect from a file
1170 // as in `uniq < input-file`
1171 // For now we treat it as equivalent of piped in stdin variant
1172 // as in `cat input-file | uniq`
1173 eprintln!("Test {}.stdin", case.name);
1174 // set environment variable for obsolete skip char option tests
1175 let mut ucmd = new_ucmd!();
1176 if case.name.starts_with("obs-plus") {
1177 ucmd.env("_POSIX2_VERSION", OBSOLETE.to_string());
1178 }
1179 let result = ucmd.args(case.args).run_piped_stdin(case.input);
1180 if let Some(stdout) = case.stdout {
1181 result.stdout_is(stdout);
1182 }
1183 if let Some(stderr) = case.stderr {
1184 result.stderr_is(stderr);
1185 }
1186 if let Some(exit) = case.exit {
1187 result.code_is(exit);
1188 }
1189 }
1190 }
1191
1192 #[test]
1193 fn test_stdin_w1_multibyte() {
1194 let input = "à\ná\n";
1195 new_ucmd!()
1196 .args(&["-w1"])
1197 .env("LC_ALL", "en_US.UTF-8")
1198 .pipe_in(input)
1199 .succeeds()
1200 .stdout_is("à\ná\n");
1201 }
1202
1203 #[test]
1204 fn test_c_locale_counts_bytes() {
1205 let input = "가나다라마\n가나다바사\n";
1206 new_ucmd!()
1207 .args(&["-w", "4"])
1208 .env("LC_ALL", "C")
1209 .pipe_in(input)
1210 .succeeds()
1211 .stdout_is("가나다라마\n");
1212 }
1213
1214 #[cfg(target_os = "linux")]
1215 #[test]
1216 fn test_failed_write_is_reported() {
1217 new_ucmd!()
1218 .pipe_in("hello")
1219 .args(&["-z"])
1220 .set_stdout(std::fs::File::create("/dev/full").unwrap())
1221 .fails()
1222 .stderr_is("uniq: write error: No space left on device\n");
1223 }
1224
1225 #[test]
1226 fn test_repeated_skip_fields_takes_the_last() {
1227 // GNU lets a later -f override an earlier one rather than erroring.
1228 new_ucmd!()
1229 .args(&["-f", "1", "-f", "2"])
1230 .pipe_in("x y a\nz w a\n")
1231 .succeeds()
1232 .stdout_is("x y a\n");
1233 }
1234
1235 #[test]
1236 fn test_nonexistent_input_file_error_matches_gnu() {
1237 new_ucmd!()
1238 .arg("nosuchfile.txt")
1239 .fails_with_code(1)
1240 .stderr_only("uniq: nosuchfile.txt: No such file or directory\n");
1241 }