xref: /freebsd/usr.bin/diff/tests/diff_test.sh (revision d38c30c092828f4882ce13b08d0bd3fd6dc7afb5)
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 Bflag
13
14simple_body()
15{
16	atf_check -o file:$(atf_get_srcdir)/simple.out -s eq:1 \
17		diff "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
18
19	atf_check -o file:$(atf_get_srcdir)/simple_e.out -s eq:1 \
20		diff -e "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
21
22	atf_check -o file:$(atf_get_srcdir)/simple_u.out -s eq:1 \
23		diff -u -L input1 -L input2 "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
24
25	atf_check -o file:$(atf_get_srcdir)/simple_n.out -s eq:1 \
26		diff -n "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
27
28	atf_check -o inline:"Files $(atf_get_srcdir)/input1.in and $(atf_get_srcdir)/input2.in differ\n" -s eq:1 \
29		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
30
31	atf_check \
32		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input1.in"
33
34	atf_check -o file:$(atf_get_srcdir)/simple_i.out -s eq:1 \
35		diff -i "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
36
37	atf_check -o file:$(atf_get_srcdir)/simple_w.out -s eq:1 \
38		diff -w "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
39
40	atf_check -o file:$(atf_get_srcdir)/simple_b.out -s eq:1 \
41		diff -b "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
42
43	atf_check -o file:$(atf_get_srcdir)/simple_p.out -s eq:1 \
44		diff --label input_c1.in --label input_c2.in -p "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
45}
46
47unified_body()
48{
49	atf_check -o file:$(atf_get_srcdir)/unified_p.out -s eq:1 \
50		diff -up -L input_c1.in -L input_c2.in  "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
51	atf_check -o file:$(atf_get_srcdir)/unified_c9999.out -s eq:1 \
52		diff -u -c9999 -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
53	atf_check -o file:$(atf_get_srcdir)/unified_9999.out -s eq:1 \
54		diff -u9999 -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
55}
56
57b230049_body()
58{
59	printf 'a\nb\r\nc\n' > b230049_a.in
60	printf 'a\r\nb\r\nc\r\n' > b230049_b.in
61	atf_check -o empty -s eq:0 \
62		diff -up --strip-trailing-cr -L b230049_a.in -L b230049_b.in \
63		    b230049_a.in b230049_b.in
64}
65
66header_body()
67{
68	export TZ=UTC
69	: > empty
70	echo hello > hello
71	touch -d 2015-04-03T01:02:03 empty
72	touch -d 2016-12-22T11:22:33 hello
73	atf_check -o "file:$(atf_get_srcdir)/header.out" -s eq:1 \
74		diff -u empty hello
75}
76
77header_ns_body()
78{
79	export TZ=UTC
80	: > empty
81	echo hello > hello
82	touch -d 2015-04-03T01:02:03.123456789 empty
83	touch -d 2016-12-22T11:22:33.987654321 hello
84	atf_check -o "file:$(atf_get_srcdir)/header_ns.out" -s eq:1 \
85		diff -u empty hello
86}
87
88ifdef_body()
89{
90	atf_check -o file:$(atf_get_srcdir)/ifdef.out -s eq:1 \
91		diff -D PLOP "$(atf_get_srcdir)/input_c1.in" \
92		"$(atf_get_srcdir)/input_c2.in"
93}
94
95group_format_body()
96{
97	atf_check -o file:$(atf_get_srcdir)/group-format.out -s eq:1 \
98		diff --changed-group-format='<<<<<<< (local)
99%<=======
100%>>>>>>>> (stock)
101' "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
102}
103
104side_by_side_body()
105{
106	atf_check -o save:A printf "A\nB\nC\n"
107	atf_check -o save:B printf "D\nB\nE\n"
108
109	exp_output="A[[:space:]]+|[[:space:]]+D\nB[[:space:]]+B\nC[[:space:]]+|[[:space:]]+E"
110	exp_output_suppressed="A[[:space:]]+|[[:space:]]+D\nC[[:space:]]+|[[:space:]]+E"
111
112	atf_check -o match:"$exp_output" -s exit:1 \
113	    diff --side-by-side A B
114	atf_check -o match:"$exp_output" -s exit:1 \
115	    diff -y A B
116	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
117	    diff -y --suppress-common-lines A B
118	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
119	    diff -W 65 -y --suppress-common-lines A B
120}
121
122brief_format_body()
123{
124	atf_check mkdir A B
125
126	atf_check -x "echo 1 > A/test-file"
127	atf_check -x "echo 2 > B/test-file"
128
129	atf_check cp -Rf A C
130	atf_check cp -Rf A D
131
132	atf_check -x "echo 3 > D/another-test-file"
133
134	atf_check \
135	    -s exit:1 \
136	    -o inline:"Files A/test-file and B/test-file differ\n" \
137	    diff -rq A B
138
139	atf_check diff -rq A C
140
141	atf_check \
142	    -s exit:1 \
143	    -o inline:"Only in D: another-test-file\n" \
144	    diff -rq A D
145
146	atf_check \
147	    -s exit:1 \
148	    -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
149	    diff -Nrq A D
150}
151
152Bflag_body()
153{
154	atf_check -x 'printf "A\nB\n" > A'
155	atf_check -x 'printf "A\n\nB\n" > B'
156	atf_check -x 'printf "A\n \nB\n" > C'
157	atf_check -x 'printf "A\nC\nB\n" > D'
158	atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
159	atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
160
161	atf_check -s exit:0 -o inline:"" diff -B A B
162	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
163	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
164	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
165}
166
167atf_init_test_cases()
168{
169	atf_add_test_case simple
170	atf_add_test_case unified
171	atf_add_test_case header
172	atf_add_test_case header_ns
173	atf_add_test_case ifdef
174	atf_add_test_case group_format
175	atf_add_test_case side_by_side
176	atf_add_test_case brief_format
177	atf_add_test_case b230049
178	atf_add_test_case Bflag
179}
180