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