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