xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1# $NetBSD: t_grep.sh,v 1.2 2013/05/17 15:39:17 christos Exp $
2#
3# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28atf_test_case basic
29basic_head()
30{
31	atf_set "descr" "Checks basic functionality"
32}
33basic_body()
34{
35	atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
36	    'jot 10000 | grep 123'
37}
38
39atf_test_case binary
40binary_head()
41{
42	atf_set "descr" "Checks handling of binary files"
43}
44binary_body()
45{
46	# Begin FreeBSD
47	#
48	# Generate stable output instead of depending on uname to match the
49	# branded OS name of /bin/sh
50	if true; then
51	dd if=/dev/zero count=1 of=test.file
52	echo -n "foobar" >> test.file
53	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
54	else
55	# End FreeBSD
56	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep $(uname) /bin/sh
57	# Begin FreeBSD
58	fi
59	# End FreeBSD
60}
61
62atf_test_case recurse
63recurse_head()
64{
65	atf_set "descr" "Checks recursive searching"
66}
67recurse_body()
68{
69	mkdir -p recurse/a/f recurse/d
70	echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
71	echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
72
73	# Begin FreeBSD
74	if true; then
75		atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
76	else
77	# End FreeBSD
78	atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" grep -r haddock recurse
79	# Begin FreeBSD
80	fi
81	# End FreeBSD
82}
83
84atf_test_case recurse_symlink
85recurse_symlink_head()
86{
87	atf_set "descr" "Checks symbolic link recursion"
88}
89recurse_symlink_body()
90{
91	mkdir -p test/c/d
92	(cd test/c/d && ln -s ../d .)
93	echo "Test string" > test/c/match
94
95	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
96	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
97	    grep -r string test
98}
99
100atf_test_case word_regexps
101word_regexps_head()
102{
103	atf_set "descr" "Checks word-regexps"
104}
105word_regexps_body()
106{
107	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
108	    grep -w separated $(atf_get_srcdir)/d_input
109}
110
111atf_test_case begin_end
112begin_end_head()
113{
114	atf_set "descr" "Checks handling of line beginnings and ends"
115}
116begin_end_body()
117{
118	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
119	    grep ^Front "$(atf_get_srcdir)/d_input"
120
121	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
122	    grep ending$ "$(atf_get_srcdir)/d_input"
123}
124
125atf_test_case ignore_case
126ignore_case_head()
127{
128	atf_set "descr" "Checks ignore-case option"
129}
130ignore_case_body()
131{
132	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
133	    grep -i Upper "$(atf_get_srcdir)/d_input"
134}
135
136atf_test_case invert
137invert_head()
138{
139	atf_set "descr" "Checks selecting non-matching lines with -v option"
140}
141invert_body()
142{
143	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
144	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
145}
146
147atf_test_case whole_line
148whole_line_head()
149{
150	atf_set "descr" "Checks whole-line matching with -x flag"
151}
152whole_line_body()
153{
154	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
155	    grep -x matchme "$(atf_get_srcdir)/d_input"
156}
157
158atf_test_case negative
159negative_head()
160{
161	atf_set "descr" "Checks handling of files with no matches"
162}
163negative_body()
164{
165	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
166}
167
168atf_test_case context
169context_head()
170{
171	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
172}
173context_body()
174{
175	cp $(atf_get_srcdir)/d_context_*.* .
176
177	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
178	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
179	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
180	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
181}
182
183atf_test_case file_exp
184file_exp_head()
185{
186	atf_set "descr" "Checks reading expressions from file"
187}
188file_exp_body()
189{
190	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
191	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
192}
193
194atf_test_case egrep
195egrep_head()
196{
197	atf_set "descr" "Checks matching special characters with egrep"
198}
199egrep_body()
200{
201	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
202		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
203}
204
205atf_test_case zgrep
206zgrep_head()
207{
208	atf_set "descr" "Checks handling of gzipped files with zgrep"
209}
210zgrep_body()
211{
212	cp "$(atf_get_srcdir)/d_input" .
213	gzip d_input || atf_fail "gzip failed"
214
215	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
216}
217
218atf_test_case nonexistent
219nonexistent_head()
220{
221	atf_set "descr" "Checks that -s flag suppresses error" \
222	                "messages about nonexistent files"
223}
224nonexistent_body()
225{
226	atf_check -s ne:0 grep -s foobar nonexistent
227}
228
229atf_test_case context2
230context2_head()
231{
232	atf_set "descr" "Checks displaying context with -z flag"
233}
234context2_body()
235{
236	printf "haddock\000cod\000plaice\000" > test1
237	printf "mackeral\000cod\000crab\000" > test2
238
239	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
240	    grep -z -A1 cod test1 test2
241
242	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
243	    grep -z -B1 cod test1 test2
244
245	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
246	    grep -z -C1 cod test1 test2
247}
248
249atf_init_test_cases()
250{
251	atf_add_test_case basic
252	atf_add_test_case binary
253	atf_add_test_case recurse
254	atf_add_test_case recurse_symlink
255	atf_add_test_case word_regexps
256	atf_add_test_case begin_end
257	atf_add_test_case ignore_case
258	atf_add_test_case invert
259	atf_add_test_case whole_line
260	atf_add_test_case negative
261	atf_add_test_case context
262	atf_add_test_case file_exp
263	atf_add_test_case egrep
264	atf_add_test_case zgrep
265	atf_add_test_case nonexistent
266	atf_add_test_case context2
267}
268