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