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