xref: /freebsd-13-stable/usr.bin/diff/tests/diff_test.sh (revision 12ea2d04c37982cabf7865e8897226c81aae4ab0)
1
2atf_test_case simple
3atf_test_case unified
4atf_test_case header
5atf_test_case header_ns
6atf_test_case ifdef
7atf_test_case group_format
8atf_test_case side_by_side
9atf_test_case brief_format
10atf_test_case b230049
11atf_test_case stripcr_o
12atf_test_case b252515
13atf_test_case b278988
14atf_test_case Bflag
15atf_test_case Nflag
16atf_test_case tabsize
17atf_test_case conflicting_format
18atf_test_case label
19atf_test_case report_identical
20atf_test_case non_regular_file
21atf_test_case binary
22
23simple_body()
24{
25	atf_check -o file:$(atf_get_srcdir)/simple.out -s eq:1 \
26		diff "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
27
28	atf_check -o file:$(atf_get_srcdir)/simple_e.out -s eq:1 \
29		diff -e "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
30
31	atf_check -o file:$(atf_get_srcdir)/simple_u.out -s eq:1 \
32		diff -u -L input1 -L input2 "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
33
34	atf_check -o file:$(atf_get_srcdir)/simple_n.out -s eq:1 \
35		diff -n "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
36
37	atf_check -o inline:"Files $(atf_get_srcdir)/input1.in and $(atf_get_srcdir)/input2.in differ\n" -s eq:1 \
38		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
39
40	atf_check \
41		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input1.in"
42
43	atf_check -o file:$(atf_get_srcdir)/simple_i.out -s eq:1 \
44		diff -i "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
45
46	atf_check -o file:$(atf_get_srcdir)/simple_w.out -s eq:1 \
47		diff -w "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
48
49	atf_check -o file:$(atf_get_srcdir)/simple_b.out -s eq:1 \
50		diff -b "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
51
52	atf_check -o file:$(atf_get_srcdir)/simple_p.out -s eq:1 \
53		diff --label input_c1.in --label input_c2.in -p "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
54}
55
56unified_body()
57{
58	atf_check -o file:$(atf_get_srcdir)/unified_p.out -s eq:1 \
59		diff -up -L input_c1.in -L input_c2.in  "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
60	atf_check -o file:$(atf_get_srcdir)/unified_9999.out -s eq:1 \
61		diff -u9999 -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
62}
63
64b230049_body()
65{
66	printf 'a\nb\r\nc\n' > b230049_a.in
67	printf 'a\r\nb\r\nc\r\n' > b230049_b.in
68	atf_check -o empty -s eq:0 \
69		diff -up --strip-trailing-cr -L b230049_a.in -L b230049_b.in \
70		    b230049_a.in b230049_b.in
71}
72
73stripcr_o_body()
74{
75	printf 'a\nX\nc\n' > stripcr_o_X.in
76	printf 'a\r\nY\r\nc\r\n' > stripcr_o_Y.in
77	atf_check -o "file:$(atf_get_srcdir)/strip_o.out" -s eq:1 \
78		diff -L1 -L2 -u --strip-trailing-cr stripcr_o_X.in stripcr_o_Y.in
79}
80
81b252515_body()
82{
83	printf 'a b\n' > b252515_a.in
84	printf 'a  b\n' > b252515_b.in
85	atf_check -o empty -s eq:0 \
86		diff -qw b252515_a.in b252515_b.in
87}
88
89b278988_body()
90{
91	printf 'a\nb\nn' > b278988.a.in
92	printf 'a\n\nb\nn' > b278988.b.in
93	atf_check -o empty -s eq:0 \
94		diff -Bw b278988.a.in b278988.b.in
95}
96
97header_body()
98{
99	export TZ=UTC
100	: > empty
101	echo hello > hello
102	touch -d 2015-04-03T01:02:03 empty
103	touch -d 2016-12-22T11:22:33 hello
104	atf_check -o "file:$(atf_get_srcdir)/header.out" -s eq:1 \
105		diff -u empty hello
106}
107
108header_ns_body()
109{
110	export TZ=UTC
111	: > empty
112	echo hello > hello
113	touch -d 2015-04-03T01:02:03.123456789 empty
114	touch -d 2016-12-22T11:22:33.987654321 hello
115	atf_check -o "file:$(atf_get_srcdir)/header_ns.out" -s eq:1 \
116		diff -u empty hello
117}
118
119ifdef_body()
120{
121	atf_check -o file:$(atf_get_srcdir)/ifdef.out -s eq:1 \
122		diff -D PLOP "$(atf_get_srcdir)/input_c1.in" \
123		"$(atf_get_srcdir)/input_c2.in"
124}
125
126group_format_body()
127{
128	atf_check -o file:$(atf_get_srcdir)/group-format.out -s eq:1 \
129		diff --changed-group-format='<<<<<<< (local)
130%<=======
131%>>>>>>>> (stock)
132' "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
133}
134
135side_by_side_body()
136{
137	atf_check -o save:A printf "A\nB\nC\n"
138	atf_check -o save:B printf "D\nB\nE\n"
139
140	exp_output=$(printf "A[[:space:]]+|[[:space:]]+D\nB[[:space:]]+B\nC[[:space:]]+|[[:space:]]+E")
141	exp_output_suppressed=$(printf "A[[:space:]]+|[[:space:]]+D\nC[[:space:]]+|[[:space:]]+E")
142
143	atf_check -o match:"$exp_output" -s exit:1 \
144	    diff --side-by-side A B
145	atf_check -o match:"$exp_output" -s exit:1 \
146	    diff -y A B
147	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
148	    diff -y --suppress-common-lines A B
149	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
150	    diff -W 65 -y --suppress-common-lines A B
151}
152
153brief_format_body()
154{
155	atf_check mkdir A B
156
157	atf_check -x "echo 1 > A/test-file"
158	atf_check -x "echo 2 > B/test-file"
159
160	atf_check cp -Rf A C
161	atf_check cp -Rf A D
162
163	atf_check -x "echo 3 > D/another-test-file"
164
165	atf_check \
166	    -s exit:1 \
167	    -o inline:"Files A/test-file and B/test-file differ\n" \
168	    diff -rq A B
169
170	atf_check diff -rq A C
171
172	atf_check \
173	    -s exit:1 \
174	    -o inline:"Only in D: another-test-file\n" \
175	    diff -rq A D
176
177	atf_check \
178	    -s exit:1 \
179	    -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
180	    diff -Nrq A D
181}
182
183Bflag_body()
184{
185	atf_check -x 'printf "A\nB\n" > A'
186	atf_check -x 'printf "A\n\nB\n" > B'
187	atf_check -x 'printf "A\n \nB\n" > C'
188	atf_check -x 'printf "A\nC\nB\n" > D'
189	atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
190	atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
191
192	atf_check -s exit:0 -o inline:"" diff -B A B
193	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
194	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
195	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
196}
197
198Nflag_body()
199{
200	atf_check -x 'printf "foo" > A'
201
202	atf_check -s exit:1 -o ignore -e ignore diff -N A NOFILE
203	atf_check -s exit:1 -o ignore -e ignore diff -N NOFILE A
204	atf_check -s exit:2 -o ignore -e ignore diff -N NOFILE1 NOFILE2
205}
206
207tabsize_body()
208{
209	printf "\tA\n" > A
210	printf "\tB\n" > B
211
212	atf_check -s exit:1 \
213	    -o inline:"1c1\n<  A\n---\n>  B\n" \
214	    diff -t --tabsize 1 A B
215}
216
217conflicting_format_body()
218{
219	printf "\tA\n" > A
220	printf "\tB\n" > B
221
222	atf_check -s exit:2 -e ignore diff -c -u A B
223	atf_check -s exit:2 -e ignore diff -e -f A B
224	atf_check -s exit:2 -e ignore diff -y -q A B
225	atf_check -s exit:2 -e ignore diff -q -u A B
226	atf_check -s exit:2 -e ignore diff -q -c A B
227	atf_check -s exit:2 -e ignore diff --normal -c A B
228	atf_check -s exit:2 -e ignore diff -c --normal A B
229
230	atf_check -s exit:1 -o ignore -e ignore diff -u -u A B
231	atf_check -s exit:1 -o ignore -e ignore diff -e -e A B
232	atf_check -s exit:1 -o ignore -e ignore diff -y -y A B
233	atf_check -s exit:1 -o ignore -e ignore diff -q -q A B
234	atf_check -s exit:1 -o ignore -e ignore diff -c -c A B
235	atf_check -s exit:1 -o ignore -e ignore diff --normal --normal A B
236}
237
238label_body()
239{
240	printf "\tA\n" > A
241
242	atf_check -o inline:"Files hello and world are identical\n" \
243		-s exit:0 diff --label hello --label world -s A A
244
245	atf_check -o inline:"Binary files hello and world differ\n" \
246		-s exit:1 diff --label hello --label world `which diff` `which ls`
247}
248
249report_identical_head()
250{
251	atf_set "require.user" unprivileged
252}
253report_identical_body()
254{
255	printf "\tA\n" > A
256	printf "\tB\n" > B
257	chmod -r B
258	atf_check -s exit:2 -e inline:"diff: B: Permission denied\n" \
259		-o empty diff -s A B
260}
261
262non_regular_file_body()
263{
264	printf "\tA\n" > A
265	mkfifo B
266	printf "\tA\n" > B &
267
268	atf_check diff A B
269	printf "\tB\n" > B &
270	atf_check -s exit:1 \
271		-o inline:"--- A\n+++ B\n@@ -1 +1 @@\n-\tA\n+\tB\n" \
272		diff --label A --label B -u A B
273}
274
275binary_body()
276{
277	# the NUL byte has to be after at least BUFSIZ bytes to trick asciifile()
278	yes 012345678901234567890123456789012345678901234567890 | head -n 174 > A
279	cp A B
280	printf '\n\0\n' >> A
281	printf '\nx\n' >> B
282
283	atf_check -o inline:"Binary files A and B differ\n" -s exit:1 diff A B
284	atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
285}
286
287atf_init_test_cases()
288{
289	atf_add_test_case simple
290	atf_add_test_case unified
291	atf_add_test_case header
292	atf_add_test_case header_ns
293	atf_add_test_case ifdef
294	atf_add_test_case group_format
295	atf_add_test_case side_by_side
296	atf_add_test_case brief_format
297	atf_add_test_case b230049
298	atf_add_test_case stripcr_o
299	atf_add_test_case b252515
300	atf_add_test_case b278988
301	atf_add_test_case Bflag
302	atf_add_test_case Nflag
303	atf_add_test_case tabsize
304	atf_add_test_case conflicting_format
305	atf_add_test_case label
306	atf_add_test_case report_identical
307	atf_add_test_case non_regular_file
308	atf_add_test_case binary
309}
310