xref: /freebsd/usr.bin/diff/tests/diff_test.sh (revision 783d3ff6)
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 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
148side_by_side_tabbed_body()
149{
150	file_a=$(atf_get_srcdir)/side_by_side_tabbed_a.in
151	file_b=$(atf_get_srcdir)/side_by_side_tabbed_b.in
152
153	atf_check -o save:diffout -s not-exit:0 \
154	    diff -y ${file_a} ${file_b}
155	atf_check -o save:diffout_expanded -s not-exit:0 \
156	    diff -yt ${file_a} ${file_b}
157
158	atf_check -o not-empty grep -Ee 'file A.+file B' diffout
159	atf_check -o not-empty grep -Ee 'file A.+file B' diffout_expanded
160
161	atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout
162	atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout_expanded
163}
164
165brief_format_body()
166{
167	atf_check mkdir A B
168
169	atf_check -x "echo 1 > A/test-file"
170	atf_check -x "echo 2 > B/test-file"
171
172	atf_check cp -Rf A C
173	atf_check cp -Rf A D
174
175	atf_check -x "echo 3 > D/another-test-file"
176
177	atf_check \
178	    -s exit:1 \
179	    -o inline:"Files A/test-file and B/test-file differ\n" \
180	    diff -rq A B
181
182	atf_check diff -rq A C
183
184	atf_check \
185	    -s exit:1 \
186	    -o inline:"Only in D: another-test-file\n" \
187	    diff -rq A D
188
189	atf_check \
190	    -s exit:1 \
191	    -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
192	    diff -Nrq A D
193}
194
195Bflag_body()
196{
197	atf_check -x 'printf "A\nB\n" > A'
198	atf_check -x 'printf "A\n\nB\n" > B'
199	atf_check -x 'printf "A\n \nB\n" > C'
200	atf_check -x 'printf "A\nC\nB\n" > D'
201	atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
202	atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
203
204	atf_check -s exit:0 -o inline:"" diff -B A B
205	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
206	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
207	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
208}
209
210Nflag_body()
211{
212	atf_check -x 'printf "foo" > A'
213
214	atf_check -s exit:1 -o ignore -e ignore diff -N A NOFILE
215	atf_check -s exit:1 -o ignore -e ignore diff -N NOFILE A
216	atf_check -s exit:2 -o ignore -e ignore diff -N NOFILE1 NOFILE2
217}
218
219tabsize_body()
220{
221	printf "\tA\n" > A
222	printf "\tB\n" > B
223
224	atf_check -s exit:1 \
225	    -o inline:"1c1\n<  A\n---\n>  B\n" \
226	    diff -t --tabsize 1 A B
227}
228
229conflicting_format_body()
230{
231	printf "\tA\n" > A
232	printf "\tB\n" > B
233
234	atf_check -s exit:2 -e ignore diff -c -u A B
235	atf_check -s exit:2 -e ignore diff -e -f A B
236	atf_check -s exit:2 -e ignore diff -y -q A B
237	atf_check -s exit:2 -e ignore diff -q -u A B
238	atf_check -s exit:2 -e ignore diff -q -c A B
239	atf_check -s exit:2 -e ignore diff --normal -c A B
240	atf_check -s exit:2 -e ignore diff -c --normal A B
241
242	atf_check -s exit:1 -o ignore -e ignore diff -u -u A B
243	atf_check -s exit:1 -o ignore -e ignore diff -e -e A B
244	atf_check -s exit:1 -o ignore -e ignore diff -y -y A B
245	atf_check -s exit:1 -o ignore -e ignore diff -q -q A B
246	atf_check -s exit:1 -o ignore -e ignore diff -c -c A B
247	atf_check -s exit:1 -o ignore -e ignore diff --normal --normal A B
248}
249
250label_body()
251{
252	printf "\tA\n" > A
253
254	atf_check -o inline:"Files hello and world are identical\n" \
255		-s exit:0 diff --label hello --label world -s A A
256
257	atf_check -o inline:"Binary files hello and world differ\n" \
258		-s exit:1 diff --label hello --label world `which diff` `which ls`
259}
260
261report_identical_head()
262{
263	atf_set "require.user" unprivileged
264}
265report_identical_body()
266{
267	printf "\tA\n" > A
268	printf "\tB\n" > B
269	atf_check -s exit:0 -o match:"are identical" \
270		  diff -s A A
271	atf_check -s exit:1 -o not-match:"are identical" \
272		  diff -s A B
273	chmod -r B
274	atf_check -s exit:2 -e inline:"diff: B: Permission denied\n" \
275		-o empty diff -s A B
276}
277
278non_regular_file_body()
279{
280	printf "\tA\n" > A
281	mkfifo B
282	printf "\tA\n" > B &
283
284	atf_check diff A B
285	printf "\tB\n" > B &
286	atf_check -s exit:1 \
287		-o inline:"--- A\n+++ B\n@@ -1 +1 @@\n-\tA\n+\tB\n" \
288		diff --label A --label B -u A B
289}
290
291binary_body()
292{
293	# the NUL byte has to be after at least BUFSIZ bytes to trick asciifile()
294	yes 012345678901234567890123456789012345678901234567890 | head -n 174 > A
295	cp A B
296	printf '\n\0\n' >> A
297	printf '\nx\n' >> B
298
299	atf_check -o inline:"Binary files A and B differ\n" -s exit:1 diff A B
300	atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
301}
302
303functionname_body()
304{
305	atf_check -o file:$(atf_get_srcdir)/functionname_c.out -s exit:1 \
306		diff -u -p -L functionname.in -L functionname_c.in \
307		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_c.in"
308
309	atf_check -o file:$(atf_get_srcdir)/functionname_objcm.out -s exit:1 \
310		diff -u -p -L functionname.in -L functionname_objcm.in \
311		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcm.in"
312
313	atf_check -o file:$(atf_get_srcdir)/functionname_objcclassm.out -s exit:1 \
314		diff -u -p -L functionname.in -L functionname_objcclassm.in \
315		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcclassm.in"
316}
317
318noderef_body()
319{
320	atf_check mkdir A B
321
322	atf_check -x "echo 1 > A/test-file"
323	atf_check -x "echo 1 > test-file"
324	atf_check -x "echo 1 > test-file2"
325
326	atf_check ln -s $(pwd)/test-file B/test-file
327
328	atf_check -o empty -s exit:0 diff -r A B
329	atf_check -o inline:"File A/test-file is a file while file B/test-file is a symbolic link\n" \
330		-s exit:1 diff -r --no-dereference A B
331
332	# both test files are now the same symbolic link
333	atf_check rm A/test-file
334
335	atf_check ln -s $(pwd)/test-file A/test-file
336	atf_check -o empty -s exit:0 diff -r A B
337	atf_check -o empty -s exit:0 diff -r --no-dereference A B
338
339	# make test files different symbolic links, but same contents
340	atf_check unlink A/test-file
341	atf_check ln -s $(pwd)/test-file2 A/test-file
342
343	atf_check -o empty -s exit:0 diff -r A B
344	atf_check -o inline:"Symbolic links A/test-file and B/test-file differ\n" -s exit:1 diff -r --no-dereference A B
345}
346
347ignorecase_body()
348{
349	atf_check mkdir A
350	atf_check mkdir B
351
352	atf_check -x "echo hello > A/foo"
353	atf_check -x "echo hello > B/FOO"
354
355	atf_check -o empty -s exit:0 diff -u -r --ignore-file-name-case A B
356}
357
358atf_init_test_cases()
359{
360	atf_add_test_case simple
361	atf_add_test_case unified
362	atf_add_test_case header
363	atf_add_test_case header_ns
364	atf_add_test_case ifdef
365	atf_add_test_case group_format
366	atf_add_test_case side_by_side
367	atf_add_test_case side_by_side_tabbed
368	atf_add_test_case brief_format
369	atf_add_test_case b230049
370	atf_add_test_case stripcr_o
371	atf_add_test_case b252515
372	atf_add_test_case Bflag
373	atf_add_test_case Nflag
374	atf_add_test_case tabsize
375	atf_add_test_case conflicting_format
376	atf_add_test_case label
377	atf_add_test_case report_identical
378	atf_add_test_case non_regular_file
379	atf_add_test_case binary
380	atf_add_test_case functionname
381	atf_add_test_case noderef
382	atf_add_test_case ignorecase
383}
384