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