xref: /freebsd/usr.bin/diff/tests/diff_test.sh (revision 29fc4075e69fd27de0cded313ac6000165d99f8b)
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.user" unprivileged
247}
248report_identical_body()
249{
250	printf "\tA\n" > A
251	printf "\tB\n" > B
252	chmod -r B
253	atf_check -s exit:2 -e inline:"diff: B: Permission denied\n" \
254		-o empty diff -s A B
255}
256
257non_regular_file_body()
258{
259	printf "\tA\n" > A
260	mkfifo B
261	printf "\tA\n" > B &
262
263	atf_check diff A B
264	printf "\tB\n" > B &
265	atf_check -s exit:1 \
266		-o inline:"--- A\n+++ B\n@@ -1 +1 @@\n-\tA\n+\tB\n" \
267		diff --label A --label B -u A B
268}
269
270binary_body()
271{
272	# the NUL byte has to be after at least BUFSIZ bytes to trick asciifile()
273	yes 012345678901234567890123456789012345678901234567890 | head -n 174 > A
274	cp A B
275	printf '\n\0\n' >> A
276	printf '\nx\n' >> B
277
278	atf_check -o inline:"Binary files A and B differ\n" -s exit:1 diff A B
279	atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
280}
281
282functionname_body()
283{
284	atf_check -o file:$(atf_get_srcdir)/functionname_c.out -s exit:1 \
285		diff -u -p -L functionname.in -L functionname_c.in \
286		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_c.in"
287
288	atf_check -o file:$(atf_get_srcdir)/functionname_objcm.out -s exit:1 \
289		diff -u -p -L functionname.in -L functionname_objcm.in \
290		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcm.in"
291
292	atf_check -o file:$(atf_get_srcdir)/functionname_objcclassm.out -s exit:1 \
293		diff -u -p -L functionname.in -L functionname_objcclassm.in \
294		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcclassm.in"
295}
296
297noderef_body()
298{
299	atf_check mkdir A B
300
301	atf_check -x "echo 1 > A/test-file"
302	atf_check -x "echo 1 > test-file"
303	atf_check -x "echo 1 > test-file2"
304
305	atf_check ln -s $(pwd)/test-file B/test-file
306
307	atf_check -o empty -s exit:0 diff -r A B
308	atf_check -o inline:"File A/test-file is a file while file B/test-file is a symbolic link\n" \
309		-s exit:1 diff -r --no-dereference A B
310
311	# both test files are now the same symbolic link
312	atf_check rm A/test-file
313
314	atf_check ln -s $(pwd)/test-file A/test-file
315	atf_check -o empty -s exit:0 diff -r A B
316	atf_check -o empty -s exit:0 diff -r --no-dereference A B
317
318	# make test files different symbolic links, but same contents
319	atf_check unlink A/test-file
320	atf_check ln -s $(pwd)/test-file2 A/test-file
321
322	atf_check -o empty -s exit:0 diff -r A B
323	atf_check -o inline:"Symbolic links A/test-file and B/test-file differ\n" -s exit:1 diff -r --no-dereference A B
324}
325
326ignorecase_body()
327{
328	atf_check mkdir A
329	atf_check mkdir B
330
331	atf_check -x "echo hello > A/foo"
332	atf_check -x "echo hello > B/FOO"
333
334	atf_check -o empty -s exit:0 diff -u -r --ignore-file-name-case A B
335}
336
337atf_init_test_cases()
338{
339	atf_add_test_case simple
340	atf_add_test_case unified
341	atf_add_test_case header
342	atf_add_test_case header_ns
343	atf_add_test_case ifdef
344	atf_add_test_case group_format
345	atf_add_test_case side_by_side
346	atf_add_test_case brief_format
347	atf_add_test_case b230049
348	atf_add_test_case stripcr_o
349	atf_add_test_case b252515
350	atf_add_test_case Bflag
351	atf_add_test_case Nflag
352	atf_add_test_case tabsize
353	atf_add_test_case conflicting_format
354	atf_add_test_case label
355	atf_add_test_case report_identical
356	atf_add_test_case non_regular_file
357	atf_add_test_case binary
358	atf_add_test_case functionname
359	atf_add_test_case noderef
360	atf_add_test_case ignorecase
361}
362