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