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