xref: /freebsd/usr.bin/diff/tests/diff_test.sh (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
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
22
23simple_body()
24{
25	atf_check -o file:$(atf_get_srcdir)/simple.out -s eq:1 \
26		diff "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
27
28	atf_check -o file:$(atf_get_srcdir)/simple_e.out -s eq:1 \
29		diff -e "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
30
31	atf_check -o file:$(atf_get_srcdir)/simple_u.out -s eq:1 \
32		diff -u -L input1 -L input2 "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
33
34	atf_check -o file:$(atf_get_srcdir)/simple_n.out -s eq:1 \
35		diff -n "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
36
37	atf_check -o inline:"Files $(atf_get_srcdir)/input1.in and $(atf_get_srcdir)/input2.in differ\n" -s eq:1 \
38		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
39
40	atf_check \
41		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input1.in"
42
43	atf_check -o file:$(atf_get_srcdir)/simple_i.out -s eq:1 \
44		diff -i "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
45
46	atf_check -o file:$(atf_get_srcdir)/simple_w.out -s eq:1 \
47		diff -w "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
48
49	atf_check -o file:$(atf_get_srcdir)/simple_b.out -s eq:1 \
50		diff -b "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
51
52	atf_check -o file:$(atf_get_srcdir)/simple_p.out -s eq:1 \
53		diff --label input_c1.in --label input_c2.in -p "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
54}
55
56unified_body()
57{
58	atf_check -o file:$(atf_get_srcdir)/unified_p.out -s eq:1 \
59		diff -up -L input_c1.in -L input_c2.in  "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
60	atf_check -o file:$(atf_get_srcdir)/unified_9999.out -s eq:1 \
61		diff -u9999 -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
62}
63
64b230049_body()
65{
66	printf 'a\nb\r\nc\n' > b230049_a.in
67	printf 'a\r\nb\r\nc\r\n' > b230049_b.in
68	atf_check -o empty -s eq:0 \
69		diff -up --strip-trailing-cr -L b230049_a.in -L b230049_b.in \
70		    b230049_a.in b230049_b.in
71}
72
73stripcr_o_body()
74{
75	printf 'a\nX\nc\n' > stripcr_o_X.in
76	printf 'a\r\nY\r\nc\r\n' > stripcr_o_Y.in
77	atf_check -o "file:$(atf_get_srcdir)/strip_o.out" -s eq:1 \
78		diff -L1 -L2 -u --strip-trailing-cr stripcr_o_X.in stripcr_o_Y.in
79}
80
81b252515_body()
82{
83	printf 'a b\n' > b252515_a.in
84	printf 'a  b\n' > b252515_b.in
85	atf_check -o empty -s eq:0 \
86		diff -qw b252515_a.in b252515_b.in
87}
88
89header_body()
90{
91	export TZ=UTC
92	: > empty
93	echo hello > hello
94	touch -d 2015-04-03T01:02:03 empty
95	touch -d 2016-12-22T11:22:33 hello
96	atf_check -o "file:$(atf_get_srcdir)/header.out" -s eq:1 \
97		diff -u empty hello
98}
99
100header_ns_body()
101{
102	export TZ=UTC
103	: > empty
104	echo hello > hello
105	touch -d 2015-04-03T01:02:03.123456789 empty
106	touch -d 2016-12-22T11:22:33.987654321 hello
107	atf_check -o "file:$(atf_get_srcdir)/header_ns.out" -s eq:1 \
108		diff -u empty hello
109}
110
111ifdef_body()
112{
113	atf_check -o file:$(atf_get_srcdir)/ifdef.out -s eq:1 \
114		diff -D PLOP "$(atf_get_srcdir)/input_c1.in" \
115		"$(atf_get_srcdir)/input_c2.in"
116}
117
118group_format_body()
119{
120	atf_check -o file:$(atf_get_srcdir)/group-format.out -s eq:1 \
121		diff --changed-group-format='<<<<<<< (local)
122%<=======
123%>>>>>>>> (stock)
124' "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
125}
126
127side_by_side_body()
128{
129	atf_check -o save:A printf "A\nB\nC\n"
130	atf_check -o save:B printf "D\nB\nE\n"
131
132	exp_output=$(printf "A[[:space:]]+|[[:space:]]+D\nB[[:space:]]+B\nC[[:space:]]+|[[:space:]]+E")
133	exp_output_suppressed=$(printf "A[[:space:]]+|[[:space:]]+D\nC[[:space:]]+|[[:space:]]+E")
134
135	atf_check -o match:"$exp_output" -s exit:1 \
136	    diff --side-by-side A B
137	atf_check -o match:"$exp_output" -s exit:1 \
138	    diff -y A B
139	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
140	    diff -y --suppress-common-lines A B
141	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
142	    diff -W 65 -y --suppress-common-lines A B
143}
144
145brief_format_body()
146{
147	atf_check mkdir A B
148
149	atf_check -x "echo 1 > A/test-file"
150	atf_check -x "echo 2 > B/test-file"
151
152	atf_check cp -Rf A C
153	atf_check cp -Rf A D
154
155	atf_check -x "echo 3 > D/another-test-file"
156
157	atf_check \
158	    -s exit:1 \
159	    -o inline:"Files A/test-file and B/test-file differ\n" \
160	    diff -rq A B
161
162	atf_check diff -rq A C
163
164	atf_check \
165	    -s exit:1 \
166	    -o inline:"Only in D: another-test-file\n" \
167	    diff -rq A D
168
169	atf_check \
170	    -s exit:1 \
171	    -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
172	    diff -Nrq A D
173}
174
175Bflag_body()
176{
177	atf_check -x 'printf "A\nB\n" > A'
178	atf_check -x 'printf "A\n\nB\n" > B'
179	atf_check -x 'printf "A\n \nB\n" > C'
180	atf_check -x 'printf "A\nC\nB\n" > D'
181	atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
182	atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
183
184	atf_check -s exit:0 -o inline:"" diff -B A B
185	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
186	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
187	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
188}
189
190Nflag_body()
191{
192	atf_check -x 'printf "foo" > A'
193
194	atf_check -s exit:1 -o ignore -e ignore diff -N A NOFILE
195	atf_check -s exit:1 -o ignore -e ignore diff -N NOFILE A
196	atf_check -s exit:2 -o ignore -e ignore diff -N NOFILE1 NOFILE2
197}
198
199tabsize_body()
200{
201	printf "\tA\n" > A
202	printf "\tB\n" > B
203
204	atf_check -s exit:1 \
205	    -o inline:"1c1\n<  A\n---\n>  B\n" \
206	    diff -t --tabsize 1 A B
207}
208
209conflicting_format_body()
210{
211	printf "\tA\n" > A
212	printf "\tB\n" > B
213
214	atf_check -s exit:2 -e ignore diff -c -u A B
215	atf_check -s exit:2 -e ignore diff -e -f A B
216	atf_check -s exit:2 -e ignore diff -y -q A B
217	atf_check -s exit:2 -e ignore diff -q -u A B
218	atf_check -s exit:2 -e ignore diff -q -c A B
219	atf_check -s exit:2 -e ignore diff --normal -c A B
220	atf_check -s exit:2 -e ignore diff -c --normal A B
221
222	atf_check -s exit:1 -o ignore -e ignore diff -u -u A B
223	atf_check -s exit:1 -o ignore -e ignore diff -e -e A B
224	atf_check -s exit:1 -o ignore -e ignore diff -y -y A B
225	atf_check -s exit:1 -o ignore -e ignore diff -q -q A B
226	atf_check -s exit:1 -o ignore -e ignore diff -c -c A B
227	atf_check -s exit:1 -o ignore -e ignore diff --normal --normal A B
228}
229
230label_body()
231{
232	printf "\tA\n" > A
233
234	atf_check -o inline:"Files hello and world are identical\n" \
235		-s exit:0 diff --label hello --label world -s A A
236
237	atf_check -o inline:"Binary files hello and world differ\n" \
238		-s exit:1 diff --label hello --label world `which diff` `which ls`
239}
240
241report_identical_head()
242{
243	atf_set "require.config" unprivileged_user
244	atf_set "require.user" root
245}
246report_identical_body()
247{
248	UNPRIVILEGED_USER=$(atf_config_get unprivileged_user)
249	printf "\tA\n" > A
250	printf "\tB\n" > B
251	chmod -r B
252	atf_check -s exit:2 -e inline:"diff: B: Permission denied\n" \
253		-o empty su -m "$UNPRIVILEGED_USER" -c 'diff -s A B'
254}
255
256non_regular_file_body()
257{
258	printf "\tA\n" > A
259	mkfifo B
260	printf "\tA\n" > B &
261
262	atf_check diff A B
263	printf "\tB\n" > B &
264	atf_check -s exit:1 \
265		-o inline:"--- A\n+++ B\n@@ -1 +1 @@\n-\tA\n+\tB\n" \
266		diff --label A --label B -u A B
267}
268
269binary_body()
270{
271	# the NUL byte has to be after at least BUFSIZ bytes to trick asciifile()
272	yes 012345678901234567890123456789012345678901234567890 | head -n 174 > A
273	cp A B
274	printf '\n\0\n' >> A
275	printf '\nx\n' >> B
276
277	atf_check -o inline:"Binary files A and B differ\n" -s exit:1 diff A B
278	atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
279}
280
281atf_init_test_cases()
282{
283	atf_add_test_case simple
284	atf_add_test_case unified
285	atf_add_test_case header
286	atf_add_test_case header_ns
287	atf_add_test_case ifdef
288	atf_add_test_case group_format
289	atf_add_test_case side_by_side
290	atf_add_test_case brief_format
291	atf_add_test_case b230049
292	atf_add_test_case stripcr_o
293	atf_add_test_case b252515
294	atf_add_test_case Bflag
295	atf_add_test_case Nflag
296	atf_add_test_case tabsize
297	atf_add_test_case conflicting_format
298	atf_add_test_case label
299	atf_add_test_case report_identical
300	atf_add_test_case non_regular_file
301	atf_add_test_case binary
302}
303