]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/uniq/tests/uniq_test.sh
uniq: Error out if writing to the output failed.
[FreeBSD/FreeBSD.git] / usr.bin / uniq / tests / uniq_test.sh
1 #
2 # Copyright (c) 2024 Klara, Inc.
3 #
4 # SPDX-License-Identifier: BSD-2-Clause
5 #
6
7 atf_check_uniq() {
8         atf_check uniq "$@" input actual
9         atf_check diff -u actual expected
10         atf_check uniq "$@" - actual <input
11         atf_check diff -u actual expected
12         atf_check -o file:expected uniq "$@" input
13         atf_check -o file:expected uniq "$@" <input
14         atf_check -o file:expected uniq "$@" - <input
15 }
16
17 atf_test_case basic
18 basic_head() {
19         atf_set descr "basic test without options"
20 }
21 basic_body() {
22         printf "a\na\nb\nb\na\na\n" >input
23         printf "a\nb\na\n" >expected
24         atf_check_uniq
25 }
26
27 atf_test_case count
28 count_head() {
29         atf_set descr "basic test showing counts"
30 }
31 count_body() {
32         printf "a\na\nb\nb\nb\na\na\na\na\n" >input
33         printf "   2 a\n   3 b\n   4 a\n" >expected
34         atf_check_uniq -c
35         atf_check_uniq --count
36 }
37
38 atf_test_case repeated
39 repeated_head() {
40         atf_set descr "print repeated lines only"
41 }
42 repeated_body() {
43         printf "a\na\nb\na\na\n" >input
44         printf "a\na\n" >expected
45         atf_check_uniq -d
46         atf_check_uniq --repeated
47 }
48
49 atf_test_case count_repeated
50 count_repeated_head() {
51         atf_set descr "count and print repeated lines only"
52 }
53 count_repeated_body() {
54         printf "a\na\nb\nb\na\n" >input
55         printf "   2 a\n   2 b\n" >expected
56         atf_check_uniq --count --repeated
57 }
58
59 atf_test_case all_repeated
60 all_repeated_head() {
61         atf_set descr "print every instance of repeated lines"
62 }
63 all_repeated_body() {
64         printf "a\na\nb\na\na\n" >input
65         printf "a\na\na\na\n" >expected
66         atf_check_uniq -D
67         atf_check_uniq --all-repeated
68 }
69
70 atf_test_case skip_fields
71 skip_fields_head() {
72         atf_set descr "skip fields"
73 }
74 skip_fields_body() {
75         printf "1 a\n2 a\n3 b\n4 b\n5 a\n6 a\n" >input
76         printf "1 a\n3 b\n5 a\n" >expected
77         atf_check_uniq -1
78         atf_check_uniq -f 1
79         atf_check_uniq --skip-fields 1
80 }
81
82 atf_test_case skip_fields_tab
83 skip_fields_tab_head() {
84         atf_set descr "skip fields (with tabs)"
85 }
86 skip_fields_tab_body() {
87         printf "1\ta\n2\ta\n3\tb\n4\tb\n5\ta\n6\ta\n" >input
88         printf "1\ta\n3\tb\n5\ta\n" >expected
89         atf_check_uniq -1
90         atf_check_uniq -f 1
91         atf_check_uniq --skip-fields 1
92 }
93
94 atf_test_case ignore_case
95 ignore_case_head() {
96         atf_set descr "ignore case"
97 }
98 ignore_case_body() {
99         printf "a\nA\nb\nB\na\nA\n" >input
100         printf "a\nb\na\n" >expected
101         atf_check_uniq -i
102         atf_check_uniq --ignore-case
103 }
104
105 atf_test_case skip_chars
106 skip_chars_head() {
107         atf_set descr "skip chars"
108 }
109 skip_chars_body() {
110         printf "1 a\n2 a\n3 b\n4 b\n5 a\n6 a\n" >input
111         printf "1 a\n3 b\n5 a\n" >expected
112         atf_check_uniq +2
113         atf_check_uniq -s 2
114         atf_check_uniq --skip-chars 2
115 }
116
117 atf_test_case unique
118 unique_head() {
119         atf_set descr "print non-repeated lines only"
120 }
121 unique_body() {
122         printf "a\na\nb\na\na\n" >input
123         printf "b\n" >expected
124         atf_check_uniq -u
125         atf_check_uniq --unique
126 }
127
128 atf_test_case count_unique
129 count_unique_head() {
130         atf_set descr "print non-repeated lines with count"
131 }
132 count_unique_body() {
133         printf "a\na\nb\n" >input
134         printf "   1 b\n" >expected
135         atf_check_uniq --unique --count
136         atf_check_uniq --count --unique
137 }
138
139 atf_test_case interactive
140 interactive_head() {
141         atf_set descr "test interactive use"
142 }
143 interactive_body() {
144         sh -c 'yes | stdbuf -oL uniq >actual' &
145         pid=$!
146         sleep 1
147         kill $!
148         atf_check -o inline:"y\n" cat actual
149 }
150
151 atf_test_case interactive_repeated
152 interactive_repeated_head() {
153         atf_set descr "test interactive use with -d"
154 }
155 interactive_repeated_body() {
156         sh -c 'yes | stdbuf -oL uniq -d >actual' &
157         pid=$!
158         sleep 1
159         kill $!
160         atf_check -o inline:"y\n" cat actual
161 }
162
163 atf_test_case stdout
164 stdout_head() {
165         atf_set descr "error writing to stdout"
166 }
167 stdout_body() {
168         (
169                 trap "" PIPE
170                 echo a | uniq 2>stderr
171                 echo $? >result
172         ) | true
173         atf_check -o inline:"1\n" cat result
174         atf_check -o match:"stdout" cat stderr
175 }
176
177 atf_init_test_cases()
178 {
179         atf_add_test_case basic
180         atf_add_test_case count
181         atf_add_test_case repeated
182         atf_add_test_case count_repeated
183         atf_add_test_case all_repeated
184         atf_add_test_case skip_fields
185         atf_add_test_case skip_fields_tab
186         atf_add_test_case ignore_case
187         atf_add_test_case skip_chars
188         atf_add_test_case unique
189         atf_add_test_case count_unique
190         atf_add_test_case interactive
191         atf_add_test_case interactive_repeated
192         atf_add_test_case stdout
193 }