xref: /freebsd/usr.bin/diff/tests/diff_test.sh (revision 0957b409)
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_expect_fail "--side-by-side not currently implemented (bug # 219933)"
107
108	atf_check -o save:A printf "A\nB\nC\n"
109	atf_check -o save:B printf "D\nB\nE\n"
110
111	exp_output="A[[:space:]]+|[[:space:]]+D\nB[[:space:]]+B\nC[[:space:]]+|[[:space:]]+E"
112	exp_output_suppressed="A[[:space:]]+|[[:space:]]+D\nC[[:space:]]+|[[:space:]]+E"
113
114	atf_check -o match:"$exp_output" -s exit:1 \
115	    diff --side-by-side A B
116	atf_check -o match:"$exp_output" -s exit:1 \
117	    diff -y A B
118	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
119	    diff -y --suppress-common-lines A B
120	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
121	    diff -W 65 -y --suppress-common-lines A B
122}
123
124brief_format_body()
125{
126	atf_check mkdir A B
127
128	atf_check -x "echo 1 > A/test-file"
129	atf_check -x "echo 2 > B/test-file"
130
131	atf_check cp -Rf A C
132	atf_check cp -Rf A D
133
134	atf_check -x "echo 3 > D/another-test-file"
135
136	atf_check \
137	    -s exit:1 \
138	    -o inline:"Files A/test-file and B/test-file differ\n" \
139	    diff -rq A B
140
141	atf_check diff -rq A C
142
143	atf_check \
144	    -s exit:1 \
145	    -o inline:"Only in D: another-test-file\n" \
146	    diff -rq A D
147
148	atf_check \
149	    -s exit:1 \
150	    -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
151	    diff -Nrq A D
152}
153
154Bflag_body()
155{
156	atf_check -x 'printf "A\nB\n" > A'
157	atf_check -x 'printf "A\n\nB\n" > B'
158	atf_check -x 'printf "A\n \nB\n" > C'
159	atf_check -x 'printf "A\nC\nB\n" > D'
160	atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
161	atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
162
163	atf_check -s exit:0 -o inline:"" diff -B A B
164	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
165	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
166	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
167}
168
169atf_init_test_cases()
170{
171	atf_add_test_case simple
172	atf_add_test_case unified
173	atf_add_test_case header
174	atf_add_test_case header_ns
175	atf_add_test_case ifdef
176	atf_add_test_case group_format
177	atf_add_test_case side_by_side
178	atf_add_test_case brief_format
179	atf_add_test_case b230049
180	atf_add_test_case Bflag
181}
182